# App routing backend infrastructure (TUN, TProxy, zapret). { lib, pkgs, cfg, singBoxCfg, proxyCfg, pureXrayEnabled, perAppRoutingCfg, perAppRoutingTun, perAppRoutingTproxy, constants, perAppZapretCfg, perAppTunChainFile, perAppTproxyRulesFile, perAppZapretRulesFile, ip, nft, awk, grepBin, findBin, headBin, seqBin, sleepBin, }: let builders = import ./builders.nix { inherit lib pkgs; }; inherit (builders) mkAnchorService; defaultUplinkIPv4Source = builders.mkDefaultUplinkIPv4Source { inherit ip awk; errorMessage = "proxy-suite: could not determine the default uplink IPv4 address for app TUN"; }; perAppTunSliceName = "proxy-suite-per-app-tun.slice"; perAppTproxySliceName = "proxy-suite-per-app-tproxy.slice"; perAppZapretSliceName = "proxy-suite-per-app-zapret.slice"; perAppRoutingProfileNames = map (profile: profile.name) perAppRoutingCfg.profiles; defaultPerAppRoutingProfiles = lib.optionals perAppRoutingCfg.createDefaultProfiles ( [ { name = "proxychains"; route = "proxychains"; } ] ++ lib.optionals perAppRoutingTun.enable [ { name = "tun"; route = "tun"; } ] ++ lib.optionals perAppRoutingTproxy.enable [ { name = "tproxy"; route = "tproxy"; } ] ++ lib.optionals perAppZapretCfg.enable [ { name = "zapret"; route = "zapret"; } ] ); effectivePerAppRoutingProfiles = perAppRoutingCfg.profiles ++ builtins.filter ( profile: !(builtins.elem profile.name perAppRoutingProfileNames) ) defaultPerAppRoutingProfiles; effectivePerAppRoutingProfileNames = map (profile: profile.name) effectivePerAppRoutingProfiles; localProxyAuth = proxyCfg.auth; localProxyAuthEnabled = localProxyAuth.username != null && (localProxyAuth.password != null || localProxyAuth.passwordFile != null); perAppRoutingProfilesFile = pkgs.writeText "proxy-suite-per-app-routing-profiles.json" ( builtins.toJSON effectivePerAppRoutingProfiles ); proxychainsConfigFile = if localProxyAuthEnabled then "/run/proxy-suite-socks/proxychains.conf" else pkgs.writeText "proxy-suite-proxychains.conf" '' strict_chain ${lib.optionalString perAppRoutingCfg.proxychains.quiet "quiet_mode"} ${lib.optionalString perAppRoutingCfg.proxychains.proxyDns "proxy_dns"} tcp_read_time_out 15000 tcp_connect_time_out 8000 [ProxyList] socks5 ${proxyCfg.listenAddress} ${toString proxyCfg.port} ''; proxychainsQuietArg = lib.optionalString perAppRoutingCfg.proxychains.quiet "-q "; hasProxychainsProfiles = builtins.any ( profile: profile.route == "proxychains" ) effectivePerAppRoutingProfiles; hasTunProfiles = builtins.any (profile: profile.route == "tun") effectivePerAppRoutingProfiles; hasTproxyProfiles = builtins.any ( profile: profile.route == "tproxy" ) effectivePerAppRoutingProfiles; hasZapretProfiles = builtins.any ( profile: profile.route == "zapret" ) effectivePerAppRoutingProfiles; inherit (constants) xrayPerAppTunIPv6Address xrayPerAppTunIPv6RoutePrefix ; perAppTunWaitForInterface = pkgs.writeShellScript "proxy-suite-per-app-tun-wait-for-interface" '' set -euo pipefail for _ in $(${seqBin} 1 50); do if ${ip} link show dev ${lib.escapeShellArg perAppRoutingTun.interface} >/dev/null 2>&1; then exit 0 fi ${sleepBin} 0.1 done echo "proxy-suite: app TUN interface ${perAppRoutingTun.interface} did not appear in time" >&2 exit 1 ''; perAppTunUpScript = pkgs.writeShellScript "proxy-suite-per-app-tun-up" '' set -euo pipefail tun_cidr=${lib.escapeShellArg perAppRoutingTun.address} tun6_cidr=${lib.escapeShellArg xrayPerAppTunIPv6Address} tun6_route_prefix=${lib.escapeShellArg xrayPerAppTunIPv6RoutePrefix} tun_addr="" tun_route_prefix="" uplink_addr="" ${builders.cidrNetworkFunction} ${builders.mkNftDeleteTable { inherit nft; family = "inet"; table = "proxy_suite_per_app_tun"; }} ${builders.mkIpRuleDeleteByFwmark { inherit ip; family = "-4"; fwmark = perAppRoutingTun.fwmark; table = perAppRoutingTun.routeTable; }} ${builders.mkIpRuleDeleteByFwmark { inherit ip; family = "-6"; fwmark = perAppRoutingTun.fwmark; table = perAppRoutingTun.routeTable; }} ${builders.mkIpRouteFlushTable { inherit ip; family = "-4"; table = perAppRoutingTun.routeTable; }} ${builders.mkIpRouteFlushTable { inherit ip; family = "-6"; table = perAppRoutingTun.routeTable; }} ${nft} -f ${perAppTunChainFile} ${perAppTunWaitForInterface} ${defaultUplinkIPv4Source} tun_addr="''${tun_cidr%%/*}" tun_route_prefix="$(cidr_network "$tun_cidr")" ${ip} -4 addr replace "$tun_cidr" dev ${lib.escapeShellArg perAppRoutingTun.interface} ${ip} -4 route replace "$tun_route_prefix" dev ${lib.escapeShellArg perAppRoutingTun.interface} src "$tun_addr" table ${toString perAppRoutingTun.routeTable} ${ip} -4 route replace default dev ${lib.escapeShellArg perAppRoutingTun.interface} src "$uplink_addr" table ${toString perAppRoutingTun.routeTable} ${ip} -4 rule add fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable} 2>/dev/null || true ${lib.optionalString pureXrayEnabled '' ${ip} -6 addr replace "$tun6_cidr" dev ${lib.escapeShellArg perAppRoutingTun.interface} ${ip} -6 route replace "$tun6_route_prefix" dev ${lib.escapeShellArg perAppRoutingTun.interface} table ${toString perAppRoutingTun.routeTable} ${ip} -6 route replace default dev ${lib.escapeShellArg perAppRoutingTun.interface} table ${toString perAppRoutingTun.routeTable} ${ip} -6 rule add fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable} 2>/dev/null || true ''} ''; perAppTunDownScript = pkgs.writeShellScript "proxy-suite-per-app-tun-down" '' set +e # Best-effort cleanup for graceful stops and for unclean previous exits. ${builders.mkNftDeleteTable { inherit nft; family = "inet"; table = "proxy_suite_per_app_tun"; }} ${builders.mkIpRuleDeleteByFwmark { inherit ip; family = "-4"; fwmark = perAppRoutingTun.fwmark; table = perAppRoutingTun.routeTable; }} ${builders.mkIpRuleDeleteByFwmark { inherit ip; family = "-6"; fwmark = perAppRoutingTun.fwmark; table = perAppRoutingTun.routeTable; }} ${builders.mkIpRouteFlushTable { inherit ip; family = "-4"; table = perAppRoutingTun.routeTable; }} ${builders.mkIpRouteFlushTable { inherit ip; family = "-6"; table = perAppRoutingTun.routeTable; }} ${builders.mkIpLinkDelete { inherit ip; interface = perAppRoutingTun.interface; }} ${builders.flushResolvedCaches} ''; perAppTproxyUpScript = pkgs.writeShellScript "proxy-suite-per-app-tproxy-up" '' set -euo pipefail ${builders.mkNftDeleteTable { inherit nft; family = "ip"; table = "proxy_suite_per_app_tproxy"; }} ${builders.mkIpRuleDeleteByFwmark { inherit ip; fwmark = perAppRoutingTproxy.fwmark; table = perAppRoutingTproxy.routeTable; }} ${builders.mkIpLocalDefaultRouteDelete { inherit ip; table = perAppRoutingTproxy.routeTable; }} ${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} ''; perAppTproxyDownScript = pkgs.writeShellScript "proxy-suite-per-app-tproxy-down" '' set +e ${builders.mkNftDeleteTable { inherit nft; family = "ip"; table = "proxy_suite_per_app_tproxy"; }} ${builders.mkIpRuleDeleteByFwmark { inherit ip; fwmark = perAppRoutingTproxy.fwmark; table = perAppRoutingTproxy.routeTable; }} ${builders.mkIpLocalDefaultRouteDelete { inherit ip; table = perAppRoutingTproxy.routeTable; }} ''; # Generate a "start" script that adds a per-user cgroup nftables mark rule. mkUserRuleStart = { name, nftFamily, nftTable, nftChain, sliceName, sliceLabel, markRule, }: pkgs.writeShellScript "proxy-suite-${name}-user-start" '' set -euo pipefail uid="$1" rule_comment_prefix="proxy-suite-${name}-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 sliceName} | ${headBin} -n1 || true) if [ -z "$cgroup_dir" ]; then echo "proxy-suite: ${sliceLabel} 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 ${nftFamily} ${nftTable} ${nftChain} 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 ${nftFamily} ${nftTable} ${nftChain} handle "$handle" || true done <<< "$handles" fi printf '%s\n' \ "add rule ${nftFamily} ${nftTable} ${nftChain} socket cgroupv2 level $cgroup_level \"$cgroup_path\" ${markRule} comment \"$mark_comment\"" \ | ${nft} -f - ''; # Generate a "stop" script that removes all per-user cgroup nftables mark rules. mkUserRuleStop = { name, nftFamily, nftTable, nftChain, }: pkgs.writeShellScript "proxy-suite-${name}-user-stop" '' set -euo pipefail uid="$1" rule_comment_prefix="proxy-suite-${name}-user-$uid" handles=$(${nft} -a list chain ${nftFamily} ${nftTable} ${nftChain} 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 ${nftFamily} ${nftTable} ${nftChain} handle "$handle" || true done <<< "$handles" fi ''; perAppTunUserRuleStart = mkUserRuleStart { name = "per-app-tun"; nftFamily = "inet"; nftTable = "proxy_suite_per_app_tun"; nftChain = "output"; sliceName = perAppTunSliceName; sliceLabel = "app TUN"; markRule = "meta mark set ${toString perAppRoutingTun.fwmark} ct mark set ${toString perAppRoutingTun.fwmark}"; }; perAppTunUserRuleStop = mkUserRuleStop { name = "per-app-tun"; nftFamily = "inet"; nftTable = "proxy_suite_per_app_tun"; nftChain = "output"; }; perAppTproxyUserRuleStart = mkUserRuleStart { name = "per-app-tproxy"; nftFamily = "ip"; nftTable = "proxy_suite_per_app_tproxy"; nftChain = "output"; sliceName = perAppTproxySliceName; sliceLabel = "app TProxy"; markRule = "meta mark set ${toString perAppRoutingTproxy.fwmark} ct mark set ${toString perAppRoutingTproxy.fwmark}"; }; perAppTproxyUserRuleStop = mkUserRuleStop { name = "per-app-tproxy"; nftFamily = "ip"; nftTable = "proxy_suite_per_app_tproxy"; nftChain = "output"; }; perAppZapretUserRuleStart = mkUserRuleStart { name = "per-app-zapret"; nftFamily = "inet"; nftTable = "proxy_suite_per_app_zapret_mark"; nftChain = "output"; sliceName = perAppZapretSliceName; sliceLabel = "app zapret"; markRule = "meta mark set meta mark or ${toString perAppZapretCfg.filterMark} ct mark set ct mark or ${toString perAppZapretCfg.filterMark}"; }; perAppZapretUserRuleStop = mkUserRuleStop { name = "per-app-zapret"; nftFamily = "inet"; nftTable = "proxy_suite_per_app_zapret_mark"; nftChain = "output"; }; in { inherit perAppTunSliceName perAppTproxySliceName perAppZapretSliceName perAppTunUpScript perAppTunDownScript perAppTproxyUpScript perAppTproxyDownScript perAppTunUserRuleStart perAppTunUserRuleStop perAppTproxyUserRuleStart perAppTproxyUserRuleStop perAppZapretUserRuleStart perAppZapretUserRuleStop mkAnchorService effectivePerAppRoutingProfiles effectivePerAppRoutingProfileNames perAppRoutingProfilesFile proxychainsConfigFile proxychainsQuietArg hasProxychainsProfiles hasTunProfiles hasTproxyProfiles hasZapretProfiles ; }