fix: overall tun/tproxy improvements
This commit is contained in:
29 files changed
+445
-149
No files matched your search
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
];
|
||||
};
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -145,7 +145,6 @@ let
|
||||
cfg
|
||||
singBoxCfg
|
||||
proxyCfg
|
||||
pureXrayEnabled
|
||||
perAppRoutingCfg
|
||||
perAppRoutingTun
|
||||
perAppRoutingTproxy
|
||||
|
||||
@@ -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";
|
||||
};
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in new issue
Block a user