From 845291ee4934693b7bcd42395efe2880889a8e0c Mon Sep 17 00:00:00 2001 From: FUFSoB Date: Sun, 27 Sep 2026 13:34:24 +0500 Subject: [PATCH] fix: tun and tproxy+zapret regressions --- .../per-app-routing/backend-scripts.nix | 3 +++ .../proxy-suite/service/routing-scripts.nix | 3 +++ modules/proxy-suite/zapret/common.nix | 5 +++++ nix/checks/per-app-routing-tun.nix | 1 + nix/checks/per-app-routing-zapret.nix | 19 +++++++++++++++++++ .../xray-backend-runtime/tun/global.nix | 1 + .../xray-backend-runtime/tun/per-app.nix | 1 + 7 files changed, 33 insertions(+) diff --git a/modules/proxy-suite/service/per-app-routing/backend-scripts.nix b/modules/proxy-suite/service/per-app-routing/backend-scripts.nix index 01cd239..616798e 100644 --- a/modules/proxy-suite/service/per-app-routing/backend-scripts.nix +++ b/modules/proxy-suite/service/per-app-routing/backend-scripts.nix @@ -75,6 +75,9 @@ let }} ${nft} -f ${perAppTunChainFile} ${perAppTunWaitForInterface} + # The backend may not have brought the link up yet, and routes via a down link fail + # with "Device for nexthop is not up". + ${ip} link set dev ${lib.escapeShellArg perAppRoutingTun.interface} up tun_addr="''${tun_cidr%%/*}" tun_route_prefix="$(cidr_network "$tun_cidr")" ${ip} -4 addr replace "$tun_cidr" dev ${lib.escapeShellArg perAppRoutingTun.interface} diff --git a/modules/proxy-suite/service/routing-scripts.nix b/modules/proxy-suite/service/routing-scripts.nix index dd496b2..59e824f 100644 --- a/modules/proxy-suite/service/routing-scripts.nix +++ b/modules/proxy-suite/service/routing-scripts.nix @@ -109,6 +109,9 @@ in ${deleteXrayTunReplyTable} ${nft} -f ${xrayTunReplyRules} + # XRay may not have brought the link up yet, and routes via a down link fail with + # "Device for nexthop is not up". + ${ip} link set dev ${lib.escapeShellArg globalTun.interface} up tun_addr="''${tun_cidr%%/*}" tun_route_prefix="$(cidr_network "$tun_cidr")" diff --git a/modules/proxy-suite/zapret/common.nix b/modules/proxy-suite/zapret/common.nix index 9891b53..d8b5045 100644 --- a/modules/proxy-suite/zapret/common.nix +++ b/modules/proxy-suite/zapret/common.nix @@ -21,6 +21,11 @@ in tunInterfaces = lib.unique ( lib.optional (cfg.proxy.enable && cfg.proxy.tun.enable) cfg.proxy.tun.interface ++ lib.optional (cfg.proxy.enable && cfg.perAppRouting.tun.enable) cfg.perAppRouting.tun.interface + # TProxy reroutes local apps' packets out lo to the backend's listener; desync there + # corrupts the handshake the backend reads, like on a TUN. + ++ lib.optional ( + cfg.proxy.enable && (cfg.proxy.tproxy.enable || cfg.perAppRouting.tproxy.enable) + ) "lo" ); # The transparent backends steer the same packets; a per-app zapret instance replaces them. diff --git a/nix/checks/per-app-routing-tun.nix b/nix/checks/per-app-routing-tun.nix index 72d012b..9f71843 100644 --- a/nix/checks/per-app-routing-tun.nix +++ b/nix/checks/per-app-routing-tun.nix @@ -148,6 +148,7 @@ in assert perAppRoutingTunFixture.config.systemd.user.services ? "proxy-suite-per-app-tun-anchor"; assert perAppRoutingTunServiceConfig.ExecStartPre == perAppRoutingTunServiceConfig.ExecStopPost; # With proxy.ipv6, wrapped apps' IPv6 goes through the app TUN too. + assert pkgs.lib.hasInfix "link set dev psperapptun0 up" perAppRoutingTunUpScript; assert pkgs.lib.hasInfix ''-6 addr replace "$tun6_cidr" dev psperapptun0'' perAppRoutingTunUpScript; assert pkgs.lib.hasInfix "-6 route replace default dev psperapptun0 table 101" perAppRoutingTunUpScript; diff --git a/nix/checks/per-app-routing-zapret.nix b/nix/checks/per-app-routing-zapret.nix index 00710f6..b82a823 100644 --- a/nix/checks/per-app-routing-zapret.nix +++ b/nix/checks/per-app-routing-zapret.nix @@ -84,6 +84,18 @@ let tunGlobalBase = mkZapretBase tunFixture; tunPerAppBase = mkZapretBaseFor tunFixture "proxy-suite-per-app-zapret"; tunWithoutPerAppZapretBase = mkZapretBase (mkTunFixture false); + tproxyBase = mkZapretBase (evalProxySuite [ + baseModule + { + services.proxy-suite = { + perAppRouting = { + enable = true; + tproxy.enable = true; + }; + zapret.enable = true; + }; + } + ]); runtime = pkgs.runCommand "proxy-suite-per-app-zapret-runtime-check" { } '' global_config=${perAppRoutingZapretBase}/config @@ -158,6 +170,13 @@ let ! grep '^4 ' ipt-rules done + # TProxy reroutes local apps' packets out lo; desync must stay off it. + source ${tproxyBase}/init.d/sysv/custom.d/50-proxy-suite-custom.sh + zapret_custom_firewall_nft > rules + grep -Fx 'insert rule inet test_zapret postrouting oifname "lo" return comment "proxy-suite TUN bypass"' rules + zapret_custom_firewall 1 > ipt-rules + grep -Fx '4 1 POSTROUTING -t mangle -o lo -m comment --comment proxy-suite global TUN bypass -j RETURN' ipt-rules + touch "$out" ''; in diff --git a/nix/checks/xray-backend-runtime/tun/global.nix b/nix/checks/xray-backend-runtime/tun/global.nix index 982a633..02bd5ab 100644 --- a/nix/checks/xray-backend-runtime/tun/global.nix +++ b/nix/checks/xray-backend-runtime/tun/global.nix @@ -78,6 +78,7 @@ assert pkgs.lib.hasInfix "xray_tun_dns_runtime" xrayTunStartScript; assert pkgs.lib.hasInfix ''tun_route_prefix="$(cidr_network "$tun_cidr")"'' xrayTunUpScript; assert pkgs.lib.hasInfix checkConstants.globalTunIPv6Address xrayTunUpScript; + assert pkgs.lib.hasInfix "link set dev singtun0 up" xrayTunUpScript; assert pkgs.lib.hasInfix ''addr replace "$tun_cidr" dev singtun0'' xrayTunUpScript; assert pkgs.lib.hasInfix ''-6 addr replace "$tun6_cidr" dev singtun0'' xrayTunUpScript; assert pkgs.lib.hasInfix diff --git a/nix/checks/xray-backend-runtime/tun/per-app.nix b/nix/checks/xray-backend-runtime/tun/per-app.nix index 35ce9b8..faa3f2b 100644 --- a/nix/checks/xray-backend-runtime/tun/per-app.nix +++ b/nix/checks/xray-backend-runtime/tun/per-app.nix @@ -39,6 +39,7 @@ assert pkgs.lib.hasInfix ''tun_route_prefix="$(cidr_network "$tun_cidr")"'' xrayPerAppTunUpScript; assert pkgs.lib.hasInfix checkConstants.perAppTunIPv6Address xrayPerAppTunUpScript; assert !(pkgs.lib.hasInfix "uplink_addr" xrayPerAppTunUpScript); + assert pkgs.lib.hasInfix "link set dev psperapptun0 up" xrayPerAppTunUpScript; assert pkgs.lib.hasInfix ''addr replace "$tun_cidr" dev psperapptun0'' xrayPerAppTunUpScript; assert pkgs.lib.hasInfix ''-6 addr replace "$tun6_cidr" dev psperapptun0'' xrayPerAppTunUpScript; assert pkgs.lib.hasInfix