fix: truly fixing xray tun

This commit is contained in:
FUFSoB committed 2026-06-09 19:35:48 +05:00
1 parent f060c4cd5e
commit 433a13ccad
6 files changed
+392 -49

No files matched your search

+135 -20
View File
@@ -19,6 +19,19 @@ let
;
perAppTun = derived.perAppRoutingTun;
direct = rules.direct;
xrayGlobalTunIPv6Address = "fd66:19::1/64";
xrayPerAppTunIPv6Address = "fd66:20::1/64";
xrayFakeDnsPools = [
{
ipPool = "198.18.0.0/15";
poolSize = 32768;
}
{
ipPool = "fc00::/18";
poolSize = 32768;
}
];
stripCidr = cidr: builtins.head (lib.splitString "/" cidr);
mkSingBoxDnsServer =
tag: upstream: detour:
@@ -154,13 +167,74 @@ let
else
"${base}:${toString upstream.port}";
xraySniffing = {
enabled = true;
destOverride = [
"http"
"tls"
"quic"
];
mkXrayDnsServer =
tag: upstream:
{
address = xrayDnsAddress upstream;
inherit tag;
queryStrategy = "UseIP";
};
mkXraySniffing =
{
fakeDnsOnly ? false,
}:
if fakeDnsOnly then
{
enabled = true;
destOverride = [ "fakedns" ];
metadataOnly = true;
}
else
{
enabled = true;
destOverride = [
"http"
"tls"
"quic"
];
};
xraySniffing = mkXraySniffing { };
mkXrayTunDnsConfig =
{
preferRemote ? proxyCfg.proxyByDefault,
}:
let
primaryTag = if preferRemote then "remote" else "local";
secondaryTag = if preferRemote then "local" else "remote";
primaryUpstream = if preferRemote then proxyCfg.dns.remote else proxyCfg.dns.local;
secondaryUpstream = if preferRemote then proxyCfg.dns.local else proxyCfg.dns.remote;
in
{
queryStrategy = "UseIP";
tag = "dns-in";
servers = [
{
address = "fakedns";
tag = "fakedns";
}
(mkXrayDnsServer primaryTag primaryUpstream)
(mkXrayDnsServer secondaryTag secondaryUpstream)
];
};
xrayTunDnsHijackRule = {
type = "field";
inboundTag = [ "tun-in" ];
network = "tcp,udp";
port = 53;
outboundTag = "dns-out";
ruleTag = "dns-hijack";
};
xrayTunDnsUpstreamRule = {
type = "field";
inboundTag = [ "dns-in" ];
network = "tcp,udp";
outboundTag = "direct";
ruleTag = "dns-upstream-direct";
};
xrayFinalRule =
@@ -198,17 +272,55 @@ let
enableTun ? false,
tunInterface ? globalTun.interface,
tunAddress ? globalTun.address,
tunIPv6Address ? null,
tunMtu ? globalTun.mtu,
useOutboundRoutingMark ? false,
enableUrlTest ? proxyCfg.selection == "urltest",
domainStrategy ? "IPIfNonMatch",
enableTunFakeDns ? false,
}:
let
tunDnsServers = [
(if proxyCfg.proxyByDefault then proxyCfg.dns.remote.address else proxyCfg.dns.local.address)
(if proxyCfg.proxyByDefault then proxyCfg.dns.local.address else proxyCfg.dns.remote.address)
];
xrayTunSniffing = mkXraySniffing { fakeDnsOnly = enableTunFakeDns; };
xrayDnsConfig =
if enableTunFakeDns then
mkXrayTunDnsConfig { }
else
{
servers = [
(xrayDnsAddress proxyCfg.dns.remote)
(xrayDnsAddress proxyCfg.dns.local)
];
};
xrayTunOutbounds = lib.optionals enableTunFakeDns [
{
protocol = "dns";
tag = "dns-out";
settings = {
userLevel = 0;
rules = [
{
action = "direct";
qType = "2-27,29-65535";
}
];
};
}
];
xrayRoutingRules =
lib.optionals enableTunFakeDns [
xrayTunDnsUpstreamRule
xrayTunDnsHijackRule
]
++ rules.xrayRoutingRules
++ [ (xrayFinalRule (if proxyCfg.proxyByDefault then "proxy" else "direct")) ];
in
{
log.loglevel = "warning";
dns.servers = [
(xrayDnsAddress proxyCfg.dns.remote)
(xrayDnsAddress proxyCfg.dns.local)
];
dns = xrayDnsConfig;
inbounds =
lib.optional enableMixed {
tag = "mixed-in";
@@ -240,14 +352,14 @@ let
settings = {
name = tunInterface;
mtu = tunMtu;
gateway = [ tunAddress ];
dns = [ proxyCfg.dns.local.address ];
gateway = [ tunAddress ] ++ lib.optionals (tunIPv6Address != null) [ tunIPv6Address ];
dns = tunDnsServers;
userLevel = 0;
autoOutboundsInterface = "auto";
};
sniffing = xraySniffing;
sniffing = xrayTunSniffing;
};
outbounds = [
outbounds = xrayTunOutbounds ++ [
(xrayDirectOutbound useOutboundRoutingMark)
{
protocol = "blackhole";
@@ -258,7 +370,7 @@ let
routing = {
domainStrategy = domainStrategy;
domainMatcher = "hybrid";
rules = xrayRouteRules;
rules = xrayRoutingRules;
}
// lib.optionalAttrs enableUrlTest {
balancers = [
@@ -276,7 +388,8 @@ let
probeUrl = proxyCfg.urlTest.url;
probeInterval = proxyCfg.urlTest.interval;
};
};
}
// lib.optionalAttrs enableTunFakeDns { fakedns = xrayFakeDnsPools; };
singBoxTproxyTemplate = mkSingBoxConfig {
enableMixed = true;
@@ -319,18 +432,20 @@ let
enableTun = true;
tunInterface = globalTun.interface;
tunAddress = globalTun.address;
tunIPv6Address = xrayGlobalTunIPv6Address;
tunMtu = globalTun.mtu;
useOutboundRoutingMark = true;
domainStrategy = "AsIs";
enableTunFakeDns = true;
};
xrayPerAppTunTemplate = mkXrayConfig {
enableTun = true;
tunInterface = perAppTun.interface;
tunAddress = perAppTun.address;
tunIPv6Address = xrayPerAppTunIPv6Address;
tunMtu = perAppTun.mtu;
useOutboundRoutingMark = globalTproxy.enable;
domainStrategy = "AsIs";
useOutboundRoutingMark = true;
enableTunFakeDns = true;
};
tproxyTemplate = if xrayEnabled then xrayTproxyTemplate else singBoxTproxyTemplate;
+6
View File
@@ -39,6 +39,12 @@ in
};
in
lib.mkMerge [
{
warnings = lib.optional (cfg.proxy.enable && cfg.proxy.xray.enable) ''
proxy-suite: XRay backend is still in test mode; sing-box should be preferable for now.
'';
}
(import ./service {
inherit
config
+30 -1
View File
@@ -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" ''
+39 -4
View File
@@ -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 =