fix: various fixes
This commit is contained in:
17 files changed
+584
-353
No files matched your search
@@ -9,6 +9,7 @@
|
||||
|
||||
let
|
||||
derived = import ./derived.nix { inherit lib cfg; };
|
||||
constants = derived.constants;
|
||||
inherit (derived)
|
||||
proxyCfg
|
||||
singBoxCfg
|
||||
@@ -19,10 +20,15 @@ let
|
||||
globalTproxy
|
||||
clashApiEnabled
|
||||
;
|
||||
inherit (constants)
|
||||
xrayGlobalTunIPv6Address
|
||||
xrayPerAppTunIPv6Address
|
||||
xrayDnsBridgePorts
|
||||
;
|
||||
perAppTun = derived.perAppRoutingTun;
|
||||
direct = rules.direct;
|
||||
xrayGlobalTunIPv6Address = "fd66:19::1/64";
|
||||
xrayPerAppTunIPv6Address = "fd66:20::1/64";
|
||||
defaultTunAutoRouteTableIndex = constants.tunAutoRouteTableIndex;
|
||||
defaultTunAutoRouteRulePriority = constants.tunAutoRouteRulePriority;
|
||||
xrayFakeDnsPools = [
|
||||
{
|
||||
ipPool = "198.18.0.0/15";
|
||||
@@ -92,15 +98,15 @@ let
|
||||
tunAddress ? globalTun.address,
|
||||
tunMtu ? globalTun.mtu,
|
||||
tunAutoRoute ? true,
|
||||
tunAutoRouteTableIndex ? 2022,
|
||||
tunAutoRouteRuleIndex ? 9000,
|
||||
tunAutoRouteTableIndex ? defaultTunAutoRouteTableIndex,
|
||||
tunAutoRouteRuleIndex ? defaultTunAutoRouteRulePriority,
|
||||
tunAutoRedirect ? true,
|
||||
tunStrictRoute ? true,
|
||||
forceLocalDnsViaProxy ? false,
|
||||
useOutboundRoutingMark ? false,
|
||||
enableClashApi ? clashApiEnabled,
|
||||
enableXrayDnsBridge ? hybridEnabled,
|
||||
xrayDnsBridgePort ? 18533,
|
||||
xrayDnsBridgePort ? xrayDnsBridgePorts.socks,
|
||||
}:
|
||||
{
|
||||
log.level = "warn";
|
||||
@@ -405,7 +411,7 @@ let
|
||||
enableMixed = true;
|
||||
enableTProxy = true;
|
||||
useOutboundRoutingMark = true;
|
||||
xrayDnsBridgePort = 18533;
|
||||
xrayDnsBridgePort = xrayDnsBridgePorts.socks;
|
||||
};
|
||||
|
||||
singBoxTunTemplate = mkSingBoxConfig {
|
||||
@@ -418,7 +424,7 @@ let
|
||||
tunStrictRoute = true;
|
||||
forceLocalDnsViaProxy = false;
|
||||
enableClashApi = false;
|
||||
xrayDnsBridgePort = 18534;
|
||||
xrayDnsBridgePort = xrayDnsBridgePorts.tun;
|
||||
};
|
||||
|
||||
singBoxPerAppTunTemplate = mkSingBoxConfig {
|
||||
@@ -432,7 +438,7 @@ let
|
||||
forceLocalDnsViaProxy = false;
|
||||
useOutboundRoutingMark = globalTproxy.enable;
|
||||
enableClashApi = false;
|
||||
xrayDnsBridgePort = 18535;
|
||||
xrayDnsBridgePort = xrayDnsBridgePorts.perAppTun;
|
||||
};
|
||||
|
||||
xrayTproxyTemplate = mkXrayConfig {
|
||||
|
||||
@@ -52,6 +52,29 @@ let
|
||||
clashApiEnabled = (singBoxEnabled || hybridEnabled) && selectionMode != "first";
|
||||
perAppZapretEnabled = perAppZapretCfg.enable;
|
||||
userControlEnabled = userControlCfg.global.enable || userControlCfg.perApp.enable;
|
||||
constants = {
|
||||
tunAutoRouteTableIndex = 2022;
|
||||
tunAutoRouteRulePriority = 9000;
|
||||
xrayTunPerAppTproxyRulePriority = 8996;
|
||||
xrayTunPerAppTunRulePriority = 8997;
|
||||
|
||||
xrayGlobalTunIPv6Address = "fd66:19::1/64";
|
||||
xrayGlobalTunIPv6RoutePrefix = "fd66:19::/64";
|
||||
xrayPerAppTunIPv6Address = "fd66:20::1/64";
|
||||
xrayPerAppTunIPv6RoutePrefix = "fd66:20::/64";
|
||||
|
||||
xrayDnsBridgePorts = {
|
||||
socks = 18533;
|
||||
tun = 18534;
|
||||
perAppTun = 18535;
|
||||
};
|
||||
|
||||
xraySidecarBasePorts = {
|
||||
socks = 33080;
|
||||
tun = 33180;
|
||||
perAppTun = 33280;
|
||||
};
|
||||
};
|
||||
|
||||
invalidRoutingTargets = lib.unique (
|
||||
map (rule: rule.outbound) (
|
||||
@@ -82,6 +105,7 @@ in
|
||||
perAppZapretEnabled
|
||||
userControlCfg
|
||||
userControlEnabled
|
||||
constants
|
||||
selectionMode
|
||||
builtinTags
|
||||
outboundTags
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
}:
|
||||
|
||||
let
|
||||
constants = derived.constants;
|
||||
assertions = import ./service/assertions-lib.nix { inherit lib; };
|
||||
inherit (assertions)
|
||||
mkAssertion
|
||||
@@ -42,7 +43,7 @@ let
|
||||
perAppZapretCfg
|
||||
;
|
||||
|
||||
globalTunAutoRouteTable = 2022;
|
||||
globalTunAutoRouteTable = constants.tunAutoRouteTableIndex;
|
||||
|
||||
featureAssertions = [
|
||||
(requireAvailable proxyCfg.singBox.enable proxyEnabled
|
||||
@@ -160,7 +161,7 @@ let
|
||||
condition = globalTun.enable && perAppRoutingTun.enable;
|
||||
left = perAppRoutingTun.routeTable;
|
||||
right = globalTunAutoRouteTable;
|
||||
message = "proxy-suite: proxy.tun.perApp.routeTable must differ from the global TUN auto-route table 2022";
|
||||
message = "proxy-suite: proxy.tun.perApp.routeTable must differ from the global TUN auto-route table ${toString globalTunAutoRouteTable}";
|
||||
}
|
||||
{
|
||||
condition = perAppRoutingTun.enable && globalTproxy.enable;
|
||||
|
||||
@@ -35,6 +35,127 @@ rec {
|
||||
)
|
||||
);
|
||||
|
||||
cidrNetworkFunction = ''
|
||||
cidr_network() {
|
||||
local cidr="$1"
|
||||
local addr="''${cidr%/*}"
|
||||
local prefix="''${cidr#*/}"
|
||||
local o1 o2 o3 o4 ip mask net
|
||||
|
||||
IFS=. read -r o1 o2 o3 o4 <<<"$addr"
|
||||
ip=$(((o1 << 24) | (o2 << 16) | (o3 << 8) | o4))
|
||||
if [ "$prefix" -eq 0 ]; then
|
||||
mask=0
|
||||
else
|
||||
mask=$(((0xFFFFFFFF << (32 - prefix)) & 0xFFFFFFFF))
|
||||
fi
|
||||
net=$((ip & mask))
|
||||
|
||||
printf '%d.%d.%d.%d/%s' \
|
||||
$(((net >> 24) & 255)) \
|
||||
$(((net >> 16) & 255)) \
|
||||
$(((net >> 8) & 255)) \
|
||||
$((net & 255)) \
|
||||
"$prefix"
|
||||
}
|
||||
'';
|
||||
|
||||
mkDefaultUplinkIPv4Source =
|
||||
{
|
||||
ip,
|
||||
awk,
|
||||
errorMessage,
|
||||
}:
|
||||
''
|
||||
uplink_addr="$(${ip} -4 route get 1.1.1.1 2>/dev/null | ${awk} '
|
||||
/src/ {
|
||||
for (i = 1; i <= NF; i++) {
|
||||
if ($i == "src" && i + 1 <= NF) {
|
||||
print $(i + 1)
|
||||
exit
|
||||
}
|
||||
}
|
||||
}
|
||||
')"
|
||||
if [ -z "$uplink_addr" ]; then
|
||||
echo ${lib.escapeShellArg errorMessage} >&2
|
||||
exit 1
|
||||
fi
|
||||
'';
|
||||
|
||||
mkNftDeleteTable =
|
||||
{
|
||||
nft,
|
||||
family,
|
||||
table,
|
||||
}:
|
||||
''
|
||||
${nft} delete table ${family} ${table} 2>/dev/null || true
|
||||
'';
|
||||
|
||||
mkIpRuleDeleteByFwmark =
|
||||
{
|
||||
ip,
|
||||
family ? "",
|
||||
fwmark,
|
||||
table,
|
||||
}:
|
||||
''
|
||||
while ${ip} ${lib.optionalString (family != "") "${family} "}rule del fwmark ${toString fwmark} table ${toString table} 2>/dev/null; do :; done
|
||||
'';
|
||||
|
||||
mkIpRuleDeleteByTable =
|
||||
{
|
||||
ip,
|
||||
family,
|
||||
table,
|
||||
}:
|
||||
''
|
||||
while ${ip} ${family} rule del table ${toString table} 2>/dev/null; do :; done
|
||||
'';
|
||||
|
||||
mkIpRuleDeleteByPriority =
|
||||
{
|
||||
ip,
|
||||
family,
|
||||
priority,
|
||||
}:
|
||||
''
|
||||
while ${ip} ${family} rule del pref ${toString priority} 2>/dev/null; do :; done
|
||||
'';
|
||||
|
||||
mkIpRouteFlushTable =
|
||||
{
|
||||
ip,
|
||||
family,
|
||||
table,
|
||||
}:
|
||||
''
|
||||
${ip} ${family} route flush table ${toString table} 2>/dev/null || true
|
||||
'';
|
||||
|
||||
mkIpLocalDefaultRouteDelete =
|
||||
{
|
||||
ip,
|
||||
table,
|
||||
}:
|
||||
''
|
||||
${ip} route del local default dev lo table ${toString table} 2>/dev/null || true
|
||||
'';
|
||||
|
||||
mkIpLinkDelete =
|
||||
{
|
||||
ip,
|
||||
interface,
|
||||
}:
|
||||
''
|
||||
${ip} link del dev ${lib.escapeShellArg interface} 2>/dev/null || true
|
||||
'';
|
||||
|
||||
flushResolvedCaches = ''
|
||||
${pkgs.systemd}/bin/resolvectl flush-caches 2>/dev/null || true
|
||||
'';
|
||||
|
||||
mkRestartingService =
|
||||
{
|
||||
description,
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
|
||||
let
|
||||
derived = import ../derived.nix { inherit lib cfg; };
|
||||
constants = derived.constants;
|
||||
inherit (derived)
|
||||
singBoxCfg
|
||||
proxyCfg
|
||||
@@ -82,6 +83,7 @@ let
|
||||
selectionMode
|
||||
collapseNamedOutbounds
|
||||
hasSubscriptions
|
||||
constants
|
||||
;
|
||||
inherit
|
||||
jq
|
||||
@@ -111,6 +113,7 @@ let
|
||||
perAppRoutingCfg
|
||||
perAppRoutingTun
|
||||
perAppRoutingTproxy
|
||||
constants
|
||||
;
|
||||
perAppZapretCfg = perAppZapretCfg;
|
||||
inherit perAppTunChainFile perAppTproxyRulesFile perAppZapretRulesFile;
|
||||
|
||||
@@ -45,6 +45,12 @@ let
|
||||
mkAnchorService
|
||||
;
|
||||
|
||||
defaultUplinkIPv4Source = builders.mkDefaultUplinkIPv4Source {
|
||||
inherit ip;
|
||||
awk = "${pkgs.gawk}/bin/awk";
|
||||
errorMessage = "proxy-suite: could not determine the default uplink IPv4 address for XRay TUN";
|
||||
};
|
||||
|
||||
inherit (context)
|
||||
derived
|
||||
polkit
|
||||
@@ -76,6 +82,15 @@ let
|
||||
invalidRoutingTargets
|
||||
builtinTags
|
||||
;
|
||||
constants = derived.constants;
|
||||
inherit (constants)
|
||||
tunAutoRouteTableIndex
|
||||
tunAutoRouteRulePriority
|
||||
xrayTunPerAppTproxyRulePriority
|
||||
xrayTunPerAppTunRulePriority
|
||||
xrayGlobalTunIPv6Address
|
||||
xrayGlobalTunIPv6RoutePrefix
|
||||
;
|
||||
|
||||
serviceNames = {
|
||||
socks = "proxy-suite-socks";
|
||||
@@ -91,19 +106,6 @@ let
|
||||
proxyCfg.auth.username != null
|
||||
&& (proxyCfg.auth.password != null || proxyCfg.auth.passwordFile != null);
|
||||
|
||||
# Must match the explicit iproute2_* indexes written to the global SingBox TUN
|
||||
# template in config.nix. XRay uses the same table for manual Linux
|
||||
# policy routing because XRay's autoSystemRoutingTable is not Linux-ready.
|
||||
tunAutoRouteTableIndex = 2022;
|
||||
tunAutoRouteRulePriority = 9000;
|
||||
# Keep app-scoped marks ahead of the global XRay TUN catch-all. Without
|
||||
# these priorities, iproute2's default priority lets global TUN capture
|
||||
# packets intended for per-app routing tables.
|
||||
xrayTunPerAppTproxyRulePriority = 8996;
|
||||
xrayTunPerAppTunRulePriority = 8997;
|
||||
xrayGlobalTunIPv6Address = "fd66:19::1/64";
|
||||
xrayGlobalTunIPv6RoutePrefix = "fd66:19::/64";
|
||||
|
||||
xrayTunUpScript = pkgs.writeShellScript "proxy-suite-xray-tun-up" ''
|
||||
set -euo pipefail
|
||||
|
||||
@@ -114,28 +116,7 @@ let
|
||||
tun_route_prefix=""
|
||||
uplink_addr=""
|
||||
|
||||
cidr_network() {
|
||||
local cidr="$1"
|
||||
local addr="''${cidr%/*}"
|
||||
local prefix="''${cidr#*/}"
|
||||
local o1 o2 o3 o4 ip mask net
|
||||
|
||||
IFS=. read -r o1 o2 o3 o4 <<<"$addr"
|
||||
ip=$(((o1 << 24) | (o2 << 16) | (o3 << 8) | o4))
|
||||
if [ "$prefix" -eq 0 ]; then
|
||||
mask=0
|
||||
else
|
||||
mask=$(((0xFFFFFFFF << (32 - prefix)) & 0xFFFFFFFF))
|
||||
fi
|
||||
net=$((ip & mask))
|
||||
|
||||
printf '%d.%d.%d.%d/%s' \
|
||||
$(((net >> 24) & 255)) \
|
||||
$(((net >> 16) & 255)) \
|
||||
$(((net >> 8) & 255)) \
|
||||
$((net & 255)) \
|
||||
"$prefix"
|
||||
}
|
||||
${builders.cidrNetworkFunction}
|
||||
|
||||
for _ in $(${pkgs.coreutils}/bin/seq 1 50); do
|
||||
if ${ip} link show dev ${lib.escapeShellArg globalTun.interface} >/dev/null 2>&1; then
|
||||
@@ -149,20 +130,7 @@ let
|
||||
exit 1
|
||||
fi
|
||||
|
||||
uplink_addr="$(${ip} -4 route get 1.1.1.1 2>/dev/null | ${pkgs.gawk}/bin/awk '
|
||||
/src/ {
|
||||
for (i = 1; i <= NF; i++) {
|
||||
if ($i == "src" && i + 1 <= NF) {
|
||||
print $(i + 1)
|
||||
exit
|
||||
}
|
||||
}
|
||||
}
|
||||
')"
|
||||
if [ -z "$uplink_addr" ]; then
|
||||
echo "proxy-suite: could not determine the default uplink IPv4 address for XRay TUN" >&2
|
||||
exit 1
|
||||
fi
|
||||
${defaultUplinkIPv4Source}
|
||||
|
||||
while ${ip} -4 rule del pref ${toString xrayTunPerAppTproxyRulePriority} 2>/dev/null; do :; done
|
||||
while ${ip} -4 rule del pref ${toString xrayTunPerAppTunRulePriority} 2>/dev/null; do :; done
|
||||
@@ -196,9 +164,13 @@ let
|
||||
# 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.
|
||||
${nft} delete table ip singbox 2>/dev/null || true
|
||||
while ${ip} rule del fwmark ${toString globalTproxy.fwmark} table ${toString globalTproxy.routeTable} 2>/dev/null; do :; done
|
||||
${ip} route del local default dev lo table ${toString globalTproxy.routeTable} 2>/dev/null || true
|
||||
${builders.mkNftDeleteTable { inherit nft; family = "ip"; table = "singbox"; }}
|
||||
${builders.mkIpRuleDeleteByFwmark {
|
||||
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}
|
||||
@@ -208,9 +180,13 @@ let
|
||||
tproxyDownScript = pkgs.writeShellScript "proxy-suite-tproxy-down" ''
|
||||
set +e
|
||||
|
||||
${nft} delete table ip singbox 2>/dev/null || true
|
||||
while ${ip} rule del fwmark ${toString globalTproxy.fwmark} table ${toString globalTproxy.routeTable} 2>/dev/null; do :; done
|
||||
${ip} route del local default dev lo table ${toString globalTproxy.routeTable} 2>/dev/null || true
|
||||
${builders.mkNftDeleteTable { inherit nft; family = "ip"; table = "singbox"; }}
|
||||
${builders.mkIpRuleDeleteByFwmark {
|
||||
inherit ip;
|
||||
fwmark = globalTproxy.fwmark;
|
||||
table = globalTproxy.routeTable;
|
||||
}}
|
||||
${builders.mkIpLocalDefaultRouteDelete { inherit ip; table = globalTproxy.routeTable; }}
|
||||
'';
|
||||
|
||||
tunCleanupScript = pkgs.writeShellScript "proxy-suite-tun-cleanup" ''
|
||||
@@ -219,18 +195,28 @@ let
|
||||
# SingBox normally removes these on graceful shutdown, but stale
|
||||
# auto_route/auto_redirect state leaves the host routing through a dead TUN
|
||||
# interface after `proxy-ctl tun off` or an unclean service stop.
|
||||
${nft} delete table inet sing-box 2>/dev/null || true
|
||||
|
||||
while ${ip} -4 rule del table ${toString tunAutoRouteTableIndex} 2>/dev/null; do :; done
|
||||
while ${ip} -6 rule del table ${toString tunAutoRouteTableIndex} 2>/dev/null; do :; done
|
||||
while ${ip} -4 rule del pref ${toString xrayTunPerAppTproxyRulePriority} 2>/dev/null; do :; done
|
||||
while ${ip} -4 rule del pref ${toString xrayTunPerAppTunRulePriority} 2>/dev/null; do :; done
|
||||
while ${ip} -6 rule del pref ${toString xrayTunPerAppTunRulePriority} 2>/dev/null; do :; done
|
||||
${ip} -4 route flush table ${toString tunAutoRouteTableIndex} 2>/dev/null || true
|
||||
${ip} -6 route flush table ${toString tunAutoRouteTableIndex} 2>/dev/null || true
|
||||
|
||||
${ip} link del dev ${lib.escapeShellArg globalTun.interface} 2>/dev/null || true
|
||||
${pkgs.systemd}/bin/resolvectl flush-caches 2>/dev/null || true
|
||||
${builders.mkNftDeleteTable { inherit nft; family = "inet"; table = "sing-box"; }}
|
||||
${builders.mkIpRuleDeleteByTable { inherit ip; family = "-4"; table = tunAutoRouteTableIndex; }}
|
||||
${builders.mkIpRuleDeleteByTable { inherit ip; family = "-6"; table = tunAutoRouteTableIndex; }}
|
||||
${builders.mkIpRuleDeleteByPriority {
|
||||
inherit ip;
|
||||
family = "-4";
|
||||
priority = xrayTunPerAppTproxyRulePriority;
|
||||
}}
|
||||
${builders.mkIpRuleDeleteByPriority {
|
||||
inherit ip;
|
||||
family = "-4";
|
||||
priority = xrayTunPerAppTunRulePriority;
|
||||
}}
|
||||
${builders.mkIpRuleDeleteByPriority {
|
||||
inherit ip;
|
||||
family = "-6";
|
||||
priority = xrayTunPerAppTunRulePriority;
|
||||
}}
|
||||
${builders.mkIpRouteFlushTable { inherit ip; family = "-4"; table = tunAutoRouteTableIndex; }}
|
||||
${builders.mkIpRouteFlushTable { inherit ip; family = "-6"; table = tunAutoRouteTableIndex; }}
|
||||
${builders.mkIpLinkDelete { inherit ip; interface = globalTun.interface; }}
|
||||
${builders.flushResolvedCaches}
|
||||
'';
|
||||
|
||||
systemServiceEntries = [
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
perAppRoutingCfg,
|
||||
perAppRoutingTun,
|
||||
perAppRoutingTproxy,
|
||||
constants,
|
||||
perAppZapretCfg,
|
||||
perAppTunChainFile,
|
||||
perAppTproxyRulesFile,
|
||||
@@ -26,6 +27,10 @@
|
||||
let
|
||||
builders = import ./builders.nix { inherit lib pkgs; };
|
||||
inherit (builders) mkAnchorService;
|
||||
defaultUplinkIPv4Source = builders.mkDefaultUplinkIPv4Source {
|
||||
inherit ip awk;
|
||||
errorMessage = "proxy-suite: could not determine the default uplink IPv4 address for app TUN";
|
||||
};
|
||||
|
||||
perAppTunSliceName = "proxy-suite-per-app-tun.slice";
|
||||
perAppTproxySliceName = "proxy-suite-per-app-tproxy.slice";
|
||||
@@ -99,8 +104,10 @@ let
|
||||
hasZapretProfiles = builtins.any (
|
||||
profile: profile.route == "zapret"
|
||||
) effectivePerAppRoutingProfiles;
|
||||
xrayPerAppTunIPv6Address = "fd66:20::1/64";
|
||||
xrayPerAppTunIPv6RoutePrefix = "fd66:20::/64";
|
||||
inherit (constants)
|
||||
xrayPerAppTunIPv6Address
|
||||
xrayPerAppTunIPv6RoutePrefix
|
||||
;
|
||||
|
||||
perAppTunWaitForInterface = pkgs.writeShellScript "proxy-suite-per-app-tun-wait-for-interface" ''
|
||||
set -euo pipefail
|
||||
@@ -124,50 +131,26 @@ let
|
||||
tun_route_prefix=""
|
||||
uplink_addr=""
|
||||
|
||||
cidr_network() {
|
||||
local cidr="$1"
|
||||
local addr="''${cidr%/*}"
|
||||
local prefix="''${cidr#*/}"
|
||||
local o1 o2 o3 o4 ip mask net
|
||||
${builders.cidrNetworkFunction}
|
||||
|
||||
IFS=. read -r o1 o2 o3 o4 <<<"$addr"
|
||||
ip=$(((o1 << 24) | (o2 << 16) | (o3 << 8) | o4))
|
||||
if [ "$prefix" -eq 0 ]; then
|
||||
mask=0
|
||||
else
|
||||
mask=$(((0xFFFFFFFF << (32 - prefix)) & 0xFFFFFFFF))
|
||||
fi
|
||||
net=$((ip & mask))
|
||||
|
||||
printf '%d.%d.%d.%d/%s' \
|
||||
$(((net >> 24) & 255)) \
|
||||
$(((net >> 16) & 255)) \
|
||||
$(((net >> 8) & 255)) \
|
||||
$((net & 255)) \
|
||||
"$prefix"
|
||||
}
|
||||
|
||||
${nft} delete table inet proxy_suite_per_app_tun 2>/dev/null || true
|
||||
while ${ip} -4 rule del fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable} 2>/dev/null; do :; done
|
||||
while ${ip} -6 rule del fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable} 2>/dev/null; do :; done
|
||||
${ip} -4 route flush table ${toString perAppRoutingTun.routeTable} 2>/dev/null || true
|
||||
${ip} -6 route flush table ${toString perAppRoutingTun.routeTable} 2>/dev/null || true
|
||||
${builders.mkNftDeleteTable { inherit nft; family = "inet"; table = "proxy_suite_per_app_tun"; }}
|
||||
${builders.mkIpRuleDeleteByFwmark {
|
||||
inherit ip;
|
||||
family = "-4";
|
||||
fwmark = perAppRoutingTun.fwmark;
|
||||
table = perAppRoutingTun.routeTable;
|
||||
}}
|
||||
${builders.mkIpRuleDeleteByFwmark {
|
||||
inherit ip;
|
||||
family = "-6";
|
||||
fwmark = perAppRoutingTun.fwmark;
|
||||
table = perAppRoutingTun.routeTable;
|
||||
}}
|
||||
${builders.mkIpRouteFlushTable { inherit ip; family = "-4"; table = perAppRoutingTun.routeTable; }}
|
||||
${builders.mkIpRouteFlushTable { inherit ip; family = "-6"; table = perAppRoutingTun.routeTable; }}
|
||||
${nft} -f ${perAppTunChainFile}
|
||||
${perAppTunWaitForInterface}
|
||||
uplink_addr="$(${ip} -4 route get 1.1.1.1 2>/dev/null | ${awk} '
|
||||
/src/ {
|
||||
for (i = 1; i <= NF; i++) {
|
||||
if ($i == "src" && i + 1 <= NF) {
|
||||
print $(i + 1)
|
||||
exit
|
||||
}
|
||||
}
|
||||
}
|
||||
')"
|
||||
if [ -z "$uplink_addr" ]; then
|
||||
echo "proxy-suite: could not determine the default uplink IPv4 address for app TUN" >&2
|
||||
exit 1
|
||||
fi
|
||||
${defaultUplinkIPv4Source}
|
||||
tun_addr="''${tun_cidr%%/*}"
|
||||
tun_route_prefix="$(cidr_network "$tun_cidr")"
|
||||
${ip} -4 addr replace "$tun_cidr" dev ${lib.escapeShellArg perAppRoutingTun.interface}
|
||||
@@ -186,21 +169,35 @@ let
|
||||
set +e
|
||||
|
||||
# Best-effort cleanup for graceful stops and for unclean previous exits.
|
||||
${nft} delete table inet proxy_suite_per_app_tun 2>/dev/null || true
|
||||
while ${ip} -4 rule del fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable} 2>/dev/null; do :; done
|
||||
while ${ip} -6 rule del fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable} 2>/dev/null; do :; done
|
||||
${ip} -4 route flush table ${toString perAppRoutingTun.routeTable} 2>/dev/null || true
|
||||
${ip} -6 route flush table ${toString perAppRoutingTun.routeTable} 2>/dev/null || true
|
||||
${ip} link del dev ${lib.escapeShellArg perAppRoutingTun.interface} 2>/dev/null || true
|
||||
${pkgs.systemd}/bin/resolvectl flush-caches 2>/dev/null || true
|
||||
${builders.mkNftDeleteTable { inherit nft; family = "inet"; table = "proxy_suite_per_app_tun"; }}
|
||||
${builders.mkIpRuleDeleteByFwmark {
|
||||
inherit ip;
|
||||
family = "-4";
|
||||
fwmark = perAppRoutingTun.fwmark;
|
||||
table = perAppRoutingTun.routeTable;
|
||||
}}
|
||||
${builders.mkIpRuleDeleteByFwmark {
|
||||
inherit ip;
|
||||
family = "-6";
|
||||
fwmark = perAppRoutingTun.fwmark;
|
||||
table = perAppRoutingTun.routeTable;
|
||||
}}
|
||||
${builders.mkIpRouteFlushTable { inherit ip; family = "-4"; table = perAppRoutingTun.routeTable; }}
|
||||
${builders.mkIpRouteFlushTable { inherit ip; family = "-6"; table = perAppRoutingTun.routeTable; }}
|
||||
${builders.mkIpLinkDelete { inherit ip; interface = perAppRoutingTun.interface; }}
|
||||
${builders.flushResolvedCaches}
|
||||
'';
|
||||
|
||||
perAppTproxyUpScript = pkgs.writeShellScript "proxy-suite-per-app-tproxy-up" ''
|
||||
set -euo pipefail
|
||||
|
||||
${nft} delete table ip proxy_suite_per_app_tproxy 2>/dev/null || true
|
||||
while ${ip} rule del fwmark ${toString perAppRoutingTproxy.fwmark} table ${toString perAppRoutingTproxy.routeTable} 2>/dev/null; do :; done
|
||||
${ip} route del local default dev lo table ${toString perAppRoutingTproxy.routeTable} 2>/dev/null || true
|
||||
${builders.mkNftDeleteTable { inherit nft; family = "ip"; table = "proxy_suite_per_app_tproxy"; }}
|
||||
${builders.mkIpRuleDeleteByFwmark {
|
||||
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}
|
||||
@@ -210,9 +207,13 @@ let
|
||||
perAppTproxyDownScript = pkgs.writeShellScript "proxy-suite-per-app-tproxy-down" ''
|
||||
set +e
|
||||
|
||||
${nft} delete table ip proxy_suite_per_app_tproxy 2>/dev/null || true
|
||||
while ${ip} rule del fwmark ${toString perAppRoutingTproxy.fwmark} table ${toString perAppRoutingTproxy.routeTable} 2>/dev/null; do :; done
|
||||
${ip} route del local default dev lo table ${toString perAppRoutingTproxy.routeTable} 2>/dev/null || true
|
||||
${builders.mkNftDeleteTable { inherit nft; family = "ip"; table = "proxy_suite_per_app_tproxy"; }}
|
||||
${builders.mkIpRuleDeleteByFwmark {
|
||||
inherit ip;
|
||||
fwmark = perAppRoutingTproxy.fwmark;
|
||||
table = perAppRoutingTproxy.routeTable;
|
||||
}}
|
||||
${builders.mkIpLocalDefaultRouteDelete { inherit ip; table = perAppRoutingTproxy.routeTable; }}
|
||||
'';
|
||||
|
||||
# Generate a "start" script that adds a per-user cgroup nftables mark rule.
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
selectionMode,
|
||||
collapseNamedOutbounds,
|
||||
hasSubscriptions,
|
||||
constants,
|
||||
jq,
|
||||
python3,
|
||||
singBox,
|
||||
@@ -34,11 +35,7 @@ let
|
||||
systemctl = "${pkgs.systemd}/bin/systemctl";
|
||||
backend = if activeBackend == null then "sing-box" else activeBackend;
|
||||
mainBackend = if pureXrayEnabled then "xray" else "sing-box";
|
||||
subscriptionBackend =
|
||||
if hybridEnabled then
|
||||
"hybrid"
|
||||
else
|
||||
mainBackend;
|
||||
subscriptionBackend = if hybridEnabled then "hybrid" else mainBackend;
|
||||
backendArg = "--backend ${mainBackend}";
|
||||
subscriptionBackendArg = "--backend ${subscriptionBackend}";
|
||||
backendBin = if pureXrayEnabled then xray else singBox;
|
||||
@@ -58,6 +55,10 @@ let
|
||||
xrayLoglevelFile = "/run/proxy-suite/xray-loglevel";
|
||||
runtimeProxychainsConfig = "/run/proxy-suite-socks/proxychains.conf";
|
||||
xraySidecarRoutingMark = globalTproxy.proxyMark;
|
||||
inherit (constants)
|
||||
xrayDnsBridgePorts
|
||||
xraySidecarBasePorts
|
||||
;
|
||||
|
||||
mkSubscriptionCacheFile = sub: "${subscriptionCacheDir}/${sub.tag}.json";
|
||||
|
||||
@@ -695,8 +696,8 @@ let
|
||||
enableLocalProxyAuth ? false,
|
||||
xrayTunEgressBinding ? false,
|
||||
xrayTunDnsRuntime ? false,
|
||||
xraySidecarBasePort ? 33080,
|
||||
xrayDnsBridgePort ? 18533,
|
||||
xraySidecarBasePort ? xraySidecarBasePorts.socks,
|
||||
xrayDnsBridgePort ? xrayDnsBridgePorts.socks,
|
||||
}:
|
||||
pkgs.writeShellScript name ''
|
||||
set -euo pipefail
|
||||
@@ -815,8 +816,8 @@ let
|
||||
configFile = tproxyFile;
|
||||
routingMark = globalTproxy.proxyMark;
|
||||
enableLocalProxyAuth = localProxyAuthEnabled;
|
||||
xraySidecarBasePort = 33080;
|
||||
xrayDnsBridgePort = 18533;
|
||||
xraySidecarBasePort = xraySidecarBasePorts.socks;
|
||||
xrayDnsBridgePort = xrayDnsBridgePorts.socks;
|
||||
};
|
||||
|
||||
startTun = mkStartScript {
|
||||
@@ -826,8 +827,8 @@ let
|
||||
routingMark = if pureXrayEnabled then globalTproxy.proxyMark else null;
|
||||
xrayTunEgressBinding = xrayEnabled;
|
||||
xrayTunDnsRuntime = pureXrayEnabled;
|
||||
xraySidecarBasePort = 33180;
|
||||
xrayDnsBridgePort = 18534;
|
||||
xraySidecarBasePort = xraySidecarBasePorts.tun;
|
||||
xrayDnsBridgePort = xrayDnsBridgePorts.tun;
|
||||
};
|
||||
|
||||
startPerAppTun = mkStartScript {
|
||||
@@ -837,8 +838,8 @@ let
|
||||
routingMark = if xrayEnabled || globalTproxy.enable then globalTproxy.proxyMark else null;
|
||||
xrayTunEgressBinding = xrayEnabled;
|
||||
xrayTunDnsRuntime = pureXrayEnabled;
|
||||
xraySidecarBasePort = 33280;
|
||||
xrayDnsBridgePort = 18535;
|
||||
xraySidecarBasePort = xraySidecarBasePorts.perAppTun;
|
||||
xrayDnsBridgePort = xrayDnsBridgePorts.perAppTun;
|
||||
};
|
||||
|
||||
mkSubscriptionFetchBlock =
|
||||
|
||||
Reference in new issue
Block a user