fix: overall tun/tproxy improvements
This commit is contained in:
29 files changed
+445
-149
No files matched your search
@@ -247,12 +247,40 @@ rec {
|
||||
mkIpLocalDefaultRouteDelete =
|
||||
{
|
||||
ip,
|
||||
family ? "",
|
||||
table,
|
||||
}:
|
||||
''
|
||||
${ip} route del local default dev lo table ${toString table} 2>/dev/null || true
|
||||
${ip} ${
|
||||
lib.optionalString (family != "") "${family} "
|
||||
}route del local default dev lo table ${toString table} 2>/dev/null || true
|
||||
'';
|
||||
|
||||
# The TProxy fwmark rule and local route: IPv4 always, IPv6 with proxy.ipv6.
|
||||
# Cleanup drops both families, whatever the setting was.
|
||||
mkTproxyRoutingDown =
|
||||
{
|
||||
ip,
|
||||
fwmark,
|
||||
table,
|
||||
}:
|
||||
lib.concatMapStrings (family: ''
|
||||
${mkIpRuleDeleteByFwmark { inherit ip family fwmark table; }}
|
||||
${mkIpLocalDefaultRouteDelete { inherit ip family table; }}
|
||||
'') [ "-4" "-6" ];
|
||||
|
||||
mkTproxyRoutingUp =
|
||||
{
|
||||
ip,
|
||||
ipv6,
|
||||
fwmark,
|
||||
table,
|
||||
}:
|
||||
lib.concatMapStrings (family: ''
|
||||
${ip} ${family} route replace local default dev lo table ${toString table}
|
||||
${ip} ${family} rule add fwmark ${toString fwmark} table ${toString table}
|
||||
'') ([ "-4" ] ++ lib.optional ipv6 "-6");
|
||||
|
||||
mkIpLinkDelete =
|
||||
{
|
||||
ip,
|
||||
|
||||
@@ -145,7 +145,6 @@ let
|
||||
cfg
|
||||
singBoxCfg
|
||||
proxyCfg
|
||||
pureXrayEnabled
|
||||
perAppRoutingCfg
|
||||
perAppRoutingTun
|
||||
perAppRoutingTproxy
|
||||
|
||||
@@ -89,8 +89,8 @@ let
|
||||
globalTun
|
||||
globalTproxy
|
||||
perAppRoutingTun
|
||||
perAppRoutingTproxy
|
||||
;
|
||||
inherit (proxyCfg) ipv6;
|
||||
};
|
||||
|
||||
serviceUnits = import ./units.nix {
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
cfg,
|
||||
singBoxCfg,
|
||||
proxyCfg,
|
||||
pureXrayEnabled,
|
||||
perAppRoutingCfg,
|
||||
perAppRoutingTun,
|
||||
perAppRoutingTproxy,
|
||||
@@ -60,7 +59,6 @@ let
|
||||
lib
|
||||
pkgs
|
||||
builders
|
||||
pureXrayEnabled
|
||||
perAppRoutingTun
|
||||
perAppRoutingTproxy
|
||||
constants
|
||||
@@ -71,6 +69,7 @@ let
|
||||
seqBin
|
||||
sleepBin
|
||||
;
|
||||
inherit (proxyCfg) ipv6;
|
||||
};
|
||||
inherit (backendScripts)
|
||||
perAppTunUpScript
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
lib,
|
||||
pkgs,
|
||||
builders,
|
||||
pureXrayEnabled,
|
||||
perAppRoutingTun,
|
||||
perAppRoutingTproxy,
|
||||
ipv6,
|
||||
constants,
|
||||
perAppTunChainFile,
|
||||
perAppTproxyRulesFile,
|
||||
@@ -17,8 +17,8 @@
|
||||
|
||||
let
|
||||
inherit (constants)
|
||||
xrayPerAppTunIPv6Address
|
||||
xrayPerAppTunIPv6RoutePrefix
|
||||
perAppTunIPv6Address
|
||||
perAppTunIPv6RoutePrefix
|
||||
;
|
||||
|
||||
perAppTunWaitForInterface = pkgs.writeShellScript "proxy-suite-per-app" ''
|
||||
@@ -37,8 +37,8 @@ let
|
||||
set -euo pipefail
|
||||
|
||||
tun_cidr=${lib.escapeShellArg perAppRoutingTun.address}
|
||||
tun6_cidr=${lib.escapeShellArg xrayPerAppTunIPv6Address}
|
||||
tun6_route_prefix=${lib.escapeShellArg xrayPerAppTunIPv6RoutePrefix}
|
||||
tun6_cidr=${lib.escapeShellArg perAppTunIPv6Address}
|
||||
tun6_route_prefix=${lib.escapeShellArg perAppTunIPv6RoutePrefix}
|
||||
tun_addr=""
|
||||
tun_route_prefix=""
|
||||
|
||||
@@ -68,12 +68,21 @@ let
|
||||
# No uplink src, as in xrayTunUpScript: it goes stale when the uplink address changes.
|
||||
${ip} -4 route replace default dev ${lib.escapeShellArg perAppRoutingTun.interface} table ${toString perAppRoutingTun.routeTable}
|
||||
${ip} -4 rule add fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable} 2>/dev/null || true
|
||||
${lib.optionalString pureXrayEnabled ''
|
||||
${ip} -6 addr replace "$tun6_cidr" dev ${lib.escapeShellArg perAppRoutingTun.interface}
|
||||
${ip} -6 route replace "$tun6_route_prefix" dev ${lib.escapeShellArg perAppRoutingTun.interface} table ${toString perAppRoutingTun.routeTable}
|
||||
${ip} -6 route replace default dev ${lib.escapeShellArg perAppRoutingTun.interface} table ${toString perAppRoutingTun.routeTable}
|
||||
${ip} -6 rule add fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable} 2>/dev/null || true
|
||||
''}
|
||||
${
|
||||
if ipv6 then
|
||||
''
|
||||
${ip} -6 addr replace "$tun6_cidr" dev ${lib.escapeShellArg perAppRoutingTun.interface}
|
||||
${ip} -6 route replace "$tun6_route_prefix" dev ${lib.escapeShellArg perAppRoutingTun.interface} table ${toString perAppRoutingTun.routeTable}
|
||||
${ip} -6 route replace default dev ${lib.escapeShellArg perAppRoutingTun.interface} table ${toString perAppRoutingTun.routeTable}
|
||||
''
|
||||
else
|
||||
''
|
||||
# No IPv6 in the app TUN: unreachable, so wrapped apps fall back to IPv4 instead of
|
||||
# leaving directly.
|
||||
${ip} -6 route replace unreachable default table ${toString perAppRoutingTun.routeTable}
|
||||
''
|
||||
}
|
||||
${ip} -6 rule add fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable} 2>/dev/null || true
|
||||
'';
|
||||
|
||||
perAppTunDownScript = pkgs.writeShellScript "proxy-suite-per-app" ''
|
||||
@@ -102,29 +111,31 @@ let
|
||||
perAppTproxyUpScript = pkgs.writeShellScript "proxy-suite-per-app" ''
|
||||
set -euo pipefail
|
||||
|
||||
${builders.mkNftDeleteTable { inherit nft; family = "ip"; table = "proxy_suite_per_app_tproxy"; }}
|
||||
${builders.mkIpRuleDeleteByFwmark {
|
||||
${builders.mkNftDeleteTable { inherit nft; family = "inet"; table = "proxy_suite_per_app_tproxy"; }}
|
||||
${builders.mkTproxyRoutingDown {
|
||||
inherit ip;
|
||||
fwmark = perAppRoutingTproxy.fwmark;
|
||||
table = perAppRoutingTproxy.routeTable;
|
||||
}}
|
||||
${builders.mkIpLocalDefaultRouteDelete { inherit ip; table = perAppRoutingTproxy.routeTable; }}
|
||||
|
||||
${nft} -f ${perAppTproxyRulesFile}
|
||||
${ip} route replace local default dev lo table ${toString perAppRoutingTproxy.routeTable}
|
||||
${ip} rule add fwmark ${toString perAppRoutingTproxy.fwmark} table ${toString perAppRoutingTproxy.routeTable}
|
||||
${builders.mkTproxyRoutingUp {
|
||||
inherit ip;
|
||||
inherit ipv6;
|
||||
fwmark = perAppRoutingTproxy.fwmark;
|
||||
table = perAppRoutingTproxy.routeTable;
|
||||
}}
|
||||
'';
|
||||
|
||||
perAppTproxyDownScript = pkgs.writeShellScript "proxy-suite-per-app" ''
|
||||
set +e
|
||||
|
||||
${builders.mkNftDeleteTable { inherit nft; family = "ip"; table = "proxy_suite_per_app_tproxy"; }}
|
||||
${builders.mkIpRuleDeleteByFwmark {
|
||||
${builders.mkNftDeleteTable { inherit nft; family = "inet"; table = "proxy_suite_per_app_tproxy"; }}
|
||||
${builders.mkTproxyRoutingDown {
|
||||
inherit ip;
|
||||
fwmark = perAppRoutingTproxy.fwmark;
|
||||
table = perAppRoutingTproxy.routeTable;
|
||||
}}
|
||||
${builders.mkIpLocalDefaultRouteDelete { inherit ip; table = perAppRoutingTproxy.routeTable; }}
|
||||
'';
|
||||
in
|
||||
{
|
||||
|
||||
@@ -100,7 +100,7 @@ let
|
||||
|
||||
perAppTproxyUserRuleStart = mkUserRuleStart {
|
||||
name = "per-app-tproxy";
|
||||
nftFamily = "ip";
|
||||
nftFamily = "inet";
|
||||
nftTable = "proxy_suite_per_app_tproxy";
|
||||
nftChain = "output";
|
||||
sliceName = perAppTproxySliceName;
|
||||
@@ -109,7 +109,7 @@ let
|
||||
};
|
||||
perAppTproxyUserRuleStop = mkUserRuleStop {
|
||||
name = "per-app-tproxy";
|
||||
nftFamily = "ip";
|
||||
nftFamily = "inet";
|
||||
nftTable = "proxy_suite_per_app_tproxy";
|
||||
nftChain = "output";
|
||||
};
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
globalTun,
|
||||
globalTproxy,
|
||||
perAppRoutingTun,
|
||||
perAppRoutingTproxy,
|
||||
ipv6,
|
||||
}:
|
||||
|
||||
let
|
||||
@@ -18,12 +18,11 @@ let
|
||||
tunAutoRouteRulePriority
|
||||
xrayTunMarkBypassRulePriority
|
||||
xrayTunServiceUserRulePriority
|
||||
xrayTunPerAppTproxyRulePriority
|
||||
xrayTunPerAppTunRulePriority
|
||||
xrayTunDnsRulePriority
|
||||
xrayTunMainRulePriority
|
||||
xrayGlobalTunIPv6Address
|
||||
xrayGlobalTunIPv6RoutePrefix
|
||||
globalTunIPv6Address
|
||||
globalTunIPv6RoutePrefix
|
||||
;
|
||||
|
||||
deleteXrayTunRules = lib.concatMapStrings (
|
||||
@@ -31,8 +30,7 @@ let
|
||||
lib.concatMapStrings (priority: builders.mkIpRuleDeleteByPriority { inherit ip family priority; }) [
|
||||
xrayTunMarkBypassRulePriority
|
||||
xrayTunServiceUserRulePriority
|
||||
xrayTunPerAppTproxyRulePriority
|
||||
xrayTunPerAppTunRulePriority
|
||||
xrayTunPerAppTunRulePriority
|
||||
xrayTunDnsRulePriority
|
||||
xrayTunMainRulePriority
|
||||
tunAutoRouteRulePriority
|
||||
@@ -62,8 +60,8 @@ in
|
||||
set -euo pipefail
|
||||
|
||||
tun_cidr=${lib.escapeShellArg globalTun.address}
|
||||
tun6_cidr=${lib.escapeShellArg xrayGlobalTunIPv6Address}
|
||||
tun6_route_prefix=${lib.escapeShellArg xrayGlobalTunIPv6RoutePrefix}
|
||||
tun6_cidr=${lib.escapeShellArg globalTunIPv6Address}
|
||||
tun6_route_prefix=${lib.escapeShellArg globalTunIPv6RoutePrefix}
|
||||
tun_addr=""
|
||||
tun_route_prefix=""
|
||||
|
||||
@@ -89,15 +87,14 @@ in
|
||||
tun_route_prefix="$(cidr_network "$tun_cidr")"
|
||||
|
||||
${ip} -4 addr replace "$tun_cidr" dev ${lib.escapeShellArg globalTun.interface}
|
||||
${ip} -6 addr replace "$tun6_cidr" dev ${lib.escapeShellArg globalTun.interface}
|
||||
${lib.optionalString ipv6 ''${ip} -6 addr replace "$tun6_cidr" dev ${lib.escapeShellArg globalTun.interface}''}
|
||||
${ip} -4 route replace "$tun_route_prefix" dev ${lib.escapeShellArg globalTun.interface} src "$tun_addr" table ${toString tunAutoRouteTableIndex}
|
||||
# No uplink src: it goes stale when the uplink address changes, and XRay's own sockets
|
||||
# (routed by uid and mark, not bound to an interface) pick theirs from main.
|
||||
${ip} -4 route replace default dev ${lib.escapeShellArg globalTun.interface} table ${toString tunAutoRouteTableIndex}
|
||||
${ip} -6 route replace "$tun6_route_prefix" dev ${lib.escapeShellArg globalTun.interface} table ${toString tunAutoRouteTableIndex}
|
||||
${ip} -6 route replace default dev ${lib.escapeShellArg globalTun.interface} table ${toString tunAutoRouteTableIndex}
|
||||
${lib.optionalString perAppRoutingTproxy.enable ''
|
||||
${ip} -4 rule add pref ${toString xrayTunPerAppTproxyRulePriority} fwmark ${toString perAppRoutingTproxy.fwmark} table ${toString perAppRoutingTproxy.routeTable}
|
||||
${lib.optionalString ipv6 ''
|
||||
${ip} -6 route replace "$tun6_route_prefix" dev ${lib.escapeShellArg globalTun.interface} table ${toString tunAutoRouteTableIndex}
|
||||
${ip} -6 route replace default dev ${lib.escapeShellArg globalTun.interface} table ${toString tunAutoRouteTableIndex}
|
||||
''}
|
||||
${lib.optionalString perAppRoutingTun.enable ''
|
||||
${ip} -4 rule add pref ${toString xrayTunPerAppTunRulePriority} fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable}
|
||||
@@ -122,29 +119,31 @@ in
|
||||
# Start from a clean policy-routing state. `ip rule add` permits duplicate
|
||||
# rules on some iproute2 versions, and a stale rule can keep packets routed
|
||||
# into a dead local table after a failed restart.
|
||||
${builders.mkNftDeleteTable { inherit nft; family = "ip"; table = "singbox"; }}
|
||||
${builders.mkIpRuleDeleteByFwmark {
|
||||
${builders.mkNftDeleteTable { inherit nft; family = "inet"; table = "singbox"; }}
|
||||
${builders.mkTproxyRoutingDown {
|
||||
inherit ip;
|
||||
fwmark = globalTproxy.fwmark;
|
||||
table = globalTproxy.routeTable;
|
||||
}}
|
||||
${builders.mkIpLocalDefaultRouteDelete { inherit ip; table = globalTproxy.routeTable; }}
|
||||
|
||||
${nft} -f ${nftablesRulesFile}
|
||||
${ip} route replace local default dev lo table ${toString globalTproxy.routeTable}
|
||||
${ip} rule add fwmark ${toString globalTproxy.fwmark} table ${toString globalTproxy.routeTable}
|
||||
${builders.mkTproxyRoutingUp {
|
||||
inherit ip;
|
||||
inherit ipv6;
|
||||
fwmark = globalTproxy.fwmark;
|
||||
table = globalTproxy.routeTable;
|
||||
}}
|
||||
'';
|
||||
|
||||
tproxyDownScript = pkgs.writeShellScript "proxy-suite-routing" ''
|
||||
set +e
|
||||
|
||||
${builders.mkNftDeleteTable { inherit nft; family = "ip"; table = "singbox"; }}
|
||||
${builders.mkIpRuleDeleteByFwmark {
|
||||
${builders.mkNftDeleteTable { inherit nft; family = "inet"; table = "singbox"; }}
|
||||
${builders.mkTproxyRoutingDown {
|
||||
inherit ip;
|
||||
fwmark = globalTproxy.fwmark;
|
||||
table = globalTproxy.routeTable;
|
||||
}}
|
||||
${builders.mkIpLocalDefaultRouteDelete { inherit ip; table = globalTproxy.routeTable; }}
|
||||
'';
|
||||
|
||||
tunCleanupScript = pkgs.writeShellScript "proxy-suite-routing" ''
|
||||
|
||||
@@ -23,7 +23,7 @@ if pureXrayEnabled then
|
||||
end;
|
||||
def xray_preserved_rules:
|
||||
[.routing.rules[]
|
||||
| select(((.ruleTag? // "") == "dns-hijack") or ((.ruleTag? // "") == "dns-upstream-direct"))];
|
||||
| select((.ruleTag? // "") | . == "dns-hijack" or . == "dns-upstream-direct" or . == "dns-upstream-remote")];
|
||||
def xray_final_rule($tag; $single_tag):
|
||||
if $tag == "proxy" and "${selectionMode}" == "urltest" then
|
||||
{type:"field",network:"tcp,udp",ruleTag:"final-default"} + xray_proxy_rule_target($single_tag)
|
||||
@@ -38,18 +38,33 @@ if pureXrayEnabled then
|
||||
+ [.dns.servers[] | select((.tag? // "") == $dns_final)]
|
||||
+ [.dns.servers[] | select((.tag? // "") != "fakedns" and (.tag? // "") != $dns_final)])
|
||||
end;
|
||||
# Names of proxy servers, which XRay's DNS resolves once a dial has a domainStrategy.
|
||||
def xray_server_names:
|
||||
[.outbounds[].settings
|
||||
| (.vnext[]?.address, .servers[]?.address, .address?)
|
||||
| strings | select(test("^[0-9.]+$|:|^localhost$") | not)]
|
||||
| unique;
|
||||
# A copy of "local" answering only for them: routed direct by its tag, so looking up a
|
||||
# proxy server never waits on that proxy.
|
||||
def xray_pin_server_names:
|
||||
xray_server_names as $names
|
||||
| if $names == [] then .
|
||||
else .dns.servers += [.dns.servers[] | select((.tag? // "") == "local")
|
||||
| . + {domains: ($names | map("full:" + .)), skipFallback: true}]
|
||||
end;
|
||||
.outbounds = ($obs + .outbounds)
|
||||
| if $xray_loglevel == "" then . else .log.loglevel = $xray_loglevel end
|
||||
| if $auth_enabled then
|
||||
(.inbounds[] | select(.protocol == "socks" and .tag == "mixed-in") | .settings.auth) = "password"
|
||||
| (.inbounds[] | select(.protocol == "socks" and .tag == "mixed-in") | .settings.accounts) = [{user:$user,pass:$password}]
|
||||
else . end
|
||||
| .dns.servers = xray_dns_server_order($dns_final)
|
||||
| xray_pin_server_names
|
||||
| if $xray_tun_dns_runtime then
|
||||
.dns.servers = xray_dns_server_order($dns_final)
|
||||
# Without a domainStrategy a dial resolves through the system resolver, which under
|
||||
# the TUN (resolved's upstream queries included) hands out fake IPs. XRay's own DNS
|
||||
# client skips fakedns.
|
||||
| .outbounds |= map(
|
||||
# Without a domainStrategy a dial resolves through the system resolver, whose upstream
|
||||
# queries the TUN answers with fake IPs and TProxy sends through the proxy being
|
||||
# dialed. XRay's own DNS client skips fakedns.
|
||||
.outbounds |= map(
|
||||
if .protocol == "blackhole" or .protocol == "dns" or .protocol == "loopback" then .
|
||||
else .streamSettings.sockopt.domainStrategy //= "UseIPv4v6" end)
|
||||
else . end
|
||||
|
||||
@@ -413,6 +413,9 @@ let
|
||||
configFile = tproxyFile;
|
||||
routingMark = globalTproxy.proxyMark;
|
||||
enableLocalProxyAuth = localProxyAuthEnabled;
|
||||
# TProxy takes the system resolver's own upstream queries too, so a proxy server's name
|
||||
# has to resolve inside XRay, as under the TUN.
|
||||
xrayTunDnsRuntime = pureXrayEnabled && globalTproxy.enable;
|
||||
xraySidecarBasePort = xraySidecarBasePorts.socks;
|
||||
xrayDnsBridgePort = xrayDnsBridgePorts.socks;
|
||||
# Only the socks unit: relayed traffic reaches it, and the prober needs one home.
|
||||
|
||||
Reference in new issue
Block a user