fix: tun not working on xray backend
This commit is contained in:
7 files changed
+523
-45
No files matched your search
@@ -94,10 +94,42 @@ let
|
||||
# policy routing because XRay's autoSystemRoutingTable is not Linux-ready.
|
||||
tunAutoRouteTableIndex = 2022;
|
||||
tunAutoRouteRulePriority = 9000;
|
||||
# Keep app-scoped marks ahead of the global XRay TUN catch-all. Without
|
||||
# these priorities, iproute2's default priority lets global TUN capture
|
||||
# packets intended for per-app routing tables.
|
||||
xrayTunPerAppTproxyRulePriority = 8996;
|
||||
xrayTunPerAppTunRulePriority = 8997;
|
||||
|
||||
xrayTunUpScript = pkgs.writeShellScript "proxy-suite-xray-tun-up" ''
|
||||
set -euo pipefail
|
||||
|
||||
tun_cidr=${lib.escapeShellArg globalTun.address}
|
||||
tun_addr=""
|
||||
tun_route_prefix=""
|
||||
|
||||
cidr_network() {
|
||||
local cidr="$1"
|
||||
local addr="''${cidr%/*}"
|
||||
local prefix="''${cidr#*/}"
|
||||
local o1 o2 o3 o4 ip mask net
|
||||
|
||||
IFS=. read -r o1 o2 o3 o4 <<<"$addr"
|
||||
ip=$(((o1 << 24) | (o2 << 16) | (o3 << 8) | o4))
|
||||
if [ "$prefix" -eq 0 ]; then
|
||||
mask=0
|
||||
else
|
||||
mask=$(((0xFFFFFFFF << (32 - prefix)) & 0xFFFFFFFF))
|
||||
fi
|
||||
net=$((ip & mask))
|
||||
|
||||
printf '%d.%d.%d.%d/%s' \
|
||||
$(((net >> 24) & 255)) \
|
||||
$(((net >> 16) & 255)) \
|
||||
$(((net >> 8) & 255)) \
|
||||
$((net & 255)) \
|
||||
"$prefix"
|
||||
}
|
||||
|
||||
for _ in $(${pkgs.coreutils}/bin/seq 1 50); do
|
||||
if ${ip} link show dev ${lib.escapeShellArg globalTun.interface} >/dev/null 2>&1; then
|
||||
break
|
||||
@@ -110,8 +142,22 @@ let
|
||||
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
|
||||
|
||||
tun_addr="''${tun_cidr%%/*}"
|
||||
tun_route_prefix="$(cidr_network "$tun_cidr")"
|
||||
|
||||
${ip} -4 addr replace "$tun_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}
|
||||
${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} -4 rule add pref ${toString tunAutoRouteRulePriority} not fwmark ${toString globalTproxy.proxyMark} table ${toString tunAutoRouteTableIndex}
|
||||
'';
|
||||
|
||||
@@ -148,6 +194,8 @@ let
|
||||
|
||||
while ${ip} -4 rule del table ${toString tunAutoRouteTableIndex} 2>/dev/null; do :; done
|
||||
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
|
||||
${ip} -4 route flush table ${toString tunAutoRouteTableIndex} 2>/dev/null || true
|
||||
${ip} -6 route flush table ${toString tunAutoRouteTableIndex} 2>/dev/null || true
|
||||
|
||||
|
||||
@@ -113,9 +113,43 @@ let
|
||||
|
||||
perAppTunUpScript = pkgs.writeShellScript "proxy-suite-per-app-tun-up" ''
|
||||
set -euo pipefail
|
||||
|
||||
tun_cidr=${lib.escapeShellArg perAppRoutingTun.address}
|
||||
tun_addr=""
|
||||
tun_route_prefix=""
|
||||
|
||||
cidr_network() {
|
||||
local cidr="$1"
|
||||
local addr="''${cidr%/*}"
|
||||
local prefix="''${cidr#*/}"
|
||||
local o1 o2 o3 o4 ip mask net
|
||||
|
||||
IFS=. read -r o1 o2 o3 o4 <<<"$addr"
|
||||
ip=$(((o1 << 24) | (o2 << 16) | (o3 << 8) | o4))
|
||||
if [ "$prefix" -eq 0 ]; then
|
||||
mask=0
|
||||
else
|
||||
mask=$(((0xFFFFFFFF << (32 - prefix)) & 0xFFFFFFFF))
|
||||
fi
|
||||
net=$((ip & mask))
|
||||
|
||||
printf '%d.%d.%d.%d/%s' \
|
||||
$(((net >> 24) & 255)) \
|
||||
$(((net >> 16) & 255)) \
|
||||
$(((net >> 8) & 255)) \
|
||||
$((net & 255)) \
|
||||
"$prefix"
|
||||
}
|
||||
|
||||
${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
|
||||
${nft} -f ${perAppTunChainFile}
|
||||
${perAppTunWaitForInterface}
|
||||
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
|
||||
'';
|
||||
@@ -126,7 +160,7 @@ 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 del default dev ${lib.escapeShellArg perAppRoutingTun.interface} table ${toString perAppRoutingTun.routeTable} 2>/dev/null || true
|
||||
${ip} route flush table ${toString perAppRoutingTun.routeTable} 2>/dev/null || true
|
||||
${ip} link del dev ${lib.escapeShellArg perAppRoutingTun.interface} 2>/dev/null || true
|
||||
'';
|
||||
|
||||
|
||||
@@ -46,6 +46,7 @@ let
|
||||
null;
|
||||
subscriptionCacheDir = "/var/lib/proxy-suite/subscriptions/${backend}";
|
||||
routeModeStateFile = "/run/proxy-suite/route-mode";
|
||||
xrayLoglevelFile = "/run/proxy-suite/xray-loglevel";
|
||||
runtimeProxychainsConfig = "/run/proxy-suite-socks/proxychains.conf";
|
||||
|
||||
mkSubscriptionCacheFile = sub: "${subscriptionCacheDir}/${sub.tag}.json";
|
||||
@@ -187,6 +188,9 @@ let
|
||||
|
||||
xrayUrltestTagFilter = ''
|
||||
OUTBOUNDS_JSON=$(${jq} 'map(.tag = ("proxy-suite-ob-" + .tag))' <<< "$OUTBOUNDS_JSON")
|
||||
if [ "$(${jq} 'length' <<< "$OUTBOUNDS_JSON")" -eq 1 ]; then
|
||||
XRAY_SINGLE_PROXY_TAG="$(${jq} -r '.[0].tag' <<< "$OUTBOUNDS_JSON")"
|
||||
fi
|
||||
'';
|
||||
|
||||
mkOutboundScript =
|
||||
@@ -272,6 +276,18 @@ let
|
||||
"proxy-suite-per-app-tun"
|
||||
];
|
||||
|
||||
routeModeBlacklistTail =
|
||||
if xrayEnabled then
|
||||
''
|
||||
+ .proxyGeo
|
||||
+ .block
|
||||
''
|
||||
else
|
||||
''
|
||||
+ .block
|
||||
+ .proxyGeo
|
||||
'';
|
||||
|
||||
routeModeCaseBlock = ''
|
||||
if [ -r "$ROUTE_MODE_STATE_FILE" ]; then
|
||||
ROUTE_MODE="$(tr -d '\r\n[:space:]' < "$ROUTE_MODE_STATE_FILE" 2>/dev/null || true)"
|
||||
@@ -287,8 +303,7 @@ let
|
||||
+ .proxyPrimary
|
||||
+ .direct
|
||||
+ .safetyDirect
|
||||
+ .block
|
||||
+ .proxyGeo
|
||||
${routeModeBlacklistTail}
|
||||
' "${routeModeRulesFile}")
|
||||
;;
|
||||
whitelist)
|
||||
@@ -301,8 +316,7 @@ let
|
||||
+ .proxyPrimary
|
||||
+ .direct
|
||||
+ .safetyDirect
|
||||
+ .block
|
||||
+ .proxyGeo
|
||||
${routeModeBlacklistTail}
|
||||
' "${routeModeRulesFile}")
|
||||
;;
|
||||
all-proxy)
|
||||
@@ -315,8 +329,7 @@ let
|
||||
+ (.custom | map(select(.category == "proxy" or .category == "block") | .entries) | add // [])
|
||||
+ .proxyPrimary
|
||||
+ .safetyDirect
|
||||
+ .block
|
||||
+ .proxyGeo
|
||||
${routeModeBlacklistTail}
|
||||
' "${routeModeRulesFile}")
|
||||
;;
|
||||
all-bypass)
|
||||
@@ -340,20 +353,52 @@ let
|
||||
mkBackendJqFilter =
|
||||
if xrayEnabled then
|
||||
''
|
||||
def xray_final_rule($tag):
|
||||
if $tag == "proxy" and "${selectionMode}" == "urltest" then
|
||||
{type:"field",network:"tcp,udp",balancerTag:"proxy"}
|
||||
def xray_bind_outbound($interface):
|
||||
if $interface == "" or .protocol == "blackhole" then
|
||||
.
|
||||
else
|
||||
{type:"field",network:"tcp,udp",outboundTag:$tag}
|
||||
.streamSettings = (.streamSettings // {})
|
||||
| .streamSettings.sockopt = (.streamSettings.sockopt // {})
|
||||
| if (.streamSettings.sockopt.interface? // "") == "" then
|
||||
.streamSettings.sockopt.interface = $interface
|
||||
else
|
||||
.
|
||||
end
|
||||
end;
|
||||
.outbounds = $obs + .outbounds
|
||||
def xray_proxy_rule_target($single_tag):
|
||||
if $single_tag != "" then
|
||||
{outboundTag:$single_tag}
|
||||
else
|
||||
{balancerTag:"proxy"}
|
||||
end;
|
||||
def xray_rewrite_proxy_rule($single_tag):
|
||||
if $single_tag != "" and (.balancerTag? // "") == "proxy" then
|
||||
.outboundTag = $single_tag | del(.balancerTag)
|
||||
else
|
||||
.
|
||||
end;
|
||||
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;
|
||||
.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 $route_enabled then
|
||||
.routing.rules = ($route_rules + [xray_final_rule($route_final)])
|
||||
.routing.rules = ($route_rules + [xray_final_rule($route_final; $xray_single_proxy_tag)])
|
||||
else . end
|
||||
| if $xray_single_proxy_tag == "" then
|
||||
.
|
||||
else
|
||||
.routing.rules |= map(xray_rewrite_proxy_rule($xray_single_proxy_tag))
|
||||
| del(.routing.balancers)
|
||||
| del(.observatory)
|
||||
end
|
||||
''
|
||||
else
|
||||
''
|
||||
@@ -376,11 +421,15 @@ let
|
||||
configFile,
|
||||
routingMark ? null,
|
||||
enableLocalProxyAuth ? false,
|
||||
xrayTunEgressBinding ? false,
|
||||
}:
|
||||
pkgs.writeShellScript name ''
|
||||
set -euo pipefail
|
||||
RUNTIME_DIR="${runtimeDir}"
|
||||
ROUTE_MODE_STATE_FILE="${routeModeStateFile}"
|
||||
XRAY_LOGLEVEL=""
|
||||
XRAY_TUN_BIND_INTERFACE=""
|
||||
XRAY_SINGLE_PROXY_TAG=""
|
||||
mkdir -p "$RUNTIME_DIR"
|
||||
OUTBOUNDS_JSON='[]'
|
||||
ROUTE_MODE=""
|
||||
@@ -395,6 +444,27 @@ let
|
||||
LOCAL_PROXY_PASSWORD="$(cat "${localProxyAuthPasswordSource}")"
|
||||
${writeProxychainsConfigBlock}
|
||||
''}
|
||||
${lib.optionalString xrayEnabled ''
|
||||
if [ -r "${xrayLoglevelFile}" ]; then
|
||||
XRAY_LOGLEVEL="$(tr -d '\r\n[:space:]' < "${xrayLoglevelFile}" 2>/dev/null || true)"
|
||||
fi
|
||||
''}
|
||||
${lib.optionalString xrayTunEgressBinding ''
|
||||
XRAY_TUN_BIND_INTERFACE="$(${pkgs.iproute2}/bin/ip -4 route get 1.1.1.1 2>/dev/null | ${pkgs.gawk}/bin/awk '
|
||||
/dev/ {
|
||||
for (i = 1; i <= NF; i++) {
|
||||
if ($i == "dev" && i + 1 <= NF) {
|
||||
print $(i + 1)
|
||||
exit
|
||||
}
|
||||
}
|
||||
}
|
||||
')"
|
||||
if [ -z "$XRAY_TUN_BIND_INTERFACE" ]; then
|
||||
echo "proxy-suite: could not determine the default uplink interface for XRay TUN" >&2
|
||||
exit 1
|
||||
fi
|
||||
''}
|
||||
|
||||
${routeModeCaseBlock}
|
||||
|
||||
@@ -410,6 +480,9 @@ let
|
||||
--arg route_final "$ROUTE_FINAL" \
|
||||
--arg dns_final "$DNS_FINAL" \
|
||||
--argjson clear_dns_rules "$CLEAR_DNS_RULES" \
|
||||
--arg xray_loglevel "$XRAY_LOGLEVEL" \
|
||||
--arg xray_bind_interface "$XRAY_TUN_BIND_INTERFACE" \
|
||||
--arg xray_single_proxy_tag "$XRAY_SINGLE_PROXY_TAG" \
|
||||
'${mkBackendJqFilter}' \
|
||||
"${configFile}" > "$RUNTIME_DIR/config.json"
|
||||
${lib.optionalString enableLocalProxyAuth ''
|
||||
@@ -432,6 +505,7 @@ let
|
||||
runtimeDir = "/run/proxy-suite-tun";
|
||||
configFile = tunFile;
|
||||
routingMark = if xrayEnabled then globalTproxy.proxyMark else null;
|
||||
xrayTunEgressBinding = xrayEnabled;
|
||||
};
|
||||
|
||||
startPerAppTun = mkStartScript {
|
||||
@@ -439,6 +513,7 @@ let
|
||||
runtimeDir = "/run/proxy-suite-per-app-tun";
|
||||
configFile = perAppTunFile;
|
||||
routingMark = if globalTproxy.enable then globalTproxy.proxyMark else null;
|
||||
xrayTunEgressBinding = xrayEnabled;
|
||||
};
|
||||
|
||||
mkSubscriptionFetchBlock =
|
||||
|
||||
Reference in new issue
Block a user