other fixes

This commit is contained in:
FUFSoB committed 2026-05-18 11:03:10 +05:00
1 parent 70c0454446
commit 86e229ff4c
11 files changed
+339 -10

No files matched your search

+6 -1
View File
@@ -11,6 +11,11 @@ let
perAppTun = singBoxCfg.tun.perApp;
perAppTproxy = singBoxCfg.tproxy.perApp;
zapretApp = cfg.zapret.perApp;
tgWsProxyCfg = cfg.tgWsProxy;
tgWsProxyBypassEnabled = tgWsProxyCfg.enable && tgWsProxyCfg.bypassTransparentProxy;
tgWsProxyBypassMarkLine = lib.optionalString tgWsProxyBypassEnabled ''
meta mark ${toString tgWsProxyCfg.routingMark} return
'';
# Shared across all three nftables rule files that do IPv4 routing.
reservedIpBlock = ''
@@ -55,7 +60,7 @@ let
ip daddr $RESERVED_IP return
${tproxyLocalSubnetLines}
meta mark ${toString globalTproxy.proxyMark} return
${lib.optionalString perAppTun.enable " meta mark ${toString perAppTun.fwmark} return\n"}${lib.optionalString perAppTproxy.enable " meta mark ${toString perAppTproxy.fwmark} return\n"} ip protocol tcp meta mark set ${toString globalTproxy.fwmark}
${tgWsProxyBypassMarkLine}${lib.optionalString perAppTun.enable " meta mark ${toString perAppTun.fwmark} return\n"}${lib.optionalString perAppTproxy.enable " meta mark ${toString perAppTproxy.fwmark} return\n"} ip protocol tcp meta mark set ${toString globalTproxy.fwmark}
ip protocol udp meta mark set ${toString globalTproxy.fwmark}
}
}
+27
View File
@@ -130,6 +130,33 @@ in
"203" = "149.154.167.220";
};
};
bypassTransparentProxy = mkOption {
type = types.bool;
default = true;
description = ''
Whether to keep tg-ws-proxy's own relay connections out of proxy-suite
transparent routing backends.
When enabled and global TUN or TProxy mode is configured, the
tg-ws-proxy systemd service receives a packet mark and an earlier
policy-routing rule back to the main table. This prevents global TUN
from routing the local Telegram MTProto relay through sing-box again,
which can otherwise break tg-ws-proxy after enabling TUN mode.
'';
example = true;
};
routingMark = mkOption {
type = types.int;
default = 4;
description = ''
Packet mark used by tgWsProxy.bypassTransparentProxy to bypass
proxy-suite transparent routing. Override this only if it collides
with another local policy-routing mark on your host.
'';
example = 4;
};
};
};
}
+9 -1
View File
@@ -11,6 +11,7 @@ let
r = cfg.singBox.routing;
singBoxCfg = derived.singBoxCfg;
zapretCfg = cfg.zapret;
tgWsProxyCfg = cfg.tgWsProxy;
trim = lib.strings.trim;
hasPrefix = lib.strings.hasPrefix;
@@ -203,12 +204,19 @@ let
(mkRulesetRule "direct" (map (s: "geoip-${s}") direct.geoips))
];
tgWsProxyRelayDirectRules = lib.optional (
tgWsProxyCfg.enable && tgWsProxyCfg.bypassTransparentProxy && tgWsProxyCfg.dcIps != { }
) {
ip_cidr = lib.unique (builtins.attrValues tgWsProxyCfg.dcIps);
outbound = "direct";
};
safetyDirectRules = [
{
ip_is_private = true;
outbound = "direct";
}
];
] ++ tgWsProxyRelayDirectRules;
blockRules = lib.flatten [
(mkDomainRule "block" r.block.domains)
@@ -215,6 +215,36 @@ let
right = perAppZapretCfg.filterMark;
message = "proxy-suite: singBox.tproxy.perApp.fwmark and zapret.perApp.filterMark must differ";
}
{
condition = tgWsProxyCfg.enable && tgWsProxyCfg.bypassTransparentProxy && globalTproxy.enable;
left = tgWsProxyCfg.routingMark;
right = globalTproxy.fwmark;
message = "proxy-suite: tgWsProxy.routingMark must differ from singBox.tproxy.fwmark";
}
{
condition = tgWsProxyCfg.enable && tgWsProxyCfg.bypassTransparentProxy && globalTproxy.enable;
left = tgWsProxyCfg.routingMark;
right = globalTproxy.proxyMark;
message = "proxy-suite: tgWsProxy.routingMark must differ from singBox.tproxy.proxyMark";
}
{
condition = tgWsProxyCfg.enable && tgWsProxyCfg.bypassTransparentProxy && perAppRoutingTun.enable;
left = tgWsProxyCfg.routingMark;
right = perAppRoutingTun.fwmark;
message = "proxy-suite: tgWsProxy.routingMark must differ from singBox.tun.perApp.fwmark";
}
{
condition = tgWsProxyCfg.enable && tgWsProxyCfg.bypassTransparentProxy && perAppRoutingTproxy.enable;
left = tgWsProxyCfg.routingMark;
right = perAppRoutingTproxy.fwmark;
message = "proxy-suite: tgWsProxy.routingMark must differ from singBox.tproxy.perApp.fwmark";
}
{
condition = tgWsProxyCfg.enable && tgWsProxyCfg.bypassTransparentProxy && perAppZapretCfg.enable;
left = tgWsProxyCfg.routingMark;
right = perAppZapretCfg.filterMark;
message = "proxy-suite: tgWsProxy.routingMark must differ from zapret.perApp.filterMark";
}
];
perAppZapretDesyncMarks = [
@@ -278,6 +308,11 @@ let
value = perAppZapretCfg.qnum;
message = "proxy-suite: zapret.perApp.qnum must be greater than zero";
}
{
condition = tgWsProxyCfg.enable && tgWsProxyCfg.bypassTransparentProxy;
value = tgWsProxyCfg.routingMark;
message = "proxy-suite: tgWsProxy.routingMark must be greater than zero";
}
];
localProxyAuthCfg = singBoxCfg.auth;
@@ -337,6 +372,12 @@ let
disallowed = perAppZapretDesyncMarks;
message = "proxy-suite: zapret.perApp.filterMark must not use per-app-zapret internal desync mark bits";
}
{
condition = tgWsProxyCfg.enable && tgWsProxyCfg.bypassTransparentProxy && perAppZapretCfg.enable;
value = tgWsProxyCfg.routingMark;
disallowed = perAppZapretDesyncMarks;
message = "proxy-suite: tgWsProxy.routingMark must not use per-app-zapret internal desync mark bits";
}
];
secretAssertions = [
+34 -3
View File
@@ -58,6 +58,31 @@ let
| PYTHONPATH="${parserScriptsPythonPath}" ${python3} ${fetchSubscriptionPy} --tag-prefix ${lib.escapeShellArg sub.tag}
'';
subscriptionCacheHelpersBlock = lib.optionalString hasSubscriptions ''
_proxy_suite_valid_subscription_cache() {
[ -s "$1" ] && ${jq} -e 'type == "array"' "$1" >/dev/null 2>&1
}
_proxy_suite_commit_subscription_cache() {
local tmp="$1" target="$2" tag="$3"
if _proxy_suite_valid_subscription_cache "$tmp"; then
mv "$tmp" "$target"
return 0
fi
rm -f "$tmp"
echo "proxy-suite: warning: subscription '$tag' produced an invalid cache; ignoring it" >&2
return 1
}
_proxy_suite_drop_invalid_subscription_cache() {
local target="$1" tag="$2"
if [ -f "$target" ] && ! _proxy_suite_valid_subscription_cache "$target"; then
rm -f "$target"
echo "proxy-suite: warning: removed invalid subscription cache for '$tag'" >&2
fi
}
'';
# Build the shell code block for a single outbound entry.
# tag overrides ob.tag (used in "first" mode to force tag = "proxy").
# routingMark is null or an int; added to outbound JSON if set.
@@ -106,10 +131,11 @@ let
# subscription: ${sub.tag}
CACHE_DIR="${subscriptionCacheDir}"
CACHE_FILE="${cacheFile}"
_proxy_suite_drop_invalid_subscription_cache "$CACHE_FILE" ${lib.escapeShellArg sub.tag}
if [ ! -f "$CACHE_FILE" ]; then
mkdir -p "$CACHE_DIR"
if ${mkSubscriptionFetchCommand sub} > "$CACHE_FILE.tmp"; then
mv "$CACHE_FILE.tmp" "$CACHE_FILE"
_proxy_suite_commit_subscription_cache "$CACHE_FILE.tmp" "$CACHE_FILE" ${lib.escapeShellArg sub.tag} || true
else
rm -f "$CACHE_FILE.tmp"
echo "proxy-suite: warning: could not fetch subscription '${sub.tag}'" >&2
@@ -233,6 +259,7 @@ let
ROUTE_RULES_JSON='[]'
ROUTE_MODE_ACTIVE=false
CLEAR_DNS_RULES=false
${subscriptionCacheHelpersBlock}
${lib.optionalString enableLocalProxyAuth ''
umask 077
LOCAL_PROXY_PASSWORD="$(cat "${localProxyAuthPasswordSource}")"
@@ -364,8 +391,11 @@ let
in
''
if ${mkSubscriptionFetchCommand sub} > "${cacheFile}.tmp"; then
mv "${cacheFile}.tmp" "${cacheFile}"
echo "Updated subscription: ${sub.tag}"
if _proxy_suite_commit_subscription_cache "${cacheFile}.tmp" "${cacheFile}" ${lib.escapeShellArg sub.tag}; then
echo "Updated subscription: ${sub.tag}"
else
FAILED=1
fi
else
rm -f "${cacheFile}.tmp"
echo "proxy-suite: failed to update subscription '${sub.tag}'" >&2
@@ -378,6 +408,7 @@ let
CACHE_DIR="${subscriptionCacheDir}"
mkdir -p "$CACHE_DIR"
FAILED=0
${subscriptionCacheHelpersBlock}
${lib.concatMapStrings mkSubscriptionFetchBlock singBoxCfg.subscriptions}
if [ "$FAILED" -eq 0 ]; then
+32
View File
@@ -9,6 +9,13 @@
let
t = cfg.tgWsProxy;
tgPkg = packages.tg-ws-proxy;
ip = "${pkgs.iproute2}/bin/ip";
transparentBypassEnabled =
t.bypassTransparentProxy
&& cfg.singBox.enable
&& (cfg.singBox.tun.enable || cfg.singBox.tproxy.enable);
bypassRulePriority = 8999;
dcArgs = lib.concatMapStrings
(id: " --dc-ip=${lib.escapeShellArg "${id}:${t.dcIps.${id}}"}")
@@ -24,6 +31,26 @@ let
"--secret=${lib.escapeShellArg t.secret}"
}${dcArgs}
'';
bypassUpScript = pkgs.writeShellScript "proxy-suite-tg-ws-proxy-bypass-up" ''
set -euo pipefail
add_bypass_rule() {
local family="$1"
while ${ip} "$family" rule del pref ${toString bypassRulePriority} fwmark ${toString t.routingMark} lookup main 2>/dev/null; do :; done
${ip} "$family" rule add pref ${toString bypassRulePriority} fwmark ${toString t.routingMark} lookup main 2>/dev/null || true
}
add_bypass_rule -4
add_bypass_rule -6
'';
bypassDownScript = pkgs.writeShellScript "proxy-suite-tg-ws-proxy-bypass-down" ''
set +e
while ${ip} -4 rule del pref ${toString bypassRulePriority} fwmark ${toString t.routingMark} lookup main 2>/dev/null; do :; done
while ${ip} -6 rule del pref ${toString bypassRulePriority} fwmark ${toString t.routingMark} lookup main 2>/dev/null; do :; done
'';
in
{
systemd.services.proxy-suite-tg-ws-proxy = {
@@ -35,6 +62,11 @@ in
LoadCredential = lib.optional (t.secretFile != null) "tg_ws_proxy_secret:${t.secretFile}";
Restart = "on-failure";
RestartSec = 5;
}
// lib.optionalAttrs transparentBypassEnabled {
ExecStartPre = bypassUpScript;
ExecStopPost = bypassDownScript;
SocketMark = toString t.routingMark;
};
};
}