fix; xray tun
This commit is contained in:
3 files changed
+142
-10
No files matched your search
@@ -14,14 +14,55 @@
|
||||
}:
|
||||
|
||||
let
|
||||
mkGlobalBypassScript =
|
||||
filterMark:
|
||||
pkgs.writeText "proxy-suite-zapret-global-bypass.sh" ''
|
||||
mkBypassScript =
|
||||
{
|
||||
filterMark ? null,
|
||||
tunInterfaces ? [ ],
|
||||
scope ? "global",
|
||||
}:
|
||||
pkgs.writeText "proxy-suite-zapret-bypass.sh" ''
|
||||
# Upstream presets can select iptables explicitly, in which case the nft
|
||||
# hook below is never called. Use upstream helpers for idempotent start
|
||||
# and stop, and distinct comments so each service owns its exemptions.
|
||||
zapret_custom_firewall() {
|
||||
local family rule_helper chain
|
||||
for family in 4 6; do
|
||||
if [ "$family" = 4 ]; then
|
||||
[ "''${DISABLE_IPV4:-0}" != 1 ] || continue
|
||||
rule_helper=ipt_add_del
|
||||
else
|
||||
[ "''${DISABLE_IPV6:-0}" != 1 ] || continue
|
||||
rule_helper=ipt6_add_del
|
||||
fi
|
||||
${lib.optionalString (filterMark != null) ''
|
||||
for chain in POSTROUTING INPUT FORWARD; do
|
||||
"$rule_helper" "$1" "$chain" -t mangle -m mark ! --mark 0/${filterMark} -m comment --comment "proxy-suite ${scope} per-app-zapret bypass" -j RETURN
|
||||
done
|
||||
''}
|
||||
${lib.concatMapStrings (interface: ''
|
||||
"$rule_helper" "$1" POSTROUTING -t mangle -o ${lib.escapeShellArg interface} -m comment --comment "proxy-suite ${scope} TUN bypass" -j RETURN
|
||||
"$rule_helper" "$1" INPUT -t mangle -i ${lib.escapeShellArg interface} -m comment --comment "proxy-suite ${scope} TUN bypass" -j RETURN
|
||||
"$rule_helper" "$1" FORWARD -t mangle -i ${lib.escapeShellArg interface} -m comment --comment "proxy-suite ${scope} TUN bypass" -j RETURN
|
||||
'') tunInterfaces}
|
||||
done
|
||||
return 0
|
||||
}
|
||||
|
||||
zapret_custom_firewall_nft() {
|
||||
nft insert rule inet $ZAPRET_NFT_TABLE postrouting mark and ${filterMark} != 0 return comment "proxy-suite per-app-zapret bypass"
|
||||
nft insert rule inet $ZAPRET_NFT_TABLE postnat mark and ${filterMark} != 0 return comment "proxy-suite per-app-zapret bypass"
|
||||
nft insert rule inet $ZAPRET_NFT_TABLE prerouting mark and ${filterMark} != 0 return comment "proxy-suite per-app-zapret bypass"
|
||||
nft insert rule inet $ZAPRET_NFT_TABLE prenat mark and ${filterMark} != 0 return comment "proxy-suite per-app-zapret bypass"
|
||||
${lib.optionalString (filterMark != null) ''
|
||||
nft insert rule inet $ZAPRET_NFT_TABLE postrouting mark and ${filterMark} != 0 return comment '"proxy-suite per-app-zapret bypass"'
|
||||
nft insert rule inet $ZAPRET_NFT_TABLE postnat mark and ${filterMark} != 0 return comment '"proxy-suite per-app-zapret bypass"'
|
||||
nft insert rule inet $ZAPRET_NFT_TABLE prerouting mark and ${filterMark} != 0 return comment '"proxy-suite per-app-zapret bypass"'
|
||||
nft insert rule inet $ZAPRET_NFT_TABLE prenat mark and ${filterMark} != 0 return comment '"proxy-suite per-app-zapret bypass"'
|
||||
''}
|
||||
# Desync packets must reach the physical network, never a TUN userspace
|
||||
# TCP stack: fake TLS payloads there can break the proxied handshake.
|
||||
${lib.concatMapStrings (interface: ''
|
||||
nft insert rule inet $ZAPRET_NFT_TABLE postrouting oifname ${lib.escapeShellArg (builtins.toJSON interface)} return comment '"proxy-suite TUN bypass"'
|
||||
nft insert rule inet $ZAPRET_NFT_TABLE postnat oifname ${lib.escapeShellArg (builtins.toJSON interface)} return comment '"proxy-suite TUN bypass"'
|
||||
nft insert rule inet $ZAPRET_NFT_TABLE prerouting iifname ${lib.escapeShellArg (builtins.toJSON interface)} return comment '"proxy-suite TUN bypass"'
|
||||
nft insert rule inet $ZAPRET_NFT_TABLE prenat iifname ${lib.escapeShellArg (builtins.toJSON interface)} return comment '"proxy-suite TUN bypass"'
|
||||
'') tunInterfaces}
|
||||
}
|
||||
'';
|
||||
|
||||
@@ -227,6 +268,6 @@ in
|
||||
{
|
||||
inherit
|
||||
mkDerivedZapretPackage
|
||||
mkGlobalBypassScript
|
||||
mkBypassScript
|
||||
;
|
||||
}
|
||||
@@ -9,6 +9,10 @@
|
||||
let
|
||||
zapretCfg = cfg.zapret;
|
||||
perAppZapretCfg = zapretCfg.perApp;
|
||||
tunInterfaces = lib.unique (
|
||||
lib.optional (cfg.proxy.enable && cfg.proxy.tun.enable) cfg.proxy.tun.interface
|
||||
++ lib.optional (cfg.proxy.enable && cfg.proxy.tun.perApp.enable) cfg.proxy.tun.perApp.interface
|
||||
);
|
||||
zapretDomainGroups = import ../zapret-domain-groups.nix { inherit lib zapret; };
|
||||
|
||||
runtimeDeps = lib.attrValues {
|
||||
@@ -100,7 +104,7 @@ let
|
||||
};
|
||||
inherit (packageBuilder)
|
||||
mkDerivedZapretPackage
|
||||
mkGlobalBypassScript
|
||||
mkBypassScript
|
||||
;
|
||||
|
||||
globalZapretPackage = mkDerivedZapretPackage {
|
||||
@@ -109,7 +113,13 @@ let
|
||||
gameFilter = zapretCfg.gameFilter;
|
||||
forceDisableFilterMark = true;
|
||||
customScript =
|
||||
if perAppZapretCfg.enable then mkGlobalBypassScript (toString perAppZapretCfg.filterMark) else null;
|
||||
if perAppZapretCfg.enable || tunInterfaces != [ ] then
|
||||
mkBypassScript {
|
||||
inherit tunInterfaces;
|
||||
filterMark = if perAppZapretCfg.enable then toString perAppZapretCfg.filterMark else null;
|
||||
}
|
||||
else
|
||||
null;
|
||||
};
|
||||
|
||||
perAppZapretPackage = mkDerivedZapretPackage {
|
||||
@@ -122,6 +132,14 @@ let
|
||||
desyncMark = 134217728;
|
||||
desyncMarkPostnat = 67108864;
|
||||
nftTable = "proxy_suite_per_app_zapret";
|
||||
customScript =
|
||||
if tunInterfaces != [ ] then
|
||||
mkBypassScript {
|
||||
inherit tunInterfaces;
|
||||
scope = "per-app";
|
||||
}
|
||||
else
|
||||
null;
|
||||
};
|
||||
|
||||
mkZapretEnv = package: [
|
||||
|
||||
@@ -60,6 +60,28 @@ let
|
||||
perAppRoutingZapretWithoutGlobalProfiles = perAppRoutingZapretWithoutGlobal.profiles;
|
||||
perAppRoutingZapretWithoutGlobalPerAppBase = mkZapretBaseFor perAppRoutingZapretWithoutGlobalFixture "proxy-suite-per-app-zapret";
|
||||
|
||||
mkTunFixture =
|
||||
perAppZapret:
|
||||
evalProxySuite [
|
||||
baseModule
|
||||
{
|
||||
services.proxy-suite = {
|
||||
proxy.tun = {
|
||||
enable = true;
|
||||
interface = "test-global0";
|
||||
perApp.enable = true;
|
||||
perApp.interface = "test-app0";
|
||||
};
|
||||
zapret.enable = true;
|
||||
zapret.perApp.enable = perAppZapret;
|
||||
};
|
||||
}
|
||||
];
|
||||
tunFixture = mkTunFixture true;
|
||||
tunGlobalBase = mkZapretBase tunFixture;
|
||||
tunPerAppBase = mkZapretBaseFor tunFixture "proxy-suite-per-app-zapret";
|
||||
tunWithoutPerAppZapretBase = mkZapretBase (mkTunFixture false);
|
||||
|
||||
runtime = pkgs.runCommand "proxy-suite-per-app-zapret-runtime-check" { } ''
|
||||
global_config=${perAppRoutingZapretBase}/config
|
||||
per_app_config=${perAppRoutingPerAppZapretBase}/config
|
||||
@@ -82,6 +104,57 @@ let
|
||||
grep -F 'proxy-suite per-app-zapret bypass' "$custom_script"
|
||||
grep -F 'mark and 268435456 != 0 return' "$custom_script"
|
||||
|
||||
# Execute the installed hooks: TUN exclusions must work with and without
|
||||
# per-app zapret, while preserving the global per-app mark exemption.
|
||||
nft() { printf '%s\n' "$*"; }
|
||||
ipt_add_del() { printf '4 %s\n' "$*"; }
|
||||
ipt6_add_del() { printf '6 %s\n' "$*"; }
|
||||
export ZAPRET_NFT_TABLE=test_zapret
|
||||
for base in ${tunGlobalBase} ${tunPerAppBase} ${tunWithoutPerAppZapretBase}; do
|
||||
source "$base/init.d/sysv/custom.d/50-proxy-suite-custom.sh"
|
||||
zapret_custom_firewall_nft > rules
|
||||
for interface in test-global0 test-app0; do
|
||||
for chain in postrouting postnat; do
|
||||
grep -Fx "insert rule inet test_zapret $chain oifname \"$interface\" return comment \"proxy-suite TUN bypass\"" rules
|
||||
done
|
||||
for chain in prerouting prenat; do
|
||||
grep -Fx "insert rule inet test_zapret $chain iifname \"$interface\" return comment \"proxy-suite TUN bypass\"" rules
|
||||
done
|
||||
done
|
||||
if [ "$base" = ${tunGlobalBase} ]; then
|
||||
test "$(wc -l < rules)" -eq 12
|
||||
test "$(grep -c 'mark and 268435456 != 0 return' rules)" -eq 4
|
||||
else
|
||||
test "$(wc -l < rules)" -eq 8
|
||||
fi
|
||||
|
||||
# The upstream FWTYPE=iptables preset dispatches this hook instead of
|
||||
# zapret_custom_firewall_nft. Check both address families and cleanup.
|
||||
scope=global
|
||||
if [ "$base" = ${tunPerAppBase} ]; then scope=per-app; fi
|
||||
for action in 1 0; do
|
||||
zapret_custom_firewall "$action" > ipt-rules
|
||||
for family in 4 6; do
|
||||
for interface in test-global0 test-app0; do
|
||||
grep -Fx "$family $action POSTROUTING -t mangle -o $interface -m comment --comment proxy-suite $scope TUN bypass -j RETURN" ipt-rules
|
||||
for chain in INPUT FORWARD; do
|
||||
grep -Fx "$family $action $chain -t mangle -i $interface -m comment --comment proxy-suite $scope TUN bypass -j RETURN" ipt-rules
|
||||
done
|
||||
done
|
||||
done
|
||||
if [ "$base" = ${tunGlobalBase} ]; then
|
||||
test "$(wc -l < ipt-rules)" -eq 18
|
||||
test "$(grep -c -- '-m mark ! --mark 0/268435456' ipt-rules)" -eq 6
|
||||
else
|
||||
test "$(wc -l < ipt-rules)" -eq 12
|
||||
fi
|
||||
done
|
||||
DISABLE_IPV6=1 zapret_custom_firewall 1 > ipt-rules
|
||||
! grep '^6 ' ipt-rules
|
||||
DISABLE_IPV4=1 zapret_custom_firewall 1 > ipt-rules
|
||||
! grep '^4 ' ipt-rules
|
||||
done
|
||||
|
||||
touch "$out"
|
||||
'';
|
||||
in
|
||||
|
||||
Reference in new issue
Block a user