fix: overall tun/tproxy improvements

This commit is contained in:
fufsob committed 2026-09-16 14:18:16 +00:00
1 parent ff04debf90
commit 32341ba323
29 files changed
+445 -149

No files matched your search

+1
View File
@@ -140,6 +140,7 @@ services.proxy-suite = {
strategy = null;
};
enable = false;
ipv6 = true;
listener = {
address = "127.0.0.1";
auth = {
+21 -1
View File
@@ -34,6 +34,7 @@ Part of the [proxy-suite options reference](./index.md).
- [rules](#services-proxy-suite-proxy-dns-singbox-rules)
- [servers](#services-proxy-suite-proxy-dns-singbox-servers)
- [strategy](#services-proxy-suite-proxy-dns-strategy)
- [ipv6](#services-proxy-suite-proxy-ipv6)
- listener
- [address](#services-proxy-suite-proxy-listener-address)
- auth
@@ -589,6 +590,24 @@ null
"ipv4_only"
```
<a id="services-proxy-suite-proxy-ipv6"></a>
## services\.proxy-suite\.proxy\.ipv6
Carry IPv6 in the transparent modes: TProxy through a second listener on ::1, the TUNs
(global and per-app) through an IPv6 address of their own\. Off, TProxy leaves IPv6
alone and the TUNs block it, so apps fall back to IPv4\. On an uplink without IPv6,
set proxy\.dns\.strategy = “ipv4_only” too: a direct IPv6 destination would otherwise
fail after the connection seems open, instead of falling back to IPv4\.
*Type:*
boolean
*Default:*
```nix
config.networking.enableIPv6
```
<a id="services-proxy-suite-proxy-listener-address"></a>
## services\.proxy-suite\.proxy\.listener\.address
@@ -1622,7 +1641,7 @@ signed integer
<a id="services-proxy-suite-proxy-tproxy-localsubnets"></a>
## services\.proxy-suite\.proxy\.tproxy\.localSubnets
Subnets that bypass interception (DNS excepted): your LAN, VM bridges\.
Subnets that bypass interception (DNS excepted): your LAN, VM bridges\. IPv6 CIDRs work too\.
*Type:*
list of string
@@ -1641,6 +1660,7 @@ list of string
[
"192.168.0.0/16"
"10.0.0.0/8"
"fd00::/8"
]
```
@@ -100,6 +100,8 @@ let
enableTun ? false,
tunInterface ? globalTun.interface,
tunAddress ? globalTun.address,
# With proxy.ipv6: both families in the TUN, so strict_route rejects neither.
tunIPv6Address ? null,
tunMtu ? globalTun.mtu,
tunAutoRoute ? true,
tunAutoRouteTableIndex ? defaultTunAutoRouteTableIndex,
@@ -139,18 +141,28 @@ let
listen = proxyCfg.listener.address;
listen_port = proxyCfg.listener.port;
}
++ lib.optional enableTProxy {
type = "tproxy";
tag = "tproxy-in";
listen = "127.0.0.1";
listen_port = globalTproxy.port;
}
++ lib.optionals enableTProxy (
[
{
type = "tproxy";
tag = "tproxy-in";
listen = "127.0.0.1";
listen_port = globalTproxy.port;
}
]
++ lib.optional proxyCfg.ipv6 {
type = "tproxy";
tag = "tproxy-in6";
listen = "::1";
listen_port = globalTproxy.port;
}
)
++ lib.optional enableTun (
{
type = "tun";
tag = "tun-in";
interface_name = tunInterface;
address = [ tunAddress ];
address = [ tunAddress ] ++ lib.optional (proxyCfg.ipv6 && tunIPv6Address != null) tunIPv6Address;
mtu = tunMtu;
auto_route = tunAutoRoute;
auto_redirect = tunAutoRedirect;
@@ -211,6 +223,7 @@ in
enableTun = true;
tunInterface = globalTun.interface;
tunAddress = globalTun.address;
tunIPv6Address = constants.globalTunIPv6Address;
tunMtu = globalTun.mtu;
tunAutoRoute = true;
tunAutoRedirect = true;
@@ -225,6 +238,7 @@ in
enableTun = true;
tunInterface = perAppRoutingTun.interface;
tunAddress = perAppRoutingTun.address;
tunIPv6Address = constants.perAppTunIPv6Address;
tunMtu = perAppRoutingTun.mtu;
tunAutoRoute = false;
tunAutoRedirect = false;
+55 -48
View File
@@ -15,8 +15,8 @@ let
perAppRoutingTun
;
inherit (constants)
xrayGlobalTunIPv6Address
xrayPerAppTunIPv6Address
globalTunIPv6Address
perAppTunIPv6Address
;
fakeDnsPools = [
@@ -40,8 +40,11 @@ let
queryStrategy = "UseIP";
};
mkTunDnsConfig =
# XRay routes each server's own queries by that server's tag: the backend filter pins
# the proxy servers' names to "local", so reaching them never needs the proxy itself.
mkDnsConfig =
{
fakeDns ? false,
preferRemote ? (proxyCfg.routing.default == "proxy"),
}:
let
@@ -52,15 +55,15 @@ let
in
{
queryStrategy = "UseIP";
tag = "dns-in";
servers = [
{
servers =
lib.optional fakeDns {
address = "fakedns";
tag = "fakedns";
}
(mkDnsServer primaryTag primaryUpstream)
(mkDnsServer secondaryTag secondaryUpstream)
];
++ [
(mkDnsServer primaryTag primaryUpstream)
(mkDnsServer secondaryTag secondaryUpstream)
];
};
mkSniffing =
@@ -88,22 +91,41 @@ let
standardSniffing = mkSniffing { };
tunDnsHijackRule = {
target =
tag:
if tag == "proxy" && proxyCfg.selection == "urltest" then
{ balancerTag = "proxy"; }
else
{ outboundTag = tag; };
dnsHijackRule = inboundTag: {
type = "field";
inboundTag = [ "tun-in" ];
inherit inboundTag;
network = "tcp,udp";
port = 53;
outboundTag = "dns-out";
ruleTag = "dns-hijack";
};
tunDnsUpstreamRule = {
type = "field";
inboundTag = [ "dns-in" ];
network = "tcp,udp";
outboundTag = "direct";
ruleTag = "dns-upstream-direct";
};
# As sing-box: remote through the proxy, local direct.
dnsUpstreamRules = [
{
type = "field";
inboundTag = [ "local" ];
network = "tcp,udp";
outboundTag = "direct";
ruleTag = "dns-upstream-direct";
}
(
{
type = "field";
inboundTag = [ "remote" ];
network = "tcp,udp";
ruleTag = "dns-upstream-remote";
}
// target "proxy"
)
];
finalRule =
tag:
@@ -112,12 +134,7 @@ let
network = "tcp,udp";
ruleTag = "final-default";
}
// (
if tag == "proxy" && proxyCfg.selection == "urltest" then
{ balancerTag = "proxy"; }
else
{ outboundTag = tag; }
);
// target tag;
directOutbound =
useOutboundRoutingMark:
@@ -146,17 +163,9 @@ let
}:
let
tunSniffing = mkSniffing { fakeDnsOnly = enableTunFakeDns; };
dnsConfig =
if enableTunFakeDns then
mkTunDnsConfig { }
else
{
servers = [
(mkDnsServer "remote" proxyCfg.dns.remote)
(mkDnsServer "local" proxyCfg.dns.local)
];
};
tunOutbounds = lib.optionals enableTunFakeDns [
tproxyInboundTags = [ "tproxy-in" ] ++ lib.optional proxyCfg.ipv6 "tproxy-in6";
# Every config takes packets for any destination, DNS included.
dnsOutbounds = [
{
protocol = "dns";
tag = "dns-out";
@@ -172,16 +181,15 @@ let
}
];
routingRules =
lib.optionals enableTunFakeDns [
tunDnsUpstreamRule
tunDnsHijackRule
]
dnsUpstreamRules
++ lib.optional enableTun (dnsHijackRule [ "tun-in" ])
++ lib.optional enableTProxy (dnsHijackRule tproxyInboundTags)
++ rules.xrayRoutingRules
++ [ (finalRule (if (proxyCfg.routing.default == "proxy") then "proxy" else "direct")) ];
in
{
log.loglevel = "warning";
dns = dnsConfig;
dns = mkDnsConfig { fakeDns = enableTunFakeDns; };
inbounds =
lib.optional enableMixed {
tag = "mixed-in";
@@ -195,10 +203,9 @@ let
};
sniffing = standardSniffing;
}
++ lib.optional enableTProxy {
tag = "tproxy-in";
++ lib.zipListsWith (tag: listen: {
inherit tag listen;
protocol = "tunnel";
listen = "127.0.0.1";
port = globalTproxy.port;
settings = {
allowedNetwork = "tcp,udp";
@@ -206,19 +213,19 @@ let
};
streamSettings.sockopt.tproxy = "tproxy";
sniffing = standardSniffing;
}
}) (lib.optionals enableTProxy tproxyInboundTags) [ "127.0.0.1" "::1" ]
++ lib.optional enableTun {
tag = "tun-in";
protocol = "tun";
settings = {
name = tunInterface;
mtu = tunMtu;
gateway = [ tunAddress ] ++ lib.optionals (tunIPv6Address != null) [ tunIPv6Address ];
gateway = [ tunAddress ] ++ lib.optional (proxyCfg.ipv6 && tunIPv6Address != null) tunIPv6Address;
userLevel = 0;
};
sniffing = tunSniffing;
};
outbounds = tunOutbounds ++ [
outbounds = dnsOutbounds ++ [
(directOutbound useOutboundRoutingMark)
{
protocol = "blackhole";
@@ -267,7 +274,7 @@ in
enableTun = true;
tunInterface = globalTun.interface;
tunAddress = globalTun.address;
tunIPv6Address = xrayGlobalTunIPv6Address;
tunIPv6Address = globalTunIPv6Address;
tunMtu = globalTun.mtu;
useOutboundRoutingMark = true;
enableTunFakeDns = true;
@@ -277,7 +284,7 @@ in
enableTun = true;
tunInterface = perAppRoutingTun.interface;
tunAddress = perAppRoutingTun.address;
tunIPv6Address = xrayPerAppTunIPv6Address;
tunIPv6Address = perAppTunIPv6Address;
tunMtu = perAppRoutingTun.mtu;
useOutboundRoutingMark = true;
enableTunFakeDns = true;
+4 -5
View File
@@ -285,7 +285,6 @@ let
# clients included) stay out of the TUN.
xrayTunMarkBypassRulePriority = 8992;
xrayTunServiceUserRulePriority = 8993;
xrayTunPerAppTproxyRulePriority = 8994;
xrayTunPerAppTunRulePriority = 8995;
# DNS stays in the TUN for fakedns, even to a LAN resolver.
xrayTunDnsRulePriority = 8996;
@@ -293,10 +292,10 @@ let
# replies to connections from there leave the way they came.
xrayTunMainRulePriority = 8997;
xrayGlobalTunIPv6Address = "fd66:19::1/64";
xrayGlobalTunIPv6RoutePrefix = "fd66:19::/64";
xrayPerAppTunIPv6Address = "fd66:20::1/64";
xrayPerAppTunIPv6RoutePrefix = "fd66:20::/64";
globalTunIPv6Address = "fd66:19::1/64";
globalTunIPv6RoutePrefix = "fd66:19::/64";
perAppTunIPv6Address = "fd66:20::1/64";
perAppTunIPv6RoutePrefix = "fd66:20::/64";
xrayDnsBridgePorts = {
socks = 18533;
+41 -12
View File
@@ -18,7 +18,7 @@ let
meta mark ${toString tgWsProxyCfg.fwmark} return
'';
# Shared across all three nftables rule files that do IPv4 routing.
# Shared across all three nftables rule files that do IP routing.
reservedIpBlock = ''
define RESERVED_IP = {
10.0.0.0/8,
@@ -31,67 +31,94 @@ let
240.0.0.0/4,
255.255.255.255/32
}
define RESERVED_IP6 = {
::/128,
::1/128,
::ffff:0:0/96,
fc00::/7,
fe80::/10,
ff00::/8
}
'';
ipFamily = cidr: if lib.hasInfix ":" cidr then "ip6" else "ip";
mkLocalSubnetLines =
subnets:
lib.concatMapStrings (cidr: ''
ip daddr ${cidr} tcp dport != 53 return
ip daddr ${cidr} udp dport != 53 return
${ipFamily cidr} daddr ${cidr} tcp dport != 53 return
${ipFamily cidr} daddr ${cidr} udp dport != 53 return
'') subnets;
tproxyLocalSubnetLines = mkLocalSubnetLines globalTproxy.localSubnets;
perAppTproxyLocalSubnetLines = mkLocalSubnetLines perAppTproxy.localSubnets;
# Without ipv6, IPv6 packets are left alone, as if the table were still `ip`.
tproxyProtocols = "${lib.optionalString (!proxyCfg.ipv6) "meta nfproto ipv4 "}meta l4proto { tcp, udp }";
# Both listeners share the port; `tproxy ip`/`tproxy ip6` only match their own family.
mkTproxyLines = prefix: suffix: ''
${prefix}meta l4proto { tcp, udp } tproxy ip to 127.0.0.1:${toString globalTproxy.port}${suffix}
${lib.optionalString proxyCfg.ipv6 "${prefix}meta l4proto { tcp, udp } tproxy ip6 to [::1]:${toString globalTproxy.port}${suffix}"}
'';
nftablesRulesFile = pkgs.writeText "proxy-suite-routing" ''
${reservedIpBlock}
table ip singbox {
table inet singbox {
chain prerouting {
type filter hook prerouting priority mangle; policy accept;
ip daddr $RESERVED_IP return
ip6 daddr $RESERVED_IP6 return
# Connections to this host itself (inbounds, sshd on a public address)
# are served here, not taken by the tproxy socket.
fib daddr type local return
# Packets re-entering via loopback after output marking should not
# be skipped just because the host has an RFC1918 source address.
iifname != "lo" ip saddr $RESERVED_IP return
iifname != "lo" ip6 saddr $RESERVED_IP6 return
${tproxyLocalSubnetLines}
ip protocol tcp tproxy to 127.0.0.1:${toString globalTproxy.port} meta mark set ${toString globalTproxy.fwmark}
ip protocol udp tproxy to 127.0.0.1:${toString globalTproxy.port} meta mark set ${toString globalTproxy.fwmark}
${mkTproxyLines "" " meta mark set ${toString globalTproxy.fwmark}"}
}
chain output {
type route hook output priority mangle; policy accept;
ip daddr $RESERVED_IP return
ip6 daddr $RESERVED_IP6 return
${tproxyLocalSubnetLines}
meta mark ${toString globalTproxy.proxyMark} return
# Replies to connections from outside, and proxy-suite's own daemons (the
# inbound XRay dials unmarked, lacking CAP_NET_ADMIN), leave as they are.
ct direction reply return
meta skuid "${serviceUser}" return
${tgWsProxyBypassMarkLine}${lib.optionalString perAppTun.enable " meta mark ${toString perAppTun.fwmark} return\n"}${lib.optionalString perAppTproxy.enable " meta mark ${toString perAppTproxy.fwmark} return\n"} ip protocol tcp meta mark set ${toString globalTproxy.fwmark}
ip protocol udp meta mark set ${toString globalTproxy.fwmark}
${tgWsProxyBypassMarkLine}${lib.optionalString perAppTun.enable " meta mark ${toString perAppTun.fwmark} return\n"}${lib.optionalString perAppTproxy.enable " meta mark ${toString perAppTproxy.fwmark} return\n"} ${tproxyProtocols} meta mark set ${toString globalTproxy.fwmark}
}
}
'';
perAppTproxyRulesFile = pkgs.writeText "proxy-suite-routing" ''
${reservedIpBlock}
table ip proxy_suite_per_app_tproxy {
table inet proxy_suite_per_app_tproxy {
chain prerouting {
type filter hook prerouting priority mangle; policy accept;
ip daddr $RESERVED_IP return
ip6 daddr $RESERVED_IP6 return
iifname != "lo" ip saddr $RESERVED_IP return
iifname != "lo" ip6 saddr $RESERVED_IP6 return
# This host's own addresses are served here, not detoured through the proxy.
fib daddr type local return
${perAppTproxyLocalSubnetLines}
# The backend's forged UDP replies belong to the app's flow too; an unconnected
# app socket is no match for the tproxy lookup, so the listener would take them.
ct direction reply return
ct mark ${toString perAppTproxy.fwmark} meta mark set ${toString perAppTproxy.fwmark}
meta mark ${toString perAppTproxy.fwmark} ip protocol tcp tproxy to 127.0.0.1:${toString globalTproxy.port}
meta mark ${toString perAppTproxy.fwmark} ip protocol udp tproxy to 127.0.0.1:${toString globalTproxy.port}
${mkTproxyLines "meta mark ${toString perAppTproxy.fwmark} " ""}
}
chain output {
type route hook output priority mangle; policy accept;
ip daddr $RESERVED_IP return
ip6 daddr $RESERVED_IP6 return
${perAppTproxyLocalSubnetLines}
meta mark ${toString globalTproxy.proxyMark} return
ct direction reply return
${lib.optionalString perAppTun.enable " meta mark ${toString perAppTun.fwmark} return\n"} ct mark ${toString perAppTproxy.fwmark} meta mark set ${toString perAppTproxy.fwmark}
meta mark ${toString perAppTproxy.fwmark} return
}
@@ -120,8 +147,10 @@ let
type route hook output priority mangle; policy accept;
ip daddr $RESERVED_IP return
${lib.concatMapStrings (cidr: ''
ip daddr ${cidr} return
${ipFamily cidr} daddr ${cidr} return
'') perAppTun.localSubnets}
# Replies to connections from outside leave the way they came, not through the TUN.
ct direction reply return
ct mark ${toString perAppTun.fwmark} meta mark set ${toString perAppTun.fwmark}
meta mark ${toString perAppTun.fwmark} return
}
+2 -1
View File
@@ -34,10 +34,11 @@ in
localSubnets = mkOption {
type = types.listOf types.str;
default = [ "192.168.0.0/16" ];
description = "Subnets that bypass interception (DNS excepted): your LAN, VM bridges.";
description = "Subnets that bypass interception (DNS excepted): your LAN, VM bridges. IPv6 CIDRs work too.";
example = [
"192.168.0.0/16"
"10.0.0.0/8"
"fd00::/8"
];
};
};
+14
View File
@@ -1,4 +1,5 @@
{
config,
lib,
proxySuiteUpstream,
...
@@ -46,6 +47,19 @@ in
example = "tun";
};
ipv6 = mkOption {
type = types.bool;
default = config.networking.enableIPv6;
defaultText = literalExpression "config.networking.enableIPv6";
description = ''
Carry IPv6 in the transparent modes: TProxy through a second listener on ::1, the TUNs
(global and per-app) through an IPv6 address of their own. Off, TProxy leaves IPv6
alone and the TUNs block it, so apps fall back to IPv4. On an uplink without IPv6,
set proxy.dns.strategy = "ipv4_only" too: a direct IPv6 destination would otherwise
fail after the connection seems open, instead of falling back to IPv4.
'';
};
listener = {
address = mkOption {
type = types.str;
+29 -1
View File
@@ -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,
-1
View File
@@ -145,7 +145,6 @@ let
cfg
singBoxCfg
proxyCfg
pureXrayEnabled
perAppRoutingCfg
perAppRoutingTun
perAppRoutingTproxy
+1 -1
View File
@@ -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";
};
+20 -21
View File
@@ -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.
+49 -2
View File
@@ -5,6 +5,8 @@
mkBadFixture,
mkFailingAssertions,
mkTunConfig,
mkTProxyConfig,
mkTProxyNftRules,
dnsServerByTag,
checkConstants,
}:
@@ -17,6 +19,8 @@ let
mkBadFixture
mkFailingAssertions
mkTunConfig
mkTProxyConfig
mkTProxyNftRules
;
};
@@ -26,6 +30,15 @@ let
tproxyManualFixture
tproxyManualStartScript
tproxyManualStopScript
tproxyManualConfig
tproxyManualNftRules
tproxyIPv4OnlyStartScript
tproxyIPv4OnlyConfig
tproxyIPv4OnlyNftRules
ipv4OnlyTunConfig
ipv4OnlyPerAppTunUpScript
xrayIPv4OnlyTunConfig
xrayIPv4OnlyTunUpScript
tproxyWithFirewall
tunAutostartFixture
tunCleanupScript
@@ -56,6 +69,42 @@ in
assert pkgs.lib.hasInfix "rule del fwmark 1 table 100" tproxyManualStartScript;
assert pkgs.lib.hasInfix "route replace local default dev lo table 100" tproxyManualStartScript;
assert pkgs.lib.hasInfix "set +e" tproxyManualStopScript;
true
)
# IPv6 goes through the same port on ::1; without it IPv6 is left alone.
(
let
tproxyTags = config: map (inbound: inbound.tag) (builtins.filter (inbound: inbound.type == "tproxy") config.inbounds);
in
assert pkgs.lib.hasInfix "delete table inet singbox" tproxyManualStartScript;
assert pkgs.lib.hasInfix "-6 route replace local default dev lo table 100" tproxyManualStartScript;
assert pkgs.lib.hasInfix "-6 rule add fwmark 1 table 100" tproxyManualStartScript;
assert pkgs.lib.hasInfix "-6 rule del fwmark 1 table 100" tproxyManualStopScript;
assert pkgs.lib.hasInfix "table inet singbox" tproxyManualNftRules;
assert pkgs.lib.hasInfix "meta l4proto { tcp, udp } tproxy ip to 127.0.0.1:1085 meta mark set 1" tproxyManualNftRules;
assert pkgs.lib.hasInfix "meta l4proto { tcp, udp } tproxy ip6 to [::1]:1085 meta mark set 1" tproxyManualNftRules;
assert tproxyTags tproxyManualConfig == [ "tproxy-in" "tproxy-in6" ];
assert !(pkgs.lib.hasInfix "-6 rule add" tproxyIPv4OnlyStartScript);
assert pkgs.lib.hasInfix "-6 rule del fwmark 1 table 100" tproxyIPv4OnlyStartScript;
assert !(pkgs.lib.hasInfix "tproxy ip6" tproxyIPv4OnlyNftRules);
assert pkgs.lib.hasInfix "meta nfproto ipv4 meta l4proto { tcp, udp } meta mark set 1" tproxyIPv4OnlyNftRules;
assert tproxyTags tproxyIPv4OnlyConfig == [ "tproxy-in" ];
true
)
# proxy.ipv6 gives the TUNs an IPv6 address: sing-box's strict_route then rejects neither
# family. Off, they stay IPv4-only and wrapped apps' IPv6 is unreachable.
(
let
tunAddress = config: (builtins.head (builtins.filter (inbound: inbound.type or "" == "tun") config.inbounds)).address;
xrayGateway = config: (builtins.head (builtins.filter (inbound: inbound.protocol or "" == "tun") config.inbounds)).settings.gateway;
in
assert tunAddress tunDefaultConfig == [ "172.19.0.1/30" checkConstants.globalTunIPv6Address ];
assert tunAddress ipv4OnlyTunConfig == [ "172.19.0.1/30" ];
assert pkgs.lib.hasInfix "-6 route replace unreachable default table 101" ipv4OnlyPerAppTunUpScript;
assert !(pkgs.lib.hasInfix "-6 addr replace" ipv4OnlyPerAppTunUpScript);
assert xrayGateway xrayIPv4OnlyTunConfig == [ "172.19.0.1/30" ];
assert !(pkgs.lib.hasInfix "-6 addr replace" xrayIPv4OnlyTunUpScript);
assert !(pkgs.lib.hasInfix "-6 route replace" xrayIPv4OnlyTunUpScript);
assert pkgs.lib.hasInfix "rule del fwmark 1 table 100" tproxyManualStopScript;
true
)
@@ -82,8 +131,6 @@ in
assert pkgs.lib.hasInfix "delete table inet sing-box" tunCleanupScript;
assert pkgs.lib.hasInfix "rule del table ${toString checkConstants.tunAutoRouteTableIndex}"
tunCleanupScript;
assert pkgs.lib.hasInfix "rule del pref ${toString checkConstants.xrayTunPerAppTproxyRulePriority}"
tunCleanupScript;
assert pkgs.lib.hasInfix "rule del pref ${toString checkConstants.xrayTunPerAppTunRulePriority}"
tunCleanupScript;
assert pkgs.lib.hasInfix "route flush table ${toString checkConstants.tunAutoRouteTableIndex}"
@@ -4,6 +4,8 @@
mkBadFixture,
mkFailingAssertions,
mkTunConfig,
mkTProxyConfig,
mkTProxyNftRules,
}:
let
@@ -40,6 +42,48 @@ let
tproxyManualFixture.config.systemd.services."proxy-suite-tproxy".serviceConfig;
tproxyManualStartScript = generated.readDerivation tproxyManualServiceConfig.ExecStart;
tproxyManualStopScript = generated.readDerivation tproxyManualServiceConfig.ExecStop;
tproxyManualConfig = mkTProxyConfig tproxyManualFixture;
tproxyManualNftRules = mkTProxyNftRules tproxyManualFixture;
tproxyIPv4OnlyFixture = evalProxySuite [
baseModule
{
networking.enableIPv6 = false;
services.proxy-suite.proxy = {
tproxy.enable = true;
tun.enable = true;
};
services.proxy-suite.perAppRouting = {
enable = true;
tun.enable = true;
};
}
];
tproxyIPv4OnlyStartScript = generated.readDerivation
tproxyIPv4OnlyFixture.config.systemd.services."proxy-suite-tproxy".serviceConfig.ExecStart;
tproxyIPv4OnlyConfig = mkTProxyConfig tproxyIPv4OnlyFixture;
tproxyIPv4OnlyNftRules = mkTProxyNftRules tproxyIPv4OnlyFixture;
ipv4OnlyTunConfig = mkTunConfig tproxyIPv4OnlyFixture;
ipv4OnlyPerAppTunUpScript = generated.readDerivation
tproxyIPv4OnlyFixture.config.systemd.services."proxy-suite-per-app-tun".serviceConfig.ExecStartPost;
# XRay's TUN configures IPv6 itself: with it off, its up script must not touch IPv6 addresses.
xrayIPv4OnlyFixture = evalProxySuite [
baseModule
(
{ lib, ... }:
{
networking.enableIPv6 = false;
services.proxy-suite.proxy = {
backend = lib.mkForce "xray";
tun.enable = true;
};
}
)
];
xrayIPv4OnlyTunConfig = mkTunConfig xrayIPv4OnlyFixture;
xrayIPv4OnlyTunUpScript = generated.readDerivation
xrayIPv4OnlyFixture.config.systemd.services."proxy-suite-tun".serviceConfig.ExecStartPost;
tproxyAutostartFixture = evalProxySuite [
baseModule
@@ -101,6 +145,15 @@ in
tproxyManualFixture
tproxyManualStartScript
tproxyManualStopScript
tproxyManualConfig
tproxyManualNftRules
tproxyIPv4OnlyStartScript
tproxyIPv4OnlyConfig
tproxyIPv4OnlyNftRules
ipv4OnlyTunConfig
ipv4OnlyPerAppTunUpScript
xrayIPv4OnlyTunConfig
xrayIPv4OnlyTunUpScript
tproxyWithFirewall
tunAutostartFixture
tunCleanupScript
+2
View File
@@ -197,6 +197,8 @@ let
mkBadFixture
mkFailingAssertions
mkTunConfig
mkTProxyConfig
mkTProxyNftRules
dnsServerByTag
checkConstants
;
+26 -1
View File
@@ -31,6 +31,12 @@ let
perAppRoutingTproxyUserStartScript = generated.readDerivation (
(mkPerAppUserRules perAppRoutingTproxyFixture).perAppTproxyUserRuleStart
);
perAppRoutingTproxyNftRules = generated.readDerivation
(import ../../modules/proxy-suite/nftables.nix {
inherit (pkgs) lib;
inherit pkgs;
cfg = perAppRoutingTproxyFixture.config.services.proxy-suite;
}).perAppTproxyRulesFile;
in
{
assertions = [
@@ -61,14 +67,33 @@ in
# -- perAppRouting: app TProxy helper installs socket cgroup mark rules --
(
assert pkgs.lib.hasInfix "socket cgroupv2" perAppRoutingTproxyUserStartScript;
assert pkgs.lib.hasInfix "add rule inet proxy_suite_per_app_tproxy output" perAppRoutingTproxyUserStartScript;
assert pkgs.lib.hasInfix "meta mark set" perAppRoutingTproxyUserStartScript;
assert pkgs.lib.hasInfix "ct mark set" perAppRoutingTproxyUserStartScript;
true
)
# -- perAppRouting: forged UDP replies to app flows skip the mark, in both chains, before
# the ct mark is copied; otherwise the tproxy listener takes them back --
(
let
inherit (pkgs.lib) hasInfix splitString;
guardedBeforeMark = chain: hasInfix "ct direction reply return" (builtins.head (splitString "ct mark 17 meta mark set 17" chain));
chains = splitString "chain output" perAppRoutingTproxyNftRules;
in
assert guardedBeforeMark (builtins.elemAt chains 0);
assert guardedBeforeMark (builtins.elemAt chains 1);
# This host's own addresses are not tproxied.
assert hasInfix "fib daddr type local return" (builtins.elemAt chains 0);
true
)
# -- perAppRouting: app TProxy startup installs nftables and loopback policy route --
(
assert pkgs.lib.hasInfix "proxy_suite_per_app_tproxy" perAppRoutingTproxyStartScript;
assert pkgs.lib.hasInfix "delete table inet proxy_suite_per_app_tproxy" perAppRoutingTproxyStartScript;
assert pkgs.lib.hasInfix "-6 route replace local default dev lo table 102" perAppRoutingTproxyStartScript;
assert pkgs.lib.hasInfix "-6 rule add fwmark 17 table 102" perAppRoutingTproxyStartScript;
assert pkgs.lib.hasInfix "-6 rule del fwmark 17 table 102" perAppRoutingTproxyStopScript;
assert pkgs.lib.hasInfix "rule del fwmark 17 table 102" perAppRoutingTproxyStartScript;
assert pkgs.lib.hasInfix "route replace local default dev lo table 102"
perAppRoutingTproxyStartScript;
+21 -4
View File
@@ -38,6 +38,12 @@ let
perAppRoutingTunUserStartScript = generated.readDerivation (
(mkPerAppUserRules perAppRoutingTunFixture).perAppTunUserRuleStart
);
perAppRoutingTunNftRules = generated.readDerivation
(import ../../modules/proxy-suite/nftables.nix {
inherit (pkgs) lib;
inherit pkgs;
cfg = perAppRoutingTunFixture.config.services.proxy-suite;
}).perAppTunChainFile;
perAppRoutingTunWithTproxyFixture = evalProxySuite [
baseModule
@@ -92,7 +98,7 @@ in
);
in
assert inbound.interface_name == "psperapptun0";
assert inbound.address == [ "172.20.0.1/30" ];
assert inbound.address == [ "172.20.0.1/30" "fd66:20::1/64" ];
assert inbound.auto_route == false;
assert inbound.auto_redirect == false;
assert inbound.strict_route == false;
@@ -139,15 +145,26 @@ in
assert perAppRoutingTunFixture.config.systemd.services ? "proxy-suite-per-app-tun-user@";
assert perAppRoutingTunFixture.config.systemd.user.services ? "proxy-suite-per-app-tun-anchor";
assert perAppRoutingTunServiceConfig.ExecStartPre == perAppRoutingTunServiceConfig.ExecStopPost;
assert !(pkgs.lib.hasInfix "-6 addr replace" perAppRoutingTunUpScript);
assert !(pkgs.lib.hasInfix "-6 route replace" perAppRoutingTunUpScript);
assert !(pkgs.lib.hasInfix "-6 rule add" perAppRoutingTunUpScript);
# With proxy.ipv6, wrapped apps' IPv6 goes through the app TUN too.
assert pkgs.lib.hasInfix ''-6 addr replace "$tun6_cidr" dev psperapptun0'' perAppRoutingTunUpScript;
assert pkgs.lib.hasInfix "-6 route replace default dev psperapptun0 table 101" perAppRoutingTunUpScript;
assert !(pkgs.lib.hasInfix "unreachable" perAppRoutingTunUpScript);
assert pkgs.lib.hasInfix "-6 rule add fwmark 16 table 101" perAppRoutingTunUpScript;
assert pkgs.lib.hasInfix "-6 route flush table 101" perAppRoutingTunCleanupScript;
assert pkgs.lib.hasInfix "link del dev" perAppRoutingTunCleanupScript;
assert pkgs.lib.hasInfix "psperapptun0" perAppRoutingTunCleanupScript;
assert pkgs.lib.hasInfix "rule del fwmark 16 table 101" perAppRoutingTunCleanupScript;
true
)
# -- perAppRouting: wrapped apps' replies to incoming connections skip the mark (and the
# user rule appended after it), so they leave directly --
(
assert pkgs.lib.hasInfix "ct direction reply return"
(builtins.head (pkgs.lib.splitString "ct mark 16 meta mark set 16" perAppRoutingTunNftRules));
true
)
# -- perAppRouting: app TUN enables nftables --
(
assert perAppRoutingTunFixture.config.networking.nftables.enable;
+6 -1
View File
@@ -42,7 +42,9 @@ let
xrayJqFilterRuntimeCheck =
pkgs.runCommand "proxy-suite-xray-jq-filter-runtime-check" { nativeBuildInputs = [ pkgs.jq ]; }
''
OBS='[{"protocol":"freedom","tag":"proxy-suite-ob-primary"}]'
OBS='[{"protocol":"freedom","tag":"proxy-suite-ob-primary"},
{"protocol":"vless","tag":"proxy-suite-ob-edge","settings":{"vnext":[{"address":"edge.example.com","port":443}]}},
{"protocol":"trojan","tag":"proxy-suite-ob-ip","settings":{"servers":[{"address":"203.0.113.1","port":443}]}}]'
check_runtime() {
local name="$1"
@@ -72,6 +74,9 @@ let
and .dns.servers[0].tag == "fakedns"
and ([.routing.rules[] | select((.ruleTag? // "") == "dns-hijack")] | length) == 1
and ([.routing.rules[] | select((.ruleTag? // "") == "dns-upstream-direct")] | length) == 1
and ([.routing.rules[] | select((.ruleTag? // "") == "dns-upstream-remote")] | length) == 1
and ([.dns.servers[] | select(.domains?)] == [.dns.servers[] | select(.tag == "local" and (has("domains") | not))
| . + {domains: ["full:edge.example.com"], skipFallback: true}])
and ([.outbounds[] | select(.streamSettings.sockopt.interface?)] | length) == 0
and ([.outbounds[] | select(.protocol == "freedom" or .tag == "proxy-suite-ob-primary") | .streamSettings.sockopt.domainStrategy] | all(. == "UseIPv4v6"))
and ([.outbounds[] | select(.protocol == "dns") | .streamSettings.sockopt.domainStrategy?] | all(. == null))
@@ -36,6 +36,14 @@
assert tproxyInbound.protocol == "tunnel";
assert tproxyInbound.settings.followRedirect == true;
assert tproxyInbound.streamSettings.sockopt.tproxy == "tproxy";
assert builtins.any (
inbound: inbound.tag == "tproxy-in6" && inbound.listen == "::1"
) xrayTproxyConfig.inbounds;
# Apps' DNS through TProxy is answered by XRay, as sing-box's hijack-dns.
assert builtins.any (
rule: (rule.ruleTag or "") == "dns-hijack" && rule.inboundTag == [ "tproxy-in" "tproxy-in6" ]
) xrayTproxyConfig.routing.rules;
assert builtins.any (outbound: outbound.tag == "dns-out") xrayTproxyConfig.outbounds;
assert specificRule.outboundTag == "proxy-suite-ob-primary";
assert specificRule.ruleTag == "custom-proxy-domain";
assert balancer.tag == "proxy";
@@ -22,6 +22,8 @@
!(pkgs.lib.hasInfix ''select(.protocol == "socks" and .tag == "mixed-in") | .settings.users'' xrayBackendJqFilter);
assert pkgs.lib.hasInfix "xray_preserved_rules" xrayBackendJqFilter;
assert pkgs.lib.hasInfix "dns-upstream-direct" xrayBackendJqFilter;
assert pkgs.lib.hasInfix "dns-upstream-remote" xrayBackendJqFilter;
assert pkgs.lib.hasInfix "xray_pin_server_names" xrayBackendJqFilter;
assert pkgs.lib.hasInfix "xray_dns_server_order" xrayBackendJqFilter;
assert pkgs.lib.hasInfix "dns-hijack" xrayBackendJqFilter;
assert pkgs.lib.hasInfix "del(.routing.balancers)" xrayBackendJqFilter;
+6 -10
View File
@@ -29,8 +29,8 @@
tunHasDnsHijackRule = builtins.any (
rule: (rule ? ruleTag) && rule.ruleTag == "dns-hijack"
) xrayTunConfig.routing.rules;
tunHasDnsUpstreamRule = builtins.any (
rule: (rule ? ruleTag) && rule.ruleTag == "dns-upstream-direct"
tunDnsUpstreamRules = builtins.filter (
rule: builtins.elem (rule.ruleTag or "") [ "dns-upstream-direct" "dns-upstream-remote" ]
) xrayTunConfig.routing.rules;
in
assert tunInbound.protocol == "tun";
@@ -38,7 +38,7 @@
assert
tunInbound.settings.gateway == [
"172.19.0.1/30"
checkConstants.xrayGlobalTunIPv6Address
checkConstants.globalTunIPv6Address
];
assert tunInbound.sniffing.destOverride == [ "fakedns" ];
assert tunInbound.sniffing.metadataOnly == false;
@@ -46,9 +46,9 @@
assert tunHasFinalRuleTag;
assert tunHasDirectGeositeRule;
assert tunHasDnsHijackRule;
assert tunHasDnsUpstreamRule;
# Servers route by their own tags: local direct, remote through the proxy.
assert map (rule: rule.inboundTag) tunDnsUpstreamRules == [ [ "local" ] [ "remote" ] ];
assert xrayTunConfig.dns.queryStrategy == "UseIP";
assert xrayTunConfig.dns.tag == "dns-in";
assert (builtins.head xrayTunConfig.dns.servers).address == "fakedns";
assert (builtins.head xrayTunConfig.dns.servers).tag == "fakedns";
assert (builtins.elemAt xrayTunConfig.dns.servers 1).tag == "remote";
@@ -69,7 +69,7 @@
assert pkgs.lib.hasInfix ''-f "$BACKEND_JQ_FILTER"'' xrayTunStartScript;
assert pkgs.lib.hasInfix "xray_tun_dns_runtime" xrayTunStartScript;
assert pkgs.lib.hasInfix ''tun_route_prefix="$(cidr_network "$tun_cidr")"'' xrayTunUpScript;
assert pkgs.lib.hasInfix checkConstants.xrayGlobalTunIPv6Address xrayTunUpScript;
assert pkgs.lib.hasInfix checkConstants.globalTunIPv6Address xrayTunUpScript;
assert pkgs.lib.hasInfix ''addr replace "$tun_cidr" dev singtun0'' xrayTunUpScript;
assert pkgs.lib.hasInfix ''-6 addr replace "$tun6_cidr" dev singtun0'' xrayTunUpScript;
assert pkgs.lib.hasInfix
@@ -84,9 +84,6 @@
assert pkgs.lib.hasInfix
"-6 route replace default dev singtun0 table ${toString checkConstants.tunAutoRouteTableIndex}"
xrayTunUpScript;
assert pkgs.lib.hasInfix
"rule add pref ${toString checkConstants.xrayTunPerAppTproxyRulePriority} fwmark 17 table 102"
xrayTunUpScript;
assert pkgs.lib.hasInfix
"rule add pref ${toString checkConstants.xrayTunPerAppTunRulePriority} fwmark 16 table 101"
xrayTunUpScript;
@@ -120,7 +117,6 @@
) [
checkConstants.xrayTunMarkBypassRulePriority
checkConstants.xrayTunServiceUserRulePriority
checkConstants.xrayTunPerAppTproxyRulePriority
checkConstants.xrayTunPerAppTunRulePriority
checkConstants.xrayTunDnsRulePriority
checkConstants.xrayTunMainRulePriority
@@ -23,13 +23,12 @@
assert
perAppTunInbound.settings.gateway == [
"172.20.0.1/30"
checkConstants.xrayPerAppTunIPv6Address
checkConstants.perAppTunIPv6Address
];
assert perAppTunInbound.sniffing.destOverride == [ "fakedns" ];
assert perAppTunInbound.sniffing.metadataOnly == false;
assert xrayPerAppTunConfig.routing.domainStrategy == "IPIfNonMatch";
assert (builtins.head xrayPerAppTunConfig.dns.servers).address == "fakedns";
assert xrayPerAppTunConfig.dns.tag == "dns-in";
assert builtins.length xrayPerAppTunConfig.fakedns == 2;
assert perAppTunDirectOutbound.streamSettings.sockopt.mark == 2;
assert pkgs.lib.hasInfix "XRAY_SINGLE_PROXY_TAG=" xrayPerAppTunStartScript;
@@ -38,7 +37,7 @@
assert pkgs.lib.hasInfix "--routing-mark 2" xrayPerAppTunStartScript;
assert pkgs.lib.hasInfix "xray_tun_dns_runtime" xrayPerAppTunStartScript;
assert pkgs.lib.hasInfix ''tun_route_prefix="$(cidr_network "$tun_cidr")"'' xrayPerAppTunUpScript;
assert pkgs.lib.hasInfix checkConstants.xrayPerAppTunIPv6Address xrayPerAppTunUpScript;
assert pkgs.lib.hasInfix checkConstants.perAppTunIPv6Address xrayPerAppTunUpScript;
assert !(pkgs.lib.hasInfix "uplink_addr" xrayPerAppTunUpScript);
assert pkgs.lib.hasInfix ''addr replace "$tun_cidr" dev psperapptun0'' xrayPerAppTunUpScript;
assert pkgs.lib.hasInfix ''-6 addr replace "$tun6_cidr" dev psperapptun0'' xrayPerAppTunUpScript;
+2 -1
View File
@@ -186,7 +186,8 @@ def portable_xray(cfg, only=None):
balanced = bool(routing.get("balancers"))
rules = []
for rule in routing.get("rules") or []:
if set(rule.get("inboundTag") or []) - {"mixed-in"}:
# "local" and "remote" are DNS servers' own queries: a proxy's name resolves direct.
if set(rule.get("inboundTag") or []) - {"mixed-in", "local", "remote"}:
continue
if rule.get("outboundTag") in dropped:
if balanced:
+2
View File
@@ -77,6 +77,7 @@ def xray_config():
"routing": {
"rules": [
{"type": "field", "inboundTag": ["tproxy-in"], "outboundTag": "direct"},
{"type": "field", "inboundTag": ["local"], "outboundTag": "direct"},
{"type": "field", "domain": ["geosite:google"], "outboundTag": "proxy-suite-ob-warp"},
{"type": "field", "network": "tcp,udp", "balancerTag": "proxy"},
],
@@ -188,6 +189,7 @@ class XrayTest(unittest.TestCase):
self.assertEqual(
cfg["routing"]["rules"],
[
{"type": "field", "inboundTag": ["local"], "outboundTag": "direct"},
{"type": "field", "domain": ["geosite:google"], "balancerTag": "proxy"},
{"type": "field", "network": "tcp,udp", "balancerTag": "proxy"},
],