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; strategy = null;
}; };
enable = false; enable = false;
ipv6 = true;
listener = { listener = {
address = "127.0.0.1"; address = "127.0.0.1";
auth = { 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) - [rules](#services-proxy-suite-proxy-dns-singbox-rules)
- [servers](#services-proxy-suite-proxy-dns-singbox-servers) - [servers](#services-proxy-suite-proxy-dns-singbox-servers)
- [strategy](#services-proxy-suite-proxy-dns-strategy) - [strategy](#services-proxy-suite-proxy-dns-strategy)
- [ipv6](#services-proxy-suite-proxy-ipv6)
- listener - listener
- [address](#services-proxy-suite-proxy-listener-address) - [address](#services-proxy-suite-proxy-listener-address)
- auth - auth
@@ -589,6 +590,24 @@ null
"ipv4_only" "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> <a id="services-proxy-suite-proxy-listener-address"></a>
## services\.proxy-suite\.proxy\.listener\.address ## services\.proxy-suite\.proxy\.listener\.address
@@ -1622,7 +1641,7 @@ signed integer
<a id="services-proxy-suite-proxy-tproxy-localsubnets"></a> <a id="services-proxy-suite-proxy-tproxy-localsubnets"></a>
## services\.proxy-suite\.proxy\.tproxy\.localSubnets ## 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:* *Type:*
list of string list of string
@@ -1641,6 +1660,7 @@ list of string
[ [
"192.168.0.0/16" "192.168.0.0/16"
"10.0.0.0/8" "10.0.0.0/8"
"fd00::/8"
] ]
``` ```
@@ -100,6 +100,8 @@ let
enableTun ? false, enableTun ? false,
tunInterface ? globalTun.interface, tunInterface ? globalTun.interface,
tunAddress ? globalTun.address, tunAddress ? globalTun.address,
# With proxy.ipv6: both families in the TUN, so strict_route rejects neither.
tunIPv6Address ? null,
tunMtu ? globalTun.mtu, tunMtu ? globalTun.mtu,
tunAutoRoute ? true, tunAutoRoute ? true,
tunAutoRouteTableIndex ? defaultTunAutoRouteTableIndex, tunAutoRouteTableIndex ? defaultTunAutoRouteTableIndex,
@@ -139,18 +141,28 @@ let
listen = proxyCfg.listener.address; listen = proxyCfg.listener.address;
listen_port = proxyCfg.listener.port; listen_port = proxyCfg.listener.port;
} }
++ lib.optional enableTProxy { ++ lib.optionals enableTProxy (
type = "tproxy"; [
tag = "tproxy-in"; {
listen = "127.0.0.1"; type = "tproxy";
listen_port = globalTproxy.port; 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 ( ++ lib.optional enableTun (
{ {
type = "tun"; type = "tun";
tag = "tun-in"; tag = "tun-in";
interface_name = tunInterface; interface_name = tunInterface;
address = [ tunAddress ]; address = [ tunAddress ] ++ lib.optional (proxyCfg.ipv6 && tunIPv6Address != null) tunIPv6Address;
mtu = tunMtu; mtu = tunMtu;
auto_route = tunAutoRoute; auto_route = tunAutoRoute;
auto_redirect = tunAutoRedirect; auto_redirect = tunAutoRedirect;
@@ -211,6 +223,7 @@ in
enableTun = true; enableTun = true;
tunInterface = globalTun.interface; tunInterface = globalTun.interface;
tunAddress = globalTun.address; tunAddress = globalTun.address;
tunIPv6Address = constants.globalTunIPv6Address;
tunMtu = globalTun.mtu; tunMtu = globalTun.mtu;
tunAutoRoute = true; tunAutoRoute = true;
tunAutoRedirect = true; tunAutoRedirect = true;
@@ -225,6 +238,7 @@ in
enableTun = true; enableTun = true;
tunInterface = perAppRoutingTun.interface; tunInterface = perAppRoutingTun.interface;
tunAddress = perAppRoutingTun.address; tunAddress = perAppRoutingTun.address;
tunIPv6Address = constants.perAppTunIPv6Address;
tunMtu = perAppRoutingTun.mtu; tunMtu = perAppRoutingTun.mtu;
tunAutoRoute = false; tunAutoRoute = false;
tunAutoRedirect = false; tunAutoRedirect = false;
+55 -48
View File
@@ -15,8 +15,8 @@ let
perAppRoutingTun perAppRoutingTun
; ;
inherit (constants) inherit (constants)
xrayGlobalTunIPv6Address globalTunIPv6Address
xrayPerAppTunIPv6Address perAppTunIPv6Address
; ;
fakeDnsPools = [ fakeDnsPools = [
@@ -40,8 +40,11 @@ let
queryStrategy = "UseIP"; 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"), preferRemote ? (proxyCfg.routing.default == "proxy"),
}: }:
let let
@@ -52,15 +55,15 @@ let
in in
{ {
queryStrategy = "UseIP"; queryStrategy = "UseIP";
tag = "dns-in"; servers =
servers = [ lib.optional fakeDns {
{
address = "fakedns"; address = "fakedns";
tag = "fakedns"; tag = "fakedns";
} }
(mkDnsServer primaryTag primaryUpstream) ++ [
(mkDnsServer secondaryTag secondaryUpstream) (mkDnsServer primaryTag primaryUpstream)
]; (mkDnsServer secondaryTag secondaryUpstream)
];
}; };
mkSniffing = mkSniffing =
@@ -88,22 +91,41 @@ let
standardSniffing = mkSniffing { }; standardSniffing = mkSniffing { };
tunDnsHijackRule = { target =
tag:
if tag == "proxy" && proxyCfg.selection == "urltest" then
{ balancerTag = "proxy"; }
else
{ outboundTag = tag; };
dnsHijackRule = inboundTag: {
type = "field"; type = "field";
inboundTag = [ "tun-in" ]; inherit inboundTag;
network = "tcp,udp"; network = "tcp,udp";
port = 53; port = 53;
outboundTag = "dns-out"; outboundTag = "dns-out";
ruleTag = "dns-hijack"; ruleTag = "dns-hijack";
}; };
tunDnsUpstreamRule = { # As sing-box: remote through the proxy, local direct.
type = "field"; dnsUpstreamRules = [
inboundTag = [ "dns-in" ]; {
network = "tcp,udp"; type = "field";
outboundTag = "direct"; inboundTag = [ "local" ];
ruleTag = "dns-upstream-direct"; network = "tcp,udp";
}; outboundTag = "direct";
ruleTag = "dns-upstream-direct";
}
(
{
type = "field";
inboundTag = [ "remote" ];
network = "tcp,udp";
ruleTag = "dns-upstream-remote";
}
// target "proxy"
)
];
finalRule = finalRule =
tag: tag:
@@ -112,12 +134,7 @@ let
network = "tcp,udp"; network = "tcp,udp";
ruleTag = "final-default"; ruleTag = "final-default";
} }
// ( // target tag;
if tag == "proxy" && proxyCfg.selection == "urltest" then
{ balancerTag = "proxy"; }
else
{ outboundTag = tag; }
);
directOutbound = directOutbound =
useOutboundRoutingMark: useOutboundRoutingMark:
@@ -146,17 +163,9 @@ let
}: }:
let let
tunSniffing = mkSniffing { fakeDnsOnly = enableTunFakeDns; }; tunSniffing = mkSniffing { fakeDnsOnly = enableTunFakeDns; };
dnsConfig = tproxyInboundTags = [ "tproxy-in" ] ++ lib.optional proxyCfg.ipv6 "tproxy-in6";
if enableTunFakeDns then # Every config takes packets for any destination, DNS included.
mkTunDnsConfig { } dnsOutbounds = [
else
{
servers = [
(mkDnsServer "remote" proxyCfg.dns.remote)
(mkDnsServer "local" proxyCfg.dns.local)
];
};
tunOutbounds = lib.optionals enableTunFakeDns [
{ {
protocol = "dns"; protocol = "dns";
tag = "dns-out"; tag = "dns-out";
@@ -172,16 +181,15 @@ let
} }
]; ];
routingRules = routingRules =
lib.optionals enableTunFakeDns [ dnsUpstreamRules
tunDnsUpstreamRule ++ lib.optional enableTun (dnsHijackRule [ "tun-in" ])
tunDnsHijackRule ++ lib.optional enableTProxy (dnsHijackRule tproxyInboundTags)
]
++ rules.xrayRoutingRules ++ rules.xrayRoutingRules
++ [ (finalRule (if (proxyCfg.routing.default == "proxy") then "proxy" else "direct")) ]; ++ [ (finalRule (if (proxyCfg.routing.default == "proxy") then "proxy" else "direct")) ];
in in
{ {
log.loglevel = "warning"; log.loglevel = "warning";
dns = dnsConfig; dns = mkDnsConfig { fakeDns = enableTunFakeDns; };
inbounds = inbounds =
lib.optional enableMixed { lib.optional enableMixed {
tag = "mixed-in"; tag = "mixed-in";
@@ -195,10 +203,9 @@ let
}; };
sniffing = standardSniffing; sniffing = standardSniffing;
} }
++ lib.optional enableTProxy { ++ lib.zipListsWith (tag: listen: {
tag = "tproxy-in"; inherit tag listen;
protocol = "tunnel"; protocol = "tunnel";
listen = "127.0.0.1";
port = globalTproxy.port; port = globalTproxy.port;
settings = { settings = {
allowedNetwork = "tcp,udp"; allowedNetwork = "tcp,udp";
@@ -206,19 +213,19 @@ let
}; };
streamSettings.sockopt.tproxy = "tproxy"; streamSettings.sockopt.tproxy = "tproxy";
sniffing = standardSniffing; sniffing = standardSniffing;
} }) (lib.optionals enableTProxy tproxyInboundTags) [ "127.0.0.1" "::1" ]
++ lib.optional enableTun { ++ lib.optional enableTun {
tag = "tun-in"; tag = "tun-in";
protocol = "tun"; protocol = "tun";
settings = { settings = {
name = tunInterface; name = tunInterface;
mtu = tunMtu; mtu = tunMtu;
gateway = [ tunAddress ] ++ lib.optionals (tunIPv6Address != null) [ tunIPv6Address ]; gateway = [ tunAddress ] ++ lib.optional (proxyCfg.ipv6 && tunIPv6Address != null) tunIPv6Address;
userLevel = 0; userLevel = 0;
}; };
sniffing = tunSniffing; sniffing = tunSniffing;
}; };
outbounds = tunOutbounds ++ [ outbounds = dnsOutbounds ++ [
(directOutbound useOutboundRoutingMark) (directOutbound useOutboundRoutingMark)
{ {
protocol = "blackhole"; protocol = "blackhole";
@@ -267,7 +274,7 @@ in
enableTun = true; enableTun = true;
tunInterface = globalTun.interface; tunInterface = globalTun.interface;
tunAddress = globalTun.address; tunAddress = globalTun.address;
tunIPv6Address = xrayGlobalTunIPv6Address; tunIPv6Address = globalTunIPv6Address;
tunMtu = globalTun.mtu; tunMtu = globalTun.mtu;
useOutboundRoutingMark = true; useOutboundRoutingMark = true;
enableTunFakeDns = true; enableTunFakeDns = true;
@@ -277,7 +284,7 @@ in
enableTun = true; enableTun = true;
tunInterface = perAppRoutingTun.interface; tunInterface = perAppRoutingTun.interface;
tunAddress = perAppRoutingTun.address; tunAddress = perAppRoutingTun.address;
tunIPv6Address = xrayPerAppTunIPv6Address; tunIPv6Address = perAppTunIPv6Address;
tunMtu = perAppRoutingTun.mtu; tunMtu = perAppRoutingTun.mtu;
useOutboundRoutingMark = true; useOutboundRoutingMark = true;
enableTunFakeDns = true; enableTunFakeDns = true;
+4 -5
View File
@@ -285,7 +285,6 @@ let
# clients included) stay out of the TUN. # clients included) stay out of the TUN.
xrayTunMarkBypassRulePriority = 8992; xrayTunMarkBypassRulePriority = 8992;
xrayTunServiceUserRulePriority = 8993; xrayTunServiceUserRulePriority = 8993;
xrayTunPerAppTproxyRulePriority = 8994;
xrayTunPerAppTunRulePriority = 8995; xrayTunPerAppTunRulePriority = 8995;
# DNS stays in the TUN for fakedns, even to a LAN resolver. # DNS stays in the TUN for fakedns, even to a LAN resolver.
xrayTunDnsRulePriority = 8996; xrayTunDnsRulePriority = 8996;
@@ -293,10 +292,10 @@ let
# replies to connections from there leave the way they came. # replies to connections from there leave the way they came.
xrayTunMainRulePriority = 8997; xrayTunMainRulePriority = 8997;
xrayGlobalTunIPv6Address = "fd66:19::1/64"; globalTunIPv6Address = "fd66:19::1/64";
xrayGlobalTunIPv6RoutePrefix = "fd66:19::/64"; globalTunIPv6RoutePrefix = "fd66:19::/64";
xrayPerAppTunIPv6Address = "fd66:20::1/64"; perAppTunIPv6Address = "fd66:20::1/64";
xrayPerAppTunIPv6RoutePrefix = "fd66:20::/64"; perAppTunIPv6RoutePrefix = "fd66:20::/64";
xrayDnsBridgePorts = { xrayDnsBridgePorts = {
socks = 18533; socks = 18533;
+41 -12
View File
@@ -18,7 +18,7 @@ let
meta mark ${toString tgWsProxyCfg.fwmark} return 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 = '' reservedIpBlock = ''
define RESERVED_IP = { define RESERVED_IP = {
10.0.0.0/8, 10.0.0.0/8,
@@ -31,67 +31,94 @@ let
240.0.0.0/4, 240.0.0.0/4,
255.255.255.255/32 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 = mkLocalSubnetLines =
subnets: subnets:
lib.concatMapStrings (cidr: '' lib.concatMapStrings (cidr: ''
ip daddr ${cidr} tcp dport != 53 return ${ipFamily cidr} daddr ${cidr} tcp dport != 53 return
ip daddr ${cidr} udp dport != 53 return ${ipFamily cidr} daddr ${cidr} udp dport != 53 return
'') subnets; '') subnets;
tproxyLocalSubnetLines = mkLocalSubnetLines globalTproxy.localSubnets; tproxyLocalSubnetLines = mkLocalSubnetLines globalTproxy.localSubnets;
perAppTproxyLocalSubnetLines = mkLocalSubnetLines perAppTproxy.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" '' nftablesRulesFile = pkgs.writeText "proxy-suite-routing" ''
${reservedIpBlock} ${reservedIpBlock}
table ip singbox { table inet singbox {
chain prerouting { chain prerouting {
type filter hook prerouting priority mangle; policy accept; type filter hook prerouting priority mangle; policy accept;
ip daddr $RESERVED_IP return ip daddr $RESERVED_IP return
ip6 daddr $RESERVED_IP6 return
# Connections to this host itself (inbounds, sshd on a public address) # Connections to this host itself (inbounds, sshd on a public address)
# are served here, not taken by the tproxy socket. # are served here, not taken by the tproxy socket.
fib daddr type local return fib daddr type local return
# Packets re-entering via loopback after output marking should not # Packets re-entering via loopback after output marking should not
# be skipped just because the host has an RFC1918 source address. # be skipped just because the host has an RFC1918 source address.
iifname != "lo" ip saddr $RESERVED_IP return iifname != "lo" ip saddr $RESERVED_IP return
iifname != "lo" ip6 saddr $RESERVED_IP6 return
${tproxyLocalSubnetLines} ${tproxyLocalSubnetLines}
ip protocol tcp tproxy to 127.0.0.1:${toString globalTproxy.port} meta mark set ${toString globalTproxy.fwmark} ${mkTproxyLines "" " meta mark set ${toString globalTproxy.fwmark}"}
ip protocol udp tproxy to 127.0.0.1:${toString globalTproxy.port} meta mark set ${toString globalTproxy.fwmark}
} }
chain output { chain output {
type route hook output priority mangle; policy accept; type route hook output priority mangle; policy accept;
ip daddr $RESERVED_IP return ip daddr $RESERVED_IP return
ip6 daddr $RESERVED_IP6 return
${tproxyLocalSubnetLines} ${tproxyLocalSubnetLines}
meta mark ${toString globalTproxy.proxyMark} return meta mark ${toString globalTproxy.proxyMark} return
# Replies to connections from outside, and proxy-suite's own daemons (the # Replies to connections from outside, and proxy-suite's own daemons (the
# inbound XRay dials unmarked, lacking CAP_NET_ADMIN), leave as they are. # inbound XRay dials unmarked, lacking CAP_NET_ADMIN), leave as they are.
ct direction reply return ct direction reply return
meta skuid "${serviceUser}" 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} ${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}
ip protocol udp meta mark set ${toString globalTproxy.fwmark}
} }
} }
''; '';
perAppTproxyRulesFile = pkgs.writeText "proxy-suite-routing" '' perAppTproxyRulesFile = pkgs.writeText "proxy-suite-routing" ''
${reservedIpBlock} ${reservedIpBlock}
table ip proxy_suite_per_app_tproxy { table inet proxy_suite_per_app_tproxy {
chain prerouting { chain prerouting {
type filter hook prerouting priority mangle; policy accept; type filter hook prerouting priority mangle; policy accept;
ip daddr $RESERVED_IP return ip daddr $RESERVED_IP return
ip6 daddr $RESERVED_IP6 return
iifname != "lo" ip saddr $RESERVED_IP 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} ${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} 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} ${mkTproxyLines "meta mark ${toString perAppTproxy.fwmark} " ""}
meta mark ${toString perAppTproxy.fwmark} ip protocol udp tproxy to 127.0.0.1:${toString globalTproxy.port}
} }
chain output { chain output {
type route hook output priority mangle; policy accept; type route hook output priority mangle; policy accept;
ip daddr $RESERVED_IP return ip daddr $RESERVED_IP return
ip6 daddr $RESERVED_IP6 return
${perAppTproxyLocalSubnetLines} ${perAppTproxyLocalSubnetLines}
meta mark ${toString globalTproxy.proxyMark} return 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} ${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 meta mark ${toString perAppTproxy.fwmark} return
} }
@@ -120,8 +147,10 @@ let
type route hook output priority mangle; policy accept; type route hook output priority mangle; policy accept;
ip daddr $RESERVED_IP return ip daddr $RESERVED_IP return
${lib.concatMapStrings (cidr: '' ${lib.concatMapStrings (cidr: ''
ip daddr ${cidr} return ${ipFamily cidr} daddr ${cidr} return
'') perAppTun.localSubnets} '') 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} ct mark ${toString perAppTun.fwmark} meta mark set ${toString perAppTun.fwmark}
meta mark ${toString perAppTun.fwmark} return meta mark ${toString perAppTun.fwmark} return
} }
+2 -1
View File
@@ -34,10 +34,11 @@ in
localSubnets = mkOption { localSubnets = mkOption {
type = types.listOf types.str; type = types.listOf types.str;
default = [ "192.168.0.0/16" ]; 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 = [ example = [
"192.168.0.0/16" "192.168.0.0/16"
"10.0.0.0/8" "10.0.0.0/8"
"fd00::/8"
]; ];
}; };
}; };
+14
View File
@@ -1,4 +1,5 @@
{ {
config,
lib, lib,
proxySuiteUpstream, proxySuiteUpstream,
... ...
@@ -46,6 +47,19 @@ in
example = "tun"; 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 = { listener = {
address = mkOption { address = mkOption {
type = types.str; type = types.str;
+29 -1
View File
@@ -247,12 +247,40 @@ rec {
mkIpLocalDefaultRouteDelete = mkIpLocalDefaultRouteDelete =
{ {
ip, ip,
family ? "",
table, 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 = mkIpLinkDelete =
{ {
ip, ip,
-1
View File
@@ -145,7 +145,6 @@ let
cfg cfg
singBoxCfg singBoxCfg
proxyCfg proxyCfg
pureXrayEnabled
perAppRoutingCfg perAppRoutingCfg
perAppRoutingTun perAppRoutingTun
perAppRoutingTproxy perAppRoutingTproxy
+1 -1
View File
@@ -89,8 +89,8 @@ let
globalTun globalTun
globalTproxy globalTproxy
perAppRoutingTun perAppRoutingTun
perAppRoutingTproxy
; ;
inherit (proxyCfg) ipv6;
}; };
serviceUnits = import ./units.nix { serviceUnits = import ./units.nix {
@@ -5,7 +5,6 @@
cfg, cfg,
singBoxCfg, singBoxCfg,
proxyCfg, proxyCfg,
pureXrayEnabled,
perAppRoutingCfg, perAppRoutingCfg,
perAppRoutingTun, perAppRoutingTun,
perAppRoutingTproxy, perAppRoutingTproxy,
@@ -60,7 +59,6 @@ let
lib lib
pkgs pkgs
builders builders
pureXrayEnabled
perAppRoutingTun perAppRoutingTun
perAppRoutingTproxy perAppRoutingTproxy
constants constants
@@ -71,6 +69,7 @@ let
seqBin seqBin
sleepBin sleepBin
; ;
inherit (proxyCfg) ipv6;
}; };
inherit (backendScripts) inherit (backendScripts)
perAppTunUpScript perAppTunUpScript
@@ -3,9 +3,9 @@
lib, lib,
pkgs, pkgs,
builders, builders,
pureXrayEnabled,
perAppRoutingTun, perAppRoutingTun,
perAppRoutingTproxy, perAppRoutingTproxy,
ipv6,
constants, constants,
perAppTunChainFile, perAppTunChainFile,
perAppTproxyRulesFile, perAppTproxyRulesFile,
@@ -17,8 +17,8 @@
let let
inherit (constants) inherit (constants)
xrayPerAppTunIPv6Address perAppTunIPv6Address
xrayPerAppTunIPv6RoutePrefix perAppTunIPv6RoutePrefix
; ;
perAppTunWaitForInterface = pkgs.writeShellScript "proxy-suite-per-app" '' perAppTunWaitForInterface = pkgs.writeShellScript "proxy-suite-per-app" ''
@@ -37,8 +37,8 @@ let
set -euo pipefail set -euo pipefail
tun_cidr=${lib.escapeShellArg perAppRoutingTun.address} tun_cidr=${lib.escapeShellArg perAppRoutingTun.address}
tun6_cidr=${lib.escapeShellArg xrayPerAppTunIPv6Address} tun6_cidr=${lib.escapeShellArg perAppTunIPv6Address}
tun6_route_prefix=${lib.escapeShellArg xrayPerAppTunIPv6RoutePrefix} tun6_route_prefix=${lib.escapeShellArg perAppTunIPv6RoutePrefix}
tun_addr="" tun_addr=""
tun_route_prefix="" tun_route_prefix=""
@@ -68,12 +68,21 @@ let
# No uplink src, as in xrayTunUpScript: it goes stale when the uplink address changes. # 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 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} -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} if ipv6 then
${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 addr replace "$tun6_cidr" dev ${lib.escapeShellArg perAppRoutingTun.interface}
${ip} -6 rule add fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable} 2>/dev/null || true ${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" '' perAppTunDownScript = pkgs.writeShellScript "proxy-suite-per-app" ''
@@ -102,29 +111,31 @@ let
perAppTproxyUpScript = pkgs.writeShellScript "proxy-suite-per-app" '' perAppTproxyUpScript = pkgs.writeShellScript "proxy-suite-per-app" ''
set -euo pipefail set -euo pipefail
${builders.mkNftDeleteTable { inherit nft; family = "ip"; table = "proxy_suite_per_app_tproxy"; }} ${builders.mkNftDeleteTable { inherit nft; family = "inet"; table = "proxy_suite_per_app_tproxy"; }}
${builders.mkIpRuleDeleteByFwmark { ${builders.mkTproxyRoutingDown {
inherit ip; inherit ip;
fwmark = perAppRoutingTproxy.fwmark; fwmark = perAppRoutingTproxy.fwmark;
table = perAppRoutingTproxy.routeTable; table = perAppRoutingTproxy.routeTable;
}} }}
${builders.mkIpLocalDefaultRouteDelete { inherit ip; table = perAppRoutingTproxy.routeTable; }}
${nft} -f ${perAppTproxyRulesFile} ${nft} -f ${perAppTproxyRulesFile}
${ip} route replace local default dev lo table ${toString perAppRoutingTproxy.routeTable} ${builders.mkTproxyRoutingUp {
${ip} rule add fwmark ${toString perAppRoutingTproxy.fwmark} table ${toString perAppRoutingTproxy.routeTable} inherit ip;
inherit ipv6;
fwmark = perAppRoutingTproxy.fwmark;
table = perAppRoutingTproxy.routeTable;
}}
''; '';
perAppTproxyDownScript = pkgs.writeShellScript "proxy-suite-per-app" '' perAppTproxyDownScript = pkgs.writeShellScript "proxy-suite-per-app" ''
set +e set +e
${builders.mkNftDeleteTable { inherit nft; family = "ip"; table = "proxy_suite_per_app_tproxy"; }} ${builders.mkNftDeleteTable { inherit nft; family = "inet"; table = "proxy_suite_per_app_tproxy"; }}
${builders.mkIpRuleDeleteByFwmark { ${builders.mkTproxyRoutingDown {
inherit ip; inherit ip;
fwmark = perAppRoutingTproxy.fwmark; fwmark = perAppRoutingTproxy.fwmark;
table = perAppRoutingTproxy.routeTable; table = perAppRoutingTproxy.routeTable;
}} }}
${builders.mkIpLocalDefaultRouteDelete { inherit ip; table = perAppRoutingTproxy.routeTable; }}
''; '';
in in
{ {
@@ -100,7 +100,7 @@ let
perAppTproxyUserRuleStart = mkUserRuleStart { perAppTproxyUserRuleStart = mkUserRuleStart {
name = "per-app-tproxy"; name = "per-app-tproxy";
nftFamily = "ip"; nftFamily = "inet";
nftTable = "proxy_suite_per_app_tproxy"; nftTable = "proxy_suite_per_app_tproxy";
nftChain = "output"; nftChain = "output";
sliceName = perAppTproxySliceName; sliceName = perAppTproxySliceName;
@@ -109,7 +109,7 @@ let
}; };
perAppTproxyUserRuleStop = mkUserRuleStop { perAppTproxyUserRuleStop = mkUserRuleStop {
name = "per-app-tproxy"; name = "per-app-tproxy";
nftFamily = "ip"; nftFamily = "inet";
nftTable = "proxy_suite_per_app_tproxy"; nftTable = "proxy_suite_per_app_tproxy";
nftChain = "output"; nftChain = "output";
}; };
+20 -21
View File
@@ -9,7 +9,7 @@
globalTun, globalTun,
globalTproxy, globalTproxy,
perAppRoutingTun, perAppRoutingTun,
perAppRoutingTproxy, ipv6,
}: }:
let let
@@ -18,12 +18,11 @@ let
tunAutoRouteRulePriority tunAutoRouteRulePriority
xrayTunMarkBypassRulePriority xrayTunMarkBypassRulePriority
xrayTunServiceUserRulePriority xrayTunServiceUserRulePriority
xrayTunPerAppTproxyRulePriority
xrayTunPerAppTunRulePriority xrayTunPerAppTunRulePriority
xrayTunDnsRulePriority xrayTunDnsRulePriority
xrayTunMainRulePriority xrayTunMainRulePriority
xrayGlobalTunIPv6Address globalTunIPv6Address
xrayGlobalTunIPv6RoutePrefix globalTunIPv6RoutePrefix
; ;
deleteXrayTunRules = lib.concatMapStrings ( deleteXrayTunRules = lib.concatMapStrings (
@@ -31,8 +30,7 @@ let
lib.concatMapStrings (priority: builders.mkIpRuleDeleteByPriority { inherit ip family priority; }) [ lib.concatMapStrings (priority: builders.mkIpRuleDeleteByPriority { inherit ip family priority; }) [
xrayTunMarkBypassRulePriority xrayTunMarkBypassRulePriority
xrayTunServiceUserRulePriority xrayTunServiceUserRulePriority
xrayTunPerAppTproxyRulePriority xrayTunPerAppTunRulePriority
xrayTunPerAppTunRulePriority
xrayTunDnsRulePriority xrayTunDnsRulePriority
xrayTunMainRulePriority xrayTunMainRulePriority
tunAutoRouteRulePriority tunAutoRouteRulePriority
@@ -62,8 +60,8 @@ in
set -euo pipefail set -euo pipefail
tun_cidr=${lib.escapeShellArg globalTun.address} tun_cidr=${lib.escapeShellArg globalTun.address}
tun6_cidr=${lib.escapeShellArg xrayGlobalTunIPv6Address} tun6_cidr=${lib.escapeShellArg globalTunIPv6Address}
tun6_route_prefix=${lib.escapeShellArg xrayGlobalTunIPv6RoutePrefix} tun6_route_prefix=${lib.escapeShellArg globalTunIPv6RoutePrefix}
tun_addr="" tun_addr=""
tun_route_prefix="" tun_route_prefix=""
@@ -89,15 +87,14 @@ in
tun_route_prefix="$(cidr_network "$tun_cidr")" tun_route_prefix="$(cidr_network "$tun_cidr")"
${ip} -4 addr replace "$tun_cidr" dev ${lib.escapeShellArg globalTun.interface} ${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} ${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 # 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. # (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} -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} ${lib.optionalString ipv6 ''
${ip} -6 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}
${lib.optionalString perAppRoutingTproxy.enable '' ${ip} -6 route replace default dev ${lib.escapeShellArg globalTun.interface} table ${toString tunAutoRouteTableIndex}
${ip} -4 rule add pref ${toString xrayTunPerAppTproxyRulePriority} fwmark ${toString perAppRoutingTproxy.fwmark} table ${toString perAppRoutingTproxy.routeTable}
''} ''}
${lib.optionalString perAppRoutingTun.enable '' ${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 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 # 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 # rules on some iproute2 versions, and a stale rule can keep packets routed
# into a dead local table after a failed restart. # into a dead local table after a failed restart.
${builders.mkNftDeleteTable { inherit nft; family = "ip"; table = "singbox"; }} ${builders.mkNftDeleteTable { inherit nft; family = "inet"; table = "singbox"; }}
${builders.mkIpRuleDeleteByFwmark { ${builders.mkTproxyRoutingDown {
inherit ip; inherit ip;
fwmark = globalTproxy.fwmark; fwmark = globalTproxy.fwmark;
table = globalTproxy.routeTable; table = globalTproxy.routeTable;
}} }}
${builders.mkIpLocalDefaultRouteDelete { inherit ip; table = globalTproxy.routeTable; }}
${nft} -f ${nftablesRulesFile} ${nft} -f ${nftablesRulesFile}
${ip} route replace local default dev lo table ${toString globalTproxy.routeTable} ${builders.mkTproxyRoutingUp {
${ip} rule add fwmark ${toString globalTproxy.fwmark} table ${toString globalTproxy.routeTable} inherit ip;
inherit ipv6;
fwmark = globalTproxy.fwmark;
table = globalTproxy.routeTable;
}}
''; '';
tproxyDownScript = pkgs.writeShellScript "proxy-suite-routing" '' tproxyDownScript = pkgs.writeShellScript "proxy-suite-routing" ''
set +e set +e
${builders.mkNftDeleteTable { inherit nft; family = "ip"; table = "singbox"; }} ${builders.mkNftDeleteTable { inherit nft; family = "inet"; table = "singbox"; }}
${builders.mkIpRuleDeleteByFwmark { ${builders.mkTproxyRoutingDown {
inherit ip; inherit ip;
fwmark = globalTproxy.fwmark; fwmark = globalTproxy.fwmark;
table = globalTproxy.routeTable; table = globalTproxy.routeTable;
}} }}
${builders.mkIpLocalDefaultRouteDelete { inherit ip; table = globalTproxy.routeTable; }}
''; '';
tunCleanupScript = pkgs.writeShellScript "proxy-suite-routing" '' tunCleanupScript = pkgs.writeShellScript "proxy-suite-routing" ''
@@ -23,7 +23,7 @@ if pureXrayEnabled then
end; end;
def xray_preserved_rules: def xray_preserved_rules:
[.routing.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): def xray_final_rule($tag; $single_tag):
if $tag == "proxy" and "${selectionMode}" == "urltest" then if $tag == "proxy" and "${selectionMode}" == "urltest" then
{type:"field",network:"tcp,udp",ruleTag:"final-default"} + xray_proxy_rule_target($single_tag) {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? // "") == $dns_final)]
+ [.dns.servers[] | select((.tag? // "") != "fakedns" and (.tag? // "") != $dns_final)]) + [.dns.servers[] | select((.tag? // "") != "fakedns" and (.tag? // "") != $dns_final)])
end; 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) .outbounds = ($obs + .outbounds)
| if $xray_loglevel == "" then . else .log.loglevel = $xray_loglevel end | if $xray_loglevel == "" then . else .log.loglevel = $xray_loglevel end
| if $auth_enabled then | if $auth_enabled then
(.inbounds[] | select(.protocol == "socks" and .tag == "mixed-in") | .settings.auth) = "password" (.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}] | (.inbounds[] | select(.protocol == "socks" and .tag == "mixed-in") | .settings.accounts) = [{user:$user,pass:$password}]
else . end else . end
| .dns.servers = xray_dns_server_order($dns_final)
| xray_pin_server_names
| if $xray_tun_dns_runtime then | if $xray_tun_dns_runtime then
.dns.servers = xray_dns_server_order($dns_final) # Without a domainStrategy a dial resolves through the system resolver, whose upstream
# Without a domainStrategy a dial resolves through the system resolver, which under # queries the TUN answers with fake IPs and TProxy sends through the proxy being
# the TUN (resolved's upstream queries included) hands out fake IPs. XRay's own DNS # dialed. XRay's own DNS client skips fakedns.
# client skips fakedns. .outbounds |= map(
| .outbounds |= map(
if .protocol == "blackhole" or .protocol == "dns" or .protocol == "loopback" then . if .protocol == "blackhole" or .protocol == "dns" or .protocol == "loopback" then .
else .streamSettings.sockopt.domainStrategy //= "UseIPv4v6" end) else .streamSettings.sockopt.domainStrategy //= "UseIPv4v6" end)
else . end else . end
@@ -413,6 +413,9 @@ let
configFile = tproxyFile; configFile = tproxyFile;
routingMark = globalTproxy.proxyMark; routingMark = globalTproxy.proxyMark;
enableLocalProxyAuth = localProxyAuthEnabled; 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; xraySidecarBasePort = xraySidecarBasePorts.socks;
xrayDnsBridgePort = xrayDnsBridgePorts.socks; xrayDnsBridgePort = xrayDnsBridgePorts.socks;
# Only the socks unit: relayed traffic reaches it, and the prober needs one home. # Only the socks unit: relayed traffic reaches it, and the prober needs one home.
+49 -2
View File
@@ -5,6 +5,8 @@
mkBadFixture, mkBadFixture,
mkFailingAssertions, mkFailingAssertions,
mkTunConfig, mkTunConfig,
mkTProxyConfig,
mkTProxyNftRules,
dnsServerByTag, dnsServerByTag,
checkConstants, checkConstants,
}: }:
@@ -17,6 +19,8 @@ let
mkBadFixture mkBadFixture
mkFailingAssertions mkFailingAssertions
mkTunConfig mkTunConfig
mkTProxyConfig
mkTProxyNftRules
; ;
}; };
@@ -26,6 +30,15 @@ let
tproxyManualFixture tproxyManualFixture
tproxyManualStartScript tproxyManualStartScript
tproxyManualStopScript tproxyManualStopScript
tproxyManualConfig
tproxyManualNftRules
tproxyIPv4OnlyStartScript
tproxyIPv4OnlyConfig
tproxyIPv4OnlyNftRules
ipv4OnlyTunConfig
ipv4OnlyPerAppTunUpScript
xrayIPv4OnlyTunConfig
xrayIPv4OnlyTunUpScript
tproxyWithFirewall tproxyWithFirewall
tunAutostartFixture tunAutostartFixture
tunCleanupScript tunCleanupScript
@@ -56,6 +69,42 @@ in
assert pkgs.lib.hasInfix "rule del fwmark 1 table 100" tproxyManualStartScript; 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 "route replace local default dev lo table 100" tproxyManualStartScript;
assert pkgs.lib.hasInfix "set +e" tproxyManualStopScript; 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; assert pkgs.lib.hasInfix "rule del fwmark 1 table 100" tproxyManualStopScript;
true true
) )
@@ -82,8 +131,6 @@ in
assert pkgs.lib.hasInfix "delete table inet sing-box" tunCleanupScript; assert pkgs.lib.hasInfix "delete table inet sing-box" tunCleanupScript;
assert pkgs.lib.hasInfix "rule del table ${toString checkConstants.tunAutoRouteTableIndex}" assert pkgs.lib.hasInfix "rule del table ${toString checkConstants.tunAutoRouteTableIndex}"
tunCleanupScript; tunCleanupScript;
assert pkgs.lib.hasInfix "rule del pref ${toString checkConstants.xrayTunPerAppTproxyRulePriority}"
tunCleanupScript;
assert pkgs.lib.hasInfix "rule del pref ${toString checkConstants.xrayTunPerAppTunRulePriority}" assert pkgs.lib.hasInfix "rule del pref ${toString checkConstants.xrayTunPerAppTunRulePriority}"
tunCleanupScript; tunCleanupScript;
assert pkgs.lib.hasInfix "route flush table ${toString checkConstants.tunAutoRouteTableIndex}" assert pkgs.lib.hasInfix "route flush table ${toString checkConstants.tunAutoRouteTableIndex}"
@@ -4,6 +4,8 @@
mkBadFixture, mkBadFixture,
mkFailingAssertions, mkFailingAssertions,
mkTunConfig, mkTunConfig,
mkTProxyConfig,
mkTProxyNftRules,
}: }:
let let
@@ -40,6 +42,48 @@ let
tproxyManualFixture.config.systemd.services."proxy-suite-tproxy".serviceConfig; tproxyManualFixture.config.systemd.services."proxy-suite-tproxy".serviceConfig;
tproxyManualStartScript = generated.readDerivation tproxyManualServiceConfig.ExecStart; tproxyManualStartScript = generated.readDerivation tproxyManualServiceConfig.ExecStart;
tproxyManualStopScript = generated.readDerivation tproxyManualServiceConfig.ExecStop; 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 [ tproxyAutostartFixture = evalProxySuite [
baseModule baseModule
@@ -101,6 +145,15 @@ in
tproxyManualFixture tproxyManualFixture
tproxyManualStartScript tproxyManualStartScript
tproxyManualStopScript tproxyManualStopScript
tproxyManualConfig
tproxyManualNftRules
tproxyIPv4OnlyStartScript
tproxyIPv4OnlyConfig
tproxyIPv4OnlyNftRules
ipv4OnlyTunConfig
ipv4OnlyPerAppTunUpScript
xrayIPv4OnlyTunConfig
xrayIPv4OnlyTunUpScript
tproxyWithFirewall tproxyWithFirewall
tunAutostartFixture tunAutostartFixture
tunCleanupScript tunCleanupScript
+2
View File
@@ -197,6 +197,8 @@ let
mkBadFixture mkBadFixture
mkFailingAssertions mkFailingAssertions
mkTunConfig mkTunConfig
mkTProxyConfig
mkTProxyNftRules
dnsServerByTag dnsServerByTag
checkConstants checkConstants
; ;
+26 -1
View File
@@ -31,6 +31,12 @@ let
perAppRoutingTproxyUserStartScript = generated.readDerivation ( perAppRoutingTproxyUserStartScript = generated.readDerivation (
(mkPerAppUserRules perAppRoutingTproxyFixture).perAppTproxyUserRuleStart (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 in
{ {
assertions = [ assertions = [
@@ -61,14 +67,33 @@ in
# -- perAppRouting: app TProxy helper installs socket cgroup mark rules -- # -- perAppRouting: app TProxy helper installs socket cgroup mark rules --
( (
assert pkgs.lib.hasInfix "socket cgroupv2" perAppRoutingTproxyUserStartScript; 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 "meta mark set" perAppRoutingTproxyUserStartScript;
assert pkgs.lib.hasInfix "ct mark set" perAppRoutingTproxyUserStartScript; assert pkgs.lib.hasInfix "ct mark set" perAppRoutingTproxyUserStartScript;
true 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 -- # -- 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 "rule del fwmark 17 table 102" perAppRoutingTproxyStartScript;
assert pkgs.lib.hasInfix "route replace local default dev lo table 102" assert pkgs.lib.hasInfix "route replace local default dev lo table 102"
perAppRoutingTproxyStartScript; perAppRoutingTproxyStartScript;
+21 -4
View File
@@ -38,6 +38,12 @@ let
perAppRoutingTunUserStartScript = generated.readDerivation ( perAppRoutingTunUserStartScript = generated.readDerivation (
(mkPerAppUserRules perAppRoutingTunFixture).perAppTunUserRuleStart (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 [ perAppRoutingTunWithTproxyFixture = evalProxySuite [
baseModule baseModule
@@ -92,7 +98,7 @@ in
); );
in in
assert inbound.interface_name == "psperapptun0"; 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_route == false;
assert inbound.auto_redirect == false; assert inbound.auto_redirect == false;
assert inbound.strict_route == 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.services ? "proxy-suite-per-app-tun-user@";
assert perAppRoutingTunFixture.config.systemd.user.services ? "proxy-suite-per-app-tun-anchor"; assert perAppRoutingTunFixture.config.systemd.user.services ? "proxy-suite-per-app-tun-anchor";
assert perAppRoutingTunServiceConfig.ExecStartPre == perAppRoutingTunServiceConfig.ExecStopPost; assert perAppRoutingTunServiceConfig.ExecStartPre == perAppRoutingTunServiceConfig.ExecStopPost;
assert !(pkgs.lib.hasInfix "-6 addr replace" perAppRoutingTunUpScript); # With proxy.ipv6, wrapped apps' IPv6 goes through the app TUN too.
assert !(pkgs.lib.hasInfix "-6 route replace" perAppRoutingTunUpScript); assert pkgs.lib.hasInfix ''-6 addr replace "$tun6_cidr" dev psperapptun0'' perAppRoutingTunUpScript;
assert !(pkgs.lib.hasInfix "-6 rule add" 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 "link del dev" perAppRoutingTunCleanupScript;
assert pkgs.lib.hasInfix "psperapptun0" perAppRoutingTunCleanupScript; assert pkgs.lib.hasInfix "psperapptun0" perAppRoutingTunCleanupScript;
assert pkgs.lib.hasInfix "rule del fwmark 16 table 101" perAppRoutingTunCleanupScript; assert pkgs.lib.hasInfix "rule del fwmark 16 table 101" perAppRoutingTunCleanupScript;
true 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 -- # -- perAppRouting: app TUN enables nftables --
( (
assert perAppRoutingTunFixture.config.networking.nftables.enable; assert perAppRoutingTunFixture.config.networking.nftables.enable;
+6 -1
View File
@@ -42,7 +42,9 @@ let
xrayJqFilterRuntimeCheck = xrayJqFilterRuntimeCheck =
pkgs.runCommand "proxy-suite-xray-jq-filter-runtime-check" { nativeBuildInputs = [ pkgs.jq ]; } 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() { check_runtime() {
local name="$1" local name="$1"
@@ -72,6 +74,9 @@ let
and .dns.servers[0].tag == "fakedns" and .dns.servers[0].tag == "fakedns"
and ([.routing.rules[] | select((.ruleTag? // "") == "dns-hijack")] | length) == 1 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-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(.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 == "freedom" or .tag == "proxy-suite-ob-primary") | .streamSettings.sockopt.domainStrategy] | all(. == "UseIPv4v6"))
and ([.outbounds[] | select(.protocol == "dns") | .streamSettings.sockopt.domainStrategy?] | all(. == null)) and ([.outbounds[] | select(.protocol == "dns") | .streamSettings.sockopt.domainStrategy?] | all(. == null))
@@ -36,6 +36,14 @@
assert tproxyInbound.protocol == "tunnel"; assert tproxyInbound.protocol == "tunnel";
assert tproxyInbound.settings.followRedirect == true; assert tproxyInbound.settings.followRedirect == true;
assert tproxyInbound.streamSettings.sockopt.tproxy == "tproxy"; 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.outboundTag == "proxy-suite-ob-primary";
assert specificRule.ruleTag == "custom-proxy-domain"; assert specificRule.ruleTag == "custom-proxy-domain";
assert balancer.tag == "proxy"; assert balancer.tag == "proxy";
@@ -22,6 +22,8 @@
!(pkgs.lib.hasInfix ''select(.protocol == "socks" and .tag == "mixed-in") | .settings.users'' xrayBackendJqFilter); !(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 "xray_preserved_rules" xrayBackendJqFilter;
assert pkgs.lib.hasInfix "dns-upstream-direct" 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 "xray_dns_server_order" xrayBackendJqFilter;
assert pkgs.lib.hasInfix "dns-hijack" xrayBackendJqFilter; assert pkgs.lib.hasInfix "dns-hijack" xrayBackendJqFilter;
assert pkgs.lib.hasInfix "del(.routing.balancers)" xrayBackendJqFilter; assert pkgs.lib.hasInfix "del(.routing.balancers)" xrayBackendJqFilter;
+6 -10
View File
@@ -29,8 +29,8 @@
tunHasDnsHijackRule = builtins.any ( tunHasDnsHijackRule = builtins.any (
rule: (rule ? ruleTag) && rule.ruleTag == "dns-hijack" rule: (rule ? ruleTag) && rule.ruleTag == "dns-hijack"
) xrayTunConfig.routing.rules; ) xrayTunConfig.routing.rules;
tunHasDnsUpstreamRule = builtins.any ( tunDnsUpstreamRules = builtins.filter (
rule: (rule ? ruleTag) && rule.ruleTag == "dns-upstream-direct" rule: builtins.elem (rule.ruleTag or "") [ "dns-upstream-direct" "dns-upstream-remote" ]
) xrayTunConfig.routing.rules; ) xrayTunConfig.routing.rules;
in in
assert tunInbound.protocol == "tun"; assert tunInbound.protocol == "tun";
@@ -38,7 +38,7 @@
assert assert
tunInbound.settings.gateway == [ tunInbound.settings.gateway == [
"172.19.0.1/30" "172.19.0.1/30"
checkConstants.xrayGlobalTunIPv6Address checkConstants.globalTunIPv6Address
]; ];
assert tunInbound.sniffing.destOverride == [ "fakedns" ]; assert tunInbound.sniffing.destOverride == [ "fakedns" ];
assert tunInbound.sniffing.metadataOnly == false; assert tunInbound.sniffing.metadataOnly == false;
@@ -46,9 +46,9 @@
assert tunHasFinalRuleTag; assert tunHasFinalRuleTag;
assert tunHasDirectGeositeRule; assert tunHasDirectGeositeRule;
assert tunHasDnsHijackRule; 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.queryStrategy == "UseIP";
assert xrayTunConfig.dns.tag == "dns-in";
assert (builtins.head xrayTunConfig.dns.servers).address == "fakedns"; assert (builtins.head xrayTunConfig.dns.servers).address == "fakedns";
assert (builtins.head xrayTunConfig.dns.servers).tag == "fakedns"; assert (builtins.head xrayTunConfig.dns.servers).tag == "fakedns";
assert (builtins.elemAt xrayTunConfig.dns.servers 1).tag == "remote"; 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 ''-f "$BACKEND_JQ_FILTER"'' xrayTunStartScript;
assert pkgs.lib.hasInfix "xray_tun_dns_runtime" 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 ''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 ''addr replace "$tun_cidr" dev singtun0'' xrayTunUpScript;
assert pkgs.lib.hasInfix ''-6 addr replace "$tun6_cidr" dev singtun0'' xrayTunUpScript; assert pkgs.lib.hasInfix ''-6 addr replace "$tun6_cidr" dev singtun0'' xrayTunUpScript;
assert pkgs.lib.hasInfix assert pkgs.lib.hasInfix
@@ -84,9 +84,6 @@
assert pkgs.lib.hasInfix assert pkgs.lib.hasInfix
"-6 route replace default dev singtun0 table ${toString checkConstants.tunAutoRouteTableIndex}" "-6 route replace default dev singtun0 table ${toString checkConstants.tunAutoRouteTableIndex}"
xrayTunUpScript; xrayTunUpScript;
assert pkgs.lib.hasInfix
"rule add pref ${toString checkConstants.xrayTunPerAppTproxyRulePriority} fwmark 17 table 102"
xrayTunUpScript;
assert pkgs.lib.hasInfix assert pkgs.lib.hasInfix
"rule add pref ${toString checkConstants.xrayTunPerAppTunRulePriority} fwmark 16 table 101" "rule add pref ${toString checkConstants.xrayTunPerAppTunRulePriority} fwmark 16 table 101"
xrayTunUpScript; xrayTunUpScript;
@@ -120,7 +117,6 @@
) [ ) [
checkConstants.xrayTunMarkBypassRulePriority checkConstants.xrayTunMarkBypassRulePriority
checkConstants.xrayTunServiceUserRulePriority checkConstants.xrayTunServiceUserRulePriority
checkConstants.xrayTunPerAppTproxyRulePriority
checkConstants.xrayTunPerAppTunRulePriority checkConstants.xrayTunPerAppTunRulePriority
checkConstants.xrayTunDnsRulePriority checkConstants.xrayTunDnsRulePriority
checkConstants.xrayTunMainRulePriority checkConstants.xrayTunMainRulePriority
@@ -23,13 +23,12 @@
assert assert
perAppTunInbound.settings.gateway == [ perAppTunInbound.settings.gateway == [
"172.20.0.1/30" "172.20.0.1/30"
checkConstants.xrayPerAppTunIPv6Address checkConstants.perAppTunIPv6Address
]; ];
assert perAppTunInbound.sniffing.destOverride == [ "fakedns" ]; assert perAppTunInbound.sniffing.destOverride == [ "fakedns" ];
assert perAppTunInbound.sniffing.metadataOnly == false; assert perAppTunInbound.sniffing.metadataOnly == false;
assert xrayPerAppTunConfig.routing.domainStrategy == "IPIfNonMatch"; assert xrayPerAppTunConfig.routing.domainStrategy == "IPIfNonMatch";
assert (builtins.head xrayPerAppTunConfig.dns.servers).address == "fakedns"; assert (builtins.head xrayPerAppTunConfig.dns.servers).address == "fakedns";
assert xrayPerAppTunConfig.dns.tag == "dns-in";
assert builtins.length xrayPerAppTunConfig.fakedns == 2; assert builtins.length xrayPerAppTunConfig.fakedns == 2;
assert perAppTunDirectOutbound.streamSettings.sockopt.mark == 2; assert perAppTunDirectOutbound.streamSettings.sockopt.mark == 2;
assert pkgs.lib.hasInfix "XRAY_SINGLE_PROXY_TAG=" xrayPerAppTunStartScript; 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 "--routing-mark 2" xrayPerAppTunStartScript;
assert pkgs.lib.hasInfix "xray_tun_dns_runtime" 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 ''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 "uplink_addr" xrayPerAppTunUpScript);
assert pkgs.lib.hasInfix ''addr replace "$tun_cidr" dev psperapptun0'' xrayPerAppTunUpScript; assert pkgs.lib.hasInfix ''addr replace "$tun_cidr" dev psperapptun0'' xrayPerAppTunUpScript;
assert pkgs.lib.hasInfix ''-6 addr replace "$tun6_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")) balanced = bool(routing.get("balancers"))
rules = [] rules = []
for rule in routing.get("rules") or []: 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 continue
if rule.get("outboundTag") in dropped: if rule.get("outboundTag") in dropped:
if balanced: if balanced:
+2
View File
@@ -77,6 +77,7 @@ def xray_config():
"routing": { "routing": {
"rules": [ "rules": [
{"type": "field", "inboundTag": ["tproxy-in"], "outboundTag": "direct"}, {"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", "domain": ["geosite:google"], "outboundTag": "proxy-suite-ob-warp"},
{"type": "field", "network": "tcp,udp", "balancerTag": "proxy"}, {"type": "field", "network": "tcp,udp", "balancerTag": "proxy"},
], ],
@@ -188,6 +189,7 @@ class XrayTest(unittest.TestCase):
self.assertEqual( self.assertEqual(
cfg["routing"]["rules"], cfg["routing"]["rules"],
[ [
{"type": "field", "inboundTag": ["local"], "outboundTag": "direct"},
{"type": "field", "domain": ["geosite:google"], "balancerTag": "proxy"}, {"type": "field", "domain": ["geosite:google"], "balancerTag": "proxy"},
{"type": "field", "network": "tcp,udp", "balancerTag": "proxy"}, {"type": "field", "network": "tcp,udp", "balancerTag": "proxy"},
], ],