feat: tor support
This commit is contained in:
42 files changed
+1853
-69
No files matched your search
@@ -50,11 +50,13 @@ let
|
||||
sshProxyOutboundEnabled
|
||||
sshProxyUnitEnabled
|
||||
warpCfg
|
||||
torCfg
|
||||
awgOutbounds
|
||||
proxyInboundsCfg
|
||||
proxyInboundsEnabled
|
||||
proxyInboundsAwg
|
||||
proxyInboundsNeedLocalProxy
|
||||
torOnionEnabled
|
||||
proxyInboundsGuardPrivate
|
||||
proxyInboundViaOutbounds
|
||||
userControlAllows
|
||||
@@ -100,6 +102,7 @@ let
|
||||
proxyCfg
|
||||
sshProxyCfg
|
||||
warpCfg
|
||||
torCfg
|
||||
awgOutbounds
|
||||
xrayEnabled
|
||||
hybridEnabled
|
||||
@@ -135,6 +138,7 @@ let
|
||||
proxyInboundsAwg
|
||||
awgBin
|
||||
proxyInboundsNeedLocalProxy
|
||||
torOnionEnabled
|
||||
proxyInboundsGuardPrivate
|
||||
proxyInboundViaOutbounds
|
||||
userControlAllows
|
||||
|
||||
@@ -63,6 +63,7 @@
|
||||
if proxyInboundsSubscriptionsBaseUrl == null then "" else proxyInboundsSubscriptionsBaseUrl;
|
||||
autoProxyEnabled = if proxyCfg.autoProxy.enable then "1" else "0";
|
||||
autoProxyStateDir = constants.autoProxyStateDir;
|
||||
torControlSocket = constants.torControlSocket;
|
||||
inherit (constants) stateDir runtimeDir serviceManager;
|
||||
privileged = if constants.privileged then "1" else "0";
|
||||
supervisorCtl = if constants.serviceManager == "supervisor" then constants.systemctl else "";
|
||||
|
||||
@@ -66,6 +66,8 @@ let
|
||||
perAppZapretEnabled
|
||||
sshProxyOutboundEnabled
|
||||
sshProxyUnitEnabled
|
||||
torOutboundEnabled
|
||||
torOnionEnabled
|
||||
proxyInboundsEnabled
|
||||
proxyInboundsNeedLocalProxy
|
||||
proxyInboundFirewallPorts
|
||||
@@ -108,6 +110,8 @@ let
|
||||
perAppZapretEnabled
|
||||
sshProxyOutboundEnabled
|
||||
sshProxyUnitEnabled
|
||||
torOutboundEnabled
|
||||
torOnionEnabled
|
||||
proxyInboundsEnabled
|
||||
proxyInboundsNeedLocalProxy
|
||||
scripts
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
proxyInboundsAwg,
|
||||
awgBin,
|
||||
proxyInboundsNeedLocalProxy,
|
||||
torOnionEnabled,
|
||||
proxyInboundViaOutbounds,
|
||||
userControlCfg,
|
||||
userControlAllows,
|
||||
@@ -53,6 +54,26 @@ let
|
||||
else
|
||||
''SERVER_ADDRESS=""'';
|
||||
|
||||
# The onion service's address, for its share links. Tor writes it as it starts; a Tor
|
||||
# that is not up in time costs only the onion links, until the next restart.
|
||||
onionAddressBlock =
|
||||
if torOnionEnabled && proxyInboundsCfg.shareLinks then
|
||||
''
|
||||
ONION_ADDRESS=""
|
||||
onion_hostname=${lib.escapeShellArg constants.torOnionHostnameFile}
|
||||
for _ in $(${pkgs.coreutils}/bin/seq 30); do
|
||||
[ -s "$onion_hostname" ] && break
|
||||
${pkgs.coreutils}/bin/sleep 1
|
||||
done
|
||||
if [ -s "$onion_hostname" ]; then
|
||||
ONION_ADDRESS=$(${pkgs.coreutils}/bin/tr -d '[:space:]' < "$onion_hostname")
|
||||
else
|
||||
echo "proxy-suite: proxy-suite-tor has not written $onion_hostname; no onion share links until a restart" >&2
|
||||
fi
|
||||
''
|
||||
else
|
||||
''ONION_ADDRESS=""'';
|
||||
|
||||
# Outbounds pinned with `via = "<tag>"`, rendered at start so urlFile contents stay
|
||||
# out of the store.
|
||||
mkViaOutboundBlock =
|
||||
@@ -131,10 +152,12 @@ let
|
||||
mkdir -p "$RUNTIME_DIR"
|
||||
|
||||
${serverAddressBlock}
|
||||
${onionAddressBlock}
|
||||
|
||||
RENDERED=$(PYTHONPATH="${parserScriptsPythonPath}" ${python3} ${buildInboundPy} \
|
||||
--spec ${proxyInboundsSpecFile} \
|
||||
--server-address "$SERVER_ADDRESS")
|
||||
--server-address "$SERVER_ADDRESS" \
|
||||
--onion-address "$ONION_ADDRESS")
|
||||
|
||||
INBOUNDS_JSON=$(${jq} -c '.inbounds' <<< "$RENDERED")
|
||||
|
||||
|
||||
@@ -104,7 +104,7 @@ else
|
||||
| .dns.final = $dns_final
|
||||
| if $clear_dns_rules then
|
||||
.dns.rules = .dns.rules[:${toString (builtins.length userDnsRules)}]
|
||||
+ [.dns.rules[${toString (builtins.length userDnsRules)}:][] | select(.server? == "fakeip")]
|
||||
+ [.dns.rules[${toString (builtins.length userDnsRules)}:][] | select(.server? == "fakeip" or .domain_suffix? == ["onion"])]
|
||||
else . end
|
||||
else . end
|
||||
# autoProxy rules, after the route-mode replace so no mode drops them: pins first
|
||||
@@ -118,10 +118,13 @@ else
|
||||
# inbounds.routing.blockPrivate, enforced here because the listener passes names unresolved.
|
||||
# Just before the first direct rule (or last, for a direct final), so names that the
|
||||
# proxy rules above take stay unresolved and none reaches a direct dial unchecked. Local
|
||||
# mixed-in clients lose names that resolve private.
|
||||
# mixed-in clients lose names that resolve private. Rules for other inbounds do not count:
|
||||
# autoProxy's direct probe pin would put the guard first, resolving every name, .onion too.
|
||||
| [{inbound: ["mixed-in"], action: "resolve"},
|
||||
{inbound: ["mixed-in"], ip_is_private: true, action: "reject"}] as $guard
|
||||
| (.route.rules | map((.outbound? // "") == "direct") | index(true)) as $first_direct
|
||||
| (.route.rules
|
||||
| map((.outbound? // "") == "direct" and ((.inbound? // ["mixed-in"]) | index(["mixed-in"])) != null)
|
||||
| index(true)) as $first_direct
|
||||
| if $first_direct != null then
|
||||
.route.rules = .route.rules[:$first_direct] + $guard + .route.rules[$first_direct:]
|
||||
elif (.route.final // "direct") == "direct" then
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
proxyCfg,
|
||||
sshProxyCfg,
|
||||
warpCfg,
|
||||
torCfg,
|
||||
awgOutbounds,
|
||||
constants,
|
||||
pureXrayEnabled,
|
||||
@@ -27,6 +28,7 @@
|
||||
|
||||
let
|
||||
sshProxyTag = "ssh-proxy";
|
||||
torOutboundEnabled = torCfg != null && torCfg.enable && torCfg.asOutbound;
|
||||
|
||||
# sing-box domain strategies in XRay's names (UseIPv4v6 = prefer IPv4).
|
||||
xrayDomainStrategies = {
|
||||
@@ -202,7 +204,7 @@ let
|
||||
tag="''${f##*/}"
|
||||
tag="''${tag%.*}"
|
||||
case "$tag" in
|
||||
proxy | direct | block)
|
||||
proxy | direct | block${lib.optionalString torOutboundEnabled " | tor"})
|
||||
echo "proxy-suite: warning: ignoring runtime outbound '$tag': reserved name" >&2
|
||||
continue
|
||||
;;
|
||||
@@ -528,10 +530,11 @@ let
|
||||
|
||||
# What selection may pick on its own: all but proxy.selectionExclude and disabled
|
||||
# outbounds. A pin still reaches the excluded ones, and a selector switched by hand both.
|
||||
# Tor is only for what is routed to it, unless it is the one outbound there is.
|
||||
selectableBlock = ''
|
||||
SELECTABLE_TAGS_JSON=$(${jq} -c --argjson ex ${lib.escapeShellArg (builtins.toJSON proxyCfg.selectionExclude)} \
|
||||
--argjson disabled "$DISABLED_TAGS_JSON" \
|
||||
'map(select(. as $t | ($ex + $disabled) | index([$t]) | not))' <<< "$OUTBOUND_TAGS_JSON")
|
||||
'${lib.optionalString torOutboundEnabled "(if length > 1 then [\"tor\"] else [] end) as $tor | "}map(select(. as $t | ($ex + $disabled${lib.optionalString torOutboundEnabled " + $tor"}) | index([$t]) | not))' <<< "$OUTBOUND_TAGS_JSON")
|
||||
if [ -z "$PINNED_OUTBOUND" ] && [ "$(${jq} 'length' <<< "$SELECTABLE_TAGS_JSON")" -eq 0 ]; then
|
||||
echo "proxy-suite: every outbound is in proxy.selectionExclude or disabled, so there is nothing to select; pin one, enable one (proxy-ctl proxy outbounds enable), or exclude fewer" >&2
|
||||
exit 1
|
||||
@@ -573,6 +576,9 @@ let
|
||||
warpBlock = lib.optionalString (warpCfg.enable && warpCfg.asOutbound == "singBox") (
|
||||
mkTunnelOutboundBlock "proxy-suite-warp-tunnel" "warp" "warp" warpCfg.tunnelPort
|
||||
);
|
||||
torBlock = lib.optionalString torOutboundEnabled (
|
||||
mkTunnelOutboundBlock "proxy-suite-tor" "tor" "tor" torCfg.socksPort
|
||||
);
|
||||
awgBlocks = lib.concatMapStrings (mkAwgOutboundBlock routingMark) awgOutbounds;
|
||||
|
||||
wrapperBlock =
|
||||
@@ -639,7 +645,9 @@ let
|
||||
# Real exits for autoProxy, taken after the wrapper may have renamed one to
|
||||
# "proxy".
|
||||
exitTagsBlock = ''
|
||||
EXIT_TAGS_JSON=$(${jq} -c '[.[] | select(.type != "selector" and .type != "urltest") | .tag]' <<< "$OUTBOUNDS_JSON")
|
||||
EXIT_TAGS_JSON=$(${jq} -c '[.[] | select(.type != "selector" and .type != "urltest"${
|
||||
lib.optionalString torOutboundEnabled " and (.tag | ltrimstr(\"proxy-suite-ob-\")) != \"tor\""
|
||||
}) | .tag]' <<< "$OUTBOUNDS_JSON")
|
||||
'';
|
||||
in
|
||||
mkUrlOutboundHelpersBlock routingMark
|
||||
@@ -650,6 +658,7 @@ let
|
||||
+ runtimeOutboundsBlock
|
||||
+ sshProxyBlock
|
||||
+ warpBlock
|
||||
+ torBlock
|
||||
+ awgBlocks
|
||||
+ detourBlock
|
||||
+ requireOutboundsBlock
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
proxyCfg,
|
||||
sshProxyCfg,
|
||||
warpCfg,
|
||||
torCfg,
|
||||
awgOutbounds,
|
||||
xrayEnabled,
|
||||
hybridEnabled,
|
||||
@@ -32,6 +33,7 @@
|
||||
proxyInboundsAwg,
|
||||
awgBin,
|
||||
proxyInboundsNeedLocalProxy,
|
||||
torOnionEnabled,
|
||||
proxyInboundsGuardPrivate,
|
||||
proxyInboundViaOutbounds,
|
||||
userControlAllows,
|
||||
@@ -115,6 +117,7 @@ let
|
||||
proxyCfg
|
||||
sshProxyCfg
|
||||
warpCfg
|
||||
torCfg
|
||||
awgOutbounds
|
||||
constants
|
||||
pureXrayEnabled
|
||||
@@ -202,6 +205,7 @@ let
|
||||
proxyInboundsAwg
|
||||
awgBin
|
||||
proxyInboundsNeedLocalProxy
|
||||
torOnionEnabled
|
||||
proxyInboundViaOutbounds
|
||||
userControlCfg
|
||||
userControlAllows
|
||||
|
||||
@@ -13,6 +13,8 @@
|
||||
perAppZapretEnabled,
|
||||
sshProxyOutboundEnabled,
|
||||
sshProxyUnitEnabled,
|
||||
torOutboundEnabled,
|
||||
torOnionEnabled,
|
||||
proxyInboundsEnabled,
|
||||
proxyInboundsNeedLocalProxy,
|
||||
scripts,
|
||||
@@ -76,10 +78,12 @@ let
|
||||
"network-online.target"
|
||||
]
|
||||
++ lib.optional (sshProxyOutboundEnabled && sshProxyUnitEnabled) "proxy-suite-ssh-proxy.service";
|
||||
# Not after Tor: it may reach its relays through this listener.
|
||||
wants = [
|
||||
"network-online.target"
|
||||
]
|
||||
++ lib.optional (sshProxyOutboundEnabled && sshProxyUnitEnabled) "proxy-suite-ssh-proxy.service";
|
||||
++ lib.optional (sshProxyOutboundEnabled && sshProxyUnitEnabled) "proxy-suite-ssh-proxy.service"
|
||||
++ lib.optional torOutboundEnabled "proxy-suite-tor.service";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
execStart = scripts.startSocks;
|
||||
runtimeDirectory = serviceNames.socks;
|
||||
@@ -97,11 +101,14 @@ let
|
||||
]
|
||||
# After the client stack, but not `requires`: direct listeners keep serving
|
||||
# without it.
|
||||
++ lib.optional proxyInboundsNeedLocalProxy "${serviceNames.socks}.service";
|
||||
++ lib.optional proxyInboundsNeedLocalProxy "${serviceNames.socks}.service"
|
||||
# For the onion address in the share links.
|
||||
++ lib.optional torOnionEnabled "proxy-suite-tor.service";
|
||||
wants = [
|
||||
"network-online.target"
|
||||
]
|
||||
++ lib.optional proxyInboundsNeedLocalProxy "${serviceNames.socks}.service";
|
||||
++ lib.optional proxyInboundsNeedLocalProxy "${serviceNames.socks}.service"
|
||||
++ lib.optional torOnionEnabled "proxy-suite-tor.service";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
execStart = scripts.startInbounds;
|
||||
runtimeDirectory = serviceNames.inbounds;
|
||||
|
||||
Reference in new issue
Block a user