breaking options changes, refactor

This commit is contained in:
FUFSoB committed 2026-04-16 23:27:05 +05:00
1 parent ba7560bfac
commit 93e792f2e5
18 files changed
+1995 -1752

No files matched your search

+4 -6
View File
@@ -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 = {
+735 -737
View File
File diff suppressed because it is too large. Load diff
+18 -16
View File
@@ -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;
}
+29 -31
View File
@@ -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;
}
))
+32 -31
View File
@@ -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;
}
-231
View File
@@ -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 -- <command>` available
without defining the profile manually, and similarly exposes
`proxy-ctl wrap tun -- <command>` or
`proxy-ctl wrap tproxy -- <command>` or
`proxy-ctl wrap zapret -- <command>` 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;
};
};
};
};
}
+1 -1
View File
@@ -4,7 +4,7 @@
imports = [
./other.nix
./sing-box.nix
./app-routing.nix
./per-app-routing.nix
./zapret.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 -- <command>` available
without defining the profile manually, and similarly exposes
`proxy-ctl wrap tun -- <command>` or
`proxy-ctl wrap tproxy -- <command>` or
`proxy-ctl wrap zapret -- <command>` 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;
};
};
};
}
+174 -66
View File
@@ -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 = {
+7 -7
View File
@@ -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 <name> -- <command>`.
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
;
}
+25
View File
@@ -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;
+133 -81
View File
@@ -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
+72 -66
View File
@@ -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" ];
@@ -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 <tag> switch to a specific outbound (selector mode)"
echo " apps list configured per-app routing profiles"
echo " wrap <profile> -- <cmd> run a command via an appRouting profile"
echo " wrap <profile> -- <cmd> 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
+2 -2
View File
@@ -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;
}
+11 -9
View File
@@ -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;
}
+29 -29
View File
@@ -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;
};
};
+500 -319
View File
File diff suppressed because it is too large. Load diff