fix: multiple fixes + cleanup and formatting

This commit is contained in:
FUFSoB committed 2026-09-17 22:51:31 +05:00
1 parent 321e9ca468
commit c62000821a
59 files changed
+1357 -920

No files matched your search

+16 -4
View File
@@ -264,10 +264,22 @@ rec {
fwmark,
table,
}:
lib.concatMapStrings (family: ''
${mkIpRuleDeleteByFwmark { inherit ip family fwmark table; }}
${mkIpLocalDefaultRouteDelete { inherit ip family table; }}
'') [ "-4" "-6" ];
lib.concatMapStrings
(family: ''
${mkIpRuleDeleteByFwmark {
inherit
ip
family
fwmark
table
;
}}
${mkIpLocalDefaultRouteDelete { inherit ip family table; }}
'')
[
"-4"
"-6"
];
mkTproxyRoutingUp =
{
@@ -140,9 +140,7 @@ let
set -euo pipefail
${subscriptionCacheHelpersBlock}
KEEP="|${
lib.concatMapStrings (sub: "${sub.tag}|") proxyCfg.subscriptions
}"
KEEP="|${lib.concatMapStrings (sub: "${sub.tag}|") proxyCfg.subscriptions}"
while IFS=$'\t' read -r TAG _; do
[ -n "$TAG" ] || continue
KEEP="$KEEP$TAG|"
@@ -44,7 +44,11 @@ let
${builders.cidrNetworkFunction}
${builders.mkNftDeleteTable { inherit nft; family = "inet"; table = "proxy_suite_per_app_tun"; }}
${builders.mkNftDeleteTable {
inherit nft;
family = "inet";
table = "proxy_suite_per_app_tun";
}}
${builders.mkIpRuleDeleteByFwmark {
inherit ip;
family = "-4";
@@ -57,8 +61,16 @@ let
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.mkIpRouteFlushTable {
inherit ip;
family = "-4";
table = perAppRoutingTun.routeTable;
}}
${builders.mkIpRouteFlushTable {
inherit ip;
family = "-6";
table = perAppRoutingTun.routeTable;
}}
${nft} -f ${perAppTunChainFile}
${perAppTunWaitForInterface}
tun_addr="''${tun_cidr%%/*}"
@@ -89,7 +101,11 @@ let
set +e
# Best-effort cleanup for graceful stops and for unclean previous exits.
${builders.mkNftDeleteTable { inherit nft; family = "inet"; table = "proxy_suite_per_app_tun"; }}
${builders.mkNftDeleteTable {
inherit nft;
family = "inet";
table = "proxy_suite_per_app_tun";
}}
${builders.mkIpRuleDeleteByFwmark {
inherit ip;
family = "-4";
@@ -102,16 +118,31 @@ let
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.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" ''
set -euo pipefail
${builders.mkNftDeleteTable { inherit nft; family = "inet"; table = "proxy_suite_per_app_tproxy"; }}
${builders.mkNftDeleteTable {
inherit nft;
family = "inet";
table = "proxy_suite_per_app_tproxy";
}}
${builders.mkTproxyRoutingDown {
inherit ip;
fwmark = perAppRoutingTproxy.fwmark;
@@ -130,7 +161,11 @@ let
perAppTproxyDownScript = pkgs.writeShellScript "proxy-suite-per-app" ''
set +e
${builders.mkNftDeleteTable { inherit nft; family = "inet"; table = "proxy_suite_per_app_tproxy"; }}
${builders.mkNftDeleteTable {
inherit nft;
family = "inet";
table = "proxy_suite_per_app_tproxy";
}}
${builders.mkTproxyRoutingDown {
inherit ip;
fwmark = perAppRoutingTproxy.fwmark;
@@ -41,7 +41,7 @@ let
serverAddressBlock =
if proxyInboundsCfg.serverAddress != null then
''SERVER_ADDRESS=${lib.escapeShellArg proxyInboundsCfg.serverAddress}''
"SERVER_ADDRESS=${lib.escapeShellArg proxyInboundsCfg.serverAddress}"
else if proxyInboundsCfg.shareLinks then
''
${builders.mkDefaultUplinkIPv4Source {
@@ -95,9 +95,7 @@ let
''
# via outbound: ${ob.tag} (static xray json)
OB_JSON=$(cat ${
pkgs.writeText "proxy-suite-inbounds" (
builtins.toJSON (ob.xrayJson // { inherit (ob) tag; })
)
pkgs.writeText "proxy-suite-inbounds" (builtins.toJSON (ob.xrayJson // { inherit (ob) tag; }))
})
${dialerBlock}
OUTBOUNDS_JSON=$(${jq} --argjson ob "$OB_JSON" '. + [$ob]' <<< "$OUTBOUNDS_JSON")
+56 -19
View File
@@ -25,17 +25,23 @@ let
globalTunIPv6RoutePrefix
;
deleteXrayTunRules = lib.concatMapStrings (
family:
lib.concatMapStrings (priority: builders.mkIpRuleDeleteByPriority { inherit ip family priority; }) [
xrayTunMarkBypassRulePriority
xrayTunServiceUserRulePriority
xrayTunPerAppTunRulePriority
xrayTunDnsRulePriority
xrayTunMainRulePriority
tunAutoRouteRulePriority
]
) [ "-4" "-6" ];
deleteXrayTunRules =
lib.concatMapStrings
(
family:
lib.concatMapStrings (priority: builders.mkIpRuleDeleteByPriority { inherit ip family priority; }) [
xrayTunMarkBypassRulePriority
xrayTunServiceUserRulePriority
xrayTunPerAppTunRulePriority
xrayTunDnsRulePriority
xrayTunMainRulePriority
tunAutoRouteRulePriority
]
)
[
"-4"
"-6"
];
# Replies to connections from outside (sshd, a game server, anything listening) take
# proxyMark, so the fwmark rule sends them back out the way they came instead of into
@@ -119,7 +125,11 @@ 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 = "inet"; table = "singbox"; }}
${builders.mkNftDeleteTable {
inherit nft;
family = "inet";
table = "singbox";
}}
${builders.mkTproxyRoutingDown {
inherit ip;
fwmark = globalTproxy.fwmark;
@@ -138,7 +148,11 @@ in
tproxyDownScript = pkgs.writeShellScript "proxy-suite-routing" ''
set +e
${builders.mkNftDeleteTable { inherit nft; family = "inet"; table = "singbox"; }}
${builders.mkNftDeleteTable {
inherit nft;
family = "inet";
table = "singbox";
}}
${builders.mkTproxyRoutingDown {
inherit ip;
fwmark = globalTproxy.fwmark;
@@ -152,14 +166,37 @@ in
# 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 proxy tun off` or an unclean service stop.
${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.mkNftDeleteTable {
inherit nft;
family = "inet";
table = "sing-box";
}}
${builders.mkIpRuleDeleteByTable {
inherit ip;
family = "-4";
table = tunAutoRouteTableIndex;
}}
${builders.mkIpRuleDeleteByTable {
inherit ip;
family = "-6";
table = tunAutoRouteTableIndex;
}}
${deleteXrayTunRules}
${deleteXrayTunReplyTable}
${builders.mkIpRouteFlushTable { inherit ip; family = "-4"; table = tunAutoRouteTableIndex; }}
${builders.mkIpRouteFlushTable { inherit ip; family = "-6"; table = tunAutoRouteTableIndex; }}
${builders.mkIpLinkDelete { inherit ip; interface = globalTun.interface; }}
${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}
'';
}
@@ -25,13 +25,17 @@ lib.optionalString hybridEnabled ''
local batch
batch=$(${jq} -c \
--argjson base "$XRAY_SIDECAR_NEXT_PORT" \
--argjson mark ${if xraySidecarRoutingMark == null then "null" else toString xraySidecarRoutingMark} '
--argjson mark ${
if xraySidecarRoutingMark == null then "null" else toString xraySidecarRoutingMark
} '
to_entries | map(
($base + .key) as $port
| ("proxy-suite-xray-" + ($port | tostring)) as $auth
| .value.tag as $tag
| {outbound: (.value
${lib.optionalString (xraySidecarRoutingMark != null) "| .streamSettings.sockopt.mark = $mark"}
${lib.optionalString (
xraySidecarRoutingMark != null
) "| .streamSettings.sockopt.mark = $mark"}
| .streamSettings.sockopt.domainStrategy = (.streamSettings.sockopt.domainStrategy // "UseIP")),
inbound: {tag: ($tag + "-inbound"), listen: "127.0.0.1", port: $port, protocol: "socks",
settings: {auth: "password", udp: true, accounts: [{user: $auth, pass: $auth}]}},
@@ -163,7 +163,9 @@ let
_proxy_suite_parse_xray_url() {
${parse "--backend xray" (
lib.optionalString (xraySidecarRoutingMark != null) " --routing-mark ${toString xraySidecarRoutingMark}"
lib.optionalString (
xraySidecarRoutingMark != null
) " --routing-mark ${toString xraySidecarRoutingMark}"
)}
}
''
@@ -238,7 +240,11 @@ let
singBoxRawOutboundJson =
ob: tag: routingMark:
ob.singBoxJson // { inherit tag; } // lib.optionalAttrs (routingMark != null) { routing_mark = routingMark; };
ob.singBoxJson
// {
inherit tag;
}
// lib.optionalAttrs (routingMark != null) { routing_mark = routingMark; };
mkHybridOutboundBlock =
ob: routingMark: tag:
@@ -435,7 +441,10 @@ let
# Declared detours, resolved once every outbound - subscription entries included - exists.
detourMap =
entries: lib.listToAttrs (map (e: lib.nameValuePair e.tag e.detour) (lib.filter (e: e.detour != null) entries));
entries:
lib.listToAttrs (
map (e: lib.nameValuePair e.tag e.detour) (lib.filter (e: e.detour != null) entries)
);
detours = {
outbounds = detourMap proxyCfg.outbounds;
subscriptions = detourMap proxyCfg.subscriptions;
@@ -645,9 +654,7 @@ let
# Real exits for autoProxy, taken after the wrapper may have renamed one to
# "proxy".
exitTagsBlock = ''
EXIT_TAGS_JSON=$(${jq} -c '[.[] | select(.type != "selector" and .type != "urltest"${
lib.optionalString torOutboundEnabled " and (.tag | ltrimstr(\"proxy-suite-ob-\")) != \"tor\""
}) | .tag]' <<< "$OUTBOUNDS_JSON")
EXIT_TAGS_JSON=$(${jq} -c '[.[] | select(.type != "selector" and .type != "urltest"${lib.optionalString torOutboundEnabled " and (.tag | ltrimstr(\"proxy-suite-ob-\")) != \"tor\""}) | .tag]' <<< "$OUTBOUNDS_JSON")
'';
in
mkUrlOutboundHelpersBlock routingMark
@@ -25,11 +25,7 @@ let
subscriptionCacheDir = "${stateDir}/subscriptions/${backend}";
mkSubscriptionUrlSource =
sub:
if sub.urlFile != null then
sub.urlFile
else
pkgs.writeText "proxy-suite-core" sub.url;
sub: if sub.urlFile != null then sub.urlFile else pkgs.writeText "proxy-suite-core" sub.url;
# Defined in every script that touches a cache: the start scripts and the
# update unit.
@@ -145,14 +141,10 @@ let
}
'';
mkSubscriptionBlock =
sub: _routingMark:
''
# subscription: ${sub.tag}
_proxy_suite_load_subscription ${lib.escapeShellArg sub.tag} ${
lib.escapeShellArg (mkSubscriptionUrlSource sub)
}
'';
mkSubscriptionBlock = sub: _routingMark: ''
# subscription: ${sub.tag}
_proxy_suite_load_subscription ${lib.escapeShellArg sub.tag} ${lib.escapeShellArg (mkSubscriptionUrlSource sub)}
'';
runtimeSubscriptionsBlock = ''
# subscriptions added at runtime
@@ -162,17 +154,13 @@ let
done < <(_proxy_suite_runtime_subscriptions)
'';
mkSubscriptionFetchBlock =
sub:
''
if _proxy_suite_fetch_subscription ${lib.escapeShellArg sub.tag} ${
lib.escapeShellArg (mkSubscriptionUrlSource sub)
}; then
echo "Updated subscription: ${sub.tag}"
else
FAILED=1
fi
'';
mkSubscriptionFetchBlock = sub: ''
if _proxy_suite_fetch_subscription ${lib.escapeShellArg sub.tag} ${lib.escapeShellArg (mkSubscriptionUrlSource sub)}; then
echo "Updated subscription: ${sub.tag}"
else
FAILED=1
fi
'';
runtimeSubscriptionsFetchBlock = ''
while IFS=$'\t' read -r RUNTIME_SUB_TAG RUNTIME_SUB_SRC; do