From c3f4088458ff320064691cab9ee16c071bb76729 Mon Sep 17 00:00:00 2001 From: FUFSoB Date: Tue, 14 Apr 2026 18:40:03 +0500 Subject: [PATCH] per-app tproxy --- modules/proxy-suite/default.nix | 2 +- modules/proxy-suite/nftables.nix | 51 ++++++- modules/proxy-suite/options.nix | 53 ++++++- modules/proxy-suite/service.nix | 234 ++++++++++++++++++++++++++++--- nix/checks.nix | 168 ++++++++++++++++++++++ 5 files changed, 482 insertions(+), 26 deletions(-) diff --git a/modules/proxy-suite/default.nix b/modules/proxy-suite/default.nix index 0d93864..74147d7 100644 --- a/modules/proxy-suite/default.nix +++ b/modules/proxy-suite/default.nix @@ -50,7 +50,7 @@ in cfg ; inherit (configs) tproxyFile tunFile appTunFile; - inherit (nftr) nftablesRulesFile ip nft; + inherit (nftr) nftablesRulesFile appTproxyRulesFile ip nft; } )) diff --git a/modules/proxy-suite/nftables.nix b/modules/proxy-suite/nftables.nix index 3d3e31f..21a2e4e 100644 --- a/modules/proxy-suite/nftables.nix +++ b/modules/proxy-suite/nftables.nix @@ -8,11 +8,15 @@ let sb = cfg.singBox; art = cfg.appRouting.backends.tun; + artp = cfg.appRouting.backends.tproxy; - localSubnetLines = lib.concatMapStrings (cidr: '' + mkLocalSubnetLines = subnets: lib.concatMapStrings (cidr: '' ip daddr ${cidr} tcp dport != 53 return ip daddr ${cidr} udp dport != 53 return - '') sb.tproxy.localSubnets; + '') subnets; + + tproxyLocalSubnetLines = mkLocalSubnetLines sb.tproxy.localSubnets; + appTproxyLocalSubnetLines = mkLocalSubnetLines artp.localSubnets; nftablesRulesFile = pkgs.writeText "proxy-suite-tproxy.nft" '' define RESERVED_IP = { @@ -34,25 +38,60 @@ let # Packets re-entering via loopback after output marking should not # be skipped just because the host has an RFC1918 source address. iifname != "lo" ip saddr $RESERVED_IP return - ${localSubnetLines} + ${tproxyLocalSubnetLines} ip protocol tcp tproxy to 127.0.0.1:${toString sb.tproxyPort} meta mark set ${toString sb.fwmark} ip protocol udp tproxy to 127.0.0.1:${toString sb.tproxyPort} meta mark set ${toString sb.fwmark} } chain output { type route hook output priority mangle; policy accept; ip daddr $RESERVED_IP return - ${localSubnetLines} + ${tproxyLocalSubnetLines} meta mark ${toString sb.proxyMark} return -${lib.optionalString art.enable " meta mark ${toString art.fwmark} return\n"} ip protocol tcp meta mark set ${toString sb.fwmark} +${lib.optionalString art.enable " meta mark ${toString art.fwmark} return\n"}${lib.optionalString artp.enable " meta mark ${toString artp.fwmark} return\n"} ip protocol tcp meta mark set ${toString sb.fwmark} ip protocol udp meta mark set ${toString sb.fwmark} } } ''; + appTproxyRulesFile = pkgs.writeText "proxy-suite-app-tproxy.nft" '' + define RESERVED_IP = { + 10.0.0.0/8, + 100.64.0.0/10, + 127.0.0.0/8, + 169.254.0.0/16, + 172.16.0.0/12, + 192.0.0.0/24, + 224.0.0.0/4, + 240.0.0.0/4, + 255.255.255.255/32 + } + + table ip proxy_suite_app_tproxy { + chain prerouting { + type filter hook prerouting priority mangle; policy accept; + ip daddr $RESERVED_IP return + iifname != "lo" ip saddr $RESERVED_IP return + ${appTproxyLocalSubnetLines} + ct mark ${toString artp.fwmark} meta mark set ${toString artp.fwmark} + meta mark ${toString artp.fwmark} ip protocol tcp tproxy to 127.0.0.1:${toString sb.tproxyPort} + meta mark ${toString artp.fwmark} ip protocol udp tproxy to 127.0.0.1:${toString sb.tproxyPort} + } + + chain output { + type route hook output priority mangle; policy accept; + ip daddr $RESERVED_IP return + ${appTproxyLocalSubnetLines} + meta mark ${toString sb.proxyMark} return +${lib.optionalString art.enable " meta mark ${toString art.fwmark} return\n"} ct mark ${toString artp.fwmark} meta mark set ${toString artp.fwmark} + meta mark ${toString artp.fwmark} return + } + } + ''; + ip = "${pkgs.iproute2}/bin/ip"; nft = "${pkgs.nftables}/bin/nft"; in { - inherit nftablesRulesFile ip nft; + inherit nftablesRulesFile appTproxyRulesFile ip nft; } diff --git a/modules/proxy-suite/options.nix b/modules/proxy-suite/options.nix index acdd964..95c3f55 100644 --- a/modules/proxy-suite/options.nix +++ b/modules/proxy-suite/options.nix @@ -300,6 +300,7 @@ let "direct" "proxychains" "tun" + "tproxy" ]; default = "proxychains"; description = '' @@ -310,6 +311,9 @@ let local proxy-suite mixed SOCKS endpoint. - "tun": launch the command in the dedicated app-routing TUN slice so only that app's traffic is policy-routed into the app TUN backend. + - "tproxy": launch the command in the dedicated app-routing TProxy + slice so only that app's traffic is transparently intercepted by + the local sing-box TProxy inbound. Additional route backends may be added in the future. ''; @@ -820,10 +824,13 @@ in Current curated defaults: - `proxychains`: route = "proxychains" - `tun`: route = "tun" when appRouting.backends.tun.enable = true + - `tproxy`: route = "tproxy" when appRouting.backends.tproxy.enable = true This makes `proxy-ctl wrap proxychains -- ` available without defining the profile manually, and similarly exposes - `proxy-ctl wrap tun -- ` when the app TUN backend is enabled. + `proxy-ctl wrap tun -- ` or + `proxy-ctl wrap tproxy -- ` when the corresponding backend + is enabled. ''; example = true; }; @@ -840,10 +847,13 @@ in instead of global TUN or TProxy interception - "tun" for app-scoped policy routing into the dedicated app TUN backend + - "tproxy" for app-scoped transparent interception through the + dedicated app TProxy backend proxychains-based wrapping depends on the local proxy-suite mixed proxy listener provided by sing-box. The "tun" route depends on - appRouting.backends.tun.enable = true. + appRouting.backends.tun.enable = true. The "tproxy" route depends on + appRouting.backends.tproxy.enable = true. When createDefaultProfiles = true, curated defaults are added on top of this list unless a user-defined profile already uses the same @@ -951,6 +961,42 @@ in ]; }; }; + + tproxy = { + enable = mkEnableOption "app-scoped TProxy backend for appRouting profiles"; + + fwmark = mkOption { + type = types.int; + default = 17; + description = '' + Packet mark used to steer wrapped app traffic into the app-routing + TProxy policy-routing table. + ''; + example = 17; + }; + + routeTable = mkOption { + type = types.int; + default = 102; + description = '' + Policy-routing table used by the app-routing TProxy backend. + ''; + example = 102; + }; + + localSubnets = mkOption { + type = types.listOf types.str; + default = [ "192.168.0.0/16" ]; + description = '' + Destination subnets that should bypass app-routing TProxy + interception, except DNS traffic on port 53. + ''; + example = [ + "192.168.0.0/16" + "10.0.0.0/8" + ]; + }; + }; }; userControl = { @@ -960,7 +1006,8 @@ in description = '' Local group allowed to start and stop app-routing backend units via polkit. Add desktop users who should be able to run - `proxy-ctl wrap ...` for route = "tun" profiles to this group. + `proxy-ctl wrap ...` for route = "tun" or route = "tproxy" + profiles to this group. ''; example = "proxy-suite"; }; diff --git a/modules/proxy-suite/service.nix b/modules/proxy-suite/service.nix index 9c5f0ab..fe21a56 100644 --- a/modules/proxy-suite/service.nix +++ b/modules/proxy-suite/service.nix @@ -8,6 +8,7 @@ tunFile, appTunFile, nftablesRulesFile, + appTproxyRulesFile, ip, nft, }: @@ -17,6 +18,7 @@ let t = cfg.tgWsProxy; ar = cfg.appRouting; art = ar.backends.tun; + artp = ar.backends.tproxy; builtinTags = [ "proxy" "direct" @@ -270,6 +272,11 @@ let name = "tun"; route = "tun"; } + ] ++ lib.optionals artp.enable [ + { + name = "tproxy"; + route = "tproxy"; + } ]; effectiveAppRoutingProfiles = ar.profiles @@ -291,7 +298,9 @@ let proxychainsQuietArg = lib.optionalString ar.proxychains.quiet "-q "; hasProxychainsProfiles = builtins.any (profile: profile.route == "proxychains") effectiveAppRoutingProfiles; hasTunProfiles = builtins.any (profile: profile.route == "tun") effectiveAppRoutingProfiles; + hasTproxyProfiles = builtins.any (profile: profile.route == "tproxy") effectiveAppRoutingProfiles; appTunSliceName = "proxy-suite-app-tun.slice"; + appTproxySliceName = "proxy-suite-app-tproxy.slice"; appTunChainFile = pkgs.writeText "proxy-suite-app-tun-chain.nft" '' define RESERVED_IP = { 10.0.0.0/8, @@ -391,6 +400,68 @@ ${lib.concatMapStrings (cidr: '' done <<< "$handles" fi ''; + appTproxyUpScript = pkgs.writeShellScript "proxy-suite-app-tproxy-up" '' + set -euo pipefail + ${nft} delete table ip proxy_suite_app_tproxy 2>/dev/null || true + ${nft} -f ${appTproxyRulesFile} + ${ip} route replace local default dev lo table ${toString artp.routeTable} + ${ip} rule add fwmark ${toString artp.fwmark} table ${toString artp.routeTable} 2>/dev/null || true + ''; + appTproxyDownScript = pkgs.writeShellScript "proxy-suite-app-tproxy-down" '' + set -euo pipefail + ${nft} delete table ip proxy_suite_app_tproxy 2>/dev/null || true + ${ip} route del local default dev lo table ${toString artp.routeTable} 2>/dev/null || true + ${ip} rule del fwmark ${toString artp.fwmark} table ${toString artp.routeTable} 2>/dev/null || true + ''; + appTproxyUserRuleStart = + pkgs.writeShellScript "proxy-suite-app-tproxy-user-start" '' + set -euo pipefail + uid="$1" + rule_comment_prefix="proxy-suite-app-tproxy-user-$uid" + mark_comment="$rule_comment_prefix-mark" + cgroup_root="/sys/fs/cgroup/user.slice/user-$uid.slice/user@$uid.service" + if ! [ -d "$cgroup_root" ]; then + echo "proxy-suite: user cgroup root does not exist for uid $uid: $cgroup_root" >&2 + exit 1 + fi + + cgroup_dir=$(${findBin} "$cgroup_root" -type d -name ${lib.escapeShellArg appTproxySliceName} | ${headBin} -n1 || true) + if [ -z "$cgroup_dir" ]; then + echo "proxy-suite: app TProxy slice cgroup does not exist for uid $uid under $cgroup_root" >&2 + exit 1 + fi + cgroup_path=''${cgroup_dir#/sys/fs/cgroup/} + cgroup_level=$(printf '%s' "$cgroup_path" | ${awk} -F/ '{ print NF }') + + handles=$(${nft} -a list chain ip proxy_suite_app_tproxy output 2>/dev/null \ + | ${grepBin} -F "comment \"$rule_comment_prefix" \ + | ${awk} '{ print $NF }' || true) + if [ -n "$handles" ]; then + while IFS= read -r handle; do + [ -n "$handle" ] || continue + ${nft} delete rule ip proxy_suite_app_tproxy output handle "$handle" || true + done <<< "$handles" + fi + + printf '%s\n' \ + "add rule ip proxy_suite_app_tproxy output socket cgroupv2 level $cgroup_level \"$cgroup_path\" meta mark set ${toString artp.fwmark} ct mark set ${toString artp.fwmark} comment \"$mark_comment\"" \ + | ${nft} -f - + ''; + appTproxyUserRuleStop = + pkgs.writeShellScript "proxy-suite-app-tproxy-user-stop" '' + set -euo pipefail + uid="$1" + rule_comment_prefix="proxy-suite-app-tproxy-user-$uid" + handles=$(${nft} -a list chain ip proxy_suite_app_tproxy output 2>/dev/null \ + | ${grepBin} -F "comment \"$rule_comment_prefix" \ + | ${awk} '{ print $NF }' || true) + if [ -n "$handles" ]; then + while IFS= read -r handle; do + [ -n "$handle" ] || continue + ${nft} delete rule ip proxy_suite_app_tproxy output handle "$handle" || true + done <<< "$handles" + fi + ''; proxyCtl = pkgs.writeShellApplication { name = "proxy-ctl"; @@ -411,10 +482,13 @@ ${lib.concatMapStrings (cidr: '' APP_ROUTING_ENABLED="${if ar.enable then "1" else "0"}" APP_ROUTING_PROXYCHAINS_ENABLED="${if ar.proxychains.enable then "1" else "0"}" APP_ROUTING_TUN_ENABLED="${if art.enable then "1" else "0"}" + APP_ROUTING_TPROXY_ENABLED="${if artp.enable then "1" else "0"}" APP_ROUTING_PROFILES_FILE="${appRoutingProfilesFile}" PROXYCHAINS_CONFIG="${proxychainsConfigFile}" APP_TUN_SLICE_BASE="proxy-suite-app-tun" APP_TUN_ANCHOR_UNIT="proxy-suite-app-tun-anchor.service" + APP_TPROXY_SLICE_BASE="proxy-suite-app-tproxy" + APP_TPROXY_ANCHOR_UNIT="proxy-suite-app-tproxy-anchor.service" ALL_SERVICES=( proxy-suite-socks @@ -475,6 +549,16 @@ ${lib.concatMapStrings (cidr: '' | ${grepBin} -q . } + _tproxy_scope_running() { + ${systemctl} --user list-units --type=scope --state=running --plain --no-legend 'proxy-suite-app-tproxy-*' \ + | ${grepBin} -q . + } + + _any_app_tproxy_user_active() { + ${systemctl} list-units --type=service --state=active --plain --no-legend 'proxy-suite-app-tproxy-user@*.service' \ + | ${grepBin} -q . + } + cmd="''${1:-status}" shift || true @@ -621,6 +705,46 @@ ${lib.concatMapStrings (cidr: '' fi fi + exit "$status" + ;; + tproxy) + if [ "$APP_ROUTING_TPROXY_ENABLED" != "1" ]; then + echo "Profile '$profile' uses route=tproxy, but appRouting.backends.tproxy.enable is false." + exit 1 + fi + if ${systemctl} is-active --quiet proxy-suite-tun.service; then + echo "Global proxy-suite-tun.service is active. Stop it before using route=tproxy profiles." + exit 1 + fi + if ${systemctl} is-active --quiet proxy-suite-tproxy.service; then + echo "Global proxy-suite-tproxy.service is active. Stop it before using route=tproxy profiles." + exit 1 + fi + + uid="$(${idBin} -u)" + scope_unit="proxy-suite-app-tproxy-$profile-$$" + + ${systemctl} --user start "$APP_TPROXY_ANCHOR_UNIT" + ${systemctl} start proxy-suite-app-tproxy.service + ${systemctl} start "proxy-suite-app-tproxy-user@$uid.service" + + if ${systemdRun} --user --scope --quiet --collect --same-dir \ + --slice="$APP_TPROXY_SLICE_BASE" \ + --unit="$scope_unit" \ + "$@"; then + status=0 + else + status=$? + fi + + if ! _tproxy_scope_running; then + ${systemctl} stop "proxy-suite-app-tproxy-user@$uid.service" || true + ${systemctl} --user stop "$APP_TPROXY_ANCHOR_UNIT" || true + if ! _any_app_tproxy_user_active; then + ${systemctl} stop proxy-suite-app-tproxy.service || true + fi + fi + exit "$status" ;; *) @@ -674,14 +798,14 @@ in environment.systemPackages = [ proxyCtl ]; # nftables must be on for TProxy to work. - networking.nftables.enable = lib.mkIf (sb.tproxy.enable || art.enable) (lib.mkDefault true); + networking.nftables.enable = lib.mkIf (sb.tproxy.enable || art.enable || artp.enable) (lib.mkDefault true); - users.groups = lib.mkIf art.enable { + users.groups = lib.mkIf (art.enable || artp.enable) { "${ar.userControl.group}" = { }; }; - security.polkit.enable = lib.mkIf art.enable true; - security.polkit.extraConfig = lib.mkIf art.enable (lib.mkAfter '' + security.polkit.enable = lib.mkIf (art.enable || artp.enable) true; + security.polkit.extraConfig = lib.mkIf (art.enable || artp.enable) (lib.mkAfter '' polkit.addRule(function(action, subject) { if (!subject.isInGroup("${ar.userControl.group}")) { return null; @@ -693,7 +817,9 @@ in var unit = action.lookup("unit"); if (unit === "proxy-suite-app-tun.service" || - unit.indexOf("proxy-suite-app-tun-user@") === 0) { + unit.indexOf("proxy-suite-app-tun-user@") === 0 || + unit === "proxy-suite-app-tproxy.service" || + unit.indexOf("proxy-suite-app-tproxy-user@") === 0) { return polkit.Result.YES; } @@ -701,18 +827,31 @@ in }); ''); - systemd.user.services = lib.optionalAttrs art.enable { - proxy-suite-app-tun-anchor = { - description = "Anchor service for proxy-suite app TUN slice"; - serviceConfig = { - Type = "oneshot"; - RemainAfterExit = true; - Slice = appTunSliceName; - ExecStart = "${pkgs.coreutils}/bin/true"; - ExecStop = "${pkgs.coreutils}/bin/true"; + systemd.user.services = + lib.optionalAttrs art.enable { + proxy-suite-app-tun-anchor = { + description = "Anchor service for proxy-suite app TUN slice"; + serviceConfig = { + Type = "oneshot"; + RemainAfterExit = true; + Slice = appTunSliceName; + ExecStart = "${pkgs.coreutils}/bin/true"; + ExecStop = "${pkgs.coreutils}/bin/true"; + }; + }; + } + // lib.optionalAttrs artp.enable { + proxy-suite-app-tproxy-anchor = { + description = "Anchor service for proxy-suite app TProxy slice"; + serviceConfig = { + Type = "oneshot"; + RemainAfterExit = true; + Slice = appTproxySliceName; + ExecStart = "${pkgs.coreutils}/bin/true"; + ExecStop = "${pkgs.coreutils}/bin/true"; + }; }; }; - }; assertions = [ { @@ -759,6 +898,14 @@ in assertion = !(hasTunProfiles && !art.enable); message = "proxy-suite: route=tun in appRouting profiles requires appRouting.backends.tun.enable = true"; } + { + assertion = !artp.enable || ar.enable; + message = "proxy-suite: appRouting.backends.tproxy.enable requires appRouting.enable = true"; + } + { + assertion = !(hasTproxyProfiles && !artp.enable); + message = "proxy-suite: route=tproxy in appRouting profiles requires appRouting.backends.tproxy.enable = true"; + } { assertion = !(art.enable && sb.tproxy.enable && art.fwmark == sb.fwmark); message = "proxy-suite: appRouting.backends.tun.fwmark must differ from singBox.fwmark when singBox.tproxy.enable = true"; @@ -771,6 +918,26 @@ in assertion = !(art.enable && sb.tproxy.enable && art.routeTable == sb.routeTable); message = "proxy-suite: appRouting.backends.tun.routeTable must differ from singBox.routeTable when singBox.tproxy.enable = true"; } + { + assertion = !(artp.enable && artp.fwmark == sb.fwmark); + message = "proxy-suite: appRouting.backends.tproxy.fwmark must differ from singBox.fwmark"; + } + { + assertion = !(artp.enable && artp.fwmark == sb.proxyMark); + message = "proxy-suite: appRouting.backends.tproxy.fwmark must differ from singBox.proxyMark"; + } + { + assertion = !(artp.enable && artp.routeTable == sb.routeTable); + message = "proxy-suite: appRouting.backends.tproxy.routeTable must differ from singBox.routeTable"; + } + { + assertion = !(art.enable && artp.enable && art.fwmark == artp.fwmark); + message = "proxy-suite: appRouting.backends.tun.fwmark and appRouting.backends.tproxy.fwmark must differ"; + } + { + assertion = !(art.enable && artp.enable && art.routeTable == artp.routeTable); + message = "proxy-suite: appRouting.backends.tun.routeTable and appRouting.backends.tproxy.routeTable must differ"; + } { assertion = !t.enable @@ -837,7 +1004,10 @@ in ]; wantedBy = lib.optionals sb.tproxy.autostart [ "multi-user.target" ]; requires = [ "proxy-suite-socks.service" ]; - conflicts = [ "proxy-suite-tun.service" ]; + conflicts = [ + "proxy-suite-tun.service" + "proxy-suite-app-tproxy.service" + ]; serviceConfig = { Type = "oneshot"; RemainAfterExit = true; @@ -902,6 +1072,38 @@ in }; }; } + // lib.optionalAttrs artp.enable { + proxy-suite-app-tproxy = { + description = "proxy-suite app-routing TProxy backend"; + after = [ + "network.target" + "proxy-suite-socks.service" + ]; + requires = [ "proxy-suite-socks.service" ]; + conflicts = [ + "proxy-suite-tproxy.service" + "proxy-suite-tun.service" + ]; + serviceConfig = { + Type = "oneshot"; + RemainAfterExit = true; + ExecStart = "${appTproxyUpScript}"; + ExecStop = "${appTproxyDownScript}"; + }; + }; + + "proxy-suite-app-tproxy-user@" = { + description = "Enable proxy-suite app TProxy marking for user %i"; + requires = [ "proxy-suite-app-tproxy.service" ]; + after = [ "proxy-suite-app-tproxy.service" ]; + serviceConfig = { + Type = "oneshot"; + RemainAfterExit = true; + ExecStart = "${appTproxyUserRuleStart} %i"; + ExecStop = "${appTproxyUserRuleStop} %i"; + }; + }; + } // lib.optionalAttrs hasSubscriptions { proxy-suite-subscription-update = { description = "Refresh proxy-suite subscription caches"; diff --git a/nix/checks.nix b/nix/checks.nix index 29219e2..b313016 100644 --- a/nix/checks.nix +++ b/nix/checks.nix @@ -822,6 +822,51 @@ let ]).config.system.build.toplevel.drvPath ); + appRoutingTproxyFixture = evalProxySuite [ + baseModule + { + services.proxy-suite.appRouting = { + enable = true; + createDefaultProfiles = true; + backends.tproxy.enable = true; + }; + } + ]; + appRoutingTproxyScript = + builtins.readFile ( + "${packageByPattern appRoutingTproxyFixture.config.environment.systemPackages + ".*/[^/]*proxy-ctl(-[0-9.]+)?$"}/bin/proxy-ctl" + ); + appRoutingTproxyProfiles = + builtins.fromJSON ( + builtins.readFile ( + quotedValueByPrefix appRoutingTproxyScript "APP_ROUTING_PROFILES_FILE=\"" + ) + ); + appRoutingTproxyStartScript = + builtins.readFile appRoutingTproxyFixture.config.systemd.services."proxy-suite-app-tproxy".serviceConfig.ExecStart; + appRoutingTproxyUserStartExec = + appRoutingTproxyFixture.config.systemd.services."proxy-suite-app-tproxy-user@".serviceConfig.ExecStart; + appRoutingTproxyUserStartScript = + builtins.readFile (builtins.head (pkgs.lib.splitString " " appRoutingTproxyUserStartExec)); + + appRoutingTproxyWithoutEnable = forceEval ( + (evalProxySuite [ + baseModule + { + services.proxy-suite.appRouting = { + enable = true; + profiles = [ + { + name = "browser"; + route = "tproxy"; + } + ]; + }; + } + ]).config.system.build.toplevel.drvPath + ); + duplicateAppRoutingProfiles = forceEval ( (evalProxySuite [ baseModule @@ -884,6 +929,66 @@ let ]).config.system.build.toplevel.drvPath ); + appRoutingTproxyFwmarkCollision = forceEval ( + (evalProxySuite [ + baseModule + { + services.proxy-suite = { + singBox.proxyMark = 17; + appRouting = { + enable = true; + backends.tproxy = { + enable = true; + fwmark = 17; + }; + }; + }; + } + ]).config.system.build.toplevel.drvPath + ); + + appRoutingTunTproxyFwmarkCollision = forceEval ( + (evalProxySuite [ + baseModule + { + services.proxy-suite.appRouting = { + enable = true; + backends = { + tun = { + enable = true; + fwmark = 23; + }; + tproxy = { + enable = true; + fwmark = 23; + }; + }; + }; + } + ]).config.system.build.toplevel.drvPath + ); + + appRoutingTunTproxyRouteTableCollision = forceEval ( + (evalProxySuite [ + baseModule + { + services.proxy-suite.appRouting = { + enable = true; + backends = { + tun = { + enable = true; + routeTable = 123; + }; + tproxy = { + enable = true; + routeTable = 123; + }; + }; + }; + } + ]).config.system.build.toplevel.drvPath + ); + # Validation failures subscriptionBothSources = forceEval ( (evalProxySuite [ @@ -1347,6 +1452,13 @@ let true ) + # -- appRouting: createDefaultProfiles injects curated tproxy profile when backend is enabled -- + ( + assert builtins.length appRoutingTproxyProfiles == 2; + assert builtins.any (profile: profile.name == "tproxy" && profile.route == "tproxy") appRoutingTproxyProfiles; + true + ) + # -- appRouting: proxy-ctl script embeds wrap/apps commands -- ( assert pkgs.lib.hasInfix "wrap -- " appRoutingProxychainsScript; @@ -1376,6 +1488,14 @@ let true ) + # -- appRouting: generated proxy-ctl script dispatches tproxy profiles through systemd slices -- + ( + assert pkgs.lib.hasInfix "APP_ROUTING_TPROXY_ENABLED" appRoutingTproxyScript; + assert pkgs.lib.hasInfix "proxy-suite-app-tproxy-user@$uid.service" appRoutingTproxyScript; + assert pkgs.lib.hasInfix "proxy-suite-app-tproxy-$profile-$$" appRoutingTproxyScript; + true + ) + # -- appRouting: user mark script installs fwmark + conntrack mark rules -- ( assert pkgs.lib.hasInfix "meta mark set" appRoutingTunUserStartScript; @@ -1430,6 +1550,14 @@ let true ) + # -- appRouting: app TProxy service and helper units are created -- + ( + assert appRoutingTproxyFixture.config.systemd.services ? "proxy-suite-app-tproxy"; + assert appRoutingTproxyFixture.config.systemd.services ? "proxy-suite-app-tproxy-user@"; + assert appRoutingTproxyFixture.config.systemd.user.services ? "proxy-suite-app-tproxy-anchor"; + true + ) + # -- appRouting: app TUN enables nftables and user control group -- ( assert appRoutingTunFixture.config.networking.nftables.enable; @@ -1437,6 +1565,22 @@ let true ) + # -- appRouting: app TProxy helper installs socket cgroup mark rules -- + ( + assert pkgs.lib.hasInfix "socket cgroupv2" appRoutingTproxyUserStartScript; + assert pkgs.lib.hasInfix "meta mark set" appRoutingTproxyUserStartScript; + assert pkgs.lib.hasInfix "ct mark set" appRoutingTproxyUserStartScript; + true + ) + + # -- appRouting: app TProxy startup installs nftables and loopback policy route -- + ( + assert pkgs.lib.hasInfix "proxy_suite_app_tproxy" appRoutingTproxyStartScript; + assert pkgs.lib.hasInfix "route replace local default dev lo table 102" appRoutingTproxyStartScript; + assert pkgs.lib.hasInfix "rule add fwmark 17 table 102" appRoutingTproxyStartScript; + true + ) + # -- appRouting: route=proxychains requires proxychains.enable -- ( assert appRoutingProxychainsWithoutEnable.success == false; @@ -1461,6 +1605,12 @@ let true ) + # -- appRouting: route=tproxy requires appRouting.backends.tproxy.enable -- + ( + assert appRoutingTproxyWithoutEnable.success == false; + true + ) + # -- appRouting: profile names must be unique -- ( assert duplicateAppRoutingProfiles.success == false; @@ -1473,6 +1623,24 @@ let true ) + # -- appRouting: app TProxy fwmark must differ from sing-box proxyMark -- + ( + assert appRoutingTproxyFwmarkCollision.success == false; + true + ) + + # -- appRouting: app TUN and app TProxy fwmarks must differ -- + ( + assert appRoutingTunTproxyFwmarkCollision.success == false; + true + ) + + # -- appRouting: app TUN and app TProxy route tables must differ -- + ( + assert appRoutingTunTproxyRouteTableCollision.success == false; + true + ) + # -- subscription: no outbounds + no subscriptions fails -- ( assert noOutboundsNoSubscriptions.success == false;