various fixes
This commit is contained in:
11 files changed
+413
-181
No files matched your search
@@ -66,6 +66,8 @@ let
|
||||
tunAddress ? globalTun.address,
|
||||
tunMtu ? globalTun.mtu,
|
||||
tunAutoRoute ? true,
|
||||
tunAutoRouteTableIndex ? 2022,
|
||||
tunAutoRouteRuleIndex ? 9000,
|
||||
tunAutoRedirect ? true,
|
||||
tunStrictRoute ? true,
|
||||
forceLocalDnsViaProxy ? false,
|
||||
@@ -95,17 +97,25 @@ let
|
||||
listen = "127.0.0.1";
|
||||
listen_port = globalTproxy.port;
|
||||
}
|
||||
++ lib.optional enableTun {
|
||||
type = "tun";
|
||||
tag = "tun-in";
|
||||
interface_name = tunInterface;
|
||||
address = [ tunAddress ];
|
||||
mtu = tunMtu;
|
||||
auto_route = tunAutoRoute;
|
||||
auto_redirect = tunAutoRedirect;
|
||||
strict_route = tunStrictRoute;
|
||||
stack = "mixed";
|
||||
};
|
||||
++ lib.optional enableTun (
|
||||
{
|
||||
type = "tun";
|
||||
tag = "tun-in";
|
||||
interface_name = tunInterface;
|
||||
address = [ tunAddress ];
|
||||
mtu = tunMtu;
|
||||
auto_route = tunAutoRoute;
|
||||
auto_redirect = tunAutoRedirect;
|
||||
strict_route = tunStrictRoute;
|
||||
stack = "mixed";
|
||||
}
|
||||
// lib.optionalAttrs tunAutoRoute {
|
||||
# Keep sing-box auto_route indexes explicit so service cleanup can
|
||||
# reliably remove stale policy-routing state after unclean stops.
|
||||
iproute2_table_index = tunAutoRouteTableIndex;
|
||||
iproute2_rule_index = tunAutoRouteRuleIndex;
|
||||
}
|
||||
);
|
||||
|
||||
# proxy outbound(s) are prepended at runtime by the start script
|
||||
outbounds = [
|
||||
|
||||
@@ -36,6 +36,8 @@ let
|
||||
perAppZapretCfg
|
||||
;
|
||||
|
||||
globalTunAutoRouteTable = 2022;
|
||||
|
||||
featureAssertions = [
|
||||
(requireAvailable singBoxCfg.enable (
|
||||
singBoxCfg.outbounds != [ ] || singBoxCfg.subscriptions != [ ]
|
||||
@@ -123,6 +125,24 @@ let
|
||||
];
|
||||
|
||||
collisionAssertions = map (item: notEqualWhen item.condition item.left item.right item.message) [
|
||||
{
|
||||
condition = globalTun.enable && perAppRoutingTun.enable;
|
||||
left = globalTun.interface;
|
||||
right = perAppRoutingTun.interface;
|
||||
message = "proxy-suite: singBox.tun.interface and singBox.tun.perApp.interface must differ";
|
||||
}
|
||||
{
|
||||
condition = globalTun.enable && perAppRoutingTun.enable;
|
||||
left = globalTun.address;
|
||||
right = perAppRoutingTun.address;
|
||||
message = "proxy-suite: singBox.tun.address and singBox.tun.perApp.address must differ";
|
||||
}
|
||||
{
|
||||
condition = globalTun.enable && perAppRoutingTun.enable;
|
||||
left = perAppRoutingTun.routeTable;
|
||||
right = globalTunAutoRouteTable;
|
||||
message = "proxy-suite: singBox.tun.perApp.routeTable must differ from the global TUN auto-route table 2022";
|
||||
}
|
||||
{
|
||||
condition = perAppRoutingTun.enable && globalTproxy.enable;
|
||||
left = perAppRoutingTun.fwmark;
|
||||
@@ -202,6 +222,64 @@ let
|
||||
134217728
|
||||
];
|
||||
|
||||
positiveNumberAssertions = map (item: mkAssertion (!item.condition || item.value > 0) item.message) [
|
||||
{
|
||||
condition = globalTun.enable;
|
||||
value = globalTun.mtu;
|
||||
message = "proxy-suite: singBox.tun.mtu must be greater than zero";
|
||||
}
|
||||
{
|
||||
condition = perAppRoutingTun.enable;
|
||||
value = perAppRoutingTun.mtu;
|
||||
message = "proxy-suite: singBox.tun.perApp.mtu must be greater than zero";
|
||||
}
|
||||
{
|
||||
condition = globalTproxy.enable;
|
||||
value = globalTproxy.fwmark;
|
||||
message = "proxy-suite: singBox.tproxy.fwmark must be greater than zero";
|
||||
}
|
||||
{
|
||||
condition = globalTproxy.enable;
|
||||
value = globalTproxy.proxyMark;
|
||||
message = "proxy-suite: singBox.tproxy.proxyMark must be greater than zero";
|
||||
}
|
||||
{
|
||||
condition = globalTproxy.enable;
|
||||
value = globalTproxy.routeTable;
|
||||
message = "proxy-suite: singBox.tproxy.routeTable must be greater than zero";
|
||||
}
|
||||
{
|
||||
condition = perAppRoutingTun.enable;
|
||||
value = perAppRoutingTun.fwmark;
|
||||
message = "proxy-suite: singBox.tun.perApp.fwmark must be greater than zero";
|
||||
}
|
||||
{
|
||||
condition = perAppRoutingTun.enable;
|
||||
value = perAppRoutingTun.routeTable;
|
||||
message = "proxy-suite: singBox.tun.perApp.routeTable must be greater than zero";
|
||||
}
|
||||
{
|
||||
condition = perAppRoutingTproxy.enable;
|
||||
value = perAppRoutingTproxy.fwmark;
|
||||
message = "proxy-suite: singBox.tproxy.perApp.fwmark must be greater than zero";
|
||||
}
|
||||
{
|
||||
condition = perAppRoutingTproxy.enable;
|
||||
value = perAppRoutingTproxy.routeTable;
|
||||
message = "proxy-suite: singBox.tproxy.perApp.routeTable must be greater than zero";
|
||||
}
|
||||
{
|
||||
condition = perAppZapretCfg.enable;
|
||||
value = perAppZapretCfg.filterMark;
|
||||
message = "proxy-suite: zapret.perApp.filterMark must be greater than zero";
|
||||
}
|
||||
{
|
||||
condition = perAppZapretCfg.enable;
|
||||
value = perAppZapretCfg.qnum;
|
||||
message = "proxy-suite: zapret.perApp.qnum must be greater than zero";
|
||||
}
|
||||
];
|
||||
|
||||
localProxyAuthCfg = singBoxCfg.auth;
|
||||
localProxyAuthUsed =
|
||||
localProxyAuthCfg.username != null
|
||||
@@ -286,6 +364,7 @@ in
|
||||
featureAssertions
|
||||
++ perAppRoutingAssertions
|
||||
++ collisionAssertions
|
||||
++ positiveNumberAssertions
|
||||
++ localProxyAuthAssertions
|
||||
++ forbiddenValueAssertions
|
||||
++ secretAssertions
|
||||
|
||||
@@ -46,6 +46,7 @@ rec {
|
||||
wants ? [ ],
|
||||
requires ? [ ],
|
||||
conflicts ? [ ],
|
||||
execStartPre ? null,
|
||||
execStartPost ? null,
|
||||
execStopPost ? null,
|
||||
extraServiceConfig ? { },
|
||||
@@ -70,6 +71,7 @@ rec {
|
||||
RuntimeDirectory = runtimeDirectory;
|
||||
}
|
||||
// lib.optionalAttrs (stateDirectory != null) { StateDirectory = stateDirectory; }
|
||||
// lib.optionalAttrs (execStartPre != null) { ExecStartPre = execStartPre; }
|
||||
// lib.optionalAttrs (execStartPost != null) { ExecStartPost = execStartPost; }
|
||||
// lib.optionalAttrs (execStopPost != null) { ExecStopPost = execStopPost; }
|
||||
// extraServiceConfig;
|
||||
|
||||
@@ -85,17 +85,47 @@ let
|
||||
singBoxCfg.auth.username != null
|
||||
&& (singBoxCfg.auth.password != null || singBoxCfg.auth.passwordFile != null);
|
||||
|
||||
# Must match the explicit iproute2_* indexes written to the global TUN
|
||||
# sing-box template in config.nix.
|
||||
tunAutoRouteTableIndex = 2022;
|
||||
|
||||
tproxyUpScript = pkgs.writeShellScript "proxy-suite-tproxy-up" ''
|
||||
set -euo pipefail
|
||||
|
||||
# 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
|
||||
|
||||
${nft} -f ${nftablesRulesFile}
|
||||
${ip} route add local default dev lo table ${toString globalTproxy.routeTable}
|
||||
${ip} route replace local default dev lo table ${toString globalTproxy.routeTable}
|
||||
${ip} rule add fwmark ${toString globalTproxy.fwmark} table ${toString globalTproxy.routeTable}
|
||||
'';
|
||||
|
||||
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
|
||||
${ip} rule del fwmark ${toString globalTproxy.fwmark} table ${toString globalTproxy.routeTable} 2>/dev/null || true
|
||||
'';
|
||||
|
||||
tunCleanupScript = pkgs.writeShellScript "proxy-suite-tun-cleanup" ''
|
||||
set +e
|
||||
|
||||
# sing-box 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
|
||||
${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
|
||||
'';
|
||||
|
||||
systemServiceEntries = [
|
||||
@@ -139,7 +169,9 @@ let
|
||||
wants = [ "network-online.target" ];
|
||||
wantedBy = lib.optionals globalTun.autostart [ "multi-user.target" ];
|
||||
conflicts = [ "${serviceNames.tproxy}.service" ];
|
||||
execStartPre = tunCleanupScript;
|
||||
execStart = scripts.startTun;
|
||||
execStopPost = tunCleanupScript;
|
||||
runtimeDirectory = serviceNames.tun;
|
||||
stateDirectory = "proxy-suite";
|
||||
};
|
||||
@@ -151,6 +183,7 @@ let
|
||||
description = "sing-box per-app-routing TUN backend";
|
||||
after = [ "network-online.target" ];
|
||||
wants = [ "network-online.target" ];
|
||||
execStartPre = perAppRouting.perAppTunDownScript;
|
||||
execStart = scripts.startPerAppTun;
|
||||
execStartPost = perAppRouting.perAppTunUpScript;
|
||||
execStopPost = perAppRouting.perAppTunDownScript;
|
||||
@@ -264,9 +297,14 @@ in
|
||||
{
|
||||
environment.systemPackages = [ control.proxyCtl ];
|
||||
|
||||
# nftables must be on for TProxy to work.
|
||||
# nftables must be on for transparent routing backends. Global TUN uses
|
||||
# sing-box auto_redirect, which programs an `inet sing-box` nftables table.
|
||||
networking.nftables.enable = lib.mkIf (
|
||||
globalTproxy.enable || perAppRoutingTun.enable || perAppRoutingTproxy.enable || perAppZapretEnabled
|
||||
globalTun.enable
|
||||
|| globalTproxy.enable
|
||||
|| perAppRoutingTun.enable
|
||||
|| perAppRoutingTproxy.enable
|
||||
|| perAppZapretEnabled
|
||||
) (lib.mkDefault true);
|
||||
|
||||
users.groups = lib.mkIf (cfg.enable && (userControlEnabled || localProxyAuthEnabled)) {
|
||||
|
||||
@@ -120,25 +120,33 @@ let
|
||||
'';
|
||||
|
||||
perAppTunDownScript = pkgs.writeShellScript "proxy-suite-per-app-tun-down" ''
|
||||
set -euo pipefail
|
||||
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} rule del fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable} 2>/dev/null; do :; done
|
||||
${ip} route del default dev ${lib.escapeShellArg perAppRoutingTun.interface} table ${toString perAppRoutingTun.routeTable} 2>/dev/null || true
|
||||
${ip} rule del fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable} 2>/dev/null || true
|
||||
${ip} link del dev ${lib.escapeShellArg perAppRoutingTun.interface} 2>/dev/null || true
|
||||
'';
|
||||
|
||||
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
|
||||
|
||||
${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} 2>/dev/null || true
|
||||
${ip} rule add fwmark ${toString perAppRoutingTproxy.fwmark} table ${toString perAppRoutingTproxy.routeTable}
|
||||
'';
|
||||
|
||||
perAppTproxyDownScript = pkgs.writeShellScript "proxy-suite-per-app-tproxy-down" ''
|
||||
set -euo pipefail
|
||||
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
|
||||
${ip} rule del fwmark ${toString perAppRoutingTproxy.fwmark} table ${toString perAppRoutingTproxy.routeTable} 2>/dev/null || true
|
||||
'';
|
||||
|
||||
# Generate a "start" script that adds a per-user cgroup nftables mark rule.
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
|
||||
let
|
||||
globalTproxy = singBoxCfg.tproxy;
|
||||
systemctl = "${pkgs.systemd}/bin/systemctl";
|
||||
localProxyAuth = singBoxCfg.auth;
|
||||
localProxyAuthEnabled =
|
||||
localProxyAuth.username != null
|
||||
@@ -139,6 +140,13 @@ let
|
||||
sub: mkSubscriptionBlock sub routingMark
|
||||
) singBoxCfg.subscriptions;
|
||||
|
||||
requireOutboundsBlock = ''
|
||||
if [ "$(${jq} 'length' <<< "$OUTBOUNDS_JSON")" -eq 0 ]; then
|
||||
echo "proxy-suite: no proxy outbounds are available; check static outbound definitions and subscription caches" >&2
|
||||
exit 1
|
||||
fi
|
||||
'';
|
||||
|
||||
wrapperBlock =
|
||||
if collapseNamedOutbounds then
|
||||
# When there are no static outbounds, subscription outbounds keep their
|
||||
@@ -173,7 +181,7 @@ let
|
||||
OUTBOUNDS_JSON=$(${jq} --argjson w "$WRAPPER" '[$w] + .' <<< "$OUTBOUNDS_JSON")
|
||||
'';
|
||||
in
|
||||
outboundBlocks + subscriptionBlocks + wrapperBlock;
|
||||
outboundBlocks + subscriptionBlocks + requireOutboundsBlock + wrapperBlock;
|
||||
|
||||
writeProxychainsConfigBlock = ''
|
||||
{
|
||||
@@ -195,6 +203,16 @@ let
|
||||
|
||||
modeBaseline = if singBoxCfg.proxyByDefault then "blacklist" else "whitelist";
|
||||
|
||||
restartActiveConfigConsumersBlock = lib.concatMapStrings (svc: ''
|
||||
if ${systemctl} is-active --quiet ${svc}; then
|
||||
${systemctl} restart ${svc}
|
||||
fi
|
||||
'') [
|
||||
"proxy-suite-socks"
|
||||
"proxy-suite-tun"
|
||||
"proxy-suite-per-app-tun"
|
||||
];
|
||||
|
||||
mkStartScript =
|
||||
{
|
||||
name,
|
||||
@@ -212,8 +230,9 @@ let
|
||||
ROUTE_MODE=""
|
||||
ROUTE_FINAL=""
|
||||
DNS_FINAL=""
|
||||
ROUTE_RULES_JSON=""
|
||||
CLEAR_DNS_RULES=0
|
||||
ROUTE_RULES_JSON='[]'
|
||||
ROUTE_MODE_ACTIVE=false
|
||||
CLEAR_DNS_RULES=false
|
||||
${lib.optionalString enableLocalProxyAuth ''
|
||||
umask 077
|
||||
LOCAL_PROXY_PASSWORD="$(cat "${localProxyAuthPasswordSource}")"
|
||||
@@ -227,6 +246,7 @@ let
|
||||
blacklist)
|
||||
ROUTE_FINAL="proxy"
|
||||
DNS_FINAL="remote"
|
||||
ROUTE_MODE_ACTIVE=true
|
||||
ROUTE_RULES_JSON=$(${jq} -c '
|
||||
.common
|
||||
+ (.custom | map(.entries) | add // [])
|
||||
@@ -240,6 +260,7 @@ let
|
||||
whitelist)
|
||||
ROUTE_FINAL="direct"
|
||||
DNS_FINAL="local"
|
||||
ROUTE_MODE_ACTIVE=true
|
||||
ROUTE_RULES_JSON=$(${jq} -c '
|
||||
.common
|
||||
+ (.custom | map(.entries) | add // [])
|
||||
@@ -253,7 +274,8 @@ let
|
||||
all-proxy)
|
||||
ROUTE_FINAL="proxy"
|
||||
DNS_FINAL="remote"
|
||||
CLEAR_DNS_RULES=1
|
||||
ROUTE_MODE_ACTIVE=true
|
||||
CLEAR_DNS_RULES=true
|
||||
ROUTE_RULES_JSON=$(${jq} -c '
|
||||
.common
|
||||
+ (.custom | map(select(.category == "proxy" or .category == "block") | .entries) | add // [])
|
||||
@@ -266,7 +288,8 @@ let
|
||||
all-bypass)
|
||||
ROUTE_FINAL="direct"
|
||||
DNS_FINAL="local"
|
||||
CLEAR_DNS_RULES=1
|
||||
ROUTE_MODE_ACTIVE=true
|
||||
CLEAR_DNS_RULES=true
|
||||
ROUTE_RULES_JSON=$(${jq} -c '
|
||||
.common
|
||||
+ (.custom | map(select(.category == "block") | .entries) | add // [])
|
||||
@@ -281,85 +304,30 @@ let
|
||||
|
||||
${mkOutboundScript routingMark}
|
||||
|
||||
${
|
||||
if enableLocalProxyAuth then
|
||||
''
|
||||
if [ -n "$ROUTE_MODE" ]; then
|
||||
if [ "$CLEAR_DNS_RULES" -eq 1 ]; then
|
||||
${jq} \
|
||||
--argjson obs "$OUTBOUNDS_JSON" \
|
||||
--arg user ${lib.escapeShellArg localProxyAuth.username} \
|
||||
--arg password "$LOCAL_PROXY_PASSWORD" \
|
||||
--argjson route_rules "$ROUTE_RULES_JSON" \
|
||||
--arg route_final "$ROUTE_FINAL" \
|
||||
--arg dns_final "$DNS_FINAL" \
|
||||
'.outbounds = $obs + .outbounds
|
||||
| (.inbounds[] | select(.type == "mixed" and .tag == "mixed-in") | .users) = [{username:$user,password:$password}]
|
||||
| .route.rules = $route_rules
|
||||
| .route.final = $route_final
|
||||
| .dns.rules = []
|
||||
| .dns.final = $dns_final' \
|
||||
"${configFile}" > "$RUNTIME_DIR/config.json"
|
||||
else
|
||||
${jq} \
|
||||
--argjson obs "$OUTBOUNDS_JSON" \
|
||||
--arg user ${lib.escapeShellArg localProxyAuth.username} \
|
||||
--arg password "$LOCAL_PROXY_PASSWORD" \
|
||||
--argjson route_rules "$ROUTE_RULES_JSON" \
|
||||
--arg route_final "$ROUTE_FINAL" \
|
||||
--arg dns_final "$DNS_FINAL" \
|
||||
'.outbounds = $obs + .outbounds
|
||||
| (.inbounds[] | select(.type == "mixed" and .tag == "mixed-in") | .users) = [{username:$user,password:$password}]
|
||||
| .route.rules = $route_rules
|
||||
| .route.final = $route_final
|
||||
| .dns.final = $dns_final' \
|
||||
"${configFile}" > "$RUNTIME_DIR/config.json"
|
||||
fi
|
||||
else
|
||||
${jq} \
|
||||
--argjson obs "$OUTBOUNDS_JSON" \
|
||||
--arg user ${lib.escapeShellArg localProxyAuth.username} \
|
||||
--arg password "$LOCAL_PROXY_PASSWORD" \
|
||||
'.outbounds = $obs + .outbounds
|
||||
| (.inbounds[] | select(.type == "mixed" and .tag == "mixed-in") | .users) = [{username:$user,password:$password}]' \
|
||||
"${configFile}" > "$RUNTIME_DIR/config.json"
|
||||
fi
|
||||
chmod 600 "$RUNTIME_DIR/config.json"
|
||||
''
|
||||
else
|
||||
''
|
||||
if [ -n "$ROUTE_MODE" ]; then
|
||||
if [ "$CLEAR_DNS_RULES" -eq 1 ]; then
|
||||
${jq} \
|
||||
--argjson obs "$OUTBOUNDS_JSON" \
|
||||
--argjson route_rules "$ROUTE_RULES_JSON" \
|
||||
--arg route_final "$ROUTE_FINAL" \
|
||||
--arg dns_final "$DNS_FINAL" \
|
||||
'.outbounds = $obs + .outbounds
|
||||
| .route.rules = $route_rules
|
||||
| .route.final = $route_final
|
||||
| .dns.rules = []
|
||||
| .dns.final = $dns_final' \
|
||||
"${configFile}" > "$RUNTIME_DIR/config.json"
|
||||
else
|
||||
${jq} \
|
||||
--argjson obs "$OUTBOUNDS_JSON" \
|
||||
--argjson route_rules "$ROUTE_RULES_JSON" \
|
||||
--arg route_final "$ROUTE_FINAL" \
|
||||
--arg dns_final "$DNS_FINAL" \
|
||||
'.outbounds = $obs + .outbounds
|
||||
| .route.rules = $route_rules
|
||||
| .route.final = $route_final
|
||||
| .dns.final = $dns_final' \
|
||||
"${configFile}" > "$RUNTIME_DIR/config.json"
|
||||
fi
|
||||
else
|
||||
${jq} --argjson obs "$OUTBOUNDS_JSON" \
|
||||
'.outbounds = $obs + .outbounds' \
|
||||
"${configFile}" > "$RUNTIME_DIR/config.json"
|
||||
fi
|
||||
''
|
||||
}
|
||||
${jq} \
|
||||
--argjson obs "$OUTBOUNDS_JSON" \
|
||||
--argjson auth_enabled ${if enableLocalProxyAuth then "true" else "false"} \
|
||||
--arg user ${if enableLocalProxyAuth then lib.escapeShellArg localProxyAuth.username else "''"} \
|
||||
--arg password ${if enableLocalProxyAuth then "\"$LOCAL_PROXY_PASSWORD\"" else "''"} \
|
||||
--argjson route_enabled "$ROUTE_MODE_ACTIVE" \
|
||||
--argjson route_rules "$ROUTE_RULES_JSON" \
|
||||
--arg route_final "$ROUTE_FINAL" \
|
||||
--arg dns_final "$DNS_FINAL" \
|
||||
--argjson clear_dns_rules "$CLEAR_DNS_RULES" \
|
||||
'.outbounds = $obs + .outbounds
|
||||
| if $auth_enabled then
|
||||
(.inbounds[] | select(.type == "mixed" and .tag == "mixed-in") | .users) = [{username:$user,password:$password}]
|
||||
else . end
|
||||
| if $route_enabled then
|
||||
.route.rules = $route_rules
|
||||
| .route.final = $route_final
|
||||
| .dns.final = $dns_final
|
||||
| if $clear_dns_rules then .dns.rules = [] else . end
|
||||
else . end' \
|
||||
"${configFile}" > "$RUNTIME_DIR/config.json"
|
||||
${lib.optionalString enableLocalProxyAuth ''
|
||||
chmod 600 "$RUNTIME_DIR/config.json"
|
||||
''}
|
||||
|
||||
exec ${singBox} run -c "$RUNTIME_DIR/config.json"
|
||||
'';
|
||||
@@ -410,26 +378,10 @@ let
|
||||
CACHE_DIR="${subscriptionCacheDir}"
|
||||
mkdir -p "$CACHE_DIR"
|
||||
FAILED=0
|
||||
SOCKS_WAS_ACTIVE=0
|
||||
TUN_WAS_ACTIVE=0
|
||||
PER_APP_TUN_WAS_ACTIVE=0
|
||||
|
||||
systemctl is-active --quiet proxy-suite-socks && SOCKS_WAS_ACTIVE=1 || true
|
||||
systemctl is-active --quiet proxy-suite-tun && TUN_WAS_ACTIVE=1 || true
|
||||
systemctl is-active --quiet proxy-suite-per-app-tun && PER_APP_TUN_WAS_ACTIVE=1 || true
|
||||
|
||||
${lib.concatMapStrings mkSubscriptionFetchBlock singBoxCfg.subscriptions}
|
||||
|
||||
if [ "$FAILED" -eq 0 ]; then
|
||||
if [ "$SOCKS_WAS_ACTIVE" -eq 1 ]; then
|
||||
systemctl restart proxy-suite-socks
|
||||
fi
|
||||
if [ "$TUN_WAS_ACTIVE" -eq 1 ]; then
|
||||
systemctl restart proxy-suite-tun
|
||||
fi
|
||||
if [ "$PER_APP_TUN_WAS_ACTIVE" -eq 1 ]; then
|
||||
systemctl restart proxy-suite-per-app-tun
|
||||
fi
|
||||
${restartActiveConfigConsumersBlock}
|
||||
fi
|
||||
exit "$FAILED"
|
||||
'';
|
||||
@@ -462,23 +414,7 @@ let
|
||||
printf '%s\n' "$mode" > "${routeModeStateFile}"
|
||||
fi
|
||||
|
||||
SOCKS_WAS_ACTIVE=0
|
||||
TUN_WAS_ACTIVE=0
|
||||
PER_APP_TUN_WAS_ACTIVE=0
|
||||
|
||||
systemctl is-active --quiet proxy-suite-socks && SOCKS_WAS_ACTIVE=1 || true
|
||||
systemctl is-active --quiet proxy-suite-tun && TUN_WAS_ACTIVE=1 || true
|
||||
systemctl is-active --quiet proxy-suite-per-app-tun && PER_APP_TUN_WAS_ACTIVE=1 || true
|
||||
|
||||
if [ "$SOCKS_WAS_ACTIVE" -eq 1 ]; then
|
||||
systemctl restart proxy-suite-socks
|
||||
fi
|
||||
if [ "$TUN_WAS_ACTIVE" -eq 1 ]; then
|
||||
systemctl restart proxy-suite-tun
|
||||
fi
|
||||
if [ "$PER_APP_TUN_WAS_ACTIVE" -eq 1 ]; then
|
||||
systemctl restart proxy-suite-per-app-tun
|
||||
fi
|
||||
${restartActiveConfigConsumersBlock}
|
||||
'';
|
||||
|
||||
subscriptionTagsFile = pkgs.writeText "proxy-suite-subscription-tags.json" (
|
||||
|
||||
@@ -296,14 +296,22 @@ let
|
||||
${nft} delete table inet proxy_suite_per_app_zapret_mark 2>/dev/null || true
|
||||
'';
|
||||
|
||||
exemptStart = lib.concatMapStrings (cidr: ''
|
||||
${iptables} -t mangle -I FORWARD 1 -d ${cidr} -j RETURN
|
||||
exemptStart = ''
|
||||
set -euo pipefail
|
||||
''
|
||||
+ lib.concatMapStrings (cidr: ''
|
||||
while ${iptables} -t mangle -D FORWARD -d ${cidr} -j RETURN 2>/dev/null; do :; done
|
||||
while ${iptables} -t mangle -D POSTROUTING -s ${cidr} -j RETURN 2>/dev/null; do :; done
|
||||
${iptables} -t mangle -I FORWARD 1 -d ${cidr} -j RETURN
|
||||
${iptables} -t mangle -I POSTROUTING 1 -s ${cidr} -j RETURN
|
||||
'') zapretCfg.cidrExemption.cidrs;
|
||||
|
||||
exemptStop = lib.concatMapStrings (cidr: ''
|
||||
${iptables} -t mangle -D FORWARD -d ${cidr} -j RETURN || true
|
||||
${iptables} -t mangle -D POSTROUTING -s ${cidr} -j RETURN || true
|
||||
exemptStop = ''
|
||||
set +e
|
||||
''
|
||||
+ lib.concatMapStrings (cidr: ''
|
||||
while ${iptables} -t mangle -D FORWARD -d ${cidr} -j RETURN 2>/dev/null; do :; done
|
||||
while ${iptables} -t mangle -D POSTROUTING -s ${cidr} -j RETURN 2>/dev/null; do :; done
|
||||
'') zapretCfg.cidrExemption.cidrs;
|
||||
in
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user