fix: truly fixing xray tun
This commit is contained in:
6 files changed
+392
-49
No files matched your search
@@ -99,13 +99,18 @@ let
|
||||
# packets intended for per-app routing tables.
|
||||
xrayTunPerAppTproxyRulePriority = 8996;
|
||||
xrayTunPerAppTunRulePriority = 8997;
|
||||
xrayGlobalTunIPv6Address = "fd66:19::1/64";
|
||||
xrayGlobalTunIPv6RoutePrefix = "fd66:19::/64";
|
||||
|
||||
xrayTunUpScript = pkgs.writeShellScript "proxy-suite-xray-tun-up" ''
|
||||
set -euo pipefail
|
||||
|
||||
tun_cidr=${lib.escapeShellArg globalTun.address}
|
||||
tun6_cidr=${lib.escapeShellArg xrayGlobalTunIPv6Address}
|
||||
tun6_route_prefix=${lib.escapeShellArg xrayGlobalTunIPv6RoutePrefix}
|
||||
tun_addr=""
|
||||
tun_route_prefix=""
|
||||
uplink_addr=""
|
||||
|
||||
cidr_network() {
|
||||
local cidr="$1"
|
||||
@@ -142,23 +147,45 @@ let
|
||||
exit 1
|
||||
fi
|
||||
|
||||
uplink_addr="$(${ip} -4 route get 1.1.1.1 2>/dev/null | ${pkgs.gawk}/bin/awk '
|
||||
/src/ {
|
||||
for (i = 1; i <= NF; i++) {
|
||||
if ($i == "src" && i + 1 <= NF) {
|
||||
print $(i + 1)
|
||||
exit
|
||||
}
|
||||
}
|
||||
}
|
||||
')"
|
||||
if [ -z "$uplink_addr" ]; then
|
||||
echo "proxy-suite: could not determine the default uplink IPv4 address for XRay TUN" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
while ${ip} -4 rule del pref ${toString xrayTunPerAppTproxyRulePriority} 2>/dev/null; do :; done
|
||||
while ${ip} -4 rule del pref ${toString xrayTunPerAppTunRulePriority} 2>/dev/null; do :; done
|
||||
while ${ip} -4 rule del pref ${toString tunAutoRouteRulePriority} 2>/dev/null; do :; done
|
||||
while ${ip} -6 rule del pref ${toString xrayTunPerAppTunRulePriority} 2>/dev/null; do :; done
|
||||
while ${ip} -6 rule del pref ${toString tunAutoRouteRulePriority} 2>/dev/null; do :; done
|
||||
|
||||
tun_addr="''${tun_cidr%%/*}"
|
||||
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}
|
||||
${ip} -4 route replace "$tun_route_prefix" dev ${lib.escapeShellArg globalTun.interface} src "$tun_addr" table ${toString tunAutoRouteTableIndex}
|
||||
${ip} -4 route replace default dev ${lib.escapeShellArg globalTun.interface} table ${toString tunAutoRouteTableIndex}
|
||||
${ip} -4 route replace default dev ${lib.escapeShellArg globalTun.interface} src "$uplink_addr" 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 perAppRoutingTun.enable ''
|
||||
${ip} -4 rule add pref ${toString xrayTunPerAppTunRulePriority} fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable}
|
||||
${ip} -6 rule add pref ${toString xrayTunPerAppTunRulePriority} fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable}
|
||||
''}
|
||||
${ip} -4 rule add pref ${toString tunAutoRouteRulePriority} not fwmark ${toString globalTproxy.proxyMark} table ${toString tunAutoRouteTableIndex}
|
||||
${ip} -6 rule add pref ${toString tunAutoRouteRulePriority} not fwmark ${toString globalTproxy.proxyMark} table ${toString tunAutoRouteTableIndex}
|
||||
'';
|
||||
|
||||
tproxyUpScript = pkgs.writeShellScript "proxy-suite-tproxy-up" ''
|
||||
@@ -196,10 +223,12 @@ let
|
||||
while ${ip} -6 rule del table ${toString tunAutoRouteTableIndex} 2>/dev/null; do :; done
|
||||
while ${ip} -4 rule del pref ${toString xrayTunPerAppTproxyRulePriority} 2>/dev/null; do :; done
|
||||
while ${ip} -4 rule del pref ${toString xrayTunPerAppTunRulePriority} 2>/dev/null; do :; done
|
||||
while ${ip} -6 rule del pref ${toString xrayTunPerAppTunRulePriority} 2>/dev/null; do :; done
|
||||
${ip} -4 route flush table ${toString tunAutoRouteTableIndex} 2>/dev/null || true
|
||||
${ip} -6 route flush table ${toString tunAutoRouteTableIndex} 2>/dev/null || true
|
||||
|
||||
${ip} link del dev ${lib.escapeShellArg globalTun.interface} 2>/dev/null || true
|
||||
${pkgs.systemd}/bin/resolvectl flush-caches 2>/dev/null || true
|
||||
'';
|
||||
|
||||
systemServiceEntries = [
|
||||
|
||||
@@ -98,6 +98,8 @@ let
|
||||
hasZapretProfiles = builtins.any (
|
||||
profile: profile.route == "zapret"
|
||||
) effectivePerAppRoutingProfiles;
|
||||
xrayPerAppTunIPv6Address = "fd66:20::1/64";
|
||||
xrayPerAppTunIPv6RoutePrefix = "fd66:20::/64";
|
||||
|
||||
perAppTunWaitForInterface = pkgs.writeShellScript "proxy-suite-per-app-tun-wait-for-interface" ''
|
||||
set -euo pipefail
|
||||
@@ -115,8 +117,11 @@ let
|
||||
set -euo pipefail
|
||||
|
||||
tun_cidr=${lib.escapeShellArg perAppRoutingTun.address}
|
||||
tun6_cidr=${lib.escapeShellArg xrayPerAppTunIPv6Address}
|
||||
tun6_route_prefix=${lib.escapeShellArg xrayPerAppTunIPv6RoutePrefix}
|
||||
tun_addr=""
|
||||
tun_route_prefix=""
|
||||
uplink_addr=""
|
||||
|
||||
cidr_network() {
|
||||
local cidr="$1"
|
||||
@@ -142,16 +147,36 @@ let
|
||||
}
|
||||
|
||||
${nft} delete table inet proxy_suite_per_app_tun 2>/dev/null || true
|
||||
while ${ip} rule del fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable} 2>/dev/null; do :; done
|
||||
${ip} route flush table ${toString perAppRoutingTun.routeTable} 2>/dev/null || true
|
||||
while ${ip} -4 rule del fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable} 2>/dev/null; do :; done
|
||||
while ${ip} -6 rule del fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable} 2>/dev/null; do :; done
|
||||
${ip} -4 route flush table ${toString perAppRoutingTun.routeTable} 2>/dev/null || true
|
||||
${ip} -6 route flush table ${toString perAppRoutingTun.routeTable} 2>/dev/null || true
|
||||
${nft} -f ${perAppTunChainFile}
|
||||
${perAppTunWaitForInterface}
|
||||
uplink_addr="$(${ip} -4 route get 1.1.1.1 2>/dev/null | ${awk} '
|
||||
/src/ {
|
||||
for (i = 1; i <= NF; i++) {
|
||||
if ($i == "src" && i + 1 <= NF) {
|
||||
print $(i + 1)
|
||||
exit
|
||||
}
|
||||
}
|
||||
}
|
||||
')"
|
||||
if [ -z "$uplink_addr" ]; then
|
||||
echo "proxy-suite: could not determine the default uplink IPv4 address for app TUN" >&2
|
||||
exit 1
|
||||
fi
|
||||
tun_addr="''${tun_cidr%%/*}"
|
||||
tun_route_prefix="$(cidr_network "$tun_cidr")"
|
||||
${ip} -4 addr replace "$tun_cidr" dev ${lib.escapeShellArg perAppRoutingTun.interface}
|
||||
${ip} route replace "$tun_route_prefix" dev ${lib.escapeShellArg perAppRoutingTun.interface} src "$tun_addr" table ${toString perAppRoutingTun.routeTable}
|
||||
${ip} route replace default dev ${lib.escapeShellArg perAppRoutingTun.interface} table ${toString perAppRoutingTun.routeTable}
|
||||
${ip} rule add fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable} 2>/dev/null || true
|
||||
${ip} -6 addr replace "$tun6_cidr" dev ${lib.escapeShellArg perAppRoutingTun.interface}
|
||||
${ip} -4 route replace "$tun_route_prefix" dev ${lib.escapeShellArg perAppRoutingTun.interface} src "$tun_addr" table ${toString perAppRoutingTun.routeTable}
|
||||
${ip} -4 route replace default dev ${lib.escapeShellArg perAppRoutingTun.interface} src "$uplink_addr" table ${toString perAppRoutingTun.routeTable}
|
||||
${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} -4 rule add fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable} 2>/dev/null || true
|
||||
${ip} -6 rule add fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable} 2>/dev/null || true
|
||||
'';
|
||||
|
||||
perAppTunDownScript = pkgs.writeShellScript "proxy-suite-per-app-tun-down" ''
|
||||
@@ -159,9 +184,12 @@ let
|
||||
|
||||
# Best-effort cleanup for graceful stops and for unclean previous exits.
|
||||
${nft} delete table inet proxy_suite_per_app_tun 2>/dev/null || true
|
||||
while ${ip} rule del fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable} 2>/dev/null; do :; done
|
||||
${ip} route flush table ${toString perAppRoutingTun.routeTable} 2>/dev/null || true
|
||||
while ${ip} -4 rule del fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable} 2>/dev/null; do :; done
|
||||
while ${ip} -6 rule del fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable} 2>/dev/null; do :; done
|
||||
${ip} -4 route flush table ${toString perAppRoutingTun.routeTable} 2>/dev/null || true
|
||||
${ip} -6 route flush table ${toString perAppRoutingTun.routeTable} 2>/dev/null || true
|
||||
${ip} link del dev ${lib.escapeShellArg perAppRoutingTun.interface} 2>/dev/null || true
|
||||
${pkgs.systemd}/bin/resolvectl flush-caches 2>/dev/null || true
|
||||
'';
|
||||
|
||||
perAppTproxyUpScript = pkgs.writeShellScript "proxy-suite-per-app-tproxy-up" ''
|
||||
|
||||
@@ -354,7 +354,7 @@ let
|
||||
if xrayEnabled then
|
||||
''
|
||||
def xray_bind_outbound($interface):
|
||||
if $interface == "" or .protocol == "blackhole" then
|
||||
if $interface == "" or .protocol == "blackhole" or .protocol == "dns" then
|
||||
.
|
||||
else
|
||||
.streamSettings = (.streamSettings // {})
|
||||
@@ -377,20 +377,47 @@ let
|
||||
else
|
||||
.
|
||||
end;
|
||||
def xray_preserved_rules:
|
||||
[.routing.rules[]
|
||||
| select(((.ruleTag? // "") == "dns-hijack") or ((.ruleTag? // "") == "dns-upstream-direct"))];
|
||||
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)
|
||||
else
|
||||
{type:"field",network:"tcp,udp",ruleTag:"final-default",outboundTag:$tag}
|
||||
end;
|
||||
def xray_dns_server_order($dns_final):
|
||||
if $dns_final == "" then
|
||||
.dns.servers
|
||||
else
|
||||
([.dns.servers[] | select((.tag? // "") == "fakedns")]
|
||||
+ [.dns.servers[] | select((.tag? // "") == $dns_final)]
|
||||
+ [.dns.servers[] | select((.tag? // "") != "fakedns" and (.tag? // "") != $dns_final)])
|
||||
end;
|
||||
def xray_tun_dns_addresses($dns_final; $local_addr; $remote_addr):
|
||||
if $dns_final == "local" then
|
||||
[$local_addr, $remote_addr]
|
||||
elif $dns_final == "remote" then
|
||||
[$remote_addr, $local_addr]
|
||||
else
|
||||
[$remote_addr, $local_addr]
|
||||
end;
|
||||
.outbounds = (($obs + .outbounds) | map(xray_bind_outbound($xray_bind_interface)))
|
||||
| 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.users) = [{user:$user,pass:$password}]
|
||||
else . end
|
||||
| if $xray_tun_dns_runtime then
|
||||
.dns.servers = xray_dns_server_order($dns_final)
|
||||
| if $dns_final == "" then
|
||||
.
|
||||
else
|
||||
(.inbounds[] | select(.protocol == "tun" and .tag == "tun-in") | .settings.dns) = xray_tun_dns_addresses($dns_final; $xray_dns_local_client; $xray_dns_remote_client)
|
||||
end
|
||||
else . end
|
||||
| if $route_enabled then
|
||||
.routing.rules = ($route_rules + [xray_final_rule($route_final; $xray_single_proxy_tag)])
|
||||
.routing.rules = (xray_preserved_rules + $route_rules + [xray_final_rule($route_final; $xray_single_proxy_tag)])
|
||||
else . end
|
||||
| if $xray_single_proxy_tag == "" then
|
||||
.
|
||||
@@ -413,6 +440,7 @@ let
|
||||
| if $clear_dns_rules then .dns.rules = [] else . end
|
||||
else . end
|
||||
'';
|
||||
backendJqFilterFile = pkgs.writeText "proxy-suite-${backend}-backend-filter.jq" mkBackendJqFilter;
|
||||
|
||||
mkStartScript =
|
||||
{
|
||||
@@ -422,11 +450,13 @@ let
|
||||
routingMark ? null,
|
||||
enableLocalProxyAuth ? false,
|
||||
xrayTunEgressBinding ? false,
|
||||
xrayTunDnsRuntime ? false,
|
||||
}:
|
||||
pkgs.writeShellScript name ''
|
||||
set -euo pipefail
|
||||
RUNTIME_DIR="${runtimeDir}"
|
||||
ROUTE_MODE_STATE_FILE="${routeModeStateFile}"
|
||||
BACKEND_JQ_FILTER=${lib.escapeShellArg backendJqFilterFile}
|
||||
XRAY_LOGLEVEL=""
|
||||
XRAY_TUN_BIND_INTERFACE=""
|
||||
XRAY_SINGLE_PROXY_TAG=""
|
||||
@@ -483,7 +513,10 @@ let
|
||||
--arg xray_loglevel "$XRAY_LOGLEVEL" \
|
||||
--arg xray_bind_interface "$XRAY_TUN_BIND_INTERFACE" \
|
||||
--arg xray_single_proxy_tag "$XRAY_SINGLE_PROXY_TAG" \
|
||||
'${mkBackendJqFilter}' \
|
||||
--argjson xray_tun_dns_runtime ${if xrayTunDnsRuntime then "true" else "false"} \
|
||||
--arg xray_dns_local_client ${lib.escapeShellArg proxyCfg.dns.local.address} \
|
||||
--arg xray_dns_remote_client ${lib.escapeShellArg proxyCfg.dns.remote.address} \
|
||||
-f "$BACKEND_JQ_FILTER" \
|
||||
"${configFile}" > "$RUNTIME_DIR/config.json"
|
||||
${lib.optionalString enableLocalProxyAuth ''
|
||||
chmod 600 "$RUNTIME_DIR/config.json"
|
||||
@@ -506,14 +539,16 @@ let
|
||||
configFile = tunFile;
|
||||
routingMark = if xrayEnabled then globalTproxy.proxyMark else null;
|
||||
xrayTunEgressBinding = xrayEnabled;
|
||||
xrayTunDnsRuntime = xrayEnabled;
|
||||
};
|
||||
|
||||
startPerAppTun = mkStartScript {
|
||||
name = "proxy-suite-start-per-app-tun";
|
||||
runtimeDir = "/run/proxy-suite-per-app-tun";
|
||||
configFile = perAppTunFile;
|
||||
routingMark = if globalTproxy.enable then globalTproxy.proxyMark else null;
|
||||
routingMark = if xrayEnabled || globalTproxy.enable then globalTproxy.proxyMark else null;
|
||||
xrayTunEgressBinding = xrayEnabled;
|
||||
xrayTunDnsRuntime = xrayEnabled;
|
||||
};
|
||||
|
||||
mkSubscriptionFetchBlock =
|
||||
|
||||
Reference in new issue
Block a user