From 93e792f2e5a9778f2aec804015f479087d923c38 Mon Sep 17 00:00:00 2001 From: FUFSoB Date: Thu, 16 Apr 2026 23:27:05 +0500 Subject: [PATCH] breaking options changes, refactor --- README.md | 10 +- docs/options.md | 1472 ++++++++--------- modules/proxy-suite/config.nix | 34 +- modules/proxy-suite/default.nix | 60 +- modules/proxy-suite/nftables.nix | 63 +- modules/proxy-suite/options/app-routing.nix | 231 --- modules/proxy-suite/options/default.nix | 2 +- .../proxy-suite/options/per-app-routing.nix | 102 ++ modules/proxy-suite/options/sing-box.nix | 240 ++- modules/proxy-suite/options/types.nix | 14 +- modules/proxy-suite/options/zapret.nix | 25 + modules/proxy-suite/service-assertions.nix | 214 ++- modules/proxy-suite/service/default.nix | 138 +- .../{app-routing.nix => per-app-routing.nix} | 241 +-- modules/proxy-suite/service/polkit.nix | 4 +- modules/proxy-suite/service/scripts.nix | 20 +- modules/proxy-suite/zapret.nix | 58 +- nix/checks.nix | 819 +++++---- 18 files changed, 1995 insertions(+), 1752 deletions(-) delete mode 100644 modules/proxy-suite/options/app-routing.nix create mode 100644 modules/proxy-suite/options/per-app-routing.nix rename modules/proxy-suite/service/{app-routing.nix => per-app-routing.nix} (68%) diff --git a/README.md b/README.md index 65944cc..a1337be 100644 --- a/README.md +++ b/README.md @@ -76,26 +76,24 @@ services.proxy-suite = { tproxy = { enable = true; autostart = false; + perApp.enable = true; }; tun = { enable = true; autostart = false; + perApp.enable = true; }; }; zapret = { enable = true; + perApp.enable = true; }; - appRouting = { + perAppRouting = { enable = true; createDefaultProfiles = true; proxychains.enable = true; - backends = { - tun.enable = true; - tproxy.enable = true; - zapret.enable = true; - }; }; tray = { diff --git a/docs/options.md b/docs/options.md index 2c834a5..55a63ef 100644 --- a/docs/options.md +++ b/docs/options.md @@ -10,35 +10,17 @@ Update module option docs there instead of editing this file by hand. - [Complete example config](#complete-example-config) - services.proxy-suite - [enable](#services-proxy-suite-enable) - - appRouting - - [enable](#services-proxy-suite-approuting-enable) - - backends - - tproxy - - [enable](#services-proxy-suite-approuting-backends-tproxy-enable) - - [fwmark](#services-proxy-suite-approuting-backends-tproxy-fwmark) - - [localSubnets](#services-proxy-suite-approuting-backends-tproxy-localsubnets) - - [routeTable](#services-proxy-suite-approuting-backends-tproxy-routetable) - - tun - - [enable](#services-proxy-suite-approuting-backends-tun-enable) - - [address](#services-proxy-suite-approuting-backends-tun-address) - - [fwmark](#services-proxy-suite-approuting-backends-tun-fwmark) - - [interface](#services-proxy-suite-approuting-backends-tun-interface) - - [localSubnets](#services-proxy-suite-approuting-backends-tun-localsubnets) - - [mtu](#services-proxy-suite-approuting-backends-tun-mtu) - - [routeTable](#services-proxy-suite-approuting-backends-tun-routetable) - - zapret - - [enable](#services-proxy-suite-approuting-backends-zapret-enable) - - [filterMark](#services-proxy-suite-approuting-backends-zapret-filtermark) - - [qnum](#services-proxy-suite-approuting-backends-zapret-qnum) - - [createDefaultProfiles](#services-proxy-suite-approuting-createdefaultprofiles) - - [profiles](#services-proxy-suite-approuting-profiles) + - perAppRouting + - [enable](#services-proxy-suite-perapprouting-enable) + - [createDefaultProfiles](#services-proxy-suite-perapprouting-createdefaultprofiles) + - [profiles](#services-proxy-suite-perapprouting-profiles) - item - - [name](#services-proxy-suite-approuting-profiles-name) - - [route](#services-proxy-suite-approuting-profiles-route) + - [name](#services-proxy-suite-perapprouting-profiles-name) + - [route](#services-proxy-suite-perapprouting-profiles-route) - proxychains - - [enable](#services-proxy-suite-approuting-proxychains-enable) - - [proxyDns](#services-proxy-suite-approuting-proxychains-proxydns) - - [quiet](#services-proxy-suite-approuting-proxychains-quiet) + - [enable](#services-proxy-suite-perapprouting-proxychains-enable) + - [proxyDns](#services-proxy-suite-perapprouting-proxychains-proxydns) + - [quiet](#services-proxy-suite-perapprouting-proxychains-quiet) - singBox - [enable](#services-proxy-suite-singbox-enable) - [clashApiPort](#services-proxy-suite-singbox-clashapiport) @@ -51,7 +33,6 @@ Update module option docs there instead of editing this file by hand. - [address](#services-proxy-suite-singbox-dns-remote-address) - [port](#services-proxy-suite-singbox-dns-remote-port) - [type](#services-proxy-suite-singbox-dns-remote-type) - - [fwmark](#services-proxy-suite-singbox-fwmark) - [listenAddress](#services-proxy-suite-singbox-listenaddress) - [outbounds](#services-proxy-suite-singbox-outbounds) - item @@ -66,8 +47,6 @@ Update module option docs there instead of editing this file by hand. - [urlFile](#services-proxy-suite-singbox-outbounds-urlfile) - [port](#services-proxy-suite-singbox-port) - [proxyByDefault](#services-proxy-suite-singbox-proxybydefault) - - [proxyMark](#services-proxy-suite-singbox-proxymark) - - [routeTable](#services-proxy-suite-singbox-routetable) - routing - [enableRuDirect](#services-proxy-suite-singbox-routing-enablerudirect) - block @@ -102,14 +81,30 @@ Update module option docs there instead of editing this file by hand. - tproxy - [enable](#services-proxy-suite-singbox-tproxy-enable) - [autostart](#services-proxy-suite-singbox-tproxy-autostart) + - [fwmark](#services-proxy-suite-singbox-tproxy-fwmark) - [localSubnets](#services-proxy-suite-singbox-tproxy-localsubnets) - - [tproxyPort](#services-proxy-suite-singbox-tproxyport) + - perApp + - [enable](#services-proxy-suite-singbox-tproxy-perapp-enable) + - [fwmark](#services-proxy-suite-singbox-tproxy-perapp-fwmark) + - [localSubnets](#services-proxy-suite-singbox-tproxy-perapp-localsubnets) + - [routeTable](#services-proxy-suite-singbox-tproxy-perapp-routetable) + - [port](#services-proxy-suite-singbox-tproxy-port) + - [proxyMark](#services-proxy-suite-singbox-tproxy-proxymark) + - [routeTable](#services-proxy-suite-singbox-tproxy-routetable) - tun - [enable](#services-proxy-suite-singbox-tun-enable) - [address](#services-proxy-suite-singbox-tun-address) - [autostart](#services-proxy-suite-singbox-tun-autostart) - [interface](#services-proxy-suite-singbox-tun-interface) - [mtu](#services-proxy-suite-singbox-tun-mtu) + - perApp + - [enable](#services-proxy-suite-singbox-tun-perapp-enable) + - [address](#services-proxy-suite-singbox-tun-perapp-address) + - [fwmark](#services-proxy-suite-singbox-tun-perapp-fwmark) + - [interface](#services-proxy-suite-singbox-tun-perapp-interface) + - [localSubnets](#services-proxy-suite-singbox-tun-perapp-localsubnets) + - [mtu](#services-proxy-suite-singbox-tun-perapp-mtu) + - [routeTable](#services-proxy-suite-singbox-tun-perapp-routetable) - urlTest - [interval](#services-proxy-suite-singbox-urltest-interval) - [tolerance](#services-proxy-suite-singbox-urltest-tolerance) @@ -150,6 +145,10 @@ Update module option docs there instead of editing this file by hand. - [ipsetExclude](#services-proxy-suite-zapret-ipsetexclude) - [listExclude](#services-proxy-suite-zapret-listexclude) - [listGeneral](#services-proxy-suite-zapret-listgeneral) + - perApp + - [enable](#services-proxy-suite-zapret-perapp-enable) + - [filterMark](#services-proxy-suite-zapret-perapp-filtermark) + - [qnum](#services-proxy-suite-zapret-perapp-qnum) - [syncDirectRouting](#services-proxy-suite-zapret-syncdirectrouting) - [syncDirectRoutingUpstreamIps](#services-proxy-suite-zapret-syncdirectroutingupstreamips) - [syncDirectRoutingUserIps](#services-proxy-suite-zapret-syncdirectroutinguserips) @@ -159,33 +158,8 @@ Update module option docs there instead of editing this file by hand. ```nix services.proxy-suite = { - appRouting = { - backends = { - tproxy = { - enable = false; - fwmark = 17; - localSubnets = [ - "192.168.0.0/16" - ]; - routeTable = 102; - }; - tun = { - address = "172.20.0.1/30"; - enable = false; - fwmark = 16; - interface = "psapptun0"; - localSubnets = [ - "192.168.0.0/16" - ]; - mtu = 1400; - routeTable = 101; - }; - zapret = { - enable = false; - filterMark = 268435456; - qnum = 201; - }; - }; + enable = false; + perAppRouting = { createDefaultProfiles = false; enable = false; profiles = [ ]; @@ -195,7 +169,6 @@ services.proxy-suite = { quiet = true; }; }; - enable = false; singBox = { clashApiPort = 9090; dns = { @@ -211,13 +184,10 @@ services.proxy-suite = { }; }; enable = true; - fwmark = 1; listenAddress = "127.0.0.1"; outbounds = [ ]; port = 1080; proxyByDefault = true; - proxyMark = 2; - routeTable = 100; routing = { block = { domains = [ ]; @@ -246,17 +216,39 @@ services.proxy-suite = { tproxy = { autostart = false; enable = false; + fwmark = 1; localSubnets = [ "192.168.0.0/16" ]; + perApp = { + enable = false; + fwmark = 17; + localSubnets = [ + "192.168.0.0/16" + ]; + routeTable = 102; + }; + port = 1081; + proxyMark = 2; + routeTable = 100; }; - tproxyPort = 1081; tun = { address = "172.19.0.1/30"; autostart = false; enable = false; interface = "singtun0"; mtu = 1400; + perApp = { + address = "172.20.0.1/30"; + enable = false; + fwmark = 16; + interface = "psperapptun0"; + localSubnets = [ + "192.168.0.0/16" + ]; + mtu = 1400; + routeTable = 101; + }; }; urlTest = { interval = "3m"; @@ -300,6 +292,11 @@ services.proxy-suite = { ipsetExclude = [ ]; listExclude = [ ]; listGeneral = [ ]; + perApp = { + enable = false; + filterMark = 268435456; + qnum = 201; + }; syncDirectRouting = true; syncDirectRoutingUpstreamIps = false; syncDirectRoutingUserIps = true; @@ -314,35 +311,8 @@ This is generated by filling in all the options with example values (or defaults ```nix services.proxy-suite = { - appRouting = { - backends = { - tproxy = { - enable = true; - fwmark = 17; - localSubnets = [ - "192.168.0.0/16" - "10.0.0.0/8" - ]; - routeTable = 102; - }; - tun = { - address = "172.20.0.1/30"; - enable = true; - fwmark = 16; - interface = "psapptun0"; - localSubnets = [ - "192.168.0.0/16" - "10.0.0.0/8" - ]; - mtu = 1400; - routeTable = 101; - }; - zapret = { - enable = true; - filterMark = 268435456; - qnum = 201; - }; - }; + enable = true; + perAppRouting = { createDefaultProfiles = true; enable = true; profiles = [ @@ -361,7 +331,6 @@ services.proxy-suite = { quiet = true; }; }; - enable = true; singBox = { clashApiPort = 9090; dns = { @@ -377,7 +346,6 @@ services.proxy-suite = { }; }; enable = true; - fwmark = 1; listenAddress = "127.0.0.1"; outbounds = [ { @@ -391,8 +359,6 @@ services.proxy-suite = { ]; port = 1080; proxyByDefault = true; - proxyMark = 2; - routeTable = 100; routing = { block = { domains = [ @@ -479,18 +445,42 @@ services.proxy-suite = { tproxy = { autostart = true; enable = true; + fwmark = 1; localSubnets = [ "192.168.0.0/16" "10.0.0.0/8" ]; + perApp = { + enable = true; + fwmark = 17; + localSubnets = [ + "192.168.0.0/16" + "10.0.0.0/8" + ]; + routeTable = 102; + }; + port = 1081; + proxyMark = 2; + routeTable = 100; }; - tproxyPort = 1081; tun = { address = "172.19.0.1/30"; autostart = true; enable = true; interface = "singtun0"; mtu = 1400; + perApp = { + address = "172.20.0.1/30"; + enable = true; + fwmark = 16; + interface = "psperapptun0"; + localSubnets = [ + "192.168.0.0/16" + "10.0.0.0/8" + ]; + mtu = 1400; + routeTable = 101; + }; }; urlTest = { interval = "1m"; @@ -569,6 +559,11 @@ services.proxy-suite = { listGeneral = [ "youtube.com" ]; + perApp = { + enable = true; + filterMark = 268435456; + qnum = 201; + }; syncDirectRouting = true; syncDirectRoutingUpstreamIps = false; syncDirectRoutingUserIps = true; @@ -579,8 +574,6 @@ services.proxy-suite = { ## services\.proxy-suite\.enable - - Whether to enable proxy suite (sing-box + zapret + tg-ws-proxy)\. @@ -609,8 +602,8 @@ true - -## services\.proxy-suite\.appRouting\.enable + +## services\.proxy-suite\.perAppRouting\.enable @@ -638,495 +631,16 @@ true ``` *Declared by:* - - [modules/proxy-suite/options/app-routing\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/app-routing.nix) + - [modules/proxy-suite/options/per-app-routing\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/per-app-routing.nix) - -## services\.proxy-suite\.appRouting\.backends\.tproxy\.enable + +## services\.proxy-suite\.perAppRouting\.createDefaultProfiles -Whether to enable app-scoped TProxy backend for appRouting profiles\. - -*Type:* -boolean - - - -*Default:* - -```nix -false -``` - - - -*Example:* - -```nix -true -``` - -*Declared by:* - - [modules/proxy-suite/options/app-routing\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/app-routing.nix) - - - - -## services\.proxy-suite\.appRouting\.backends\.tproxy\.fwmark - - - -Packet mark used to steer wrapped app traffic into the app-routing -TProxy policy-routing table\. - - - -*Type:* -signed integer - - - -*Default:* - -```nix -17 -``` - - - -*Example:* - -```nix -17 -``` - -*Declared by:* - - [modules/proxy-suite/options/app-routing\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/app-routing.nix) - - - - -## services\.proxy-suite\.appRouting\.backends\.tproxy\.localSubnets - - - -Destination subnets that should bypass app-routing TProxy -interception, except DNS traffic on port 53\. - - - -*Type:* -list of string - - - -*Default:* - -```nix -[ - "192.168.0.0/16" -] -``` - - - -*Example:* - -```nix -[ - "192.168.0.0/16" - "10.0.0.0/8" -] -``` - -*Declared by:* - - [modules/proxy-suite/options/app-routing\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/app-routing.nix) - - - - -## services\.proxy-suite\.appRouting\.backends\.tproxy\.routeTable - - - -Policy-routing table used by the app-routing TProxy backend\. - - - -*Type:* -signed integer - - - -*Default:* - -```nix -102 -``` - - - -*Example:* - -```nix -102 -``` - -*Declared by:* - - [modules/proxy-suite/options/app-routing\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/app-routing.nix) - - - - -## services\.proxy-suite\.appRouting\.backends\.tun\.enable - - - -Whether to enable app-scoped TUN backend for appRouting profiles\. - - - -*Type:* -boolean - - - -*Default:* - -```nix -false -``` - - - -*Example:* - -```nix -true -``` - -*Declared by:* - - [modules/proxy-suite/options/app-routing\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/app-routing.nix) - - - - -## services\.proxy-suite\.appRouting\.backends\.tun\.address - - - -Address assigned to the app-routing TUN interface in CIDR -notation\. - - - -*Type:* -string - - - -*Default:* - -```nix -"172.20.0.1/30" -``` - - - -*Example:* - -```nix -"172.20.0.1/30" -``` - -*Declared by:* - - [modules/proxy-suite/options/app-routing\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/app-routing.nix) - - - - -## services\.proxy-suite\.appRouting\.backends\.tun\.fwmark - - - -Packet mark used to steer wrapped app traffic into the app-routing -TUN policy-routing table\. - - - -*Type:* -signed integer - - - -*Default:* - -```nix -16 -``` - - - -*Example:* - -```nix -16 -``` - -*Declared by:* - - [modules/proxy-suite/options/app-routing\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/app-routing.nix) - - - - -## services\.proxy-suite\.appRouting\.backends\.tun\.interface - - - -Name of the dedicated app-routing TUN interface used by -proxy-suite-app-tun\. - - - -*Type:* -string - - - -*Default:* - -```nix -"psapptun0" -``` - - - -*Example:* - -```nix -"psapptun0" -``` - -*Declared by:* - - [modules/proxy-suite/options/app-routing\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/app-routing.nix) - - - - -## services\.proxy-suite\.appRouting\.backends\.tun\.localSubnets - - - -Destination subnets that should bypass the app-routing TUN mark, -so wrapped apps can still reach local LAN resources directly\. - - - -*Type:* -list of string - - - -*Default:* - -```nix -[ - "192.168.0.0/16" -] -``` - - - -*Example:* - -```nix -[ - "192.168.0.0/16" - "10.0.0.0/8" -] -``` - -*Declared by:* - - [modules/proxy-suite/options/app-routing\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/app-routing.nix) - - - - -## services\.proxy-suite\.appRouting\.backends\.tun\.mtu - - - -MTU for the app-routing TUN interface\. - - - -*Type:* -signed integer - - - -*Default:* - -```nix -1400 -``` - - - -*Example:* - -```nix -1400 -``` - -*Declared by:* - - [modules/proxy-suite/options/app-routing\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/app-routing.nix) - - - - -## services\.proxy-suite\.appRouting\.backends\.tun\.routeTable - - - -Policy-routing table used by the app-routing TUN backend\. - - - -*Type:* -signed integer - - - -*Default:* - -```nix -101 -``` - - - -*Example:* - -```nix -101 -``` - -*Declared by:* - - [modules/proxy-suite/options/app-routing\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/app-routing.nix) - - - - -## services\.proxy-suite\.appRouting\.backends\.zapret\.enable - - - -Whether to enable app-scoped zapret backend for appRouting profiles\. - - - -*Type:* -boolean - - - -*Default:* - -```nix -false -``` - - - -*Example:* - -```nix -true -``` - -*Declared by:* - - [modules/proxy-suite/options/app-routing\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/app-routing.nix) - - - - -## services\.proxy-suite\.appRouting\.backends\.zapret\.filterMark - - - -Packet mark bit used to mark wrapped app traffic for the -dedicated app-scoped zapret instance\. - - - -*Type:* -signed integer - - - -*Default:* - -```nix -268435456 -``` - - - -*Example:* - -```nix -268435456 -``` - -*Declared by:* - - [modules/proxy-suite/options/app-routing\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/app-routing.nix) - - - - -## services\.proxy-suite\.appRouting\.backends\.zapret\.qnum - - - -NFQUEUE number used by the dedicated app-scoped zapret instance\. -This backend runs as a second zapret daemon and should use a -queue distinct from the global zapret instance\. - - - -*Type:* -signed integer - - - -*Default:* - -```nix -201 -``` - - - -*Example:* - -```nix -201 -``` - -*Declared by:* - - [modules/proxy-suite/options/app-routing\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/app-routing.nix) - - - - -## services\.proxy-suite\.appRouting\.createDefaultProfiles - - - -Whether to automatically add curated appRouting profiles\. +Whether to automatically add curated perAppRouting profiles\. This is opt-in\. Generated defaults are appended only when no user-defined profile with the same name already exists\. @@ -1134,10 +648,10 @@ user-defined profile with the same name already exists\. Current curated defaults: - ` proxychains `: route = “proxychains” - - ` tun `: route = “tun” when appRouting\.backends\.tun\.enable = true - - ` tproxy `: route = “tproxy” when appRouting\.backends\.tproxy\.enable = true - - ` zapret `: route = “zapret” when appRouting\.backends\.zapret\.enable = true - and zapret\.enable = true + - ` tun `: route = “tun” when singBox\.tun\.perApp\.enable = true + - ` tproxy `: route = “tproxy” when singBox\.tproxy\.perApp\.enable = true + - ` zapret `: route = “zapret” when zapret\.perApp\.enable = true and + zapret\.enable = true This makes ` proxy-ctl wrap proxychains -- ` available without defining the profile manually, and similarly exposes @@ -1168,12 +682,12 @@ true ``` *Declared by:* - - [modules/proxy-suite/options/app-routing\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/app-routing.nix) + - [modules/proxy-suite/options/per-app-routing\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/per-app-routing.nix) - -## services\.proxy-suite\.appRouting\.profiles + +## services\.proxy-suite\.perAppRouting\.profiles @@ -1184,18 +698,18 @@ This initial implementation supports: - “direct” for running a command unchanged - “proxychains” for TCP apps that can use an LD_PRELOAD wrapper instead of global TUN or TProxy interception - - “tun” for app-scoped policy routing into the dedicated app TUN + - “tun” for per-app-scoped policy routing into the dedicated app TUN backend - - “tproxy” for app-scoped transparent interception through the + - “tproxy” for per-app-scoped transparent interception through the dedicated app TProxy backend - - “zapret” for app-scoped zapret handling through a separate + - “zapret” for per-app-scoped zapret handling through a separate zapret instance without changing the app’s network path or exit IP -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\. The “tproxy” route depends on -appRouting\.backends\.tproxy\.enable = true\. The “zapret” route depends -on appRouting\.backends\.zapret\.enable = true and zapret\.enable = true\. +proxychains-based wrapping depends on singBox\.enable = true and the +local proxy-suite mixed proxy listener provided by sing-box\. The +“tun” route depends on singBox\.tun\.perApp\.enable = true\. The “tproxy” +route depends on singBox\.tproxy\.perApp\.enable = true\. The “zapret” +route depends on zapret\.perApp\.enable = true and zapret\.enable = true\. When createDefaultProfiles = true, curated defaults are added on top of this list unless a user-defined profile already uses the same @@ -1232,17 +746,17 @@ list of (submodule) ``` *Declared by:* - - [modules/proxy-suite/options/app-routing\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/app-routing.nix) + - [modules/proxy-suite/options/per-app-routing\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/per-app-routing.nix) - -## services\.proxy-suite\.appRouting\.profiles\.\*\.name + +## services\.proxy-suite\.perAppRouting\.profiles\.\*\.name Profile name used by ` proxy-ctl wrap -- `\. -Must be unique within appRouting\.profiles\. +Must be unique within perAppRouting\.profiles\. @@ -1258,12 +772,12 @@ string matching the pattern ^\[a-z0-9]\[a-z0-9-]\*$ ``` *Declared by:* - - [modules/proxy-suite/options/app-routing\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/app-routing.nix) + - [modules/proxy-suite/options/per-app-routing\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/per-app-routing.nix) - -## services\.proxy-suite\.appRouting\.profiles\.\*\.route + +## services\.proxy-suite\.perAppRouting\.profiles\.\*\.route @@ -1272,14 +786,14 @@ Per-app route backend used by proxy-ctl wrap\. - “direct”: run the command unchanged\. - “proxychains”: run the command through proxychains-ng using the local proxy-suite mixed SOCKS endpoint\. - - “tun”: launch the command in the dedicated app-routing TUN slice so + - “tun”: launch the command in the dedicated per-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 + - “tproxy”: launch the command in the dedicated per-app-routing TProxy slice so only that app’s traffic is transparently intercepted by the local sing-box TProxy inbound\. - - “zapret”: launch the command in the dedicated app-routing zapret + - “zapret”: launch the command in the dedicated per-app-routing zapret slice so only that app’s traffic is handled by the separate - app-scoped zapret instance\. + per-app-scoped zapret instance\. Additional route backends may be added in the future\. @@ -1305,16 +819,16 @@ one of “direct”, “proxychains”, “tun”, “tproxy”, “zapret” ``` *Declared by:* - - [modules/proxy-suite/options/app-routing\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/app-routing.nix) + - [modules/proxy-suite/options/per-app-routing\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/per-app-routing.nix) - -## services\.proxy-suite\.appRouting\.proxychains\.enable + +## services\.proxy-suite\.perAppRouting\.proxychains\.enable -Whether to enable proxychains-backed appRouting profiles\. +Whether to enable proxychains-backed perAppRouting profiles\. @@ -1338,12 +852,12 @@ true ``` *Declared by:* - - [modules/proxy-suite/options/app-routing\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/app-routing.nix) + - [modules/proxy-suite/options/per-app-routing\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/per-app-routing.nix) - -## services\.proxy-suite\.appRouting\.proxychains\.proxyDns + +## services\.proxy-suite\.perAppRouting\.proxychains\.proxyDns @@ -1373,12 +887,12 @@ true ``` *Declared by:* - - [modules/proxy-suite/options/app-routing\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/app-routing.nix) + - [modules/proxy-suite/options/per-app-routing\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/per-app-routing.nix) - -## services\.proxy-suite\.appRouting\.proxychains\.quiet + +## services\.proxy-suite\.perAppRouting\.proxychains\.quiet @@ -1407,7 +921,7 @@ true ``` *Declared by:* - - [modules/proxy-suite/options/app-routing\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/app-routing.nix) + - [modules/proxy-suite/options/per-app-routing\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/per-app-routing.nix) @@ -1417,8 +931,8 @@ true Whether to configure and run sing-box services for proxy-suite\. -When disabled, singBox\.\* options are ignored even if proxy-suite itself -is enabled\. +When disabled, sing-box services and generated sing-box configs are +skipped even if proxy-suite itself is enabled\. @@ -1482,7 +996,7 @@ DNS upstream used for the built-in “local” resolver role\. This resolver is also used as sing-box route\.default_domain_resolver\. The module keeps detour policy automatic: in mixed/TProxy mode and -app-routing TUN mode, “local” stays on the direct path (without an +per-app-routing TUN mode, “local” stays on the direct path (without an explicit detour); in global TUN mode, it is forced through the proxy to avoid auto_redirect conflicts\. @@ -1746,41 +1260,6 @@ one of “udp”, “tcp”, “tls” - -## services\.proxy-suite\.singBox\.fwmark - - - -Mark applied to intercepted packets in TProxy mode\. -A matching ` ip rule ` routes this mark to singBox\.routeTable, which -points traffic to loopback for local proxy processing\. - - - -*Type:* -signed integer - - - -*Default:* - -```nix -1 -``` - - - -*Example:* - -```nix -1 -``` - -*Declared by:* - - [modules/proxy-suite/options/sing-box\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/sing-box.nix) - - - ## services\.proxy-suite\.singBox\.listenAddress @@ -2230,75 +1709,6 @@ true - -## services\.proxy-suite\.singBox\.proxyMark - - - -Mark applied to sing-box egress packets in TProxy mode so they bypass -re-interception and do not loop back into the transparent proxy path\. - - - -*Type:* -signed integer - - - -*Default:* - -```nix -2 -``` - - - -*Example:* - -```nix -2 -``` - -*Declared by:* - - [modules/proxy-suite/options/sing-box\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/sing-box.nix) - - - - -## services\.proxy-suite\.singBox\.routeTable - - - -Policy-routing table number used for TProxy interception flow\. -The module installs a local default route in this table and binds it -to singBox\.fwmark\. - - - -*Type:* -signed integer - - - -*Default:* - -```nix -100 -``` - - - -*Example:* - -```nix -100 -``` - -*Declared by:* - - [modules/proxy-suite/options/sing-box\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/sing-box.nix) - - - ## services\.proxy-suite\.singBox\.routing\.enableRuDirect @@ -3254,7 +2664,7 @@ null -Whether to enable TProxy mode service (opt-in, transparent proxy via nftables)\. +Whether to enable global sing-box TProxy mode service\. @@ -3317,12 +2727,49 @@ true + +## services\.proxy-suite\.singBox\.tproxy\.fwmark + + + +Mark applied to intercepted packets in global TProxy mode\. +A matching ` ip rule ` routes this mark to +singBox\.tproxy\.routeTable, which points traffic to +loopback for local proxy processing\. + + + +*Type:* +signed integer + + + +*Default:* + +```nix +1 +``` + + + +*Example:* + +```nix +1 +``` + +*Declared by:* + - [modules/proxy-suite/options/sing-box\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/sing-box.nix) + + + ## services\.proxy-suite\.singBox\.tproxy\.localSubnets -Subnets whose traffic bypasses TProxy interception, except DNS (port 53)\. +Subnets whose traffic bypasses global TProxy interception, except +DNS (port 53)\. Typically this should include your LAN subnet(s)\. VM bridge networks should usually go here too, or use zapret\.cidrExemption for @@ -3359,8 +2806,147 @@ list of string - -## services\.proxy-suite\.singBox\.tproxyPort + +## services\.proxy-suite\.singBox\.tproxy\.perApp\.enable + + + +Whether to enable per-app-scoped sing-box TProxy backend for perAppRouting profiles\. + + + +*Type:* +boolean + + + +*Default:* + +```nix +false +``` + + + +*Example:* + +```nix +true +``` + +*Declared by:* + - [modules/proxy-suite/options/sing-box\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/sing-box.nix) + + + + +## services\.proxy-suite\.singBox\.tproxy\.perApp\.fwmark + + + +Packet mark used to steer wrapped app traffic into the per-app-scoped +TProxy policy-routing table\. + + + +*Type:* +signed integer + + + +*Default:* + +```nix +17 +``` + + + +*Example:* + +```nix +17 +``` + +*Declared by:* + - [modules/proxy-suite/options/sing-box\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/sing-box.nix) + + + + +## services\.proxy-suite\.singBox\.tproxy\.perApp\.localSubnets + + + +Subnets whose traffic bypasses per-app-scoped TProxy interception, +except DNS (port 53)\. + + + +*Type:* +list of string + + + +*Default:* + +```nix +[ + "192.168.0.0/16" +] +``` + + + +*Example:* + +```nix +[ + "192.168.0.0/16" + "10.0.0.0/8" +] +``` + +*Declared by:* + - [modules/proxy-suite/options/sing-box\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/sing-box.nix) + + + + +## services\.proxy-suite\.singBox\.tproxy\.perApp\.routeTable + + + +Policy-routing table used by the per-app-scoped TProxy backend\. + + + +*Type:* +signed integer + + + +*Default:* + +```nix +102 +``` + + + +*Example:* + +```nix +102 +``` + +*Declared by:* + - [modules/proxy-suite/options/sing-box\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/sing-box.nix) + + + + +## services\.proxy-suite\.singBox\.tproxy\.port @@ -3394,12 +2980,82 @@ TCP/UDP traffic to this port\. + +## services\.proxy-suite\.singBox\.tproxy\.proxyMark + + + +Mark applied to sing-box egress packets in global TProxy mode so +they bypass re-interception and do not loop back into the +transparent proxy path\. + + + +*Type:* +signed integer + + + +*Default:* + +```nix +2 +``` + + + +*Example:* + +```nix +2 +``` + +*Declared by:* + - [modules/proxy-suite/options/sing-box\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/sing-box.nix) + + + + +## services\.proxy-suite\.singBox\.tproxy\.routeTable + + + +Policy-routing table number used for global TProxy interception flow\. +The module installs a local default route in this table and binds it +to singBox\.tproxy\.fwmark\. + + + +*Type:* +signed integer + + + +*Default:* + +```nix +100 +``` + + + +*Example:* + +```nix +100 +``` + +*Declared by:* + - [modules/proxy-suite/options/sing-box\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/sing-box.nix) + + + ## services\.proxy-suite\.singBox\.tun\.enable -Whether to enable TUN mode service (opt-in, mutually exclusive with proxy-suite-tproxy)\. +Whether to enable global sing-box TUN mode service\. @@ -3432,7 +3088,7 @@ true -Address assigned to the TUN interface in CIDR notation\. +Address assigned to the global TUN interface in CIDR notation\. @@ -3533,7 +3189,7 @@ string -MTU for the TUN interface created by proxy-suite-tun\. +MTU for the global TUN interface created by proxy-suite-tun\. @@ -3561,6 +3217,246 @@ signed integer + +## services\.proxy-suite\.singBox\.tun\.perApp\.enable + + + +Whether to enable per-app-scoped sing-box TUN backend for perAppRouting profiles\. + + + +*Type:* +boolean + + + +*Default:* + +```nix +false +``` + + + +*Example:* + +```nix +true +``` + +*Declared by:* + - [modules/proxy-suite/options/sing-box\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/sing-box.nix) + + + + +## services\.proxy-suite\.singBox\.tun\.perApp\.address + + + +Address assigned to the dedicated per-app-routing TUN interface in CIDR +notation\. + + + +*Type:* +string + + + +*Default:* + +```nix +"172.20.0.1/30" +``` + + + +*Example:* + +```nix +"172.20.0.1/30" +``` + +*Declared by:* + - [modules/proxy-suite/options/sing-box\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/sing-box.nix) + + + + +## services\.proxy-suite\.singBox\.tun\.perApp\.fwmark + + + +Packet mark used to steer wrapped app traffic into the per-app-scoped +TUN policy-routing table\. + + + +*Type:* +signed integer + + + +*Default:* + +```nix +16 +``` + + + +*Example:* + +```nix +16 +``` + +*Declared by:* + - [modules/proxy-suite/options/sing-box\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/sing-box.nix) + + + + +## services\.proxy-suite\.singBox\.tun\.perApp\.interface + + + +Name of the dedicated per-app-routing TUN interface used by +proxy-suite-per-app-tun\. + + + +*Type:* +string + + + +*Default:* + +```nix +"psperapptun0" +``` + + + +*Example:* + +```nix +"psperapptun0" +``` + +*Declared by:* + - [modules/proxy-suite/options/sing-box\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/sing-box.nix) + + + + +## services\.proxy-suite\.singBox\.tun\.perApp\.localSubnets + + + +Destination subnets that should bypass the per-app-scoped TUN mark, +so wrapped apps can still reach local LAN resources directly\. + + + +*Type:* +list of string + + + +*Default:* + +```nix +[ + "192.168.0.0/16" +] +``` + + + +*Example:* + +```nix +[ + "192.168.0.0/16" + "10.0.0.0/8" +] +``` + +*Declared by:* + - [modules/proxy-suite/options/sing-box\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/sing-box.nix) + + + + +## services\.proxy-suite\.singBox\.tun\.perApp\.mtu + + + +MTU for the dedicated per-app-routing TUN interface\. + + + +*Type:* +signed integer + + + +*Default:* + +```nix +1400 +``` + + + +*Example:* + +```nix +1400 +``` + +*Declared by:* + - [modules/proxy-suite/options/sing-box\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/sing-box.nix) + + + + +## services\.proxy-suite\.singBox\.tun\.perApp\.routeTable + + + +Policy-routing table used by the per-app-scoped TUN backend\. + + + +*Type:* +signed integer + + + +*Default:* + +```nix +101 +``` + + + +*Example:* + +```nix +101 +``` + +*Declared by:* + - [modules/proxy-suite/options/sing-box\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/sing-box.nix) + + + ## services\.proxy-suite\.singBox\.urlTest\.interval @@ -4098,6 +3994,8 @@ true ## services\.proxy-suite\.zapret\.enable + + Whether to enable zapret DPI bypass\. @@ -4350,8 +4248,6 @@ true ## services\.proxy-suite\.zapret\.hostlistRules\.\*\.domains - - Domains written into the generated custom zapret hostlist file\. Must be non-empty for every hostlistRules entry\. @@ -4668,6 +4564,108 @@ list of string + +## services\.proxy-suite\.zapret\.perApp\.enable + + + +Whether to enable per-app-scoped zapret backend for perAppRouting profiles\. + + + +*Type:* +boolean + + + +*Default:* + +```nix +false +``` + + + +*Example:* + +```nix +true +``` + +*Declared by:* + - [modules/proxy-suite/options/zapret\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/zapret.nix) + + + + +## services\.proxy-suite\.zapret\.perApp\.filterMark + + + +Packet mark bit used to mark wrapped app traffic for the +dedicated per-app-scoped zapret instance\. + + + +*Type:* +signed integer + + + +*Default:* + +```nix +268435456 +``` + + + +*Example:* + +```nix +268435456 +``` + +*Declared by:* + - [modules/proxy-suite/options/zapret\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/zapret.nix) + + + + +## services\.proxy-suite\.zapret\.perApp\.qnum + + + +NFQUEUE number used by the dedicated per-app-scoped zapret instance\. +This backend runs as a second zapret daemon and should use a +queue distinct from the global zapret instance\. + + + +*Type:* +signed integer + + + +*Default:* + +```nix +201 +``` + + + +*Example:* + +```nix +201 +``` + +*Declared by:* + - [modules/proxy-suite/options/zapret\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/zapret.nix) + + + ## services\.proxy-suite\.zapret\.syncDirectRouting diff --git a/modules/proxy-suite/config.nix b/modules/proxy-suite/config.nix index 0e1aa3a..29ba004 100644 --- a/modules/proxy-suite/config.nix +++ b/modules/proxy-suite/config.nix @@ -9,7 +9,9 @@ let singBoxCfg = cfg.singBox; - appRoutingTun = cfg.appRouting.backends.tun; + globalTun = singBoxCfg.tun; + globalTproxy = singBoxCfg.tproxy; + perAppTun = singBoxCfg.tun.perApp; direct = rules.direct; mkDnsServer = @@ -54,9 +56,9 @@ let enableMixed ? false, enableTProxy ? false, enableTun ? false, - tunInterface ? singBoxCfg.tun.interface, - tunAddress ? singBoxCfg.tun.address, - tunMtu ? singBoxCfg.tun.mtu, + tunInterface ? globalTun.interface, + tunAddress ? globalTun.address, + tunMtu ? globalTun.mtu, tunAutoRoute ? true, tunAutoRedirect ? true, tunStrictRoute ? true, @@ -85,7 +87,7 @@ let type = "tproxy"; tag = "tproxy-in"; listen = "127.0.0.1"; - listen_port = singBoxCfg.tproxyPort; + listen_port = globalTproxy.port; } ++ lib.optional enableTun { type = "tun"; @@ -106,7 +108,7 @@ let type = "direct"; tag = "direct"; } - // lib.optionalAttrs useOutboundRoutingMark { routing_mark = singBoxCfg.proxyMark; } + // lib.optionalAttrs useOutboundRoutingMark { routing_mark = globalTproxy.proxyMark; } ) { type = "block"; @@ -133,9 +135,9 @@ let tunTemplate = mkConfig { enableTun = true; - tunInterface = singBoxCfg.tun.interface; - tunAddress = singBoxCfg.tun.address; - tunMtu = singBoxCfg.tun.mtu; + tunInterface = globalTun.interface; + tunAddress = globalTun.address; + tunMtu = globalTun.mtu; tunAutoRoute = true; tunAutoRedirect = true; tunStrictRoute = true; @@ -143,26 +145,26 @@ let enableClashApi = false; }; - appTunTemplate = mkConfig { + perAppTunTemplate = mkConfig { enableTun = true; - tunInterface = appRoutingTun.interface; - tunAddress = appRoutingTun.address; - tunMtu = appRoutingTun.mtu; + tunInterface = perAppTun.interface; + tunAddress = perAppTun.address; + tunMtu = perAppTun.mtu; tunAutoRoute = false; tunAutoRedirect = false; tunStrictRoute = false; forceLocalDnsViaProxy = false; # App TUN can be used alongside TProxy; mark direct egress so TProxy output # rules do not re-intercept sing-box's own packets. - useOutboundRoutingMark = singBoxCfg.tproxy.enable; + useOutboundRoutingMark = globalTproxy.enable; enableClashApi = false; }; tproxyFile = pkgs.writeText "proxy-suite-tproxy-template.json" (builtins.toJSON tproxyTemplate); tunFile = pkgs.writeText "proxy-suite-tun-template.json" (builtins.toJSON tunTemplate); - appTunFile = pkgs.writeText "proxy-suite-app-tun-template.json" (builtins.toJSON appTunTemplate); + perAppTunFile = pkgs.writeText "proxy-suite-per-app-tun-template.json" (builtins.toJSON perAppTunTemplate); in { - inherit tproxyFile tunFile appTunFile; + inherit tproxyFile tunFile perAppTunFile; } diff --git a/modules/proxy-suite/default.nix b/modules/proxy-suite/default.nix index 8c05422..61626ba 100644 --- a/modules/proxy-suite/default.nix +++ b/modules/proxy-suite/default.nix @@ -19,37 +19,35 @@ in ]; config = lib.mkIf cfg.enable ( + let + rules = import ./rules.nix { + inherit + lib + pkgs + cfg + zapret + ; + }; + configs = import ./config.nix { + inherit + lib + pkgs + cfg + rules + ; + }; + in lib.mkMerge [ - (lib.mkIf cfg.singBox.enable ( - let - rules = import ./rules.nix { - inherit - lib - pkgs - cfg - zapret - ; - }; - configs = import ./config.nix { - inherit - lib - pkgs - cfg - rules - ; - }; - in - import ./service { - inherit - config - lib - pkgs - cfg - ; - inherit (configs) tproxyFile tunFile appTunFile; - inherit (nftr) nftablesRulesFile appTproxyRulesFile appZapretRulesFile appTunChainFile ip nft; - } - )) + (import ./service { + inherit + config + lib + pkgs + cfg + ; + inherit (configs) tproxyFile tunFile perAppTunFile; + inherit (nftr) nftablesRulesFile perAppTproxyRulesFile perAppZapretRulesFile perAppTunChainFile ip nft; + }) (lib.mkIf cfg.zapret.enable ( import ./zapret.nix { @@ -59,7 +57,7 @@ in cfg zapret ; - inherit (nftr) appZapretRulesFile nft; + inherit (nftr) perAppZapretRulesFile nft; } )) diff --git a/modules/proxy-suite/nftables.nix b/modules/proxy-suite/nftables.nix index c054e21..40b09ce 100644 --- a/modules/proxy-suite/nftables.nix +++ b/modules/proxy-suite/nftables.nix @@ -7,9 +7,10 @@ let singBoxCfg = cfg.singBox; - appRoutingTun = cfg.appRouting.backends.tun; - appRoutingTproxy = cfg.appRouting.backends.tproxy; - appRoutingZapret = cfg.appRouting.backends.zapret; + globalTproxy = singBoxCfg.tproxy; + perAppTun = singBoxCfg.tun.perApp; + perAppTproxy = singBoxCfg.tproxy.perApp; + zapretApp = cfg.zapret.perApp; # Shared across all three nftables rule files that do IPv4 routing. reservedIpBlock = '' @@ -31,8 +32,8 @@ let ip daddr ${cidr} udp dport != 53 return '') subnets; - tproxyLocalSubnetLines = mkLocalSubnetLines singBoxCfg.tproxy.localSubnets; - appTproxyLocalSubnetLines = mkLocalSubnetLines appRoutingTproxy.localSubnets; + tproxyLocalSubnetLines = mkLocalSubnetLines globalTproxy.localSubnets; + perAppTproxyLocalSubnetLines = mkLocalSubnetLines perAppTproxy.localSubnets; nftablesRulesFile = pkgs.writeText "proxy-suite-tproxy.nft" '' ${reservedIpBlock} @@ -44,70 +45,70 @@ let # be skipped just because the host has an RFC1918 source address. iifname != "lo" ip saddr $RESERVED_IP return ${tproxyLocalSubnetLines} - ip protocol tcp tproxy to 127.0.0.1:${toString singBoxCfg.tproxyPort} meta mark set ${toString singBoxCfg.fwmark} - ip protocol udp tproxy to 127.0.0.1:${toString singBoxCfg.tproxyPort} meta mark set ${toString singBoxCfg.fwmark} + ip protocol tcp tproxy to 127.0.0.1:${toString globalTproxy.port} meta mark set ${toString globalTproxy.fwmark} + ip protocol udp tproxy to 127.0.0.1:${toString globalTproxy.port} meta mark set ${toString globalTproxy.fwmark} } chain output { type route hook output priority mangle; policy accept; ip daddr $RESERVED_IP return ${tproxyLocalSubnetLines} - meta mark ${toString singBoxCfg.proxyMark} return -${lib.optionalString appRoutingTun.enable " meta mark ${toString appRoutingTun.fwmark} return\n"}${lib.optionalString appRoutingTproxy.enable " meta mark ${toString appRoutingTproxy.fwmark} return\n"} ip protocol tcp meta mark set ${toString singBoxCfg.fwmark} - ip protocol udp meta mark set ${toString singBoxCfg.fwmark} + 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} + ip protocol udp meta mark set ${toString globalTproxy.fwmark} } } ''; - appTproxyRulesFile = pkgs.writeText "proxy-suite-app-tproxy.nft" '' + perAppTproxyRulesFile = pkgs.writeText "proxy-suite-per-app-tproxy.nft" '' ${reservedIpBlock} - table ip proxy_suite_app_tproxy { + table ip proxy_suite_per_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 appRoutingTproxy.fwmark} meta mark set ${toString appRoutingTproxy.fwmark} - meta mark ${toString appRoutingTproxy.fwmark} ip protocol tcp tproxy to 127.0.0.1:${toString singBoxCfg.tproxyPort} - meta mark ${toString appRoutingTproxy.fwmark} ip protocol udp tproxy to 127.0.0.1:${toString singBoxCfg.tproxyPort} + ${perAppTproxyLocalSubnetLines} + ct mark ${toString perAppTproxy.fwmark} meta mark set ${toString perAppTproxy.fwmark} + meta mark ${toString perAppTproxy.fwmark} ip protocol tcp tproxy to 127.0.0.1:${toString globalTproxy.port} + meta mark ${toString perAppTproxy.fwmark} ip protocol udp tproxy to 127.0.0.1:${toString globalTproxy.port} } chain output { type route hook output priority mangle; policy accept; ip daddr $RESERVED_IP return - ${appTproxyLocalSubnetLines} - meta mark ${toString singBoxCfg.proxyMark} return -${lib.optionalString appRoutingTun.enable " meta mark ${toString appRoutingTun.fwmark} return\n"} ct mark ${toString appRoutingTproxy.fwmark} meta mark set ${toString appRoutingTproxy.fwmark} - meta mark ${toString appRoutingTproxy.fwmark} return + ${perAppTproxyLocalSubnetLines} + meta mark ${toString globalTproxy.proxyMark} return +${lib.optionalString perAppTun.enable " meta mark ${toString perAppTun.fwmark} return\n"} ct mark ${toString perAppTproxy.fwmark} meta mark set ${toString perAppTproxy.fwmark} + meta mark ${toString perAppTproxy.fwmark} return } } ''; - appZapretRulesFile = pkgs.writeText "proxy-suite-app-zapret.nft" '' - table inet proxy_suite_app_zapret_mark { + perAppZapretRulesFile = pkgs.writeText "proxy-suite-per-app-zapret.nft" '' + table inet proxy_suite_per_app_zapret_mark { chain prerouting { type filter hook prerouting priority -103; policy accept; - ct mark and ${toString appRoutingZapret.filterMark} == ${toString appRoutingZapret.filterMark} meta mark set meta mark or ${toString appRoutingZapret.filterMark} + ct mark and ${toString zapretApp.filterMark} == ${toString zapretApp.filterMark} meta mark set meta mark or ${toString zapretApp.filterMark} } chain output { type route hook output priority -103; policy accept; - ct mark and ${toString appRoutingZapret.filterMark} == ${toString appRoutingZapret.filterMark} meta mark set meta mark or ${toString appRoutingZapret.filterMark} - meta mark and ${toString appRoutingZapret.filterMark} == ${toString appRoutingZapret.filterMark} return + ct mark and ${toString zapretApp.filterMark} == ${toString zapretApp.filterMark} meta mark set meta mark or ${toString zapretApp.filterMark} + meta mark and ${toString zapretApp.filterMark} == ${toString zapretApp.filterMark} return } } ''; - appTunChainFile = pkgs.writeText "proxy-suite-app-tun-chain.nft" '' + perAppTunChainFile = pkgs.writeText "proxy-suite-per-app-tun-chain.nft" '' ${reservedIpBlock} - table inet proxy_suite_app_tun { + table inet proxy_suite_per_app_tun { chain output { type route hook output priority mangle; policy accept; ip daddr $RESERVED_IP return ${lib.concatMapStrings (cidr: '' ip daddr ${cidr} return - '') appRoutingTun.localSubnets} - ct mark ${toString appRoutingTun.fwmark} meta mark set ${toString appRoutingTun.fwmark} - meta mark ${toString appRoutingTun.fwmark} return + '') perAppTun.localSubnets} + ct mark ${toString perAppTun.fwmark} meta mark set ${toString perAppTun.fwmark} + meta mark ${toString perAppTun.fwmark} return } } ''; @@ -117,5 +118,5 @@ ${lib.concatMapStrings (cidr: '' in { - inherit nftablesRulesFile appTproxyRulesFile appZapretRulesFile appTunChainFile ip nft; + inherit nftablesRulesFile perAppTproxyRulesFile perAppZapretRulesFile perAppTunChainFile ip nft; } diff --git a/modules/proxy-suite/options/app-routing.nix b/modules/proxy-suite/options/app-routing.nix deleted file mode 100644 index 0fded13..0000000 --- a/modules/proxy-suite/options/app-routing.nix +++ /dev/null @@ -1,231 +0,0 @@ -# Per-app routing options (backends and profiles). -{ lib, ... }: - -let - inherit (lib) mkOption mkEnableOption types; - t = import ./types.nix { inherit lib; }; -in -{ - options.services.proxy-suite.appRouting = { - enable = mkEnableOption "per-app routing helpers via proxy-ctl wrap"; - - createDefaultProfiles = mkOption { - type = types.bool; - default = false; - description = '' - Whether to automatically add curated appRouting profiles. - - This is opt-in. Generated defaults are appended only when no - user-defined profile with the same name already exists. - - Current curated defaults: - - `proxychains`: route = "proxychains" - - `tun`: route = "tun" when appRouting.backends.tun.enable = true - - `tproxy`: route = "tproxy" when appRouting.backends.tproxy.enable = true - - `zapret`: route = "zapret" when appRouting.backends.zapret.enable = true - and zapret.enable = true - - This makes `proxy-ctl wrap proxychains -- ` available - without defining the profile manually, and similarly exposes - `proxy-ctl wrap tun -- ` or - `proxy-ctl wrap tproxy -- ` or - `proxy-ctl wrap zapret -- ` when the corresponding backend - is enabled. - ''; - example = true; - }; - - profiles = mkOption { - type = types.listOf t.appRoutingProfileType; - default = [ ]; - description = '' - Named per-app route profiles consumed by `proxy-ctl wrap`. - - This initial implementation supports: - - "direct" for running a command unchanged - - "proxychains" for TCP apps that can use an LD_PRELOAD wrapper - 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 - - "zapret" for app-scoped zapret handling through a separate - zapret instance without changing the app's network path or exit IP - - 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. The "tproxy" route depends on - appRouting.backends.tproxy.enable = true. The "zapret" route depends - on appRouting.backends.zapret.enable = true and zapret.enable = true. - - When createDefaultProfiles = true, curated defaults are added on top - of this list unless a user-defined profile already uses the same - name. - ''; - example = [ - { - name = "steam-browser"; - route = "proxychains"; - } - { - name = "native-direct"; - route = "direct"; - } - ]; - }; - - proxychains = { - enable = mkEnableOption "proxychains-backed appRouting profiles"; - - quiet = mkOption { - type = types.bool; - default = true; - description = '' - Whether generated proxychains wrappers should suppress their normal - startup chatter. This maps to proxychains-ng quiet_mode. - ''; - example = true; - }; - - proxyDns = mkOption { - type = types.bool; - default = true; - description = '' - Whether generated proxychains wrappers should resolve DNS through - the proxy instead of the local resolver. This maps to proxychains-ng - proxy_dns. - ''; - example = true; - }; - }; - - backends = { - tun = { - enable = mkEnableOption "app-scoped TUN backend for appRouting profiles"; - - interface = mkOption { - type = types.str; - default = "psapptun0"; - description = '' - Name of the dedicated app-routing TUN interface used by - proxy-suite-app-tun. - ''; - example = "psapptun0"; - }; - - address = mkOption { - type = types.str; - default = "172.20.0.1/30"; - description = '' - Address assigned to the app-routing TUN interface in CIDR - notation. - ''; - example = "172.20.0.1/30"; - }; - - mtu = mkOption { - type = types.int; - default = 1400; - description = '' - MTU for the app-routing TUN interface. - ''; - example = 1400; - }; - - fwmark = mkOption { - type = types.int; - default = 16; - description = '' - Packet mark used to steer wrapped app traffic into the app-routing - TUN policy-routing table. - ''; - example = 16; - }; - - routeTable = mkOption { - type = types.int; - default = 101; - description = '' - Policy-routing table used by the app-routing TUN backend. - ''; - example = 101; - }; - - localSubnets = mkOption { - type = types.listOf types.str; - default = [ "192.168.0.0/16" ]; - description = '' - Destination subnets that should bypass the app-routing TUN mark, - so wrapped apps can still reach local LAN resources directly. - ''; - example = [ - "192.168.0.0/16" - "10.0.0.0/8" - ]; - }; - }; - - 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" - ]; - }; - }; - - zapret = { - enable = mkEnableOption "app-scoped zapret backend for appRouting profiles"; - - filterMark = mkOption { - type = types.int; - default = 268435456; - description = '' - Packet mark bit used to mark wrapped app traffic for the - dedicated app-scoped zapret instance. - ''; - example = 268435456; - }; - - qnum = mkOption { - type = types.int; - default = 201; - description = '' - NFQUEUE number used by the dedicated app-scoped zapret instance. - This backend runs as a second zapret daemon and should use a - queue distinct from the global zapret instance. - ''; - example = 201; - }; - }; - }; - - }; -} diff --git a/modules/proxy-suite/options/default.nix b/modules/proxy-suite/options/default.nix index 9d0c54a..ed36dea 100644 --- a/modules/proxy-suite/options/default.nix +++ b/modules/proxy-suite/options/default.nix @@ -4,7 +4,7 @@ imports = [ ./other.nix ./sing-box.nix - ./app-routing.nix + ./per-app-routing.nix ./zapret.nix ]; } diff --git a/modules/proxy-suite/options/per-app-routing.nix b/modules/proxy-suite/options/per-app-routing.nix new file mode 100644 index 0000000..e56a6a1 --- /dev/null +++ b/modules/proxy-suite/options/per-app-routing.nix @@ -0,0 +1,102 @@ +# Per-app routing options (backends and profiles). +{ lib, ... }: + +let + inherit (lib) mkOption mkEnableOption types; + t = import ./types.nix { inherit lib; }; +in +{ + options.services.proxy-suite.perAppRouting = { + enable = mkEnableOption "per-app routing helpers via proxy-ctl wrap"; + + createDefaultProfiles = mkOption { + type = types.bool; + default = false; + description = '' + Whether to automatically add curated perAppRouting profiles. + + This is opt-in. Generated defaults are appended only when no + user-defined profile with the same name already exists. + + Current curated defaults: + - `proxychains`: route = "proxychains" + - `tun`: route = "tun" when singBox.tun.perApp.enable = true + - `tproxy`: route = "tproxy" when singBox.tproxy.perApp.enable = true + - `zapret`: route = "zapret" when zapret.perApp.enable = true and + zapret.enable = true + + This makes `proxy-ctl wrap proxychains -- ` available + without defining the profile manually, and similarly exposes + `proxy-ctl wrap tun -- ` or + `proxy-ctl wrap tproxy -- ` or + `proxy-ctl wrap zapret -- ` when the corresponding backend + is enabled. + ''; + example = true; + }; + + profiles = mkOption { + type = types.listOf t.perAppRoutingProfileType; + default = [ ]; + description = '' + Named per-app route profiles consumed by `proxy-ctl wrap`. + + This initial implementation supports: + - "direct" for running a command unchanged + - "proxychains" for TCP apps that can use an LD_PRELOAD wrapper + instead of global TUN or TProxy interception + - "tun" for per-app-scoped policy routing into the dedicated app TUN + backend + - "tproxy" for per-app-scoped transparent interception through the + dedicated app TProxy backend + - "zapret" for per-app-scoped zapret handling through a separate + zapret instance without changing the app's network path or exit IP + + proxychains-based wrapping depends on singBox.enable = true and the + local proxy-suite mixed proxy listener provided by sing-box. The + "tun" route depends on singBox.tun.perApp.enable = true. The "tproxy" + route depends on singBox.tproxy.perApp.enable = true. The "zapret" + route depends on zapret.perApp.enable = true and zapret.enable = true. + + When createDefaultProfiles = true, curated defaults are added on top + of this list unless a user-defined profile already uses the same + name. + ''; + example = [ + { + name = "steam-browser"; + route = "proxychains"; + } + { + name = "native-direct"; + route = "direct"; + } + ]; + }; + + proxychains = { + enable = mkEnableOption "proxychains-backed perAppRouting profiles"; + + quiet = mkOption { + type = types.bool; + default = true; + description = '' + Whether generated proxychains wrappers should suppress their normal + startup chatter. This maps to proxychains-ng quiet_mode. + ''; + example = true; + }; + + proxyDns = mkOption { + type = types.bool; + default = true; + description = '' + Whether generated proxychains wrappers should resolve DNS through + the proxy instead of the local resolver. This maps to proxychains-ng + proxy_dns. + ''; + example = true; + }; + }; + }; +} diff --git a/modules/proxy-suite/options/sing-box.nix b/modules/proxy-suite/options/sing-box.nix index fd3ec8e..f11e81b 100644 --- a/modules/proxy-suite/options/sing-box.nix +++ b/modules/proxy-suite/options/sing-box.nix @@ -4,6 +4,38 @@ let inherit (lib) mkOption mkEnableOption types; t = import ./types.nix { inherit lib; }; + + mkTunCommonFields = + { + interfaceDefault, + interfaceDescription, + addressDefault, + addressDescription, + mtuDefault, + mtuDescription, + }: + { + interface = mkOption { + type = types.str; + default = interfaceDefault; + description = interfaceDescription; + example = interfaceDefault; + }; + + address = mkOption { + type = types.str; + default = addressDefault; + description = addressDescription; + example = addressDefault; + }; + + mtu = mkOption { + type = types.int; + default = mtuDefault; + description = mtuDescription; + example = mtuDefault; + }; + }; in { options.services.proxy-suite.singBox = { @@ -12,8 +44,8 @@ in default = true; description = '' Whether to configure and run sing-box services for proxy-suite. - When disabled, singBox.* options are ignored even if proxy-suite itself - is enabled. + When disabled, sing-box services and generated sing-box configs are + skipped even if proxy-suite itself is enabled. ''; }; @@ -183,49 +215,6 @@ in example = 1080; }; - tproxyPort = mkOption { - type = types.port; - default = 1081; - description = '' - Local listen port for sing-box's TProxy inbound. - nftables redirection created by proxy-suite-tproxy sends intercepted - TCP/UDP traffic to this port. - ''; - example = 1081; - }; - - fwmark = mkOption { - type = types.int; - default = 1; - description = '' - Mark applied to intercepted packets in TProxy mode. - A matching `ip rule` routes this mark to singBox.routeTable, which - points traffic to loopback for local proxy processing. - ''; - example = 1; - }; - - proxyMark = mkOption { - type = types.int; - default = 2; - description = '' - Mark applied to sing-box egress packets in TProxy mode so they bypass - re-interception and do not loop back into the transparent proxy path. - ''; - example = 2; - }; - - routeTable = mkOption { - type = types.int; - default = 100; - description = '' - Policy-routing table number used for TProxy interception flow. - The module installs a local default route in this table and binds it - to singBox.fwmark. - ''; - example = 100; - }; - proxyByDefault = mkOption { type = types.bool; default = true; @@ -252,7 +241,7 @@ in This resolver is also used as sing-box route.default_domain_resolver. The module keeps detour policy automatic: in mixed/TProxy mode and - app-routing TUN mode, "local" stays on the direct path (without an + per-app-routing TUN mode, "local" stays on the direct path (without an explicit detour); in global TUN mode, it is forced through the proxy to avoid auto_redirect conflicts. ''; @@ -285,7 +274,7 @@ in }; tun = { - enable = mkEnableOption "TUN mode service (opt-in, mutually exclusive with proxy-suite-tproxy)"; + enable = mkEnableOption "global sing-box TUN mode service"; autostart = mkOption { type = types.bool; @@ -298,30 +287,67 @@ in example = true; }; - interface = mkOption { - type = types.str; - default = "singtun0"; - description = "Name of the TUN interface created by proxy-suite-tun."; - example = "singtun0"; - }; + perApp = { + enable = mkEnableOption "per-app-scoped sing-box TUN backend for perAppRouting profiles"; - address = mkOption { - type = types.str; - default = "172.19.0.1/30"; - description = "Address assigned to the TUN interface in CIDR notation."; - example = "172.19.0.1/30"; - }; + fwmark = mkOption { + type = types.int; + default = 16; + description = '' + Packet mark used to steer wrapped app traffic into the per-app-scoped + TUN policy-routing table. + ''; + example = 16; + }; - mtu = mkOption { - type = types.int; - default = 1400; - description = "MTU for the TUN interface created by proxy-suite-tun."; - example = 1400; + routeTable = mkOption { + type = types.int; + default = 101; + description = '' + Policy-routing table used by the per-app-scoped TUN backend. + ''; + example = 101; + }; + + localSubnets = mkOption { + type = types.listOf types.str; + default = [ "192.168.0.0/16" ]; + description = '' + Destination subnets that should bypass the per-app-scoped TUN mark, + so wrapped apps can still reach local LAN resources directly. + ''; + example = [ + "192.168.0.0/16" + "10.0.0.0/8" + ]; + }; + } + // mkTunCommonFields { + interfaceDefault = "psperapptun0"; + interfaceDescription = '' + Name of the dedicated per-app-routing TUN interface used by + proxy-suite-per-app-tun. + ''; + addressDefault = "172.20.0.1/30"; + addressDescription = '' + Address assigned to the dedicated per-app-routing TUN interface in CIDR + notation. + ''; + mtuDefault = 1400; + mtuDescription = "MTU for the dedicated per-app-routing TUN interface."; }; + } + // mkTunCommonFields { + interfaceDefault = "singtun0"; + interfaceDescription = "Name of the TUN interface created by proxy-suite-tun."; + addressDefault = "172.19.0.1/30"; + addressDescription = "Address assigned to the global TUN interface in CIDR notation."; + mtuDefault = 1400; + mtuDescription = "MTU for the global TUN interface created by proxy-suite-tun."; }; tproxy = { - enable = mkEnableOption "TProxy mode service (opt-in, transparent proxy via nftables)"; + enable = mkEnableOption "global sing-box TProxy mode service"; autostart = mkOption { type = types.bool; @@ -334,11 +360,57 @@ in example = true; }; + port = mkOption { + type = types.port; + default = 1081; + description = '' + Local listen port for sing-box's TProxy inbound. + nftables redirection created by proxy-suite-tproxy sends intercepted + TCP/UDP traffic to this port. + ''; + example = 1081; + }; + + fwmark = mkOption { + type = types.int; + default = 1; + description = '' + Mark applied to intercepted packets in global TProxy mode. + A matching `ip rule` routes this mark to + singBox.tproxy.routeTable, which points traffic to + loopback for local proxy processing. + ''; + example = 1; + }; + + proxyMark = mkOption { + type = types.int; + default = 2; + description = '' + Mark applied to sing-box egress packets in global TProxy mode so + they bypass re-interception and do not loop back into the + transparent proxy path. + ''; + example = 2; + }; + + routeTable = mkOption { + type = types.int; + default = 100; + description = '' + Policy-routing table number used for global TProxy interception flow. + The module installs a local default route in this table and binds it + to singBox.tproxy.fwmark. + ''; + example = 100; + }; + localSubnets = mkOption { type = types.listOf types.str; default = [ "192.168.0.0/16" ]; description = '' - Subnets whose traffic bypasses TProxy interception, except DNS (port 53). + Subnets whose traffic bypasses global TProxy interception, except + DNS (port 53). Typically this should include your LAN subnet(s). VM bridge networks should usually go here too, or use zapret.cidrExemption for @@ -349,6 +421,42 @@ in "10.0.0.0/8" ]; }; + + perApp = { + enable = mkEnableOption "per-app-scoped sing-box TProxy backend for perAppRouting profiles"; + + fwmark = mkOption { + type = types.int; + default = 17; + description = '' + Packet mark used to steer wrapped app traffic into the per-app-scoped + TProxy policy-routing table. + ''; + example = 17; + }; + + routeTable = mkOption { + type = types.int; + default = 102; + description = '' + Policy-routing table used by the per-app-scoped TProxy backend. + ''; + example = 102; + }; + + localSubnets = mkOption { + type = types.listOf types.str; + default = [ "192.168.0.0/16" ]; + description = '' + Subnets whose traffic bypasses per-app-scoped TProxy interception, + except DNS (port 53). + ''; + example = [ + "192.168.0.0/16" + "10.0.0.0/8" + ]; + }; + }; }; routing = { diff --git a/modules/proxy-suite/options/types.nix b/modules/proxy-suite/options/types.nix index 6606d8b..ea46180 100644 --- a/modules/proxy-suite/options/types.nix +++ b/modules/proxy-suite/options/types.nix @@ -284,13 +284,13 @@ let }; }; - appRoutingProfileType = types.submodule { + perAppRoutingProfileType = types.submodule { options = { name = mkOption { type = types.strMatching "^[a-z0-9][a-z0-9-]*$"; description = '' Profile name used by `proxy-ctl wrap -- `. - Must be unique within appRouting.profiles. + Must be unique within perAppRouting.profiles. ''; example = "steam-browser"; }; @@ -310,14 +310,14 @@ let - "direct": run the command unchanged. - "proxychains": run the command through proxychains-ng using the local proxy-suite mixed SOCKS endpoint. - - "tun": launch the command in the dedicated app-routing TUN slice so + - "tun": launch the command in the dedicated per-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 + - "tproxy": launch the command in the dedicated per-app-routing TProxy slice so only that app's traffic is transparently intercepted by the local sing-box TProxy inbound. - - "zapret": launch the command in the dedicated app-routing zapret + - "zapret": launch the command in the dedicated per-app-routing zapret slice so only that app's traffic is handled by the separate - app-scoped zapret instance. + per-app-scoped zapret instance. Additional route backends may be added in the future. ''; @@ -336,6 +336,6 @@ in dnsUpstreamType zapretPresetType zapretHostlistRuleType - appRoutingProfileType + perAppRoutingProfileType ; } diff --git a/modules/proxy-suite/options/zapret.nix b/modules/proxy-suite/options/zapret.nix index 368f095..16ca7e5 100644 --- a/modules/proxy-suite/options/zapret.nix +++ b/modules/proxy-suite/options/zapret.nix @@ -9,6 +9,31 @@ in options.services.proxy-suite.zapret = { enable = mkEnableOption "zapret DPI bypass"; + perApp = { + enable = mkEnableOption "per-app-scoped zapret backend for perAppRouting profiles"; + + filterMark = mkOption { + type = types.int; + default = 268435456; + description = '' + Packet mark bit used to mark wrapped app traffic for the + dedicated per-app-scoped zapret instance. + ''; + example = 268435456; + }; + + qnum = mkOption { + type = types.int; + default = 201; + description = '' + NFQUEUE number used by the dedicated per-app-scoped zapret instance. + This backend runs as a second zapret daemon and should use a + queue distinct from the global zapret instance. + ''; + example = 201; + }; + }; + syncDirectRouting = mkOption { type = types.bool; default = true; diff --git a/modules/proxy-suite/service-assertions.nix b/modules/proxy-suite/service-assertions.nix index 9a15f89..5e41951 100644 --- a/modules/proxy-suite/service-assertions.nix +++ b/modules/proxy-suite/service-assertions.nix @@ -3,156 +3,204 @@ cfg, singBoxCfg, tgWsProxyCfg, - appRoutingCfg, - appRoutingTun, - appRoutingTproxy, - appRoutingZapret, + perAppRoutingCfg, + perAppRoutingTun, + perAppRoutingTproxy, + perAppZapretCfg, builtinTags, outboundTags, invalidRoutingTargets, - effectiveAppRoutingProfileNames, + effectivePerAppRoutingProfileNames, hasProxychainsProfiles, hasTunProfiles, hasTproxyProfiles, hasZapretProfiles, }: +let + globalTun = singBoxCfg.tun; + globalTproxy = singBoxCfg.tproxy; +in [ { assertion = !singBoxCfg.enable || (singBoxCfg.outbounds != [ ] || singBoxCfg.subscriptions != [ ]); message = "proxy-suite: at least one outbound or subscription is required when singBox.enable = true"; } { - assertion = builtins.length outboundTags == builtins.length (lib.unique outboundTags); + assertion = !singBoxCfg.enable || builtins.length outboundTags == builtins.length (lib.unique outboundTags); message = "proxy-suite: outbound tags must be unique"; } { - assertion = builtins.all (tag: !builtins.elem tag builtinTags) outboundTags; + assertion = !singBoxCfg.enable || builtins.all (tag: !builtins.elem tag builtinTags) outboundTags; message = "proxy-suite: outbound tags must not use reserved names: proxy, direct, block"; } { - assertion = invalidRoutingTargets == [ ]; + assertion = !singBoxCfg.enable || invalidRoutingTargets == [ ]; message = "proxy-suite: routing.rules reference unknown outbound tag(s): ${lib.concatStringsSep ", " invalidRoutingTargets}"; } { - assertion = !(singBoxCfg.tproxy.enable && singBoxCfg.tproxy.autostart && singBoxCfg.tun.enable && singBoxCfg.tun.autostart); - message = "proxy-suite: singBox.tproxy.autostart and singBox.tun.autostart cannot both be enabled at the same time"; + assertion = !globalTun.enable || singBoxCfg.enable; + message = "proxy-suite: singBox.tun.enable requires singBox.enable = true"; } { - assertion = appRoutingCfg.enable || appRoutingCfg.profiles == [ ]; - message = "proxy-suite: appRouting.profiles requires appRouting.enable = true"; + assertion = !globalTproxy.enable || singBoxCfg.enable; + message = "proxy-suite: singBox.tproxy.enable requires singBox.enable = true"; } { - assertion = !appRoutingCfg.proxychains.enable || appRoutingCfg.enable; - message = "proxy-suite: appRouting.proxychains.enable requires appRouting.enable = true"; + assertion = !globalTun.autostart || globalTun.enable; + message = "proxy-suite: singBox.tun.autostart requires singBox.tun.enable = true"; } { - assertion = builtins.length effectiveAppRoutingProfileNames == builtins.length (lib.unique effectiveAppRoutingProfileNames); - message = "proxy-suite: appRouting profile names must be unique"; + assertion = !globalTproxy.autostart || globalTproxy.enable; + message = "proxy-suite: singBox.tproxy.autostart requires singBox.tproxy.enable = true"; } { - assertion = !hasProxychainsProfiles || appRoutingCfg.proxychains.enable; - message = "proxy-suite: route=proxychains in appRouting.profiles requires appRouting.proxychains.enable = true"; + assertion = !(globalTproxy.enable && globalTproxy.autostart && globalTun.enable && globalTun.autostart); + message = + "proxy-suite: singBox.tproxy.autostart and singBox.tun.autostart cannot both be enabled at the same time"; } { - assertion = !appRoutingTun.enable || appRoutingCfg.enable; - message = "proxy-suite: appRouting.backends.tun.enable requires appRouting.enable = true"; + assertion = perAppRoutingCfg.enable || perAppRoutingCfg.profiles == [ ]; + message = "proxy-suite: perAppRouting.profiles requires perAppRouting.enable = true"; } { - assertion = !(hasTunProfiles && !appRoutingTun.enable); - message = "proxy-suite: route=tun in appRouting profiles requires appRouting.backends.tun.enable = true"; + assertion = !perAppRoutingCfg.proxychains.enable || perAppRoutingCfg.enable; + message = "proxy-suite: perAppRouting.proxychains.enable requires perAppRouting.enable = true"; } { - assertion = !appRoutingTproxy.enable || appRoutingCfg.enable; - message = "proxy-suite: appRouting.backends.tproxy.enable requires appRouting.enable = true"; + assertion = !perAppRoutingCfg.proxychains.enable || singBoxCfg.enable; + message = "proxy-suite: perAppRouting.proxychains.enable requires singBox.enable = true"; } { - assertion = !(hasTproxyProfiles && !appRoutingTproxy.enable); - message = "proxy-suite: route=tproxy in appRouting profiles requires appRouting.backends.tproxy.enable = true"; + assertion = builtins.length effectivePerAppRoutingProfileNames == builtins.length (lib.unique effectivePerAppRoutingProfileNames); + message = "proxy-suite: perAppRouting profile names must be unique"; } { - assertion = !appRoutingZapret.enable || appRoutingCfg.enable; - message = "proxy-suite: appRouting.backends.zapretgWsProxyCfg.enable requires appRouting.enable = true"; + assertion = !hasProxychainsProfiles || perAppRoutingCfg.proxychains.enable; + message = "proxy-suite: route=proxychains in perAppRouting.profiles requires perAppRouting.proxychains.enable = true"; } { - assertion = !appRoutingZapret.enable || cfg.zapret.enable; - message = "proxy-suite: appRouting.backends.zapretgWsProxyCfg.enable requires zapretgWsProxyCfg.enable = true"; + assertion = !hasProxychainsProfiles || singBoxCfg.enable; + message = "proxy-suite: route=proxychains in perAppRouting.profiles requires singBox.enable = true"; } { - assertion = !(hasZapretProfiles && (!appRoutingZapret.enable || !cfg.zapret.enable)); - message = "proxy-suite: route=zapret in appRouting profiles requires appRouting.backends.zapretgWsProxyCfg.enable = true and zapretgWsProxyCfg.enable = true"; + assertion = !perAppRoutingTun.enable || perAppRoutingCfg.enable; + message = "proxy-suite: singBox.tun.perApp.enable requires perAppRouting.enable = true"; } { - assertion = !(appRoutingTun.enable && singBoxCfg.tproxy.enable && appRoutingTun.fwmark == singBoxCfg.fwmark); - message = "proxy-suite: appRouting.backends.tun.fwmark must differ from singBox.fwmark when singBox.tproxy.enable = true"; + assertion = !perAppRoutingTun.enable || singBoxCfg.enable; + message = "proxy-suite: singBox.tun.perApp.enable requires singBox.enable = true"; } { - assertion = !(appRoutingTun.enable && singBoxCfg.tproxy.enable && appRoutingTun.fwmark == singBoxCfg.proxyMark); - message = "proxy-suite: appRouting.backends.tun.fwmark must differ from singBox.proxyMark when singBox.tproxy.enable = true"; + assertion = !(hasTunProfiles && !perAppRoutingTun.enable); + message = "proxy-suite: route=tun in perAppRouting.profiles requires singBox.tun.perApp.enable = true"; } { - assertion = !(appRoutingTun.enable && singBoxCfg.tproxy.enable && appRoutingTun.routeTable == singBoxCfg.routeTable); - message = "proxy-suite: appRouting.backends.tun.routeTable must differ from singBox.routeTable when singBox.tproxy.enable = true"; + assertion = !hasTunProfiles || singBoxCfg.enable; + message = "proxy-suite: route=tun in perAppRouting.profiles requires singBox.enable = true"; } { - assertion = !(appRoutingTproxy.enable && appRoutingTproxy.fwmark == singBoxCfg.fwmark); - message = "proxy-suite: appRouting.backends.tproxy.fwmark must differ from singBox.fwmark"; + assertion = !perAppRoutingTproxy.enable || perAppRoutingCfg.enable; + message = "proxy-suite: singBox.tproxy.perApp.enable requires perAppRouting.enable = true"; } { - assertion = !(appRoutingTproxy.enable && appRoutingTproxy.fwmark == singBoxCfg.proxyMark); - message = "proxy-suite: appRouting.backends.tproxy.fwmark must differ from singBox.proxyMark"; + assertion = !perAppRoutingTproxy.enable || singBoxCfg.enable; + message = "proxy-suite: singBox.tproxy.perApp.enable requires singBox.enable = true"; } { - assertion = !(appRoutingTproxy.enable && appRoutingTproxy.routeTable == singBoxCfg.routeTable); - message = "proxy-suite: appRouting.backends.tproxy.routeTable must differ from singBox.routeTable"; + assertion = !(hasTproxyProfiles && !perAppRoutingTproxy.enable); + message = "proxy-suite: route=tproxy in perAppRouting.profiles requires singBox.tproxy.perApp.enable = true"; } { - assertion = !(appRoutingTun.enable && appRoutingTproxy.enable && appRoutingTun.fwmark == appRoutingTproxy.fwmark); - message = "proxy-suite: appRouting.backends.tun.fwmark and appRouting.backends.tproxy.fwmark must differ"; + assertion = !hasTproxyProfiles || singBoxCfg.enable; + message = "proxy-suite: route=tproxy in perAppRouting.profiles requires singBox.enable = true"; } { - assertion = !(appRoutingTun.enable && appRoutingTproxy.enable && appRoutingTun.routeTable == appRoutingTproxy.routeTable); - message = "proxy-suite: appRouting.backends.tun.routeTable and appRouting.backends.tproxy.routeTable must differ"; + assertion = !perAppZapretCfg.enable || perAppRoutingCfg.enable; + message = "proxy-suite: zapret.perApp.enable requires perAppRouting.enable = true"; } { - assertion = !(appRoutingZapret.enable && appRoutingZapret.filterMark == singBoxCfg.fwmark); - message = "proxy-suite: appRouting.backends.zapretgWsProxyCfg.filterMark must differ from singBox.fwmark"; + assertion = !perAppZapretCfg.enable || cfg.zapret.enable; + message = "proxy-suite: zapret.perApp.enable requires zapret.enable = true"; } { - assertion = !(appRoutingZapret.enable && appRoutingZapret.filterMark == singBoxCfg.proxyMark); - message = "proxy-suite: appRouting.backends.zapretgWsProxyCfg.filterMark must differ from singBox.proxyMark"; + assertion = !(hasZapretProfiles && (!perAppZapretCfg.enable || !cfg.zapret.enable)); + message = "proxy-suite: route=zapret in perAppRouting.profiles requires zapret.perApp.enable = true and zapret.enable = true"; } { - assertion = !(appRoutingTun.enable && appRoutingZapret.enable && appRoutingTun.fwmark == appRoutingZapret.filterMark); - message = "proxy-suite: appRouting.backends.tun.fwmark and appRouting.backends.zapretgWsProxyCfg.filterMark must differ"; + assertion = !(perAppRoutingTun.enable && globalTproxy.enable && perAppRoutingTun.fwmark == globalTproxy.fwmark); + message = + "proxy-suite: singBox.tun.perApp.fwmark must differ from singBox.tproxy.fwmark when global TProxy is enabled"; } { - assertion = !(appRoutingTproxy.enable && appRoutingZapret.enable && appRoutingTproxy.fwmark == appRoutingZapret.filterMark); - message = "proxy-suite: appRouting.backends.tproxy.fwmark and appRouting.backends.zapretgWsProxyCfg.filterMark must differ"; + assertion = !(perAppRoutingTun.enable && globalTproxy.enable && perAppRoutingTun.fwmark == globalTproxy.proxyMark); + message = + "proxy-suite: singBox.tun.perApp.fwmark must differ from singBox.tproxy.proxyMark when global TProxy is enabled"; } { - assertion = !(appRoutingZapret.enable && builtins.elem appRoutingZapret.filterMark [ 536870912 1073741824 ]); - message = "proxy-suite: appRouting.backends.zapretgWsProxyCfg.filterMark must not use zapret internal desync mark bits"; + assertion = !(perAppRoutingTun.enable && globalTproxy.enable && perAppRoutingTun.routeTable == globalTproxy.routeTable); + message = + "proxy-suite: singBox.tun.perApp.routeTable must differ from singBox.tproxy.routeTable when global TProxy is enabled"; } { - assertion = !(appRoutingZapret.enable && builtins.elem singBoxCfg.fwmark [ 67108864 134217728 ]); - message = "proxy-suite: singBox.fwmark must not use app-zapret internal desync mark bits"; + assertion = !(perAppRoutingTproxy.enable && perAppRoutingTproxy.fwmark == globalTproxy.fwmark); + message = "proxy-suite: singBox.tproxy.perApp.fwmark must differ from singBox.tproxy.fwmark"; } { - assertion = !(appRoutingZapret.enable && builtins.elem singBoxCfg.proxyMark [ 67108864 134217728 ]); - message = "proxy-suite: singBox.proxyMark must not use app-zapret internal desync mark bits"; + assertion = !(perAppRoutingTproxy.enable && perAppRoutingTproxy.fwmark == globalTproxy.proxyMark); + message = "proxy-suite: singBox.tproxy.perApp.fwmark must differ from singBox.tproxy.proxyMark"; } { - assertion = !(appRoutingTun.enable && appRoutingZapret.enable && builtins.elem appRoutingTun.fwmark [ 67108864 134217728 ]); - message = "proxy-suite: appRouting.backends.tun.fwmark must not use app-zapret internal desync mark bits"; + assertion = !(perAppRoutingTproxy.enable && perAppRoutingTproxy.routeTable == globalTproxy.routeTable); + message = "proxy-suite: singBox.tproxy.perApp.routeTable must differ from singBox.tproxy.routeTable"; } { - assertion = !(appRoutingTproxy.enable && appRoutingZapret.enable && builtins.elem appRoutingTproxy.fwmark [ 67108864 134217728 ]); - message = "proxy-suite: appRouting.backends.tproxy.fwmark must not use app-zapret internal desync mark bits"; + assertion = !(perAppRoutingTun.enable && perAppRoutingTproxy.enable && perAppRoutingTun.fwmark == perAppRoutingTproxy.fwmark); + message = "proxy-suite: singBox.tun.perApp.fwmark and singBox.tproxy.perApp.fwmark must differ"; } { - assertion = !(appRoutingZapret.enable && builtins.elem appRoutingZapret.filterMark [ 67108864 134217728 ]); - message = "proxy-suite: appRouting.backends.zapretgWsProxyCfg.filterMark must not use app-zapret internal desync mark bits"; + assertion = !(perAppRoutingTun.enable && perAppRoutingTproxy.enable && perAppRoutingTun.routeTable == perAppRoutingTproxy.routeTable); + message = "proxy-suite: singBox.tun.perApp.routeTable and singBox.tproxy.perApp.routeTable must differ"; + } + { + assertion = !(perAppZapretCfg.enable && perAppZapretCfg.filterMark == globalTproxy.fwmark); + message = "proxy-suite: zapret.perApp.filterMark must differ from singBox.tproxy.fwmark"; + } + { + assertion = !(perAppZapretCfg.enable && perAppZapretCfg.filterMark == globalTproxy.proxyMark); + message = "proxy-suite: zapret.perApp.filterMark must differ from singBox.tproxy.proxyMark"; + } + { + assertion = !(perAppRoutingTun.enable && perAppZapretCfg.enable && perAppRoutingTun.fwmark == perAppZapretCfg.filterMark); + message = "proxy-suite: singBox.tun.perApp.fwmark and zapret.perApp.filterMark must differ"; + } + { + assertion = !(perAppRoutingTproxy.enable && perAppZapretCfg.enable && perAppRoutingTproxy.fwmark == perAppZapretCfg.filterMark); + message = "proxy-suite: singBox.tproxy.perApp.fwmark and zapret.perApp.filterMark must differ"; + } + { + assertion = !(perAppZapretCfg.enable && builtins.elem perAppZapretCfg.filterMark [ 536870912 1073741824 ]); + message = "proxy-suite: zapret.perApp.filterMark must not use zapret internal desync mark bits"; + } + { + assertion = !(perAppZapretCfg.enable && builtins.elem globalTproxy.fwmark [ 67108864 134217728 ]); + message = "proxy-suite: singBox.tproxy.fwmark must not use per-app-zapret internal desync mark bits"; + } + { + assertion = !(perAppZapretCfg.enable && builtins.elem globalTproxy.proxyMark [ 67108864 134217728 ]); + message = "proxy-suite: singBox.tproxy.proxyMark must not use per-app-zapret internal desync mark bits"; + } + { + assertion = !(perAppRoutingTun.enable && perAppZapretCfg.enable && builtins.elem perAppRoutingTun.fwmark [ 67108864 134217728 ]); + message = "proxy-suite: singBox.tun.perApp.fwmark must not use per-app-zapret internal desync mark bits"; + } + { + assertion = !(perAppRoutingTproxy.enable && perAppZapretCfg.enable && builtins.elem perAppRoutingTproxy.fwmark [ 67108864 134217728 ]); + message = "proxy-suite: singBox.tproxy.perApp.fwmark must not use per-app-zapret internal desync mark bits"; + } + { + assertion = !(perAppZapretCfg.enable && builtins.elem perAppZapretCfg.filterMark [ 67108864 134217728 ]); + message = "proxy-suite: zapret.perApp.filterMark must not use per-app-zapret internal desync mark bits"; } { assertion = @@ -170,25 +218,29 @@ ++ lib.concatMap (ob: [ { assertion = - builtins.length ( - builtins.filter (x: x != null) [ - ob.urlFile - ob.url - ob.json - ] - ) == 1; + !singBoxCfg.enable + || + builtins.length ( + builtins.filter (x: x != null) [ + ob.urlFile + ob.url + ob.json + ] + ) == 1; message = "proxy-suite: outbound '${ob.tag}': set exactly one of urlFile, url, or json"; } ]) singBoxCfg.outbounds ++ lib.concatMap (sub: [ { assertion = - builtins.length ( - builtins.filter (x: x != null) [ - sub.urlFile - sub.url - ] - ) == 1; + !singBoxCfg.enable + || + builtins.length ( + builtins.filter (x: x != null) [ + sub.urlFile + sub.url + ] + ) == 1; message = "proxy-suite: subscription '${sub.tag}': set exactly one of urlFile or url"; } ]) singBoxCfg.subscriptions diff --git a/modules/proxy-suite/service/default.nix b/modules/proxy-suite/service/default.nix index cd59f59..ccb493b 100644 --- a/modules/proxy-suite/service/default.nix +++ b/modules/proxy-suite/service/default.nix @@ -6,21 +6,23 @@ cfg, tproxyFile, tunFile, - appTunFile, + perAppTunFile, nftablesRulesFile, - appTproxyRulesFile, - appZapretRulesFile, - appTunChainFile, + perAppTproxyRulesFile, + perAppZapretRulesFile, + perAppTunChainFile, ip, nft, }: let singBoxCfg = cfg.singBox; - appRoutingCfg = cfg.appRouting; - appRoutingTun = appRoutingCfg.backends.tun; - appRoutingTproxy = appRoutingCfg.backends.tproxy; - appRoutingZapret = appRoutingCfg.backends.zapret; + perAppRoutingCfg = cfg.perAppRouting; + globalTun = singBoxCfg.tun; + globalTproxy = singBoxCfg.tproxy; + perAppRoutingTun = singBoxCfg.tun.perApp; + perAppRoutingTproxy = singBoxCfg.tproxy.perApp; + perAppZapretCfg = cfg.zapret.perApp; userControlCfg = cfg.userControl; builtinTags = [ "proxy" "direct" "block" ]; @@ -57,14 +59,15 @@ let }; scripts = import ./scripts.nix { - inherit lib pkgs singBoxCfg appRoutingTun; + inherit lib pkgs singBoxCfg perAppRoutingTun; inherit jq python3 singBox parserScriptsPythonPath buildOutboundPy fetchSubscriptionPy; - inherit tproxyFile tunFile appTunFile; + inherit tproxyFile tunFile perAppTunFile; }; - appRouting = import ./app-routing.nix { - inherit lib pkgs cfg singBoxCfg appRoutingCfg appRoutingTun appRoutingTproxy appRoutingZapret; - inherit appTunChainFile appTproxyRulesFile appZapretRulesFile; + perAppRouting = import ./per-app-routing.nix { + inherit lib pkgs cfg singBoxCfg perAppRoutingCfg perAppRoutingTun perAppRoutingTproxy; + perAppZapretCfg = perAppZapretCfg; + inherit perAppTunChainFile perAppTproxyRulesFile perAppZapretRulesFile; inherit ip nft; inherit awk grepBin findBin headBin seqBin sleepBin; inherit jq proxychains4 systemdRun systemctl journalctl idBin; @@ -74,11 +77,11 @@ let }; in { - environment.systemPackages = [ appRouting.proxyCtl ]; + environment.systemPackages = [ perAppRouting.proxyCtl ]; # nftables must be on for TProxy to work. networking.nftables.enable = lib.mkIf ( - singBoxCfg.tproxy.enable || appRoutingTun.enable || appRoutingTproxy.enable || appRoutingZapret.enable + globalTproxy.enable || perAppRoutingTun.enable || perAppRoutingTproxy.enable || perAppZapretCfg.enable ) (lib.mkDefault true); users.groups = lib.mkIf (cfg.enable && polkit.userControlEnabled) { @@ -104,26 +107,27 @@ in ''); systemd.user.services = - lib.optionalAttrs appRoutingTun.enable { - proxy-suite-app-tun-anchor = - appRouting.mkAnchorService appRouting.appTunSliceName "Anchor service for proxy-suite app TUN slice"; + lib.optionalAttrs perAppRoutingTun.enable { + proxy-suite-per-app-tun-anchor = + perAppRouting.mkAnchorService perAppRouting.perAppTunSliceName "Anchor service for proxy-suite app TUN slice"; } - // lib.optionalAttrs appRoutingTproxy.enable { - proxy-suite-app-tproxy-anchor = - appRouting.mkAnchorService appRouting.appTproxySliceName "Anchor service for proxy-suite app TProxy slice"; + // lib.optionalAttrs perAppRoutingTproxy.enable { + proxy-suite-per-app-tproxy-anchor = + perAppRouting.mkAnchorService perAppRouting.perAppTproxySliceName "Anchor service for proxy-suite app TProxy slice"; } - // lib.optionalAttrs (appRoutingZapret.enable && cfg.zapret.enable) { - proxy-suite-app-zapret-anchor = - appRouting.mkAnchorService appRouting.appZapretSliceName "Anchor service for proxy-suite app zapret slice"; + // lib.optionalAttrs (perAppZapretCfg.enable && cfg.zapret.enable) { + proxy-suite-per-app-zapret-anchor = + perAppRouting.mkAnchorService perAppRouting.perAppZapretSliceName "Anchor service for proxy-suite app zapret slice"; }; assertions = import ../service-assertions.nix { inherit lib cfg; - inherit singBoxCfg appRoutingCfg appRoutingTun appRoutingTproxy appRoutingZapret; + inherit singBoxCfg perAppRoutingCfg perAppRoutingTun perAppRoutingTproxy; + perAppZapretCfg = perAppZapretCfg; tgWsProxyCfg = cfg.tgWsProxy; inherit builtinTags outboundTags invalidRoutingTargets; - inherit (appRouting) - effectiveAppRoutingProfileNames + inherit (perAppRouting) + effectivePerAppRoutingProfileNames hasProxychainsProfiles hasTunProfiles hasTproxyProfiles @@ -132,6 +136,8 @@ in }; systemd.services = { + } + // lib.optionalAttrs singBoxCfg.enable { proxy-suite-socks = { description = "sing-box proxy client (SOCKS + TProxy-ready)"; after = [ "network.target" ]; @@ -145,18 +151,18 @@ in }; }; } - // lib.optionalAttrs singBoxCfg.tproxy.enable { + // lib.optionalAttrs (singBoxCfg.enable && globalTproxy.enable) { proxy-suite-tproxy = { description = "sing-box TProxy – nftables rules and policy routing"; after = [ "network.target" "proxy-suite-socks.service" ]; - wantedBy = lib.optionals singBoxCfg.tproxy.autostart [ "multi-user.target" ]; + wantedBy = lib.optionals globalTproxy.autostart [ "multi-user.target" ]; requires = [ "proxy-suite-socks.service" ]; conflicts = [ "proxy-suite-tun.service" - "proxy-suite-app-tproxy.service" + "proxy-suite-per-app-tproxy.service" ]; serviceConfig = { Type = "oneshot"; @@ -164,22 +170,22 @@ in ExecStart = pkgs.writeShellScript "proxy-suite-tproxy-up" '' ${nft} delete table ip singbox 2>/dev/null || true ${nft} -f ${nftablesRulesFile} - ${ip} route add local default dev lo table ${toString singBoxCfg.routeTable} - ${ip} rule add fwmark ${toString singBoxCfg.fwmark} table ${toString singBoxCfg.routeTable} + ${ip} route add local default dev lo table ${toString globalTproxy.routeTable} + ${ip} rule add fwmark ${toString globalTproxy.fwmark} table ${toString globalTproxy.routeTable} ''; ExecStop = pkgs.writeShellScript "proxy-suite-tproxy-down" '' ${nft} delete table ip singbox 2>/dev/null || true - ${ip} route del local default dev lo table ${toString singBoxCfg.routeTable} 2>/dev/null || true - ${ip} rule del fwmark ${toString singBoxCfg.fwmark} table ${toString singBoxCfg.routeTable} 2>/dev/null || true + ${ip} route del local default dev lo table ${toString globalTproxy.routeTable} 2>/dev/null || true + ${ip} rule del fwmark ${toString globalTproxy.fwmark} table ${toString globalTproxy.routeTable} 2>/dev/null || true ''; }; }; } - // lib.optionalAttrs singBoxCfg.tun.enable { + // lib.optionalAttrs (singBoxCfg.enable && globalTun.enable) { proxy-suite-tun = { description = "sing-box TUN proxy client"; after = [ "network-online.target" ]; - wantedBy = lib.optionals singBoxCfg.tun.autostart [ "multi-user.target" ]; + wantedBy = lib.optionals globalTun.autostart [ "multi-user.target" ]; wants = [ "network-online.target" ]; conflicts = [ "proxy-suite-tproxy.service" ]; serviceConfig = { @@ -191,37 +197,37 @@ in }; }; } - // lib.optionalAttrs appRoutingTun.enable { - proxy-suite-app-tun = { - description = "sing-box app-routing TUN backend"; + // lib.optionalAttrs (singBoxCfg.enable && perAppRoutingTun.enable) { + proxy-suite-per-app-tun = { + description = "sing-box per-app-routing TUN backend"; after = [ "network-online.target" ]; wants = [ "network-online.target" ]; serviceConfig = { - ExecStart = "${scripts.startAppTun}"; - ExecStartPost = "${appRouting.appTunUpScript}"; - ExecStopPost = "${appRouting.appTunDownScript}"; + ExecStart = "${scripts.startPerAppTun}"; + ExecStartPost = "${perAppRouting.perAppTunUpScript}"; + ExecStopPost = "${perAppRouting.perAppTunDownScript}"; Restart = "on-failure"; RestartSec = 5; - RuntimeDirectory = "proxy-suite-app-tun"; + RuntimeDirectory = "proxy-suite-per-app-tun"; StateDirectory = "proxy-suite"; }; }; - "proxy-suite-app-tun-user@" = { + "proxy-suite-per-app-tun-user@" = { description = "Enable proxy-suite app TUN marking for user %i"; - requires = [ "proxy-suite-app-tun.service" ]; - after = [ "proxy-suite-app-tun.service" ]; + requires = [ "proxy-suite-per-app-tun.service" ]; + after = [ "proxy-suite-per-app-tun.service" ]; serviceConfig = { Type = "oneshot"; RemainAfterExit = true; - ExecStart = "${appRouting.appTunUserRuleStart} %i"; - ExecStop = "${appRouting.appTunUserRuleStop} %i"; + ExecStart = "${perAppRouting.perAppTunUserRuleStart} %i"; + ExecStop = "${perAppRouting.perAppTunUserRuleStop} %i"; }; }; } - // lib.optionalAttrs appRoutingTproxy.enable { - proxy-suite-app-tproxy = { - description = "proxy-suite app-routing TProxy backend"; + // lib.optionalAttrs (singBoxCfg.enable && perAppRoutingTproxy.enable) { + proxy-suite-per-app-tproxy = { + description = "proxy-suite per-app-routing TProxy backend"; after = [ "network.target" "proxy-suite-socks.service" @@ -234,37 +240,37 @@ in serviceConfig = { Type = "oneshot"; RemainAfterExit = true; - ExecStart = "${appRouting.appTproxyUpScript}"; - ExecStop = "${appRouting.appTproxyDownScript}"; + ExecStart = "${perAppRouting.perAppTproxyUpScript}"; + ExecStop = "${perAppRouting.perAppTproxyDownScript}"; }; }; - "proxy-suite-app-tproxy-user@" = { + "proxy-suite-per-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" ]; + requires = [ "proxy-suite-per-app-tproxy.service" ]; + after = [ "proxy-suite-per-app-tproxy.service" ]; serviceConfig = { Type = "oneshot"; RemainAfterExit = true; - ExecStart = "${appRouting.appTproxyUserRuleStart} %i"; - ExecStop = "${appRouting.appTproxyUserRuleStop} %i"; + ExecStart = "${perAppRouting.perAppTproxyUserRuleStart} %i"; + ExecStop = "${perAppRouting.perAppTproxyUserRuleStop} %i"; }; }; } - // lib.optionalAttrs (appRoutingZapret.enable && cfg.zapret.enable) { - "proxy-suite-app-zapret-user@" = { + // lib.optionalAttrs (perAppZapretCfg.enable && cfg.zapret.enable) { + "proxy-suite-per-app-zapret-user@" = { description = "Enable proxy-suite app zapret marking for user %i"; - requires = [ "proxy-suite-app-zapret.service" ]; - after = [ "proxy-suite-app-zapret.service" ]; + requires = [ "proxy-suite-per-app-zapret.service" ]; + after = [ "proxy-suite-per-app-zapret.service" ]; serviceConfig = { Type = "oneshot"; RemainAfterExit = true; - ExecStart = "${appRouting.appZapretUserRuleStart} %i"; - ExecStop = "${appRouting.appZapretUserRuleStop} %i"; + ExecStart = "${perAppRouting.perAppZapretUserRuleStart} %i"; + ExecStop = "${perAppRouting.perAppZapretUserRuleStop} %i"; }; }; } - // lib.optionalAttrs scripts.hasSubscriptions { + // lib.optionalAttrs (singBoxCfg.enable && scripts.hasSubscriptions) { proxy-suite-subscription-update = { description = "Refresh proxy-suite subscription caches"; after = [ "network-online.target" ]; @@ -277,7 +283,7 @@ in }; }; - systemd.timers = lib.optionalAttrs scripts.hasSubscriptions { + systemd.timers = lib.optionalAttrs (singBoxCfg.enable && scripts.hasSubscriptions) { proxy-suite-subscription-update = { description = "Periodic proxy-suite subscription refresh"; wantedBy = [ "timers.target" ]; diff --git a/modules/proxy-suite/service/app-routing.nix b/modules/proxy-suite/service/per-app-routing.nix similarity index 68% rename from modules/proxy-suite/service/app-routing.nix rename to modules/proxy-suite/service/per-app-routing.nix index 38e208c..0abbf5d 100644 --- a/modules/proxy-suite/service/app-routing.nix +++ b/modules/proxy-suite/service/per-app-routing.nix @@ -4,13 +4,13 @@ pkgs, cfg, singBoxCfg, - appRoutingCfg, - appRoutingTun, - appRoutingTproxy, - appRoutingZapret, - appTunChainFile, - appTproxyRulesFile, - appZapretRulesFile, + perAppRoutingCfg, + perAppRoutingTun, + perAppRoutingTproxy, + perAppZapretCfg, + perAppTunChainFile, + perAppTproxyRulesFile, + perAppZapretRulesFile, ip, nft, awk, @@ -31,103 +31,104 @@ }: let - appTunSliceName = "proxy-suite-app-tun.slice"; - appTproxySliceName = "proxy-suite-app-tproxy.slice"; - appZapretSliceName = "proxy-suite-app-zapret.slice"; + perAppTunSliceName = "proxy-suite-per-app-tun.slice"; + perAppTproxySliceName = "proxy-suite-per-app-tproxy.slice"; + perAppZapretSliceName = "proxy-suite-per-app-zapret.slice"; - appRoutingProfileNames = map (profile: profile.name) appRoutingCfg.profiles; - defaultAppRoutingProfiles = - lib.optionals appRoutingCfg.createDefaultProfiles [ + perAppRoutingProfileNames = map (profile: profile.name) perAppRoutingCfg.profiles; + defaultPerAppRoutingProfiles = lib.optionals perAppRoutingCfg.createDefaultProfiles ( + [ { name = "proxychains"; route = "proxychains"; } ] - ++ lib.optionals appRoutingTun.enable [ + ++ lib.optionals perAppRoutingTun.enable [ { name = "tun"; route = "tun"; } ] - ++ lib.optionals appRoutingTproxy.enable [ + ++ lib.optionals perAppRoutingTproxy.enable [ { name = "tproxy"; route = "tproxy"; } ] - ++ lib.optionals (appRoutingZapret.enable && cfg.zapret.enable) [ + ++ lib.optionals (perAppZapretCfg.enable && cfg.zapret.enable) [ { name = "zapret"; route = "zapret"; } - ]; - effectiveAppRoutingProfiles = - appRoutingCfg.profiles - ++ builtins.filter (profile: !(builtins.elem profile.name appRoutingProfileNames)) defaultAppRoutingProfiles; - effectiveAppRoutingProfileNames = map (profile: profile.name) effectiveAppRoutingProfiles; + ] + ); + effectivePerAppRoutingProfiles = + perAppRoutingCfg.profiles + ++ builtins.filter (profile: !(builtins.elem profile.name perAppRoutingProfileNames)) defaultPerAppRoutingProfiles; + effectivePerAppRoutingProfileNames = map (profile: profile.name) effectivePerAppRoutingProfiles; - appRoutingProfilesFile = pkgs.writeText "proxy-suite-app-routing-profiles.json" ( - builtins.toJSON effectiveAppRoutingProfiles + perAppRoutingProfilesFile = pkgs.writeText "proxy-suite-per-app-routing-profiles.json" ( + builtins.toJSON effectivePerAppRoutingProfiles ); proxychainsConfigFile = pkgs.writeText "proxy-suite-proxychains.conf" '' strict_chain - ${lib.optionalString appRoutingCfg.proxychains.quiet "quiet_mode"} - ${lib.optionalString appRoutingCfg.proxychains.proxyDns "proxy_dns"} + ${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 ${singBoxCfg.listenAddress} ${toString singBoxCfg.port} ''; - proxychainsQuietArg = lib.optionalString appRoutingCfg.proxychains.quiet "-q "; + proxychainsQuietArg = lib.optionalString perAppRoutingCfg.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; - hasZapretProfiles = builtins.any (profile: profile.route == "zapret") effectiveAppRoutingProfiles; + 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; - appTunWaitForInterface = pkgs.writeShellScript "proxy-suite-app-tun-wait-for-interface" '' + 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 appRoutingTun.interface} >/dev/null 2>&1; then + 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 ${appRoutingTun.interface} did not appear in time" >&2 + echo "proxy-suite: app TUN interface ${perAppRoutingTun.interface} did not appear in time" >&2 exit 1 ''; - appTunUpScript = pkgs.writeShellScript "proxy-suite-app-tun-up" '' + perAppTunUpScript = pkgs.writeShellScript "proxy-suite-per-app-tun-up" '' set -euo pipefail - ${nft} delete table inet proxy_suite_app_tun 2>/dev/null || true - ${nft} -f ${appTunChainFile} - ${appTunWaitForInterface} - ${ip} route replace default dev ${lib.escapeShellArg appRoutingTun.interface} table ${toString appRoutingTun.routeTable} - ${ip} rule add fwmark ${toString appRoutingTun.fwmark} table ${toString appRoutingTun.routeTable} 2>/dev/null || true + ${nft} delete table inet proxy_suite_per_app_tun 2>/dev/null || true + ${nft} -f ${perAppTunChainFile} + ${perAppTunWaitForInterface} + ${ip} route replace default dev ${lib.escapeShellArg perAppRoutingTun.interface} table ${toString perAppRoutingTun.routeTable} + ${ip} rule add fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable} 2>/dev/null || true ''; - appTunDownScript = pkgs.writeShellScript "proxy-suite-app-tun-down" '' + perAppTunDownScript = pkgs.writeShellScript "proxy-suite-per-app-tun-down" '' set -euo pipefail - ${nft} delete table inet proxy_suite_app_tun 2>/dev/null || true - ${ip} route del default dev ${lib.escapeShellArg appRoutingTun.interface} table ${toString appRoutingTun.routeTable} 2>/dev/null || true - ${ip} rule del fwmark ${toString appRoutingTun.fwmark} table ${toString appRoutingTun.routeTable} 2>/dev/null || true + ${nft} delete table inet proxy_suite_per_app_tun 2>/dev/null || true + ${ip} route del default dev ${lib.escapeShellArg perAppRoutingTun.interface} table ${toString perAppRoutingTun.routeTable} 2>/dev/null || true + ${ip} rule del fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable} 2>/dev/null || true ''; - appTproxyUpScript = pkgs.writeShellScript "proxy-suite-app-tproxy-up" '' + perAppTproxyUpScript = pkgs.writeShellScript "proxy-suite-per-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 appRoutingTproxy.routeTable} - ${ip} rule add fwmark ${toString appRoutingTproxy.fwmark} table ${toString appRoutingTproxy.routeTable} 2>/dev/null || true + ${nft} delete table ip proxy_suite_per_app_tproxy 2>/dev/null || true + ${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} 2>/dev/null || true ''; - appTproxyDownScript = pkgs.writeShellScript "proxy-suite-app-tproxy-down" '' + perAppTproxyDownScript = pkgs.writeShellScript "proxy-suite-per-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 appRoutingTproxy.routeTable} 2>/dev/null || true - ${ip} rule del fwmark ${toString appRoutingTproxy.fwmark} table ${toString appRoutingTproxy.routeTable} 2>/dev/null || true + ${nft} delete table ip proxy_suite_per_app_tproxy 2>/dev/null || true + ${ip} route del local default dev lo table ${toString perAppRoutingTproxy.routeTable} 2>/dev/null || true + ${ip} rule del fwmark ${toString perAppRoutingTproxy.fwmark} table ${toString perAppRoutingTproxy.routeTable} 2>/dev/null || true ''; # Generate a "start" script that adds a per-user cgroup nftables mark rule. @@ -198,51 +199,51 @@ let fi ''; - appTunUserRuleStart = mkUserRuleStart { - name = "app-tun"; + perAppTunUserRuleStart = mkUserRuleStart { + name = "per-app-tun"; nftFamily = "inet"; - nftTable = "proxy_suite_app_tun"; + nftTable = "proxy_suite_per_app_tun"; nftChain = "output"; - sliceName = appTunSliceName; + sliceName = perAppTunSliceName; sliceLabel = "app TUN"; - markRule = "meta mark set ${toString appRoutingTun.fwmark} ct mark set ${toString appRoutingTun.fwmark}"; + markRule = "meta mark set ${toString perAppRoutingTun.fwmark} ct mark set ${toString perAppRoutingTun.fwmark}"; }; - appTunUserRuleStop = mkUserRuleStop { - name = "app-tun"; + perAppTunUserRuleStop = mkUserRuleStop { + name = "per-app-tun"; nftFamily = "inet"; - nftTable = "proxy_suite_app_tun"; + nftTable = "proxy_suite_per_app_tun"; nftChain = "output"; }; - appTproxyUserRuleStart = mkUserRuleStart { - name = "app-tproxy"; + perAppTproxyUserRuleStart = mkUserRuleStart { + name = "per-app-tproxy"; nftFamily = "ip"; - nftTable = "proxy_suite_app_tproxy"; + nftTable = "proxy_suite_per_app_tproxy"; nftChain = "output"; - sliceName = appTproxySliceName; + sliceName = perAppTproxySliceName; sliceLabel = "app TProxy"; - markRule = "meta mark set ${toString appRoutingTproxy.fwmark} ct mark set ${toString appRoutingTproxy.fwmark}"; + markRule = "meta mark set ${toString perAppRoutingTproxy.fwmark} ct mark set ${toString perAppRoutingTproxy.fwmark}"; }; - appTproxyUserRuleStop = mkUserRuleStop { - name = "app-tproxy"; + perAppTproxyUserRuleStop = mkUserRuleStop { + name = "per-app-tproxy"; nftFamily = "ip"; - nftTable = "proxy_suite_app_tproxy"; + nftTable = "proxy_suite_per_app_tproxy"; nftChain = "output"; }; - appZapretUserRuleStart = mkUserRuleStart { - name = "app-zapret"; + perAppZapretUserRuleStart = mkUserRuleStart { + name = "per-app-zapret"; nftFamily = "inet"; - nftTable = "proxy_suite_app_zapret_mark"; + nftTable = "proxy_suite_per_app_zapret_mark"; nftChain = "output"; - sliceName = appZapretSliceName; + sliceName = perAppZapretSliceName; sliceLabel = "app zapret"; - markRule = "meta mark set meta mark or ${toString appRoutingZapret.filterMark} ct mark set ct mark or ${toString appRoutingZapret.filterMark}"; + markRule = "meta mark set meta mark or ${toString perAppZapretCfg.filterMark} ct mark set ct mark or ${toString perAppZapretCfg.filterMark}"; }; - appZapretUserRuleStop = mkUserRuleStop { - name = "app-zapret"; + perAppZapretUserRuleStop = mkUserRuleStop { + name = "per-app-zapret"; nftFamily = "inet"; - nftTable = "proxy_suite_app_zapret_mark"; + nftTable = "proxy_suite_per_app_zapret_mark"; nftChain = "output"; }; @@ -273,16 +274,16 @@ let CLASH_API="${clashApi}" SELECTION="${selection}" SUB_TAGS=(${subscriptionTagsList}) - APP_ROUTING_ENABLED="${if appRoutingCfg.enable then "1" else "0"}" - APP_ROUTING_PROXYCHAINS_ENABLED="${if appRoutingCfg.proxychains.enable then "1" else "0"}" - APP_ROUTING_TUN_ENABLED="${if appRoutingTun.enable then "1" else "0"}" - APP_ROUTING_TPROXY_ENABLED="${if appRoutingTproxy.enable then "1" else "0"}" - APP_ROUTING_ZAPRET_ENABLED="${if (appRoutingZapret.enable && cfg.zapret.enable) then "1" else "0"}" - APP_ROUTING_PROFILES_FILE="${appRoutingProfilesFile}" + PER_APP_ROUTING_ENABLED="${if perAppRoutingCfg.enable then "1" else "0"}" + PER_APP_ROUTING_PROXYCHAINS_ENABLED="${if perAppRoutingCfg.proxychains.enable then "1" else "0"}" + PER_APP_ROUTING_TUN_ENABLED="${if perAppRoutingTun.enable then "1" else "0"}" + PER_APP_ROUTING_TPROXY_ENABLED="${if perAppRoutingTproxy.enable then "1" else "0"}" + PER_APP_ROUTING_ZAPRET_ENABLED="${if (perAppZapretCfg.enable && cfg.zapret.enable) then "1" else "0"}" + PER_APP_ROUTING_PROFILES_FILE="${perAppRoutingProfilesFile}" PROXYCHAINS_CONFIG="${proxychainsConfigFile}" - APP_TUN_SLICE_BASE="proxy-suite-app-tun" - APP_TPROXY_SLICE_BASE="proxy-suite-app-tproxy" - APP_ZAPRET_SLICE_BASE="proxy-suite-app-zapret" + PER_APP_TUN_SLICE_BASE="proxy-suite-per-app-tun" + PER_APP_TPROXY_SLICE_BASE="proxy-suite-per-app-tproxy" + PER_APP_ZAPRET_SLICE_BASE="proxy-suite-per-app-zapret" ALL_SERVICES=( proxy-suite-socks @@ -307,7 +308,7 @@ let echo " outbounds list outbounds and current selection" echo " select switch to a specific outbound (selector mode)" echo " apps list configured per-app routing profiles" - echo " wrap -- run a command via an appRouting profile" + echo " wrap -- run a command via an perAppRouting profile" echo " subscription list show subscriptions, cache age, and proxy count" echo " subscription update force-refresh all subscription caches and restart" exit 1 @@ -352,15 +353,15 @@ let } _ensure_app_routing() { - if [ "$APP_ROUTING_ENABLED" != "1" ]; then - echo "appRouting is not enabled in services.proxy-suite.appRouting." + if [ "$PER_APP_ROUTING_ENABLED" != "1" ]; then + echo "perAppRouting is not enabled in services.proxy-suite.perAppRouting." exit 1 fi } _profile_route() { local profile="$1" - ${jq} -r --arg name "$profile" '.[] | select(.name == $name) | .route' "$APP_ROUTING_PROFILES_FILE" + ${jq} -r --arg name "$profile" '.[] | select(.name == $name) | .route' "$PER_APP_ROUTING_PROFILES_FILE" } _scope_running() { @@ -523,12 +524,12 @@ let apps) _ensure_app_routing - if [ "$(${jq} 'length' "$APP_ROUTING_PROFILES_FILE")" -eq 0 ]; then - echo "No appRouting profiles configured." + if [ "$(${jq} 'length' "$PER_APP_ROUTING_PROFILES_FILE")" -eq 0 ]; then + echo "No perAppRouting profiles configured." else printf " %-24s %s\n" "PROFILE" "ROUTE" ${jq} -r '.[] | " " + (.name | tostring) + "\t" + (.route | tostring)' \ - "$APP_ROUTING_PROFILES_FILE" \ + "$PER_APP_ROUTING_PROFILES_FILE" \ | while IFS=$'\t' read -r profile route; do printf "%-26s %s\n" "$profile" "$route" done @@ -549,7 +550,7 @@ let _ensure_app_routing route="$(_profile_route "$profile")" if [ -z "$route" ] || [ "$route" = "null" ]; then - echo "Unknown appRouting profile: $profile" + echo "Unknown perAppRouting profile: $profile" exit 1 fi @@ -558,28 +559,28 @@ let exec "$@" ;; proxychains) - if [ "$APP_ROUTING_PROXYCHAINS_ENABLED" != "1" ]; then - echo "Profile '$profile' uses route=proxychains, but appRouting.proxychains.enable is false." + if [ "$PER_APP_ROUTING_PROXYCHAINS_ENABLED" != "1" ]; then + echo "Profile '$profile' uses route=proxychains, but perAppRouting.proxychains.enable is false." exit 1 fi exec ${proxychains4} ${proxychainsQuietArg}-f "$PROXYCHAINS_CONFIG" "$@" ;; tun) - _wrap_slice "$APP_TUN_SLICE_BASE" "$APP_ROUTING_TUN_ENABLED" \ - "Profile '$profile' uses route=tun, but appRouting.backends.tun.enable is false." \ - "proxy-suite-app-tun.service" "$@" + _wrap_slice "$PER_APP_TUN_SLICE_BASE" "$PER_APP_ROUTING_TUN_ENABLED" \ + "Profile '$profile' uses route=tun, but singBox.tun.perApp.enable is false." \ + "proxy-suite-per-app-tun.service" "$@" ;; tproxy) _check_no_global_proxy tproxy - _wrap_slice "$APP_TPROXY_SLICE_BASE" "$APP_ROUTING_TPROXY_ENABLED" \ - "Profile '$profile' uses route=tproxy, but appRouting.backends.tproxy.enable is false." \ - "proxy-suite-app-tproxy.service" "$@" + _wrap_slice "$PER_APP_TPROXY_SLICE_BASE" "$PER_APP_ROUTING_TPROXY_ENABLED" \ + "Profile '$profile' uses route=tproxy, but singBox.tproxy.perApp.enable is false." \ + "proxy-suite-per-app-tproxy.service" "$@" ;; zapret) _check_no_global_proxy zapret - _wrap_slice "$APP_ZAPRET_SLICE_BASE" "$APP_ROUTING_ZAPRET_ENABLED" \ - "Profile '$profile' uses route=zapret, but the app-zapret backend or zapret service is not enabled." \ - "proxy-suite-app-zapret.service" "$@" + _wrap_slice "$PER_APP_ZAPRET_SLICE_BASE" "$PER_APP_ROUTING_ZAPRET_ENABLED" \ + "Profile '$profile' uses route=zapret, but zapret.perApp.enable or zapret.enable is false." \ + "proxy-suite-per-app-zapret.service" "$@" ;; *) echo "Route backend '$route' is not implemented in this build." @@ -629,23 +630,23 @@ let in { inherit - appTunSliceName - appTproxySliceName - appZapretSliceName - appTunUpScript - appTunDownScript - appTproxyUpScript - appTproxyDownScript - appTunUserRuleStart - appTunUserRuleStop - appTproxyUserRuleStart - appTproxyUserRuleStop - appZapretUserRuleStart - appZapretUserRuleStop + perAppTunSliceName + perAppTproxySliceName + perAppZapretSliceName + perAppTunUpScript + perAppTunDownScript + perAppTproxyUpScript + perAppTproxyDownScript + perAppTunUserRuleStart + perAppTunUserRuleStop + perAppTproxyUserRuleStart + perAppTproxyUserRuleStop + perAppZapretUserRuleStart + perAppZapretUserRuleStop mkAnchorService - effectiveAppRoutingProfiles - effectiveAppRoutingProfileNames - appRoutingProfilesFile + effectivePerAppRoutingProfiles + effectivePerAppRoutingProfileNames + perAppRoutingProfilesFile proxychainsConfigFile proxychainsQuietArg hasProxychainsProfiles diff --git a/modules/proxy-suite/service/polkit.nix b/modules/proxy-suite/service/polkit.nix index 16022a0..e37e59b 100644 --- a/modules/proxy-suite/service/polkit.nix +++ b/modules/proxy-suite/service/polkit.nix @@ -6,13 +6,13 @@ let userControlPolkitRules = lib.optionalString userControlCfg.perApp.enable '' - if (unit.indexOf("proxy-suite-app-") === 0) { + if (unit.indexOf("proxy-suite-per-app-") === 0) { return polkit.Result.YES; } '' + lib.optionalString userControlCfg.global.enable '' if ((unit.indexOf("proxy-suite-") === 0 && - unit.indexOf("proxy-suite-app-") !== 0) || + unit.indexOf("proxy-suite-per-app-") !== 0) || unit === "zapret-discord-youtube.service") { return polkit.Result.YES; } diff --git a/modules/proxy-suite/service/scripts.nix b/modules/proxy-suite/service/scripts.nix index fbb7a99..187e989 100644 --- a/modules/proxy-suite/service/scripts.nix +++ b/modules/proxy-suite/service/scripts.nix @@ -3,7 +3,7 @@ lib, pkgs, singBoxCfg, - appRoutingTun, + perAppRoutingTun, jq, python3, singBox, @@ -12,10 +12,12 @@ fetchSubscriptionPy, tproxyFile, tunFile, - appTunFile, + perAppTunFile, }: let + globalTproxy = singBoxCfg.tproxy; + mkSubscriptionUrlSource = sub: if sub.urlFile != null then @@ -171,7 +173,7 @@ let name = "proxy-suite-start-socks"; runtimeDir = "/run/proxy-suite-socks"; configFile = tproxyFile; - routingMark = singBoxCfg.proxyMark; + routingMark = globalTproxy.proxyMark; }; startTun = mkStartScript { @@ -181,13 +183,13 @@ let routingMark = null; }; - startAppTun = mkStartScript { - name = "proxy-suite-start-app-tun"; - runtimeDir = "/run/proxy-suite-app-tun"; - configFile = appTunFile; + startPerAppTun = mkStartScript { + name = "proxy-suite-start-per-app-tun"; + runtimeDir = "/run/proxy-suite-per-app-tun"; + configFile = perAppTunFile; # Only needed when TProxy mode is enabled; otherwise avoid tagging app TUN # proxy outbounds with a host-global mark. - routingMark = if singBoxCfg.tproxy.enable then singBoxCfg.proxyMark else null; + routingMark = if globalTproxy.enable then globalTproxy.proxyMark else null; }; # Shell code to fetch a single subscription into the cache (used in update service). @@ -224,6 +226,6 @@ let subscriptionTagsList = lib.concatStringsSep " " (map (sub: lib.escapeShellArg sub.tag) singBoxCfg.subscriptions); in { - inherit startSocks startTun startAppTun; + inherit startSocks startTun startPerAppTun; inherit subscriptionUpdateScript hasSubscriptions subscriptionTagsList; } diff --git a/modules/proxy-suite/zapret.nix b/modules/proxy-suite/zapret.nix index 10535ab..671c57a 100644 --- a/modules/proxy-suite/zapret.nix +++ b/modules/proxy-suite/zapret.nix @@ -4,13 +4,13 @@ pkgs, cfg, zapret, - appZapretRulesFile, + perAppZapretRulesFile, nft, }: let zapretCfg = cfg.zapret; - appRoutingZapret = cfg.appRouting.backends.zapret; + perAppZapretCfg = zapretCfg.perApp; iptables = "${pkgs.iptables}/bin/iptables"; @@ -76,10 +76,10 @@ let filterMark: pkgs.writeText "proxy-suite-zapret-global-bypass.sh" '' zapret_custom_firewall_nft() { - nft insert rule inet $ZAPRET_NFT_TABLE postrouting mark and ${filterMark} != 0 return comment "proxy-suite app-zapret bypass" - nft insert rule inet $ZAPRET_NFT_TABLE postnat mark and ${filterMark} != 0 return comment "proxy-suite app-zapret bypass" - nft insert rule inet $ZAPRET_NFT_TABLE prerouting mark and ${filterMark} != 0 return comment "proxy-suite app-zapret bypass" - nft insert rule inet $ZAPRET_NFT_TABLE prenat mark and ${filterMark} != 0 return comment "proxy-suite app-zapret bypass" + 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" } ''; @@ -221,19 +221,19 @@ let pidDir = "/run/proxy-suite-zapret"; gameFilter = zapretCfg.gameFilter; forceDisableFilterMark = true; - customScript = if appRoutingZapret.enable then mkGlobalBypassScript (toString appRoutingZapret.filterMark) else null; + customScript = if perAppZapretCfg.enable then mkGlobalBypassScript (toString perAppZapretCfg.filterMark) else null; }; - appZapretPackage = mkDerivedZapretPackage { - packageName = "proxy-suite-app-zapret-${selectedConfigName}"; - pidDir = "/run/proxy-suite-app-zapret"; + perAppZapretPackage = mkDerivedZapretPackage { + packageName = "proxy-suite-per-app-zapret-${selectedConfigName}"; + pidDir = "/run/proxy-suite-per-app-zapret"; gameFilter = "all"; - filterMark = appRoutingZapret.filterMark; - qnum = appRoutingZapret.qnum; + filterMark = perAppZapretCfg.filterMark; + qnum = perAppZapretCfg.qnum; modeFilter = "none"; desyncMark = 134217728; desyncMarkPostnat = 67108864; - nftTable = "proxy_suite_app_zapret"; + nftTable = "proxy_suite_per_app_zapret"; }; zapretCommonPreStart = package: '' @@ -253,17 +253,17 @@ let "PATH=${lib.makeBinPath runtimeDeps}" ]; globalZapretEnv = mkZapretEnv globalZapretPackage; - appZapretEnv = mkZapretEnv appZapretPackage; + perAppZapretEnv = mkZapretEnv perAppZapretPackage; - appZapretMarkUpScript = pkgs.writeShellScript "proxy-suite-app-zapret-mark-up" '' + perAppZapretMarkUpScript = pkgs.writeShellScript "proxy-suite-per-app-zapret-mark-up" '' set -euo pipefail - ${nft} delete table inet proxy_suite_app_zapret_mark 2>/dev/null || true - ${nft} -f ${appZapretRulesFile} + ${nft} delete table inet proxy_suite_per_app_zapret_mark 2>/dev/null || true + ${nft} -f ${perAppZapretRulesFile} ''; - appZapretMarkDownScript = pkgs.writeShellScript "proxy-suite-app-zapret-mark-down" '' + perAppZapretMarkDownScript = pkgs.writeShellScript "proxy-suite-per-app-zapret-mark-down" '' set -euo pipefail - ${nft} delete table inet proxy_suite_app_zapret_mark 2>/dev/null || true + ${nft} delete table inet proxy_suite_per_app_zapret_mark 2>/dev/null || true ''; exemptStart = lib.concatMapStrings (cidr: '' @@ -294,7 +294,7 @@ in environment.systemPackages = lib.mkBefore ( - [ globalZapretPackage ] ++ lib.optionals appRoutingZapret.enable [ appZapretPackage ] + [ globalZapretPackage ] ++ lib.optionals perAppZapretCfg.enable [ perAppZapretPackage ] ); systemd.services.zapret-discord-youtube = { @@ -313,23 +313,23 @@ in }; }; - systemd.services.proxy-suite-app-zapret = lib.mkIf appRoutingZapret.enable { - description = "proxy-suite app-routing zapret backend"; + systemd.services.proxy-suite-per-app-zapret = lib.mkIf perAppZapretCfg.enable { + description = "proxy-suite per-app-routing zapret backend"; after = [ "network.target" ]; conflicts = [ "proxy-suite-tproxy.service" "proxy-suite-tun.service" ]; - preStart = zapretCommonPreStart appZapretPackage; + preStart = zapretCommonPreStart perAppZapretPackage; serviceConfig = { Type = "oneshot"; RemainAfterExit = true; - RuntimeDirectory = "proxy-suite-app-zapret"; - ExecStartPre = "${appZapretMarkUpScript}"; - ExecStart = "${appZapretPackage}/opt/zapret/init.d/sysv/zapret start"; - ExecStop = "${appZapretPackage}/opt/zapret/init.d/sysv/zapret stop"; - ExecStopPost = "${appZapretMarkDownScript}"; - Environment = appZapretEnv; + RuntimeDirectory = "proxy-suite-per-app-zapret"; + ExecStartPre = "${perAppZapretMarkUpScript}"; + ExecStart = "${perAppZapretPackage}/opt/zapret/init.d/sysv/zapret start"; + ExecStop = "${perAppZapretPackage}/opt/zapret/init.d/sysv/zapret stop"; + ExecStopPost = "${perAppZapretMarkDownScript}"; + Environment = perAppZapretEnv; }; }; diff --git a/nix/checks.nix b/nix/checks.nix index d1b3269..fa68361 100644 --- a/nix/checks.nix +++ b/nix/checks.nix @@ -48,7 +48,7 @@ let }; in builtins.fromJSON (builtins.unsafeDiscardStringContext (builtins.readFile configs.tunFile)); - mkAppTunConfig = + mkPerAppTunConfig = fixture: let cfg = fixture.config.services.proxy-suite; @@ -58,7 +58,7 @@ let inherit pkgs cfg rules; }; in - builtins.fromJSON (builtins.unsafeDiscardStringContext (builtins.readFile configs.appTunFile)); + builtins.fromJSON (builtins.unsafeDiscardStringContext (builtins.readFile configs.perAppTunFile)); hasDirectDomain = rules: domain: builtins.any ( @@ -269,6 +269,30 @@ let ]).config.system.build.toplevel.drvPath ); + globalTunWithoutSingBox = forceEval ( + (evalProxySuite [ + baseModule + { + services.proxy-suite.singBox = { + enable = false; + tun.enable = true; + }; + } + ]).config.system.build.toplevel.drvPath + ); + + globalTproxyWithoutSingBox = forceEval ( + (evalProxySuite [ + baseModule + { + services.proxy-suite.singBox = { + enable = false; + tproxy.enable = true; + }; + } + ]).config.system.build.toplevel.drvPath + ); + routingOrFixture = evalProxySuite [ { system.stateVersion = "26.05"; @@ -685,10 +709,10 @@ let } ]; - appRoutingProxychainsFixture = evalProxySuite [ + perAppRoutingProxychainsFixture = evalProxySuite [ baseModule { - services.proxy-suite.appRouting = { + services.proxy-suite.perAppRouting = { enable = true; proxychains.enable = true; profiles = [ @@ -704,49 +728,81 @@ let }; } ]; - appRoutingProxychainsScript = + perAppRoutingProxychainsScript = builtins.readFile ( - "${packageByPattern appRoutingProxychainsFixture.config.environment.systemPackages + "${packageByPattern perAppRoutingProxychainsFixture.config.environment.systemPackages ".*/[^/]*proxy-ctl(-[0-9.]+)?$"}/bin/proxy-ctl" ); - appRoutingProxychainsConfig = + perAppRoutingProxychainsConfig = builtins.readFile ( - quotedValueByPrefix appRoutingProxychainsScript "PROXYCHAINS_CONFIG=\"" + quotedValueByPrefix perAppRoutingProxychainsScript "PROXYCHAINS_CONFIG=\"" ); - appRoutingProxychainsProfiles = + perAppRoutingProxychainsProfiles = builtins.fromJSON ( builtins.readFile ( - quotedValueByPrefix appRoutingProxychainsScript "APP_ROUTING_PROFILES_FILE=\"" + quotedValueByPrefix perAppRoutingProxychainsScript "PER_APP_ROUTING_PROFILES_FILE=\"" ) ); - appRoutingDefaultProfilesFixture = evalProxySuite [ + perAppRoutingDefaultProfilesFixture = evalProxySuite [ baseModule { - services.proxy-suite.appRouting = { + services.proxy-suite.perAppRouting = { enable = true; createDefaultProfiles = true; proxychains.enable = true; }; } ]; - appRoutingDefaultProfilesScript = + perAppRoutingDefaultProfilesScript = builtins.readFile ( - "${packageByPattern appRoutingDefaultProfilesFixture.config.environment.systemPackages + "${packageByPattern perAppRoutingDefaultProfilesFixture.config.environment.systemPackages ".*/[^/]*proxy-ctl(-[0-9.]+)?$"}/bin/proxy-ctl" ); - appRoutingDefaultProfiles = + perAppRoutingDefaultProfiles = builtins.fromJSON ( builtins.readFile ( - quotedValueByPrefix appRoutingDefaultProfilesScript "APP_ROUTING_PROFILES_FILE=\"" + quotedValueByPrefix perAppRoutingDefaultProfilesScript "PER_APP_ROUTING_PROFILES_FILE=\"" ) ); - appRoutingDefaultProfilesWithoutProxychainsEnable = forceEval ( + perAppRoutingNoDefaultProfilesFixture = evalProxySuite [ + baseModule + { + services.proxy-suite = { + singBox = { + tun.perApp.enable = true; + tproxy.perApp.enable = true; + }; + zapret = { + enable = true; + perApp.enable = true; + }; + perAppRouting = { + enable = true; + createDefaultProfiles = false; + proxychains.enable = true; + }; + }; + } + ]; + perAppRoutingNoDefaultProfilesScript = + builtins.readFile ( + "${packageByPattern perAppRoutingNoDefaultProfilesFixture.config.environment.systemPackages + ".*/[^/]*proxy-ctl(-[0-9.]+)?$"}/bin/proxy-ctl" + ); + perAppRoutingNoDefaultProfiles = + builtins.fromJSON ( + builtins.readFile ( + quotedValueByPrefix perAppRoutingNoDefaultProfilesScript "PER_APP_ROUTING_PROFILES_FILE=\"" + ) + ); + + perAppRoutingDefaultProfilesWithoutProxychainsEnable = forceEval ( (evalProxySuite [ baseModule { - services.proxy-suite.appRouting = { + services.proxy-suite.perAppRouting = { enable = true; createDefaultProfiles = true; }; @@ -754,61 +810,65 @@ let ]).config.system.build.toplevel.drvPath ); - appRoutingTunFixture = evalProxySuite [ - baseModule - { - services.proxy-suite.appRouting = { - enable = true; - createDefaultProfiles = true; - backends.tun.enable = true; - }; - } - ]; - appRoutingTunScript = - builtins.readFile ( - "${packageByPattern appRoutingTunFixture.config.environment.systemPackages - ".*/[^/]*proxy-ctl(-[0-9.]+)?$"}/bin/proxy-ctl" - ); - appRoutingTunProfiles = - builtins.fromJSON ( - builtins.readFile ( - quotedValueByPrefix appRoutingTunScript "APP_ROUTING_PROFILES_FILE=\"" - ) - ); - appRoutingTunStartScript = - builtins.readFile appRoutingTunFixture.config.systemd.services."proxy-suite-app-tun".serviceConfig.ExecStart; - appRoutingTunConfig = mkAppTunConfig appRoutingTunFixture; - appRoutingTunDirectOutbound = - builtins.head (builtins.filter (item: item.tag == "direct") appRoutingTunConfig.outbounds); - appRoutingTunUserStartExec = - appRoutingTunFixture.config.systemd.services."proxy-suite-app-tun-user@".serviceConfig.ExecStart; - appRoutingTunUserStartScript = - builtins.readFile (builtins.head (pkgs.lib.splitString " " appRoutingTunUserStartExec)); - - appRoutingTunWithTproxyFixture = evalProxySuite [ + perAppRoutingTunFixture = evalProxySuite [ baseModule { services.proxy-suite = { - singBox.tproxy.enable = true; - appRouting = { + singBox.tun.perApp.enable = true; + perAppRouting = { enable = true; createDefaultProfiles = true; - backends.tun.enable = true; }; }; } ]; - appRoutingTunWithTproxyStartScript = - builtins.readFile appRoutingTunWithTproxyFixture.config.systemd.services."proxy-suite-app-tun".serviceConfig.ExecStart; - appRoutingTunWithTproxyConfig = mkAppTunConfig appRoutingTunWithTproxyFixture; - appRoutingTunWithTproxyDirectOutbound = - builtins.head (builtins.filter (item: item.tag == "direct") appRoutingTunWithTproxyConfig.outbounds); + perAppRoutingTunScript = + builtins.readFile ( + "${packageByPattern perAppRoutingTunFixture.config.environment.systemPackages + ".*/[^/]*proxy-ctl(-[0-9.]+)?$"}/bin/proxy-ctl" + ); + perAppRoutingTunProfiles = + builtins.fromJSON ( + builtins.readFile ( + quotedValueByPrefix perAppRoutingTunScript "PER_APP_ROUTING_PROFILES_FILE=\"" + ) + ); + perAppRoutingTunStartScript = + builtins.readFile perAppRoutingTunFixture.config.systemd.services."proxy-suite-per-app-tun".serviceConfig.ExecStart; + perAppRoutingTunConfig = mkPerAppTunConfig perAppRoutingTunFixture; + perAppRoutingTunDirectOutbound = + builtins.head (builtins.filter (item: item.tag == "direct") perAppRoutingTunConfig.outbounds); + perAppRoutingTunUserStartExec = + perAppRoutingTunFixture.config.systemd.services."proxy-suite-per-app-tun-user@".serviceConfig.ExecStart; + perAppRoutingTunUserStartScript = + builtins.readFile (builtins.head (pkgs.lib.splitString " " perAppRoutingTunUserStartExec)); - appRoutingTunWithoutEnable = forceEval ( + perAppRoutingTunWithTproxyFixture = evalProxySuite [ + baseModule + { + services.proxy-suite = { + singBox = { + tproxy.enable = true; + tun.perApp.enable = true; + }; + perAppRouting = { + enable = true; + createDefaultProfiles = true; + }; + }; + } + ]; + perAppRoutingTunWithTproxyStartScript = + builtins.readFile perAppRoutingTunWithTproxyFixture.config.systemd.services."proxy-suite-per-app-tun".serviceConfig.ExecStart; + perAppRoutingTunWithTproxyConfig = mkPerAppTunConfig perAppRoutingTunWithTproxyFixture; + perAppRoutingTunWithTproxyDirectOutbound = + builtins.head (builtins.filter (item: item.tag == "direct") perAppRoutingTunWithTproxyConfig.outbounds); + + perAppRoutingTunWithoutEnable = forceEval ( (evalProxySuite [ baseModule { - services.proxy-suite.appRouting = { + services.proxy-suite.perAppRouting = { enable = true; profiles = [ { @@ -821,39 +881,41 @@ let ]).config.system.build.toplevel.drvPath ); - appRoutingTproxyFixture = evalProxySuite [ + perAppRoutingTproxyFixture = evalProxySuite [ baseModule { - services.proxy-suite.appRouting = { - enable = true; - createDefaultProfiles = true; - backends.tproxy.enable = true; + services.proxy-suite = { + singBox.tproxy.perApp.enable = true; + perAppRouting = { + enable = true; + createDefaultProfiles = true; + }; }; } ]; - appRoutingTproxyScript = + perAppRoutingTproxyScript = builtins.readFile ( - "${packageByPattern appRoutingTproxyFixture.config.environment.systemPackages + "${packageByPattern perAppRoutingTproxyFixture.config.environment.systemPackages ".*/[^/]*proxy-ctl(-[0-9.]+)?$"}/bin/proxy-ctl" ); - appRoutingTproxyProfiles = + perAppRoutingTproxyProfiles = builtins.fromJSON ( builtins.readFile ( - quotedValueByPrefix appRoutingTproxyScript "APP_ROUTING_PROFILES_FILE=\"" + quotedValueByPrefix perAppRoutingTproxyScript "PER_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)); + perAppRoutingTproxyStartScript = + builtins.readFile perAppRoutingTproxyFixture.config.systemd.services."proxy-suite-per-app-tproxy".serviceConfig.ExecStart; + perAppRoutingTproxyUserStartExec = + perAppRoutingTproxyFixture.config.systemd.services."proxy-suite-per-app-tproxy-user@".serviceConfig.ExecStart; + perAppRoutingTproxyUserStartScript = + builtins.readFile (builtins.head (pkgs.lib.splitString " " perAppRoutingTproxyUserStartExec)); - appRoutingTproxyWithoutEnable = forceEval ( + perAppRoutingTproxyWithoutEnable = forceEval ( (evalProxySuite [ baseModule { - services.proxy-suite.appRouting = { + services.proxy-suite.perAppRouting = { enable = true; profiles = [ { @@ -866,44 +928,131 @@ let ]).config.system.build.toplevel.drvPath ); - appRoutingZapretFixture = evalProxySuite [ + perAppRoutingProxychainsWithoutSingBox = forceEval ( + (evalProxySuite [ + baseModule + { + services.proxy-suite = { + singBox.enable = false; + perAppRouting = { + enable = true; + proxychains.enable = true; + }; + }; + } + ]).config.system.build.toplevel.drvPath + ); + + perAppRoutingTunBackendWithoutSingBox = forceEval ( + (evalProxySuite [ + baseModule + { + services.proxy-suite = { + singBox = { + enable = false; + tun.perApp.enable = true; + }; + perAppRouting.enable = true; + }; + } + ]).config.system.build.toplevel.drvPath + ); + + perAppRoutingTproxyBackendWithoutSingBox = forceEval ( + (evalProxySuite [ + baseModule + { + services.proxy-suite = { + singBox = { + enable = false; + tproxy.perApp.enable = true; + }; + perAppRouting.enable = true; + }; + } + ]).config.system.build.toplevel.drvPath + ); + + perAppRoutingTunProfileWithoutSingBox = forceEval ( + (evalProxySuite [ + baseModule + { + services.proxy-suite = { + singBox.enable = false; + perAppRouting = { + enable = true; + profiles = [ + { + name = "game"; + route = "tun"; + } + ]; + }; + }; + } + ]).config.system.build.toplevel.drvPath + ); + + perAppRoutingTproxyProfileWithoutSingBox = forceEval ( + (evalProxySuite [ + baseModule + { + services.proxy-suite = { + singBox.enable = false; + perAppRouting = { + enable = true; + profiles = [ + { + name = "browser"; + route = "tproxy"; + } + ]; + }; + }; + } + ]).config.system.build.toplevel.drvPath + ); + + perAppRoutingZapretFixture = evalProxySuite [ baseModule { services.proxy-suite = { - zapret.enable = true; - appRouting = { + zapret = { + enable = true; + perApp.enable = true; + }; + perAppRouting = { enable = true; createDefaultProfiles = true; proxychains.enable = true; - backends.zapret.enable = true; }; }; } ]; - appRoutingZapretScript = + perAppRoutingZapretScript = builtins.readFile ( - "${packageByPattern appRoutingZapretFixture.config.environment.systemPackages + "${packageByPattern perAppRoutingZapretFixture.config.environment.systemPackages ".*/[^/]*proxy-ctl(-[0-9.]+)?$"}/bin/proxy-ctl" ); - appRoutingZapretProfiles = + perAppRoutingZapretProfiles = builtins.fromJSON ( builtins.readFile ( - quotedValueByPrefix appRoutingZapretScript "APP_ROUTING_PROFILES_FILE=\"" + quotedValueByPrefix perAppRoutingZapretScript "PER_APP_ROUTING_PROFILES_FILE=\"" ) ); - appRoutingZapretStartScript = - readExecScripts appRoutingZapretFixture.config.systemd.services."proxy-suite-app-zapret".serviceConfig.ExecStartPre; - appRoutingZapretUserStartExec = - appRoutingZapretFixture.config.systemd.services."proxy-suite-app-zapret-user@".serviceConfig.ExecStart; - appRoutingZapretUserStartScript = - builtins.readFile (builtins.head (pkgs.lib.splitString " " appRoutingZapretUserStartExec)); - appRoutingZapretBase = mkZapretBase appRoutingZapretFixture; - appRoutingZapretConfig = builtins.readFile "${appRoutingZapretBase}/config"; - appRoutingAppZapretBase = mkZapretBaseFor appRoutingZapretFixture "proxy-suite-app-zapret"; - appRoutingAppZapretConfig = builtins.readFile "${appRoutingAppZapretBase}/config"; - appRoutingZapretGlobalCustomScript = - builtins.readFile "${appRoutingZapretBase}/init.d/sysv/custom.d/50-proxy-suite-custom.sh"; - appRoutingTunPolkitConfig = appRoutingTunFixture.config.security.polkit.extraConfig; + perAppRoutingZapretStartScript = + readExecScripts perAppRoutingZapretFixture.config.systemd.services."proxy-suite-per-app-zapret".serviceConfig.ExecStartPre; + perAppRoutingZapretUserStartExec = + perAppRoutingZapretFixture.config.systemd.services."proxy-suite-per-app-zapret-user@".serviceConfig.ExecStart; + perAppRoutingZapretUserStartScript = + builtins.readFile (builtins.head (pkgs.lib.splitString " " perAppRoutingZapretUserStartExec)); + perAppRoutingZapretBase = mkZapretBase perAppRoutingZapretFixture; + perAppRoutingZapretConfig = builtins.readFile "${perAppRoutingZapretBase}/config"; + perAppRoutingPerAppZapretBase = mkZapretBaseFor perAppRoutingZapretFixture "proxy-suite-per-app-zapret"; + perAppRoutingPerAppZapretConfig = builtins.readFile "${perAppRoutingPerAppZapretBase}/config"; + perAppRoutingZapretGlobalCustomScript = + builtins.readFile "${perAppRoutingZapretBase}/init.d/sysv/custom.d/50-proxy-suite-custom.sh"; + perAppRoutingTunPolkitConfig = perAppRoutingTunFixture.config.security.polkit.extraConfig; userControlGlobalOnlyFixture = evalProxySuite [ baseModule @@ -920,10 +1069,10 @@ let baseModule { services.proxy-suite = { - appRouting = { + singBox.tun.perApp.enable = true; + perAppRouting = { enable = true; createDefaultProfiles = true; - backends.tun.enable = true; }; userControl = { global.enable = false; @@ -944,13 +1093,13 @@ let } ]; - appRoutingZapretWithoutEnable = forceEval ( + perAppRoutingZapretWithoutEnable = forceEval ( (evalProxySuite [ baseModule { services.proxy-suite = { zapret.enable = true; - appRouting = { + perAppRouting = { enable = true; profiles = [ { @@ -964,23 +1113,26 @@ let ]).config.system.build.toplevel.drvPath ); - appRoutingZapretWithoutZapretService = forceEval ( + perAppRoutingZapretWithoutZapretService = forceEval ( (evalProxySuite [ baseModule { - services.proxy-suite.appRouting = { - enable = true; - backends.zapret.enable = true; + services.proxy-suite = { + zapret = { + enable = false; + perApp.enable = true; + }; + perAppRouting.enable = true; }; } ]).config.system.build.toplevel.drvPath ); - duplicateAppRoutingProfiles = forceEval ( + duplicatePerAppRoutingProfiles = forceEval ( (evalProxySuite [ baseModule { - services.proxy-suite.appRouting = { + services.proxy-suite.perAppRouting = { enable = true; profiles = [ { name = "dup"; } @@ -991,20 +1143,20 @@ let ]).config.system.build.toplevel.drvPath ); - appRoutingProfilesWithoutEnable = forceEval ( + perAppRoutingProfilesWithoutEnable = forceEval ( (evalProxySuite [ baseModule { - services.proxy-suite.appRouting.profiles = [ { name = "oops"; } ]; + services.proxy-suite.perAppRouting.profiles = [ { name = "oops"; } ]; } ]).config.system.build.toplevel.drvPath ); - appRoutingProxychainsWithoutEnable = forceEval ( + perAppRoutingProxychainsWithoutEnable = forceEval ( (evalProxySuite [ baseModule { - services.proxy-suite.appRouting = { + services.proxy-suite.perAppRouting = { enable = true; profiles = [ { @@ -1017,125 +1169,123 @@ let ]).config.system.build.toplevel.drvPath ); - appRoutingTunFwmarkCollision = forceEval ( + perAppRoutingTunFwmarkCollision = forceEval ( (evalProxySuite [ baseModule { services.proxy-suite = { singBox = { - tproxy.enable = true; - proxyMark = 16; - }; - appRouting = { - enable = true; - backends.tun = { + tproxy = { + enable = true; + proxyMark = 16; + }; + tun.perApp = { enable = true; fwmark = 16; }; }; + perAppRouting.enable = true; }; } ]).config.system.build.toplevel.drvPath ); - appRoutingTproxyFwmarkCollision = forceEval ( + perAppRoutingTproxyFwmarkCollision = forceEval ( (evalProxySuite [ baseModule { services.proxy-suite = { - singBox.proxyMark = 17; - appRouting = { - enable = true; - backends.tproxy = { + singBox.tproxy = { + proxyMark = 17; + perApp = { enable = true; fwmark = 17; }; }; + perAppRouting.enable = true; }; } ]).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 - ); - - appRoutingZapretFilterMarkCollision = forceEval ( + perAppRoutingTunTproxyFwmarkCollision = forceEval ( (evalProxySuite [ baseModule { services.proxy-suite = { - zapret.enable = true; - singBox.proxyMark = 268435456; - appRouting = { + singBox = { + tun.perApp = { + enable = true; + fwmark = 23; + }; + tproxy.perApp = { + enable = true; + fwmark = 23; + }; + }; + perAppRouting.enable = true; + }; + } + ]).config.system.build.toplevel.drvPath + ); + + perAppRoutingTunTproxyRouteTableCollision = forceEval ( + (evalProxySuite [ + baseModule + { + services.proxy-suite = { + singBox = { + tun.perApp = { + enable = true; + routeTable = 123; + }; + tproxy.perApp = { + enable = true; + routeTable = 123; + }; + }; + perAppRouting.enable = true; + }; + } + ]).config.system.build.toplevel.drvPath + ); + + perAppRoutingZapretFilterMarkCollision = forceEval ( + (evalProxySuite [ + baseModule + { + services.proxy-suite = { + zapret = { enable = true; - backends.zapret = { + perApp = { enable = true; filterMark = 268435456; }; }; + singBox.tproxy.proxyMark = 268435456; + perAppRouting.enable = true; }; } ]).config.system.build.toplevel.drvPath ); - appRoutingTproxyZapretMarkCollision = forceEval ( + perAppRoutingTproxyZapretMarkCollision = forceEval ( (evalProxySuite [ baseModule { services.proxy-suite = { - zapret.enable = true; - appRouting = { + zapret = { enable = true; - backends = { - tproxy = { - enable = true; - fwmark = 23; - }; - zapret = { - enable = true; - filterMark = 23; - }; + perApp = { + enable = true; + filterMark = 23; }; }; + singBox.tproxy.perApp = { + enable = true; + fwmark = 23; + }; + perAppRouting.enable = true; }; } ]).config.system.build.toplevel.drvPath @@ -1286,6 +1436,11 @@ let assert conflictingAutostartModes.success == false; true ) + ( + assert globalTunWithoutSingBox.success == false; + assert globalTproxyWithoutSingBox.success == false; + true + ) ( assert builtins.length routingOrDomainRules == 1; true @@ -1471,7 +1626,7 @@ let ( assert ruDefaultConfig.route.default_domain_resolver == "local"; assert tunDefaultConfig.route.default_domain_resolver == "local"; - assert appRoutingTunConfig.route.default_domain_resolver == "local"; + assert perAppRoutingTunConfig.route.default_domain_resolver == "local"; true ) ( @@ -1597,108 +1752,114 @@ let true ) - # -- appRouting: proxychains/direct profile config is accepted -- + # -- perAppRouting: proxychains/direct profile config is accepted -- ( - assert appRoutingProxychainsFixture.config.services.proxy-suite.appRouting.enable; + assert perAppRoutingProxychainsFixture.config.services.proxy-suite.perAppRouting.enable; true ) ( - assert builtins.length appRoutingProxychainsFixture.config.services.proxy-suite.appRouting.profiles == 2; + assert builtins.length perAppRoutingProxychainsFixture.config.services.proxy-suite.perAppRouting.profiles == 2; true ) ( - assert builtins.length appRoutingProxychainsProfiles == 2; + assert builtins.length perAppRoutingProxychainsProfiles == 2; true ) - # -- appRouting: createDefaultProfiles injects curated proxychains profile -- + # -- perAppRouting: createDefaultProfiles injects curated proxychains profile -- ( - assert builtins.length appRoutingDefaultProfiles == 1; - assert (builtins.head appRoutingDefaultProfiles).name == "proxychains"; - assert (builtins.head appRoutingDefaultProfiles).route == "proxychains"; + assert builtins.length perAppRoutingDefaultProfiles == 1; + assert (builtins.head perAppRoutingDefaultProfiles).name == "proxychains"; + assert (builtins.head perAppRoutingDefaultProfiles).route == "proxychains"; true ) - # -- appRouting: createDefaultProfiles injects curated tun profile when backend is enabled -- + # -- perAppRouting: createDefaultProfiles = false injects no curated profiles -- ( - assert builtins.length appRoutingTunProfiles == 2; - assert builtins.any (profile: profile.name == "tun" && profile.route == "tun") appRoutingTunProfiles; + assert builtins.length perAppRoutingNoDefaultProfiles == 0; true ) - # -- appRouting: createDefaultProfiles injects curated tproxy profile when backend is enabled -- + # -- perAppRouting: createDefaultProfiles injects curated tun profile when backend is enabled -- ( - assert builtins.length appRoutingTproxyProfiles == 2; - assert builtins.any (profile: profile.name == "tproxy" && profile.route == "tproxy") appRoutingTproxyProfiles; + assert builtins.length perAppRoutingTunProfiles == 2; + assert builtins.any (profile: profile.name == "tun" && profile.route == "tun") perAppRoutingTunProfiles; true ) - # -- appRouting: createDefaultProfiles injects curated zapret profile when backend and zapret are enabled -- + # -- perAppRouting: createDefaultProfiles injects curated tproxy profile when backend is enabled -- ( - assert builtins.length appRoutingZapretProfiles == 2; - assert builtins.any (profile: profile.name == "zapret" && profile.route == "zapret") appRoutingZapretProfiles; + assert builtins.length perAppRoutingTproxyProfiles == 2; + assert builtins.any (profile: profile.name == "tproxy" && profile.route == "tproxy") perAppRoutingTproxyProfiles; true ) - # -- appRouting: proxy-ctl script embeds wrap/apps commands -- + # -- perAppRouting: createDefaultProfiles injects curated zapret profile when backend and zapret are enabled -- ( - assert pkgs.lib.hasInfix "wrap -- " appRoutingProxychainsScript; - assert pkgs.lib.hasInfix "apps" appRoutingProxychainsScript; + assert builtins.length perAppRoutingZapretProfiles == 2; + assert builtins.any (profile: profile.name == "zapret" && profile.route == "zapret") perAppRoutingZapretProfiles; true ) - # -- appRouting: proxychains config is quiet and points at local SOCKS listener -- + # -- perAppRouting: proxy-ctl script embeds wrap/apps commands -- ( - assert pkgs.lib.hasInfix "quiet_mode" appRoutingProxychainsConfig; - assert pkgs.lib.hasInfix "proxy_dns" appRoutingProxychainsConfig; - assert pkgs.lib.hasInfix "socks5 127.0.0.1 1080" appRoutingProxychainsConfig; + assert pkgs.lib.hasInfix "wrap -- " perAppRoutingProxychainsScript; + assert pkgs.lib.hasInfix "apps" perAppRoutingProxychainsScript; true ) - # -- appRouting: generated proxy-ctl script dispatches through proxychains4 -- + # -- perAppRouting: proxychains config is quiet and points at local SOCKS listener -- ( - assert pkgs.lib.hasInfix "proxychains4 -q -f \"$PROXYCHAINS_CONFIG\" \"$@\"" appRoutingProxychainsScript; + assert pkgs.lib.hasInfix "quiet_mode" perAppRoutingProxychainsConfig; + assert pkgs.lib.hasInfix "proxy_dns" perAppRoutingProxychainsConfig; + assert pkgs.lib.hasInfix "socks5 127.0.0.1 1080" perAppRoutingProxychainsConfig; true ) - # -- appRouting: generated proxy-ctl script dispatches tun profiles through systemd slices -- + # -- perAppRouting: generated proxy-ctl script dispatches through proxychains4 -- ( - assert pkgs.lib.hasInfix "APP_ROUTING_TUN_ENABLED" appRoutingTunScript; - assert pkgs.lib.hasInfix "systemd-run --user --scope --quiet --collect --same-dir" appRoutingTunScript; - assert pkgs.lib.hasInfix ''APP_TUN_SLICE_BASE="proxy-suite-app-tun"'' appRoutingTunScript; - assert pkgs.lib.hasInfix "$slice_base-user@$uid.service" appRoutingTunScript; + assert pkgs.lib.hasInfix "proxychains4 -q -f \"$PROXYCHAINS_CONFIG\" \"$@\"" perAppRoutingProxychainsScript; true ) - # -- appRouting: generated proxy-ctl script dispatches tproxy profiles through systemd slices -- + # -- perAppRouting: generated proxy-ctl script dispatches tun profiles through systemd slices -- ( - assert pkgs.lib.hasInfix "APP_ROUTING_TPROXY_ENABLED" appRoutingTproxyScript; - assert pkgs.lib.hasInfix ''APP_TPROXY_SLICE_BASE="proxy-suite-app-tproxy"'' appRoutingTproxyScript; - assert pkgs.lib.hasInfix ''$slice_base-''${profile}-$$'' appRoutingTproxyScript; + assert pkgs.lib.hasInfix "PER_APP_ROUTING_TUN_ENABLED" perAppRoutingTunScript; + assert pkgs.lib.hasInfix "systemd-run --user --scope --quiet --collect --same-dir" perAppRoutingTunScript; + assert pkgs.lib.hasInfix ''PER_APP_TUN_SLICE_BASE="proxy-suite-per-app-tun"'' perAppRoutingTunScript; + assert pkgs.lib.hasInfix "$slice_base-user@$uid.service" perAppRoutingTunScript; true ) - # -- appRouting: generated proxy-ctl script dispatches zapret profiles through systemd slices -- + # -- perAppRouting: generated proxy-ctl script dispatches tproxy profiles through systemd slices -- ( - assert pkgs.lib.hasInfix "APP_ROUTING_ZAPRET_ENABLED" appRoutingZapretScript; - assert pkgs.lib.hasInfix ''APP_ZAPRET_SLICE_BASE="proxy-suite-app-zapret"'' appRoutingZapretScript; - assert pkgs.lib.hasInfix ''$slice_base-''${profile}-$$'' appRoutingZapretScript; + assert pkgs.lib.hasInfix "PER_APP_ROUTING_TPROXY_ENABLED" perAppRoutingTproxyScript; + assert pkgs.lib.hasInfix ''PER_APP_TPROXY_SLICE_BASE="proxy-suite-per-app-tproxy"'' perAppRoutingTproxyScript; + assert pkgs.lib.hasInfix ''$slice_base-''${profile}-$$'' perAppRoutingTproxyScript; true ) - # -- appRouting: user mark script installs fwmark + conntrack mark rules -- + # -- perAppRouting: generated proxy-ctl script dispatches zapret profiles through systemd slices -- ( - assert pkgs.lib.hasInfix "meta mark set" appRoutingTunUserStartScript; - assert pkgs.lib.hasInfix "ct mark set" appRoutingTunUserStartScript; + assert pkgs.lib.hasInfix "PER_APP_ROUTING_ZAPRET_ENABLED" perAppRoutingZapretScript; + assert pkgs.lib.hasInfix ''PER_APP_ZAPRET_SLICE_BASE="proxy-suite-per-app-zapret"'' perAppRoutingZapretScript; + assert pkgs.lib.hasInfix ''$slice_base-''${profile}-$$'' perAppRoutingZapretScript; true ) - # -- appRouting: app TUN config is separate and does not auto-route globally -- + # -- perAppRouting: user mark script installs fwmark + conntrack mark rules -- + ( + assert pkgs.lib.hasInfix "meta mark set" perAppRoutingTunUserStartScript; + assert pkgs.lib.hasInfix "ct mark set" perAppRoutingTunUserStartScript; + true + ) + + # -- perAppRouting: app TUN config is separate and does not auto-route globally -- ( let - inbound = builtins.head (builtins.filter (item: item.tag == "tun-in") appRoutingTunConfig.inbounds); + inbound = builtins.head (builtins.filter (item: item.tag == "tun-in") perAppRoutingTunConfig.inbounds); in - assert inbound.interface_name == "psapptun0"; + assert inbound.interface_name == "psperapptun0"; assert inbound.address == [ "172.20.0.1/30" ]; assert inbound.auto_route == false; assert inbound.auto_redirect == false; @@ -1706,69 +1867,69 @@ let true ) - # -- appRouting: app TUN local DNS keeps direct path without explicit detour -- + # -- perAppRouting: app TUN local DNS keeps direct path without explicit detour -- ( let - localDns = dnsServerByTag appRoutingTunConfig "local"; + localDns = dnsServerByTag perAppRoutingTunConfig "local"; in assert !(localDns ? detour); true ) - # -- appRouting: app TUN does not set outbound routing marks when TProxy is disabled -- + # -- perAppRouting: app TUN does not set outbound routing marks when TProxy is disabled -- ( - assert builtins.match ".*--routing-mark.*" appRoutingTunStartScript == null; - assert !(appRoutingTunDirectOutbound ? routing_mark); + assert builtins.match ".*--routing-mark.*" perAppRoutingTunStartScript == null; + assert !(perAppRoutingTunDirectOutbound ? routing_mark); true ) - # -- appRouting: app TUN applies outbound routing marks when TProxy is enabled -- + # -- perAppRouting: app TUN applies outbound routing marks when TProxy is enabled -- ( let - expectedMark = appRoutingTunWithTproxyFixture.config.services.proxy-suite.singBox.proxyMark; + expectedMark = perAppRoutingTunWithTproxyFixture.config.services.proxy-suite.singBox.tproxy.proxyMark; in - assert builtins.match ".*--routing-mark.*" appRoutingTunWithTproxyStartScript != null; - assert appRoutingTunWithTproxyDirectOutbound.routing_mark == expectedMark; + assert builtins.match ".*--routing-mark.*" perAppRoutingTunWithTproxyStartScript != null; + assert perAppRoutingTunWithTproxyDirectOutbound.routing_mark == expectedMark; true ) - # -- appRouting: app TUN service and helper units are created -- + # -- perAppRouting: app TUN service and helper units are created -- ( - assert appRoutingTunFixture.config.systemd.services ? "proxy-suite-app-tun"; - assert appRoutingTunFixture.config.systemd.services ? "proxy-suite-app-tun-user@"; - assert appRoutingTunFixture.config.systemd.user.services ? "proxy-suite-app-tun-anchor"; + assert perAppRoutingTunFixture.config.systemd.services ? "proxy-suite-per-app-tun"; + assert perAppRoutingTunFixture.config.systemd.services ? "proxy-suite-per-app-tun-user@"; + assert perAppRoutingTunFixture.config.systemd.user.services ? "proxy-suite-per-app-tun-anchor"; true ) - # -- appRouting: app TProxy service and helper units are created -- + # -- perAppRouting: 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"; + assert perAppRoutingTproxyFixture.config.systemd.services ? "proxy-suite-per-app-tproxy"; + assert perAppRoutingTproxyFixture.config.systemd.services ? "proxy-suite-per-app-tproxy-user@"; + assert perAppRoutingTproxyFixture.config.systemd.user.services ? "proxy-suite-per-app-tproxy-anchor"; true ) - # -- appRouting: app zapret service and helper units are created -- + # -- perAppRouting: app zapret service and helper units are created -- ( - assert appRoutingZapretFixture.config.systemd.services ? "proxy-suite-app-zapret"; - assert appRoutingZapretFixture.config.systemd.services ? "proxy-suite-app-zapret-user@"; - assert appRoutingZapretFixture.config.systemd.user.services ? "proxy-suite-app-zapret-anchor"; + assert perAppRoutingZapretFixture.config.systemd.services ? "proxy-suite-per-app-zapret"; + assert perAppRoutingZapretFixture.config.systemd.services ? "proxy-suite-per-app-zapret-user@"; + assert perAppRoutingZapretFixture.config.systemd.user.services ? "proxy-suite-per-app-zapret-anchor"; true ) - # -- appRouting: app TUN enables nftables and user control group -- + # -- perAppRouting: app TUN enables nftables and user control group -- ( - assert appRoutingTunFixture.config.networking.nftables.enable; - assert appRoutingTunFixture.config.users.groups ? "proxy-suite"; + assert perAppRoutingTunFixture.config.networking.nftables.enable; + assert perAppRoutingTunFixture.config.users.groups ? "proxy-suite"; true ) # -- userControl: default polkit rule covers both per-app and global proxy-ctl managed units -- ( - assert pkgs.lib.hasInfix "unit.indexOf(\"proxy-suite-app-\") === 0" appRoutingTunPolkitConfig; - assert pkgs.lib.hasInfix "unit.indexOf(\"proxy-suite-\") === 0" appRoutingTunPolkitConfig; - assert pkgs.lib.hasInfix "unit.indexOf(\"proxy-suite-app-\") !== 0" appRoutingTunPolkitConfig; - assert pkgs.lib.hasInfix "unit === \"zapret-discord-youtube.service\"" appRoutingTunPolkitConfig; + assert pkgs.lib.hasInfix "unit.indexOf(\"proxy-suite-per-app-\") === 0" perAppRoutingTunPolkitConfig; + assert pkgs.lib.hasInfix "unit.indexOf(\"proxy-suite-\") === 0" perAppRoutingTunPolkitConfig; + assert pkgs.lib.hasInfix "unit.indexOf(\"proxy-suite-per-app-\") !== 0" perAppRoutingTunPolkitConfig; + assert pkgs.lib.hasInfix "unit === \"zapret-discord-youtube.service\"" perAppRoutingTunPolkitConfig; true ) @@ -1776,17 +1937,17 @@ let ( assert userControlGlobalOnlyFixture.config.users.groups ? "proxy-suite"; assert pkgs.lib.hasInfix "unit.indexOf(\"proxy-suite-\") === 0" userControlGlobalOnlyPolkitConfig; - assert pkgs.lib.hasInfix "unit.indexOf(\"proxy-suite-app-\") !== 0" userControlGlobalOnlyPolkitConfig; - assert builtins.match ".*unit\\.indexOf\\(\"proxy-suite-app-\"\\) === 0.*" userControlGlobalOnlyPolkitConfig == null; + assert pkgs.lib.hasInfix "unit.indexOf(\"proxy-suite-per-app-\") !== 0" userControlGlobalOnlyPolkitConfig; + assert builtins.match ".*unit\\.indexOf\\(\"proxy-suite-per-app-\"\\) === 0.*" userControlGlobalOnlyPolkitConfig == null; assert pkgs.lib.hasInfix "unit === \"zapret-discord-youtube.service\"" userControlGlobalOnlyPolkitConfig; true ) - # -- userControl: per-app-only rule covers app-scoped helpers only -- + # -- userControl: per-app-only rule covers per-app-scoped helpers only -- ( assert userControlPerAppOnlyFixture.config.users.groups ? "proxy-suite"; - assert pkgs.lib.hasInfix "unit.indexOf(\"proxy-suite-app-\") === 0" userControlPerAppOnlyPolkitConfig; - assert builtins.match ".*unit\\.indexOf\\(\"proxy-suite-app-\"\\) !== 0.*" userControlPerAppOnlyPolkitConfig == null; + assert pkgs.lib.hasInfix "unit.indexOf(\"proxy-suite-per-app-\") === 0" userControlPerAppOnlyPolkitConfig; + assert builtins.match ".*unit\\.indexOf\\(\"proxy-suite-per-app-\"\\) !== 0.*" userControlPerAppOnlyPolkitConfig == null; assert builtins.match ".*unit === \"zapret-discord-youtube\\.service\".*" userControlPerAppOnlyPolkitConfig == null; true ) @@ -1800,140 +1961,160 @@ let true ) - # -- appRouting: app TProxy helper installs socket cgroup mark rules -- + # -- perAppRouting: 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; + assert pkgs.lib.hasInfix "socket cgroupv2" perAppRoutingTproxyUserStartScript; + assert pkgs.lib.hasInfix "meta mark set" perAppRoutingTproxyUserStartScript; + assert pkgs.lib.hasInfix "ct mark set" perAppRoutingTproxyUserStartScript; true ) - # -- appRouting: app TProxy startup installs nftables and loopback policy route -- + # -- perAppRouting: 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; + assert pkgs.lib.hasInfix "proxy_suite_per_app_tproxy" perAppRoutingTproxyStartScript; + assert pkgs.lib.hasInfix "route replace local default dev lo table 102" perAppRoutingTproxyStartScript; + assert pkgs.lib.hasInfix "rule add fwmark 17 table 102" perAppRoutingTproxyStartScript; true ) - # -- appRouting: app zapret helper installs socket cgroup bitwise mark rules -- + # -- perAppRouting: app zapret helper installs socket cgroup bitwise mark rules -- ( - assert pkgs.lib.hasInfix "socket cgroupv2" appRoutingZapretUserStartScript; - assert pkgs.lib.hasInfix "meta mark set meta mark or 268435456" appRoutingZapretUserStartScript; - assert pkgs.lib.hasInfix "ct mark set ct mark or 268435456" appRoutingZapretUserStartScript; + assert pkgs.lib.hasInfix "socket cgroupv2" perAppRoutingZapretUserStartScript; + assert pkgs.lib.hasInfix "meta mark set meta mark or 268435456" perAppRoutingZapretUserStartScript; + assert pkgs.lib.hasInfix "ct mark set ct mark or 268435456" perAppRoutingZapretUserStartScript; true ) - # -- appRouting: app zapret startup installs nftables backend -- + # -- perAppRouting: app zapret startup installs nftables backend -- ( - assert pkgs.lib.hasInfix "proxy_suite_app_zapret_mark" appRoutingZapretStartScript; + assert pkgs.lib.hasInfix "proxy_suite_per_app_zapret_mark" perAppRoutingZapretStartScript; true ) - # -- appRouting: global zapret config keeps FILTER_MARK disabled -- + # -- perAppRouting: global zapret config keeps FILTER_MARK disabled -- ( - assert pkgs.lib.hasInfix "FILTER_MARK=" appRoutingZapretConfig; - assert builtins.match ".*FILTER_MARK=0x10000000.*" appRoutingZapretConfig == null; + assert pkgs.lib.hasInfix "FILTER_MARK=" perAppRoutingZapretConfig; + assert builtins.match ".*FILTER_MARK=0x10000000.*" perAppRoutingZapretConfig == null; true ) - # -- appRouting: app zapret config is a separate instance with its own filter/qnum/marks -- + # -- perAppRouting: app zapret config is a separate instance with its own filter/qnum/marks -- ( - assert pkgs.lib.hasInfix "FILTER_MARK=0x10000000" appRoutingAppZapretConfig; - assert pkgs.lib.hasInfix "MODE_FILTER=none" appRoutingAppZapretConfig; - assert pkgs.lib.hasInfix "QNUM=201" appRoutingAppZapretConfig; - assert pkgs.lib.hasInfix "DESYNC_MARK=0x8000000" appRoutingAppZapretConfig; - assert pkgs.lib.hasInfix "DESYNC_MARK_POSTNAT=0x4000000" appRoutingAppZapretConfig; - assert pkgs.lib.hasInfix "ZAPRET_NFT_TABLE=proxy_suite_app_zapret" appRoutingAppZapretConfig; + assert pkgs.lib.hasInfix "FILTER_MARK=0x10000000" perAppRoutingPerAppZapretConfig; + assert pkgs.lib.hasInfix "MODE_FILTER=none" perAppRoutingPerAppZapretConfig; + assert pkgs.lib.hasInfix "QNUM=201" perAppRoutingPerAppZapretConfig; + assert pkgs.lib.hasInfix "DESYNC_MARK=0x8000000" perAppRoutingPerAppZapretConfig; + assert pkgs.lib.hasInfix "DESYNC_MARK_POSTNAT=0x4000000" perAppRoutingPerAppZapretConfig; + assert pkgs.lib.hasInfix "ZAPRET_NFT_TABLE=proxy_suite_per_app_zapret" perAppRoutingPerAppZapretConfig; true ) - # -- appRouting: global zapret installs a bypass hook for app-marked traffic -- + # -- perAppRouting: global zapret installs a bypass hook for app-marked traffic -- ( - assert pkgs.lib.hasInfix "proxy-suite app-zapret bypass" appRoutingZapretGlobalCustomScript; - assert pkgs.lib.hasInfix "mark and 268435456 != 0 return" appRoutingZapretGlobalCustomScript; + assert pkgs.lib.hasInfix "proxy-suite per-app-zapret bypass" perAppRoutingZapretGlobalCustomScript; + assert pkgs.lib.hasInfix "mark and 268435456 != 0 return" perAppRoutingZapretGlobalCustomScript; true ) - # -- appRouting: route=proxychains requires proxychains.enable -- + # -- perAppRouting: route=proxychains requires proxychains.enable -- ( - assert appRoutingProxychainsWithoutEnable.success == false; + assert perAppRoutingProxychainsWithoutEnable.success == false; true ) - # -- appRouting: profiles require appRouting.enable -- + # -- perAppRouting: proxychains requires singBox.enable -- ( - assert appRoutingProfilesWithoutEnable.success == false; + assert perAppRoutingProxychainsWithoutSingBox.success == false; true ) - # -- appRouting: default proxychains profile still requires proxychains.enable -- + # -- perAppRouting: profiles require perAppRouting.enable -- ( - assert appRoutingDefaultProfilesWithoutProxychainsEnable.success == false; + assert perAppRoutingProfilesWithoutEnable.success == false; true ) - # -- appRouting: route=tun requires appRouting.backends.tun.enable -- + # -- perAppRouting: default proxychains profile still requires proxychains.enable -- ( - assert appRoutingTunWithoutEnable.success == false; + assert perAppRoutingDefaultProfilesWithoutProxychainsEnable.success == false; true ) - # -- appRouting: route=tproxy requires appRouting.backends.tproxy.enable -- + # -- perAppRouting: route=tun requires singBox.tun.perApp.enable -- ( - assert appRoutingTproxyWithoutEnable.success == false; + assert perAppRoutingTunWithoutEnable.success == false; true ) - # -- appRouting: route=zapret requires appRouting.backends.zapret.enable and zapret.enable -- + # -- perAppRouting: app TUN backend requires singBox.enable -- ( - assert appRoutingZapretWithoutEnable.success == false; - true - ) - ( - assert appRoutingZapretWithoutZapretService.success == false; + assert perAppRoutingTunBackendWithoutSingBox.success == false; + assert perAppRoutingTunProfileWithoutSingBox.success == false; true ) - # -- appRouting: profile names must be unique -- + # -- perAppRouting: route=tproxy requires singBox.tproxy.perApp.enable -- ( - assert duplicateAppRoutingProfiles.success == false; + assert perAppRoutingTproxyWithoutEnable.success == false; true ) - # -- appRouting: with TProxy enabled, app TUN fwmark must differ from proxyMark -- + # -- perAppRouting: app TProxy backend requires singBox.enable -- ( - assert appRoutingTunFwmarkCollision.success == false; + assert perAppRoutingTproxyBackendWithoutSingBox.success == false; + assert perAppRoutingTproxyProfileWithoutSingBox.success == false; true ) - # -- appRouting: app TProxy fwmark must differ from sing-box proxyMark -- + # -- perAppRouting: route=zapret requires zapret.perApp.enable and zapret.enable -- ( - assert appRoutingTproxyFwmarkCollision.success == false; + assert perAppRoutingZapretWithoutEnable.success == false; + true + ) + ( + assert perAppRoutingZapretWithoutZapretService.success == false; true ) - # -- appRouting: app TUN and app TProxy fwmarks must differ -- + # -- perAppRouting: profile names must be unique -- ( - assert appRoutingTunTproxyFwmarkCollision.success == false; + assert duplicatePerAppRoutingProfiles.success == false; true ) - # -- appRouting: app TUN and app TProxy route tables must differ -- + # -- perAppRouting: with TProxy enabled, app TUN fwmark must differ from proxyMark -- ( - assert appRoutingTunTproxyRouteTableCollision.success == false; + assert perAppRoutingTunFwmarkCollision.success == false; true ) - # -- appRouting: app zapret filter mark must differ from sing-box proxyMark -- + # -- perAppRouting: app TProxy fwmark must differ from sing-box proxyMark -- ( - assert appRoutingZapretFilterMarkCollision.success == false; + assert perAppRoutingTproxyFwmarkCollision.success == false; true ) - # -- appRouting: app TProxy and app zapret marks must differ -- + # -- perAppRouting: app TUN and app TProxy fwmarks must differ -- ( - assert appRoutingTproxyZapretMarkCollision.success == false; + assert perAppRoutingTunTproxyFwmarkCollision.success == false; + true + ) + + # -- perAppRouting: app TUN and app TProxy route tables must differ -- + ( + assert perAppRoutingTunTproxyRouteTableCollision.success == false; + true + ) + + # -- perAppRouting: app zapret filter mark must differ from sing-box proxyMark -- + ( + assert perAppRoutingZapretFilterMarkCollision.success == false; + true + ) + + # -- perAppRouting: app TProxy and app zapret marks must differ -- + ( + assert perAppRoutingTproxyZapretMarkCollision.success == false; true )