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 = { tproxy = {
enable = true; enable = true;
autostart = false; autostart = false;
perApp.enable = true;
}; };
tun = { tun = {
enable = true; enable = true;
autostart = false; autostart = false;
perApp.enable = true;
}; };
}; };
zapret = { zapret = {
enable = true; enable = true;
perApp.enable = true;
}; };
appRouting = { perAppRouting = {
enable = true; enable = true;
createDefaultProfiles = true; createDefaultProfiles = true;
proxychains.enable = true; proxychains.enable = true;
backends = {
tun.enable = true;
tproxy.enable = true;
zapret.enable = true;
};
}; };
tray = { tray = {
+735 -737
View File
File diff suppressed because it is too large. Load diff
+18 -16
View File
@@ -9,7 +9,9 @@
let let
singBoxCfg = cfg.singBox; singBoxCfg = cfg.singBox;
appRoutingTun = cfg.appRouting.backends.tun; globalTun = singBoxCfg.tun;
globalTproxy = singBoxCfg.tproxy;
perAppTun = singBoxCfg.tun.perApp;
direct = rules.direct; direct = rules.direct;
mkDnsServer = mkDnsServer =
@@ -54,9 +56,9 @@ let
enableMixed ? false, enableMixed ? false,
enableTProxy ? false, enableTProxy ? false,
enableTun ? false, enableTun ? false,
tunInterface ? singBoxCfg.tun.interface, tunInterface ? globalTun.interface,
tunAddress ? singBoxCfg.tun.address, tunAddress ? globalTun.address,
tunMtu ? singBoxCfg.tun.mtu, tunMtu ? globalTun.mtu,
tunAutoRoute ? true, tunAutoRoute ? true,
tunAutoRedirect ? true, tunAutoRedirect ? true,
tunStrictRoute ? true, tunStrictRoute ? true,
@@ -85,7 +87,7 @@ let
type = "tproxy"; type = "tproxy";
tag = "tproxy-in"; tag = "tproxy-in";
listen = "127.0.0.1"; listen = "127.0.0.1";
listen_port = singBoxCfg.tproxyPort; listen_port = globalTproxy.port;
} }
++ lib.optional enableTun { ++ lib.optional enableTun {
type = "tun"; type = "tun";
@@ -106,7 +108,7 @@ let
type = "direct"; type = "direct";
tag = "direct"; tag = "direct";
} }
// lib.optionalAttrs useOutboundRoutingMark { routing_mark = singBoxCfg.proxyMark; } // lib.optionalAttrs useOutboundRoutingMark { routing_mark = globalTproxy.proxyMark; }
) )
{ {
type = "block"; type = "block";
@@ -133,9 +135,9 @@ let
tunTemplate = mkConfig { tunTemplate = mkConfig {
enableTun = true; enableTun = true;
tunInterface = singBoxCfg.tun.interface; tunInterface = globalTun.interface;
tunAddress = singBoxCfg.tun.address; tunAddress = globalTun.address;
tunMtu = singBoxCfg.tun.mtu; tunMtu = globalTun.mtu;
tunAutoRoute = true; tunAutoRoute = true;
tunAutoRedirect = true; tunAutoRedirect = true;
tunStrictRoute = true; tunStrictRoute = true;
@@ -143,26 +145,26 @@ let
enableClashApi = false; enableClashApi = false;
}; };
appTunTemplate = mkConfig { perAppTunTemplate = mkConfig {
enableTun = true; enableTun = true;
tunInterface = appRoutingTun.interface; tunInterface = perAppTun.interface;
tunAddress = appRoutingTun.address; tunAddress = perAppTun.address;
tunMtu = appRoutingTun.mtu; tunMtu = perAppTun.mtu;
tunAutoRoute = false; tunAutoRoute = false;
tunAutoRedirect = false; tunAutoRedirect = false;
tunStrictRoute = false; tunStrictRoute = false;
forceLocalDnsViaProxy = false; forceLocalDnsViaProxy = false;
# App TUN can be used alongside TProxy; mark direct egress so TProxy output # App TUN can be used alongside TProxy; mark direct egress so TProxy output
# rules do not re-intercept sing-box's own packets. # rules do not re-intercept sing-box's own packets.
useOutboundRoutingMark = singBoxCfg.tproxy.enable; useOutboundRoutingMark = globalTproxy.enable;
enableClashApi = false; enableClashApi = false;
}; };
tproxyFile = pkgs.writeText "proxy-suite-tproxy-template.json" (builtins.toJSON tproxyTemplate); tproxyFile = pkgs.writeText "proxy-suite-tproxy-template.json" (builtins.toJSON tproxyTemplate);
tunFile = pkgs.writeText "proxy-suite-tun-template.json" (builtins.toJSON tunTemplate); 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 in
{ {
inherit tproxyFile tunFile appTunFile; inherit tproxyFile tunFile perAppTunFile;
} }
+29 -31
View File
@@ -19,37 +19,35 @@ in
]; ];
config = lib.mkIf cfg.enable ( 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.mkMerge [
(lib.mkIf cfg.singBox.enable ( (import ./service {
let inherit
rules = import ./rules.nix { config
inherit lib
lib pkgs
pkgs cfg
cfg ;
zapret inherit (configs) tproxyFile tunFile perAppTunFile;
; inherit (nftr) nftablesRulesFile perAppTproxyRulesFile perAppZapretRulesFile perAppTunChainFile ip nft;
}; })
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;
}
))
(lib.mkIf cfg.zapret.enable ( (lib.mkIf cfg.zapret.enable (
import ./zapret.nix { import ./zapret.nix {
@@ -59,7 +57,7 @@ in
cfg cfg
zapret zapret
; ;
inherit (nftr) appZapretRulesFile nft; inherit (nftr) perAppZapretRulesFile nft;
} }
)) ))
+32 -31
View File
@@ -7,9 +7,10 @@
let let
singBoxCfg = cfg.singBox; singBoxCfg = cfg.singBox;
appRoutingTun = cfg.appRouting.backends.tun; globalTproxy = singBoxCfg.tproxy;
appRoutingTproxy = cfg.appRouting.backends.tproxy; perAppTun = singBoxCfg.tun.perApp;
appRoutingZapret = cfg.appRouting.backends.zapret; perAppTproxy = singBoxCfg.tproxy.perApp;
zapretApp = cfg.zapret.perApp;
# Shared across all three nftables rule files that do IPv4 routing. # Shared across all three nftables rule files that do IPv4 routing.
reservedIpBlock = '' reservedIpBlock = ''
@@ -31,8 +32,8 @@ let
ip daddr ${cidr} udp dport != 53 return ip daddr ${cidr} udp dport != 53 return
'') subnets; '') subnets;
tproxyLocalSubnetLines = mkLocalSubnetLines singBoxCfg.tproxy.localSubnets; tproxyLocalSubnetLines = mkLocalSubnetLines globalTproxy.localSubnets;
appTproxyLocalSubnetLines = mkLocalSubnetLines appRoutingTproxy.localSubnets; perAppTproxyLocalSubnetLines = mkLocalSubnetLines perAppTproxy.localSubnets;
nftablesRulesFile = pkgs.writeText "proxy-suite-tproxy.nft" '' nftablesRulesFile = pkgs.writeText "proxy-suite-tproxy.nft" ''
${reservedIpBlock} ${reservedIpBlock}
@@ -44,70 +45,70 @@ let
# be skipped just because the host has an RFC1918 source address. # be skipped just because the host has an RFC1918 source address.
iifname != "lo" ip saddr $RESERVED_IP return iifname != "lo" ip saddr $RESERVED_IP return
${tproxyLocalSubnetLines} ${tproxyLocalSubnetLines}
ip protocol tcp 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 singBoxCfg.tproxyPort} meta mark set ${toString singBoxCfg.fwmark} ip protocol udp tproxy to 127.0.0.1:${toString globalTproxy.port} meta mark set ${toString globalTproxy.fwmark}
} }
chain output { chain output {
type route hook output priority mangle; policy accept; type route hook output priority mangle; policy accept;
ip daddr $RESERVED_IP return ip daddr $RESERVED_IP return
${tproxyLocalSubnetLines} ${tproxyLocalSubnetLines}
meta mark ${toString singBoxCfg.proxyMark} return meta mark ${toString globalTproxy.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} ${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 singBoxCfg.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} ${reservedIpBlock}
table ip proxy_suite_app_tproxy { table ip proxy_suite_per_app_tproxy {
chain prerouting { chain prerouting {
type filter hook prerouting priority mangle; policy accept; type filter hook prerouting priority mangle; policy accept;
ip daddr $RESERVED_IP return ip daddr $RESERVED_IP return
iifname != "lo" ip saddr $RESERVED_IP return iifname != "lo" ip saddr $RESERVED_IP return
${appTproxyLocalSubnetLines} ${perAppTproxyLocalSubnetLines}
ct mark ${toString appRoutingTproxy.fwmark} meta mark set ${toString appRoutingTproxy.fwmark} ct mark ${toString perAppTproxy.fwmark} meta mark set ${toString perAppTproxy.fwmark}
meta mark ${toString appRoutingTproxy.fwmark} ip protocol tcp tproxy to 127.0.0.1:${toString singBoxCfg.tproxyPort} meta mark ${toString perAppTproxy.fwmark} ip protocol tcp tproxy to 127.0.0.1:${toString globalTproxy.port}
meta mark ${toString appRoutingTproxy.fwmark} ip protocol udp tproxy to 127.0.0.1:${toString singBoxCfg.tproxyPort} meta mark ${toString perAppTproxy.fwmark} ip protocol udp tproxy to 127.0.0.1:${toString globalTproxy.port}
} }
chain output { chain output {
type route hook output priority mangle; policy accept; type route hook output priority mangle; policy accept;
ip daddr $RESERVED_IP return ip daddr $RESERVED_IP return
${appTproxyLocalSubnetLines} ${perAppTproxyLocalSubnetLines}
meta mark ${toString singBoxCfg.proxyMark} return meta mark ${toString globalTproxy.proxyMark} return
${lib.optionalString appRoutingTun.enable " meta mark ${toString appRoutingTun.fwmark} return\n"} ct mark ${toString appRoutingTproxy.fwmark} meta mark set ${toString appRoutingTproxy.fwmark} ${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 appRoutingTproxy.fwmark} return meta mark ${toString perAppTproxy.fwmark} return
} }
} }
''; '';
appZapretRulesFile = pkgs.writeText "proxy-suite-app-zapret.nft" '' perAppZapretRulesFile = pkgs.writeText "proxy-suite-per-app-zapret.nft" ''
table inet proxy_suite_app_zapret_mark { table inet proxy_suite_per_app_zapret_mark {
chain prerouting { chain prerouting {
type filter hook prerouting priority -103; policy accept; 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 { chain output {
type route hook output priority -103; policy accept; 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} ct mark and ${toString zapretApp.filterMark} == ${toString zapretApp.filterMark} meta mark set meta mark or ${toString zapretApp.filterMark}
meta mark and ${toString appRoutingZapret.filterMark} == ${toString appRoutingZapret.filterMark} return 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} ${reservedIpBlock}
table inet proxy_suite_app_tun { table inet proxy_suite_per_app_tun {
chain output { chain output {
type route hook output priority mangle; policy accept; type route hook output priority mangle; policy accept;
ip daddr $RESERVED_IP return ip daddr $RESERVED_IP return
${lib.concatMapStrings (cidr: '' ${lib.concatMapStrings (cidr: ''
ip daddr ${cidr} return ip daddr ${cidr} return
'') appRoutingTun.localSubnets} '') perAppTun.localSubnets}
ct mark ${toString appRoutingTun.fwmark} meta mark set ${toString appRoutingTun.fwmark} ct mark ${toString perAppTun.fwmark} meta mark set ${toString perAppTun.fwmark}
meta mark ${toString appRoutingTun.fwmark} return meta mark ${toString perAppTun.fwmark} return
} }
} }
''; '';
@@ -117,5 +118,5 @@ ${lib.concatMapStrings (cidr: ''
in 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 = [ imports = [
./other.nix ./other.nix
./sing-box.nix ./sing-box.nix
./app-routing.nix ./per-app-routing.nix
./zapret.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 let
inherit (lib) mkOption mkEnableOption types; inherit (lib) mkOption mkEnableOption types;
t = import ./types.nix { inherit lib; }; 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 in
{ {
options.services.proxy-suite.singBox = { options.services.proxy-suite.singBox = {
@@ -12,8 +44,8 @@ in
default = true; default = true;
description = '' description = ''
Whether to configure and run sing-box services for proxy-suite. Whether to configure and run sing-box services for proxy-suite.
When disabled, singBox.* options are ignored even if proxy-suite itself When disabled, sing-box services and generated sing-box configs are
is enabled. skipped even if proxy-suite itself is enabled.
''; '';
}; };
@@ -183,49 +215,6 @@ in
example = 1080; 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 { proxyByDefault = mkOption {
type = types.bool; type = types.bool;
default = true; default = true;
@@ -252,7 +241,7 @@ in
This resolver is also used as sing-box route.default_domain_resolver. This resolver is also used as sing-box route.default_domain_resolver.
The module keeps detour policy automatic: in mixed/TProxy mode and 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 explicit detour); in global TUN mode, it is forced through the proxy to avoid
auto_redirect conflicts. auto_redirect conflicts.
''; '';
@@ -285,7 +274,7 @@ in
}; };
tun = { tun = {
enable = mkEnableOption "TUN mode service (opt-in, mutually exclusive with proxy-suite-tproxy)"; enable = mkEnableOption "global sing-box TUN mode service";
autostart = mkOption { autostart = mkOption {
type = types.bool; type = types.bool;
@@ -298,30 +287,67 @@ in
example = true; example = true;
}; };
interface = mkOption { perApp = {
type = types.str; enable = mkEnableOption "per-app-scoped sing-box TUN backend for perAppRouting profiles";
default = "singtun0";
description = "Name of the TUN interface created by proxy-suite-tun.";
example = "singtun0";
};
address = mkOption { fwmark = mkOption {
type = types.str; type = types.int;
default = "172.19.0.1/30"; default = 16;
description = "Address assigned to the TUN interface in CIDR notation."; description = ''
example = "172.19.0.1/30"; Packet mark used to steer wrapped app traffic into the per-app-scoped
}; TUN policy-routing table.
'';
example = 16;
};
mtu = mkOption { routeTable = mkOption {
type = types.int; type = types.int;
default = 1400; default = 101;
description = "MTU for the TUN interface created by proxy-suite-tun."; description = ''
example = 1400; 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 = { tproxy = {
enable = mkEnableOption "TProxy mode service (opt-in, transparent proxy via nftables)"; enable = mkEnableOption "global sing-box TProxy mode service";
autostart = mkOption { autostart = mkOption {
type = types.bool; type = types.bool;
@@ -334,11 +360,57 @@ in
example = true; 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 { localSubnets = mkOption {
type = types.listOf types.str; type = types.listOf types.str;
default = [ "192.168.0.0/16" ]; default = [ "192.168.0.0/16" ];
description = '' 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 Typically this should include your LAN subnet(s). VM bridge networks
should usually go here too, or use zapret.cidrExemption for should usually go here too, or use zapret.cidrExemption for
@@ -349,6 +421,42 @@ in
"10.0.0.0/8" "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 = { routing = {
+7 -7
View File
@@ -284,13 +284,13 @@ let
}; };
}; };
appRoutingProfileType = types.submodule { perAppRoutingProfileType = types.submodule {
options = { options = {
name = mkOption { name = mkOption {
type = types.strMatching "^[a-z0-9][a-z0-9-]*$"; type = types.strMatching "^[a-z0-9][a-z0-9-]*$";
description = '' description = ''
Profile name used by `proxy-ctl wrap <name> -- <command>`. Profile name used by `proxy-ctl wrap <name> -- <command>`.
Must be unique within appRouting.profiles. Must be unique within perAppRouting.profiles.
''; '';
example = "steam-browser"; example = "steam-browser";
}; };
@@ -310,14 +310,14 @@ let
- "direct": run the command unchanged. - "direct": run the command unchanged.
- "proxychains": run the command through proxychains-ng using the - "proxychains": run the command through proxychains-ng using the
local proxy-suite mixed SOCKS endpoint. 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. 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 slice so only that app's traffic is transparently intercepted by
the local sing-box TProxy inbound. 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 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. Additional route backends may be added in the future.
''; '';
@@ -336,6 +336,6 @@ in
dnsUpstreamType dnsUpstreamType
zapretPresetType zapretPresetType
zapretHostlistRuleType zapretHostlistRuleType
appRoutingProfileType perAppRoutingProfileType
; ;
} }
+25
View File
@@ -9,6 +9,31 @@ in
options.services.proxy-suite.zapret = { options.services.proxy-suite.zapret = {
enable = mkEnableOption "zapret DPI bypass"; 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 { syncDirectRouting = mkOption {
type = types.bool; type = types.bool;
default = true; default = true;
+133 -81
View File
@@ -3,156 +3,204 @@
cfg, cfg,
singBoxCfg, singBoxCfg,
tgWsProxyCfg, tgWsProxyCfg,
appRoutingCfg, perAppRoutingCfg,
appRoutingTun, perAppRoutingTun,
appRoutingTproxy, perAppRoutingTproxy,
appRoutingZapret, perAppZapretCfg,
builtinTags, builtinTags,
outboundTags, outboundTags,
invalidRoutingTargets, invalidRoutingTargets,
effectiveAppRoutingProfileNames, effectivePerAppRoutingProfileNames,
hasProxychainsProfiles, hasProxychainsProfiles,
hasTunProfiles, hasTunProfiles,
hasTproxyProfiles, hasTproxyProfiles,
hasZapretProfiles, hasZapretProfiles,
}: }:
let
globalTun = singBoxCfg.tun;
globalTproxy = singBoxCfg.tproxy;
in
[ [
{ {
assertion = !singBoxCfg.enable || (singBoxCfg.outbounds != [ ] || singBoxCfg.subscriptions != [ ]); assertion = !singBoxCfg.enable || (singBoxCfg.outbounds != [ ] || singBoxCfg.subscriptions != [ ]);
message = "proxy-suite: at least one outbound or subscription is required when singBox.enable = true"; 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"; 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"; 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}"; 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); assertion = !globalTun.enable || singBoxCfg.enable;
message = "proxy-suite: singBox.tproxy.autostart and singBox.tun.autostart cannot both be enabled at the same time"; message = "proxy-suite: singBox.tun.enable requires singBox.enable = true";
} }
{ {
assertion = appRoutingCfg.enable || appRoutingCfg.profiles == [ ]; assertion = !globalTproxy.enable || singBoxCfg.enable;
message = "proxy-suite: appRouting.profiles requires appRouting.enable = true"; message = "proxy-suite: singBox.tproxy.enable requires singBox.enable = true";
} }
{ {
assertion = !appRoutingCfg.proxychains.enable || appRoutingCfg.enable; assertion = !globalTun.autostart || globalTun.enable;
message = "proxy-suite: appRouting.proxychains.enable requires appRouting.enable = true"; message = "proxy-suite: singBox.tun.autostart requires singBox.tun.enable = true";
} }
{ {
assertion = builtins.length effectiveAppRoutingProfileNames == builtins.length (lib.unique effectiveAppRoutingProfileNames); assertion = !globalTproxy.autostart || globalTproxy.enable;
message = "proxy-suite: appRouting profile names must be unique"; message = "proxy-suite: singBox.tproxy.autostart requires singBox.tproxy.enable = true";
} }
{ {
assertion = !hasProxychainsProfiles || appRoutingCfg.proxychains.enable; assertion = !(globalTproxy.enable && globalTproxy.autostart && globalTun.enable && globalTun.autostart);
message = "proxy-suite: route=proxychains in appRouting.profiles requires appRouting.proxychains.enable = true"; message =
"proxy-suite: singBox.tproxy.autostart and singBox.tun.autostart cannot both be enabled at the same time";
} }
{ {
assertion = !appRoutingTun.enable || appRoutingCfg.enable; assertion = perAppRoutingCfg.enable || perAppRoutingCfg.profiles == [ ];
message = "proxy-suite: appRouting.backends.tun.enable requires appRouting.enable = true"; message = "proxy-suite: perAppRouting.profiles requires perAppRouting.enable = true";
} }
{ {
assertion = !(hasTunProfiles && !appRoutingTun.enable); assertion = !perAppRoutingCfg.proxychains.enable || perAppRoutingCfg.enable;
message = "proxy-suite: route=tun in appRouting profiles requires appRouting.backends.tun.enable = true"; message = "proxy-suite: perAppRouting.proxychains.enable requires perAppRouting.enable = true";
} }
{ {
assertion = !appRoutingTproxy.enable || appRoutingCfg.enable; assertion = !perAppRoutingCfg.proxychains.enable || singBoxCfg.enable;
message = "proxy-suite: appRouting.backends.tproxy.enable requires appRouting.enable = true"; message = "proxy-suite: perAppRouting.proxychains.enable requires singBox.enable = true";
} }
{ {
assertion = !(hasTproxyProfiles && !appRoutingTproxy.enable); assertion = builtins.length effectivePerAppRoutingProfileNames == builtins.length (lib.unique effectivePerAppRoutingProfileNames);
message = "proxy-suite: route=tproxy in appRouting profiles requires appRouting.backends.tproxy.enable = true"; message = "proxy-suite: perAppRouting profile names must be unique";
} }
{ {
assertion = !appRoutingZapret.enable || appRoutingCfg.enable; assertion = !hasProxychainsProfiles || perAppRoutingCfg.proxychains.enable;
message = "proxy-suite: appRouting.backends.zapretgWsProxyCfg.enable requires appRouting.enable = true"; message = "proxy-suite: route=proxychains in perAppRouting.profiles requires perAppRouting.proxychains.enable = true";
} }
{ {
assertion = !appRoutingZapret.enable || cfg.zapret.enable; assertion = !hasProxychainsProfiles || singBoxCfg.enable;
message = "proxy-suite: appRouting.backends.zapretgWsProxyCfg.enable requires zapretgWsProxyCfg.enable = true"; message = "proxy-suite: route=proxychains in perAppRouting.profiles requires singBox.enable = true";
} }
{ {
assertion = !(hasZapretProfiles && (!appRoutingZapret.enable || !cfg.zapret.enable)); assertion = !perAppRoutingTun.enable || perAppRoutingCfg.enable;
message = "proxy-suite: route=zapret in appRouting profiles requires appRouting.backends.zapretgWsProxyCfg.enable = true and zapretgWsProxyCfg.enable = true"; message = "proxy-suite: singBox.tun.perApp.enable requires perAppRouting.enable = true";
} }
{ {
assertion = !(appRoutingTun.enable && singBoxCfg.tproxy.enable && appRoutingTun.fwmark == singBoxCfg.fwmark); assertion = !perAppRoutingTun.enable || singBoxCfg.enable;
message = "proxy-suite: appRouting.backends.tun.fwmark must differ from singBox.fwmark when singBox.tproxy.enable = true"; message = "proxy-suite: singBox.tun.perApp.enable requires singBox.enable = true";
} }
{ {
assertion = !(appRoutingTun.enable && singBoxCfg.tproxy.enable && appRoutingTun.fwmark == singBoxCfg.proxyMark); assertion = !(hasTunProfiles && !perAppRoutingTun.enable);
message = "proxy-suite: appRouting.backends.tun.fwmark must differ from singBox.proxyMark when singBox.tproxy.enable = true"; message = "proxy-suite: route=tun in perAppRouting.profiles requires singBox.tun.perApp.enable = true";
} }
{ {
assertion = !(appRoutingTun.enable && singBoxCfg.tproxy.enable && appRoutingTun.routeTable == singBoxCfg.routeTable); assertion = !hasTunProfiles || singBoxCfg.enable;
message = "proxy-suite: appRouting.backends.tun.routeTable must differ from singBox.routeTable when singBox.tproxy.enable = true"; message = "proxy-suite: route=tun in perAppRouting.profiles requires singBox.enable = true";
} }
{ {
assertion = !(appRoutingTproxy.enable && appRoutingTproxy.fwmark == singBoxCfg.fwmark); assertion = !perAppRoutingTproxy.enable || perAppRoutingCfg.enable;
message = "proxy-suite: appRouting.backends.tproxy.fwmark must differ from singBox.fwmark"; message = "proxy-suite: singBox.tproxy.perApp.enable requires perAppRouting.enable = true";
} }
{ {
assertion = !(appRoutingTproxy.enable && appRoutingTproxy.fwmark == singBoxCfg.proxyMark); assertion = !perAppRoutingTproxy.enable || singBoxCfg.enable;
message = "proxy-suite: appRouting.backends.tproxy.fwmark must differ from singBox.proxyMark"; message = "proxy-suite: singBox.tproxy.perApp.enable requires singBox.enable = true";
} }
{ {
assertion = !(appRoutingTproxy.enable && appRoutingTproxy.routeTable == singBoxCfg.routeTable); assertion = !(hasTproxyProfiles && !perAppRoutingTproxy.enable);
message = "proxy-suite: appRouting.backends.tproxy.routeTable must differ from singBox.routeTable"; message = "proxy-suite: route=tproxy in perAppRouting.profiles requires singBox.tproxy.perApp.enable = true";
} }
{ {
assertion = !(appRoutingTun.enable && appRoutingTproxy.enable && appRoutingTun.fwmark == appRoutingTproxy.fwmark); assertion = !hasTproxyProfiles || singBoxCfg.enable;
message = "proxy-suite: appRouting.backends.tun.fwmark and appRouting.backends.tproxy.fwmark must differ"; message = "proxy-suite: route=tproxy in perAppRouting.profiles requires singBox.enable = true";
} }
{ {
assertion = !(appRoutingTun.enable && appRoutingTproxy.enable && appRoutingTun.routeTable == appRoutingTproxy.routeTable); assertion = !perAppZapretCfg.enable || perAppRoutingCfg.enable;
message = "proxy-suite: appRouting.backends.tun.routeTable and appRouting.backends.tproxy.routeTable must differ"; message = "proxy-suite: zapret.perApp.enable requires perAppRouting.enable = true";
} }
{ {
assertion = !(appRoutingZapret.enable && appRoutingZapret.filterMark == singBoxCfg.fwmark); assertion = !perAppZapretCfg.enable || cfg.zapret.enable;
message = "proxy-suite: appRouting.backends.zapretgWsProxyCfg.filterMark must differ from singBox.fwmark"; message = "proxy-suite: zapret.perApp.enable requires zapret.enable = true";
} }
{ {
assertion = !(appRoutingZapret.enable && appRoutingZapret.filterMark == singBoxCfg.proxyMark); assertion = !(hasZapretProfiles && (!perAppZapretCfg.enable || !cfg.zapret.enable));
message = "proxy-suite: appRouting.backends.zapretgWsProxyCfg.filterMark must differ from singBox.proxyMark"; 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); assertion = !(perAppRoutingTun.enable && globalTproxy.enable && perAppRoutingTun.fwmark == globalTproxy.fwmark);
message = "proxy-suite: appRouting.backends.tun.fwmark and appRouting.backends.zapretgWsProxyCfg.filterMark must differ"; 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); assertion = !(perAppRoutingTun.enable && globalTproxy.enable && perAppRoutingTun.fwmark == globalTproxy.proxyMark);
message = "proxy-suite: appRouting.backends.tproxy.fwmark and appRouting.backends.zapretgWsProxyCfg.filterMark must differ"; 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 ]); assertion = !(perAppRoutingTun.enable && globalTproxy.enable && perAppRoutingTun.routeTable == globalTproxy.routeTable);
message = "proxy-suite: appRouting.backends.zapretgWsProxyCfg.filterMark must not use zapret internal desync mark bits"; 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 ]); assertion = !(perAppRoutingTproxy.enable && perAppRoutingTproxy.fwmark == globalTproxy.fwmark);
message = "proxy-suite: singBox.fwmark must not use app-zapret internal desync mark bits"; message = "proxy-suite: singBox.tproxy.perApp.fwmark must differ from singBox.tproxy.fwmark";
} }
{ {
assertion = !(appRoutingZapret.enable && builtins.elem singBoxCfg.proxyMark [ 67108864 134217728 ]); assertion = !(perAppRoutingTproxy.enable && perAppRoutingTproxy.fwmark == globalTproxy.proxyMark);
message = "proxy-suite: singBox.proxyMark must not use app-zapret internal desync mark bits"; message = "proxy-suite: singBox.tproxy.perApp.fwmark must differ from singBox.tproxy.proxyMark";
} }
{ {
assertion = !(appRoutingTun.enable && appRoutingZapret.enable && builtins.elem appRoutingTun.fwmark [ 67108864 134217728 ]); assertion = !(perAppRoutingTproxy.enable && perAppRoutingTproxy.routeTable == globalTproxy.routeTable);
message = "proxy-suite: appRouting.backends.tun.fwmark must not use app-zapret internal desync mark bits"; message = "proxy-suite: singBox.tproxy.perApp.routeTable must differ from singBox.tproxy.routeTable";
} }
{ {
assertion = !(appRoutingTproxy.enable && appRoutingZapret.enable && builtins.elem appRoutingTproxy.fwmark [ 67108864 134217728 ]); assertion = !(perAppRoutingTun.enable && perAppRoutingTproxy.enable && perAppRoutingTun.fwmark == perAppRoutingTproxy.fwmark);
message = "proxy-suite: appRouting.backends.tproxy.fwmark must not use app-zapret internal desync mark bits"; message = "proxy-suite: singBox.tun.perApp.fwmark and singBox.tproxy.perApp.fwmark must differ";
} }
{ {
assertion = !(appRoutingZapret.enable && builtins.elem appRoutingZapret.filterMark [ 67108864 134217728 ]); assertion = !(perAppRoutingTun.enable && perAppRoutingTproxy.enable && perAppRoutingTun.routeTable == perAppRoutingTproxy.routeTable);
message = "proxy-suite: appRouting.backends.zapretgWsProxyCfg.filterMark must not use app-zapret internal desync mark bits"; 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 = assertion =
@@ -170,25 +218,29 @@
++ lib.concatMap (ob: [ ++ lib.concatMap (ob: [
{ {
assertion = assertion =
builtins.length ( !singBoxCfg.enable
builtins.filter (x: x != null) [ ||
ob.urlFile builtins.length (
ob.url builtins.filter (x: x != null) [
ob.json ob.urlFile
] ob.url
) == 1; ob.json
]
) == 1;
message = "proxy-suite: outbound '${ob.tag}': set exactly one of urlFile, url, or json"; message = "proxy-suite: outbound '${ob.tag}': set exactly one of urlFile, url, or json";
} }
]) singBoxCfg.outbounds ]) singBoxCfg.outbounds
++ lib.concatMap (sub: [ ++ lib.concatMap (sub: [
{ {
assertion = assertion =
builtins.length ( !singBoxCfg.enable
builtins.filter (x: x != null) [ ||
sub.urlFile builtins.length (
sub.url builtins.filter (x: x != null) [
] sub.urlFile
) == 1; sub.url
]
) == 1;
message = "proxy-suite: subscription '${sub.tag}': set exactly one of urlFile or url"; message = "proxy-suite: subscription '${sub.tag}': set exactly one of urlFile or url";
} }
]) singBoxCfg.subscriptions ]) singBoxCfg.subscriptions
+72 -66
View File
@@ -6,21 +6,23 @@
cfg, cfg,
tproxyFile, tproxyFile,
tunFile, tunFile,
appTunFile, perAppTunFile,
nftablesRulesFile, nftablesRulesFile,
appTproxyRulesFile, perAppTproxyRulesFile,
appZapretRulesFile, perAppZapretRulesFile,
appTunChainFile, perAppTunChainFile,
ip, ip,
nft, nft,
}: }:
let let
singBoxCfg = cfg.singBox; singBoxCfg = cfg.singBox;
appRoutingCfg = cfg.appRouting; perAppRoutingCfg = cfg.perAppRouting;
appRoutingTun = appRoutingCfg.backends.tun; globalTun = singBoxCfg.tun;
appRoutingTproxy = appRoutingCfg.backends.tproxy; globalTproxy = singBoxCfg.tproxy;
appRoutingZapret = appRoutingCfg.backends.zapret; perAppRoutingTun = singBoxCfg.tun.perApp;
perAppRoutingTproxy = singBoxCfg.tproxy.perApp;
perAppZapretCfg = cfg.zapret.perApp;
userControlCfg = cfg.userControl; userControlCfg = cfg.userControl;
builtinTags = [ "proxy" "direct" "block" ]; builtinTags = [ "proxy" "direct" "block" ];
@@ -57,14 +59,15 @@ let
}; };
scripts = import ./scripts.nix { scripts = import ./scripts.nix {
inherit lib pkgs singBoxCfg appRoutingTun; inherit lib pkgs singBoxCfg perAppRoutingTun;
inherit jq python3 singBox parserScriptsPythonPath buildOutboundPy fetchSubscriptionPy; inherit jq python3 singBox parserScriptsPythonPath buildOutboundPy fetchSubscriptionPy;
inherit tproxyFile tunFile appTunFile; inherit tproxyFile tunFile perAppTunFile;
}; };
appRouting = import ./app-routing.nix { perAppRouting = import ./per-app-routing.nix {
inherit lib pkgs cfg singBoxCfg appRoutingCfg appRoutingTun appRoutingTproxy appRoutingZapret; inherit lib pkgs cfg singBoxCfg perAppRoutingCfg perAppRoutingTun perAppRoutingTproxy;
inherit appTunChainFile appTproxyRulesFile appZapretRulesFile; perAppZapretCfg = perAppZapretCfg;
inherit perAppTunChainFile perAppTproxyRulesFile perAppZapretRulesFile;
inherit ip nft; inherit ip nft;
inherit awk grepBin findBin headBin seqBin sleepBin; inherit awk grepBin findBin headBin seqBin sleepBin;
inherit jq proxychains4 systemdRun systemctl journalctl idBin; inherit jq proxychains4 systemdRun systemctl journalctl idBin;
@@ -74,11 +77,11 @@ let
}; };
in in
{ {
environment.systemPackages = [ appRouting.proxyCtl ]; environment.systemPackages = [ perAppRouting.proxyCtl ];
# nftables must be on for TProxy to work. # nftables must be on for TProxy to work.
networking.nftables.enable = lib.mkIf ( 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); ) (lib.mkDefault true);
users.groups = lib.mkIf (cfg.enable && polkit.userControlEnabled) { users.groups = lib.mkIf (cfg.enable && polkit.userControlEnabled) {
@@ -104,26 +107,27 @@ in
''); '');
systemd.user.services = systemd.user.services =
lib.optionalAttrs appRoutingTun.enable { lib.optionalAttrs perAppRoutingTun.enable {
proxy-suite-app-tun-anchor = proxy-suite-per-app-tun-anchor =
appRouting.mkAnchorService appRouting.appTunSliceName "Anchor service for proxy-suite app TUN slice"; perAppRouting.mkAnchorService perAppRouting.perAppTunSliceName "Anchor service for proxy-suite app TUN slice";
} }
// lib.optionalAttrs appRoutingTproxy.enable { // lib.optionalAttrs perAppRoutingTproxy.enable {
proxy-suite-app-tproxy-anchor = proxy-suite-per-app-tproxy-anchor =
appRouting.mkAnchorService appRouting.appTproxySliceName "Anchor service for proxy-suite app TProxy slice"; perAppRouting.mkAnchorService perAppRouting.perAppTproxySliceName "Anchor service for proxy-suite app TProxy slice";
} }
// lib.optionalAttrs (appRoutingZapret.enable && cfg.zapret.enable) { // lib.optionalAttrs (perAppZapretCfg.enable && cfg.zapret.enable) {
proxy-suite-app-zapret-anchor = proxy-suite-per-app-zapret-anchor =
appRouting.mkAnchorService appRouting.appZapretSliceName "Anchor service for proxy-suite app zapret slice"; perAppRouting.mkAnchorService perAppRouting.perAppZapretSliceName "Anchor service for proxy-suite app zapret slice";
}; };
assertions = import ../service-assertions.nix { assertions = import ../service-assertions.nix {
inherit lib cfg; inherit lib cfg;
inherit singBoxCfg appRoutingCfg appRoutingTun appRoutingTproxy appRoutingZapret; inherit singBoxCfg perAppRoutingCfg perAppRoutingTun perAppRoutingTproxy;
perAppZapretCfg = perAppZapretCfg;
tgWsProxyCfg = cfg.tgWsProxy; tgWsProxyCfg = cfg.tgWsProxy;
inherit builtinTags outboundTags invalidRoutingTargets; inherit builtinTags outboundTags invalidRoutingTargets;
inherit (appRouting) inherit (perAppRouting)
effectiveAppRoutingProfileNames effectivePerAppRoutingProfileNames
hasProxychainsProfiles hasProxychainsProfiles
hasTunProfiles hasTunProfiles
hasTproxyProfiles hasTproxyProfiles
@@ -132,6 +136,8 @@ in
}; };
systemd.services = { systemd.services = {
}
// lib.optionalAttrs singBoxCfg.enable {
proxy-suite-socks = { proxy-suite-socks = {
description = "sing-box proxy client (SOCKS + TProxy-ready)"; description = "sing-box proxy client (SOCKS + TProxy-ready)";
after = [ "network.target" ]; after = [ "network.target" ];
@@ -145,18 +151,18 @@ in
}; };
}; };
} }
// lib.optionalAttrs singBoxCfg.tproxy.enable { // lib.optionalAttrs (singBoxCfg.enable && globalTproxy.enable) {
proxy-suite-tproxy = { proxy-suite-tproxy = {
description = "sing-box TProxy – nftables rules and policy routing"; description = "sing-box TProxy – nftables rules and policy routing";
after = [ after = [
"network.target" "network.target"
"proxy-suite-socks.service" "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" ]; requires = [ "proxy-suite-socks.service" ];
conflicts = [ conflicts = [
"proxy-suite-tun.service" "proxy-suite-tun.service"
"proxy-suite-app-tproxy.service" "proxy-suite-per-app-tproxy.service"
]; ];
serviceConfig = { serviceConfig = {
Type = "oneshot"; Type = "oneshot";
@@ -164,22 +170,22 @@ in
ExecStart = pkgs.writeShellScript "proxy-suite-tproxy-up" '' ExecStart = pkgs.writeShellScript "proxy-suite-tproxy-up" ''
${nft} delete table ip singbox 2>/dev/null || true ${nft} delete table ip singbox 2>/dev/null || true
${nft} -f ${nftablesRulesFile} ${nft} -f ${nftablesRulesFile}
${ip} route add local default dev lo table ${toString singBoxCfg.routeTable} ${ip} route add local default dev lo table ${toString globalTproxy.routeTable}
${ip} rule add fwmark ${toString singBoxCfg.fwmark} table ${toString singBoxCfg.routeTable} ${ip} rule add fwmark ${toString globalTproxy.fwmark} table ${toString globalTproxy.routeTable}
''; '';
ExecStop = pkgs.writeShellScript "proxy-suite-tproxy-down" '' ExecStop = pkgs.writeShellScript "proxy-suite-tproxy-down" ''
${nft} delete table ip singbox 2>/dev/null || true ${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} route del local default dev lo table ${toString globalTproxy.routeTable} 2>/dev/null || true
${ip} rule del fwmark ${toString singBoxCfg.fwmark} table ${toString singBoxCfg.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 = { proxy-suite-tun = {
description = "sing-box TUN proxy client"; description = "sing-box TUN proxy client";
after = [ "network-online.target" ]; 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" ]; wants = [ "network-online.target" ];
conflicts = [ "proxy-suite-tproxy.service" ]; conflicts = [ "proxy-suite-tproxy.service" ];
serviceConfig = { serviceConfig = {
@@ -191,37 +197,37 @@ in
}; };
}; };
} }
// lib.optionalAttrs appRoutingTun.enable { // lib.optionalAttrs (singBoxCfg.enable && perAppRoutingTun.enable) {
proxy-suite-app-tun = { proxy-suite-per-app-tun = {
description = "sing-box app-routing TUN backend"; description = "sing-box per-app-routing TUN backend";
after = [ "network-online.target" ]; after = [ "network-online.target" ];
wants = [ "network-online.target" ]; wants = [ "network-online.target" ];
serviceConfig = { serviceConfig = {
ExecStart = "${scripts.startAppTun}"; ExecStart = "${scripts.startPerAppTun}";
ExecStartPost = "${appRouting.appTunUpScript}"; ExecStartPost = "${perAppRouting.perAppTunUpScript}";
ExecStopPost = "${appRouting.appTunDownScript}"; ExecStopPost = "${perAppRouting.perAppTunDownScript}";
Restart = "on-failure"; Restart = "on-failure";
RestartSec = 5; RestartSec = 5;
RuntimeDirectory = "proxy-suite-app-tun"; RuntimeDirectory = "proxy-suite-per-app-tun";
StateDirectory = "proxy-suite"; StateDirectory = "proxy-suite";
}; };
}; };
"proxy-suite-app-tun-user@" = { "proxy-suite-per-app-tun-user@" = {
description = "Enable proxy-suite app TUN marking for user %i"; description = "Enable proxy-suite app TUN marking for user %i";
requires = [ "proxy-suite-app-tun.service" ]; requires = [ "proxy-suite-per-app-tun.service" ];
after = [ "proxy-suite-app-tun.service" ]; after = [ "proxy-suite-per-app-tun.service" ];
serviceConfig = { serviceConfig = {
Type = "oneshot"; Type = "oneshot";
RemainAfterExit = true; RemainAfterExit = true;
ExecStart = "${appRouting.appTunUserRuleStart} %i"; ExecStart = "${perAppRouting.perAppTunUserRuleStart} %i";
ExecStop = "${appRouting.appTunUserRuleStop} %i"; ExecStop = "${perAppRouting.perAppTunUserRuleStop} %i";
}; };
}; };
} }
// lib.optionalAttrs appRoutingTproxy.enable { // lib.optionalAttrs (singBoxCfg.enable && perAppRoutingTproxy.enable) {
proxy-suite-app-tproxy = { proxy-suite-per-app-tproxy = {
description = "proxy-suite app-routing TProxy backend"; description = "proxy-suite per-app-routing TProxy backend";
after = [ after = [
"network.target" "network.target"
"proxy-suite-socks.service" "proxy-suite-socks.service"
@@ -234,37 +240,37 @@ in
serviceConfig = { serviceConfig = {
Type = "oneshot"; Type = "oneshot";
RemainAfterExit = true; RemainAfterExit = true;
ExecStart = "${appRouting.appTproxyUpScript}"; ExecStart = "${perAppRouting.perAppTproxyUpScript}";
ExecStop = "${appRouting.appTproxyDownScript}"; ExecStop = "${perAppRouting.perAppTproxyDownScript}";
}; };
}; };
"proxy-suite-app-tproxy-user@" = { "proxy-suite-per-app-tproxy-user@" = {
description = "Enable proxy-suite app TProxy marking for user %i"; description = "Enable proxy-suite app TProxy marking for user %i";
requires = [ "proxy-suite-app-tproxy.service" ]; requires = [ "proxy-suite-per-app-tproxy.service" ];
after = [ "proxy-suite-app-tproxy.service" ]; after = [ "proxy-suite-per-app-tproxy.service" ];
serviceConfig = { serviceConfig = {
Type = "oneshot"; Type = "oneshot";
RemainAfterExit = true; RemainAfterExit = true;
ExecStart = "${appRouting.appTproxyUserRuleStart} %i"; ExecStart = "${perAppRouting.perAppTproxyUserRuleStart} %i";
ExecStop = "${appRouting.appTproxyUserRuleStop} %i"; ExecStop = "${perAppRouting.perAppTproxyUserRuleStop} %i";
}; };
}; };
} }
// lib.optionalAttrs (appRoutingZapret.enable && cfg.zapret.enable) { // lib.optionalAttrs (perAppZapretCfg.enable && cfg.zapret.enable) {
"proxy-suite-app-zapret-user@" = { "proxy-suite-per-app-zapret-user@" = {
description = "Enable proxy-suite app zapret marking for user %i"; description = "Enable proxy-suite app zapret marking for user %i";
requires = [ "proxy-suite-app-zapret.service" ]; requires = [ "proxy-suite-per-app-zapret.service" ];
after = [ "proxy-suite-app-zapret.service" ]; after = [ "proxy-suite-per-app-zapret.service" ];
serviceConfig = { serviceConfig = {
Type = "oneshot"; Type = "oneshot";
RemainAfterExit = true; RemainAfterExit = true;
ExecStart = "${appRouting.appZapretUserRuleStart} %i"; ExecStart = "${perAppRouting.perAppZapretUserRuleStart} %i";
ExecStop = "${appRouting.appZapretUserRuleStop} %i"; ExecStop = "${perAppRouting.perAppZapretUserRuleStop} %i";
}; };
}; };
} }
// lib.optionalAttrs scripts.hasSubscriptions { // lib.optionalAttrs (singBoxCfg.enable && scripts.hasSubscriptions) {
proxy-suite-subscription-update = { proxy-suite-subscription-update = {
description = "Refresh proxy-suite subscription caches"; description = "Refresh proxy-suite subscription caches";
after = [ "network-online.target" ]; 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 = { proxy-suite-subscription-update = {
description = "Periodic proxy-suite subscription refresh"; description = "Periodic proxy-suite subscription refresh";
wantedBy = [ "timers.target" ]; wantedBy = [ "timers.target" ];
@@ -4,13 +4,13 @@
pkgs, pkgs,
cfg, cfg,
singBoxCfg, singBoxCfg,
appRoutingCfg, perAppRoutingCfg,
appRoutingTun, perAppRoutingTun,
appRoutingTproxy, perAppRoutingTproxy,
appRoutingZapret, perAppZapretCfg,
appTunChainFile, perAppTunChainFile,
appTproxyRulesFile, perAppTproxyRulesFile,
appZapretRulesFile, perAppZapretRulesFile,
ip, ip,
nft, nft,
awk, awk,
@@ -31,103 +31,104 @@
}: }:
let let
appTunSliceName = "proxy-suite-app-tun.slice"; perAppTunSliceName = "proxy-suite-per-app-tun.slice";
appTproxySliceName = "proxy-suite-app-tproxy.slice"; perAppTproxySliceName = "proxy-suite-per-app-tproxy.slice";
appZapretSliceName = "proxy-suite-app-zapret.slice"; perAppZapretSliceName = "proxy-suite-per-app-zapret.slice";
appRoutingProfileNames = map (profile: profile.name) appRoutingCfg.profiles; perAppRoutingProfileNames = map (profile: profile.name) perAppRoutingCfg.profiles;
defaultAppRoutingProfiles = defaultPerAppRoutingProfiles = lib.optionals perAppRoutingCfg.createDefaultProfiles (
lib.optionals appRoutingCfg.createDefaultProfiles [ [
{ {
name = "proxychains"; name = "proxychains";
route = "proxychains"; route = "proxychains";
} }
] ]
++ lib.optionals appRoutingTun.enable [ ++ lib.optionals perAppRoutingTun.enable [
{ {
name = "tun"; name = "tun";
route = "tun"; route = "tun";
} }
] ]
++ lib.optionals appRoutingTproxy.enable [ ++ lib.optionals perAppRoutingTproxy.enable [
{ {
name = "tproxy"; name = "tproxy";
route = "tproxy"; route = "tproxy";
} }
] ]
++ lib.optionals (appRoutingZapret.enable && cfg.zapret.enable) [ ++ lib.optionals (perAppZapretCfg.enable && cfg.zapret.enable) [
{ {
name = "zapret"; name = "zapret";
route = "zapret"; route = "zapret";
} }
]; ]
effectiveAppRoutingProfiles = );
appRoutingCfg.profiles effectivePerAppRoutingProfiles =
++ builtins.filter (profile: !(builtins.elem profile.name appRoutingProfileNames)) defaultAppRoutingProfiles; perAppRoutingCfg.profiles
effectiveAppRoutingProfileNames = map (profile: profile.name) effectiveAppRoutingProfiles; ++ builtins.filter (profile: !(builtins.elem profile.name perAppRoutingProfileNames)) defaultPerAppRoutingProfiles;
effectivePerAppRoutingProfileNames = map (profile: profile.name) effectivePerAppRoutingProfiles;
appRoutingProfilesFile = pkgs.writeText "proxy-suite-app-routing-profiles.json" ( perAppRoutingProfilesFile = pkgs.writeText "proxy-suite-per-app-routing-profiles.json" (
builtins.toJSON effectiveAppRoutingProfiles builtins.toJSON effectivePerAppRoutingProfiles
); );
proxychainsConfigFile = pkgs.writeText "proxy-suite-proxychains.conf" '' proxychainsConfigFile = pkgs.writeText "proxy-suite-proxychains.conf" ''
strict_chain strict_chain
${lib.optionalString appRoutingCfg.proxychains.quiet "quiet_mode"} ${lib.optionalString perAppRoutingCfg.proxychains.quiet "quiet_mode"}
${lib.optionalString appRoutingCfg.proxychains.proxyDns "proxy_dns"} ${lib.optionalString perAppRoutingCfg.proxychains.proxyDns "proxy_dns"}
tcp_read_time_out 15000 tcp_read_time_out 15000
tcp_connect_time_out 8000 tcp_connect_time_out 8000
[ProxyList] [ProxyList]
socks5 ${singBoxCfg.listenAddress} ${toString singBoxCfg.port} 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; hasProxychainsProfiles = builtins.any (profile: profile.route == "proxychains") effectivePerAppRoutingProfiles;
hasTunProfiles = builtins.any (profile: profile.route == "tun") effectiveAppRoutingProfiles; hasTunProfiles = builtins.any (profile: profile.route == "tun") effectivePerAppRoutingProfiles;
hasTproxyProfiles = builtins.any (profile: profile.route == "tproxy") effectiveAppRoutingProfiles; hasTproxyProfiles = builtins.any (profile: profile.route == "tproxy") effectivePerAppRoutingProfiles;
hasZapretProfiles = builtins.any (profile: profile.route == "zapret") effectiveAppRoutingProfiles; 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 set -euo pipefail
for _ in $(${seqBin} 1 50); do 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 exit 0
fi fi
${sleepBin} 0.1 ${sleepBin} 0.1
done 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 exit 1
''; '';
appTunUpScript = pkgs.writeShellScript "proxy-suite-app-tun-up" '' perAppTunUpScript = pkgs.writeShellScript "proxy-suite-per-app-tun-up" ''
set -euo pipefail set -euo pipefail
${nft} delete table inet proxy_suite_app_tun 2>/dev/null || true ${nft} delete table inet proxy_suite_per_app_tun 2>/dev/null || true
${nft} -f ${appTunChainFile} ${nft} -f ${perAppTunChainFile}
${appTunWaitForInterface} ${perAppTunWaitForInterface}
${ip} route replace default dev ${lib.escapeShellArg appRoutingTun.interface} table ${toString appRoutingTun.routeTable} ${ip} route replace default dev ${lib.escapeShellArg perAppRoutingTun.interface} table ${toString perAppRoutingTun.routeTable}
${ip} rule add fwmark ${toString appRoutingTun.fwmark} table ${toString appRoutingTun.routeTable} 2>/dev/null || true ${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 set -euo pipefail
${nft} delete table inet proxy_suite_app_tun 2>/dev/null || true ${nft} delete table inet proxy_suite_per_app_tun 2>/dev/null || true
${ip} route del default dev ${lib.escapeShellArg appRoutingTun.interface} table ${toString appRoutingTun.routeTable} 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 appRoutingTun.fwmark} table ${toString appRoutingTun.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 set -euo pipefail
${nft} delete table ip proxy_suite_app_tproxy 2>/dev/null || true ${nft} delete table ip proxy_suite_per_app_tproxy 2>/dev/null || true
${nft} -f ${appTproxyRulesFile} ${nft} -f ${perAppTproxyRulesFile}
${ip} route replace local default dev lo table ${toString appRoutingTproxy.routeTable} ${ip} route replace local default dev lo table ${toString perAppRoutingTproxy.routeTable}
${ip} rule add fwmark ${toString appRoutingTproxy.fwmark} table ${toString appRoutingTproxy.routeTable} 2>/dev/null || true ${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 set -euo pipefail
${nft} delete table ip proxy_suite_app_tproxy 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 appRoutingTproxy.routeTable} 2>/dev/null || true ${ip} route del local default dev lo table ${toString perAppRoutingTproxy.routeTable} 2>/dev/null || true
${ip} rule del fwmark ${toString appRoutingTproxy.fwmark} table ${toString appRoutingTproxy.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. # Generate a "start" script that adds a per-user cgroup nftables mark rule.
@@ -198,51 +199,51 @@ let
fi fi
''; '';
appTunUserRuleStart = mkUserRuleStart { perAppTunUserRuleStart = mkUserRuleStart {
name = "app-tun"; name = "per-app-tun";
nftFamily = "inet"; nftFamily = "inet";
nftTable = "proxy_suite_app_tun"; nftTable = "proxy_suite_per_app_tun";
nftChain = "output"; nftChain = "output";
sliceName = appTunSliceName; sliceName = perAppTunSliceName;
sliceLabel = "app TUN"; 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 { perAppTunUserRuleStop = mkUserRuleStop {
name = "app-tun"; name = "per-app-tun";
nftFamily = "inet"; nftFamily = "inet";
nftTable = "proxy_suite_app_tun"; nftTable = "proxy_suite_per_app_tun";
nftChain = "output"; nftChain = "output";
}; };
appTproxyUserRuleStart = mkUserRuleStart { perAppTproxyUserRuleStart = mkUserRuleStart {
name = "app-tproxy"; name = "per-app-tproxy";
nftFamily = "ip"; nftFamily = "ip";
nftTable = "proxy_suite_app_tproxy"; nftTable = "proxy_suite_per_app_tproxy";
nftChain = "output"; nftChain = "output";
sliceName = appTproxySliceName; sliceName = perAppTproxySliceName;
sliceLabel = "app TProxy"; 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 { perAppTproxyUserRuleStop = mkUserRuleStop {
name = "app-tproxy"; name = "per-app-tproxy";
nftFamily = "ip"; nftFamily = "ip";
nftTable = "proxy_suite_app_tproxy"; nftTable = "proxy_suite_per_app_tproxy";
nftChain = "output"; nftChain = "output";
}; };
appZapretUserRuleStart = mkUserRuleStart { perAppZapretUserRuleStart = mkUserRuleStart {
name = "app-zapret"; name = "per-app-zapret";
nftFamily = "inet"; nftFamily = "inet";
nftTable = "proxy_suite_app_zapret_mark"; nftTable = "proxy_suite_per_app_zapret_mark";
nftChain = "output"; nftChain = "output";
sliceName = appZapretSliceName; sliceName = perAppZapretSliceName;
sliceLabel = "app zapret"; 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 { perAppZapretUserRuleStop = mkUserRuleStop {
name = "app-zapret"; name = "per-app-zapret";
nftFamily = "inet"; nftFamily = "inet";
nftTable = "proxy_suite_app_zapret_mark"; nftTable = "proxy_suite_per_app_zapret_mark";
nftChain = "output"; nftChain = "output";
}; };
@@ -273,16 +274,16 @@ let
CLASH_API="${clashApi}" CLASH_API="${clashApi}"
SELECTION="${selection}" SELECTION="${selection}"
SUB_TAGS=(${subscriptionTagsList}) SUB_TAGS=(${subscriptionTagsList})
APP_ROUTING_ENABLED="${if appRoutingCfg.enable then "1" else "0"}" PER_APP_ROUTING_ENABLED="${if perAppRoutingCfg.enable then "1" else "0"}"
APP_ROUTING_PROXYCHAINS_ENABLED="${if appRoutingCfg.proxychains.enable then "1" else "0"}" PER_APP_ROUTING_PROXYCHAINS_ENABLED="${if perAppRoutingCfg.proxychains.enable then "1" else "0"}"
APP_ROUTING_TUN_ENABLED="${if appRoutingTun.enable then "1" else "0"}" PER_APP_ROUTING_TUN_ENABLED="${if perAppRoutingTun.enable then "1" else "0"}"
APP_ROUTING_TPROXY_ENABLED="${if appRoutingTproxy.enable then "1" else "0"}" PER_APP_ROUTING_TPROXY_ENABLED="${if perAppRoutingTproxy.enable then "1" else "0"}"
APP_ROUTING_ZAPRET_ENABLED="${if (appRoutingZapret.enable && cfg.zapret.enable) then "1" else "0"}" PER_APP_ROUTING_ZAPRET_ENABLED="${if (perAppZapretCfg.enable && cfg.zapret.enable) then "1" else "0"}"
APP_ROUTING_PROFILES_FILE="${appRoutingProfilesFile}" PER_APP_ROUTING_PROFILES_FILE="${perAppRoutingProfilesFile}"
PROXYCHAINS_CONFIG="${proxychainsConfigFile}" PROXYCHAINS_CONFIG="${proxychainsConfigFile}"
APP_TUN_SLICE_BASE="proxy-suite-app-tun" PER_APP_TUN_SLICE_BASE="proxy-suite-per-app-tun"
APP_TPROXY_SLICE_BASE="proxy-suite-app-tproxy" PER_APP_TPROXY_SLICE_BASE="proxy-suite-per-app-tproxy"
APP_ZAPRET_SLICE_BASE="proxy-suite-app-zapret" PER_APP_ZAPRET_SLICE_BASE="proxy-suite-per-app-zapret"
ALL_SERVICES=( ALL_SERVICES=(
proxy-suite-socks proxy-suite-socks
@@ -307,7 +308,7 @@ let
echo " outbounds list outbounds and current selection" echo " outbounds list outbounds and current selection"
echo " select <tag> switch to a specific outbound (selector mode)" echo " select <tag> switch to a specific outbound (selector mode)"
echo " apps list configured per-app routing profiles" 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 list show subscriptions, cache age, and proxy count"
echo " subscription update force-refresh all subscription caches and restart" echo " subscription update force-refresh all subscription caches and restart"
exit 1 exit 1
@@ -352,15 +353,15 @@ let
} }
_ensure_app_routing() { _ensure_app_routing() {
if [ "$APP_ROUTING_ENABLED" != "1" ]; then if [ "$PER_APP_ROUTING_ENABLED" != "1" ]; then
echo "appRouting is not enabled in services.proxy-suite.appRouting." echo "perAppRouting is not enabled in services.proxy-suite.perAppRouting."
exit 1 exit 1
fi fi
} }
_profile_route() { _profile_route() {
local profile="$1" 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() { _scope_running() {
@@ -523,12 +524,12 @@ let
apps) apps)
_ensure_app_routing _ensure_app_routing
if [ "$(${jq} 'length' "$APP_ROUTING_PROFILES_FILE")" -eq 0 ]; then if [ "$(${jq} 'length' "$PER_APP_ROUTING_PROFILES_FILE")" -eq 0 ]; then
echo "No appRouting profiles configured." echo "No perAppRouting profiles configured."
else else
printf " %-24s %s\n" "PROFILE" "ROUTE" printf " %-24s %s\n" "PROFILE" "ROUTE"
${jq} -r '.[] | " " + (.name | tostring) + "\t" + (.route | tostring)' \ ${jq} -r '.[] | " " + (.name | tostring) + "\t" + (.route | tostring)' \
"$APP_ROUTING_PROFILES_FILE" \ "$PER_APP_ROUTING_PROFILES_FILE" \
| while IFS=$'\t' read -r profile route; do | while IFS=$'\t' read -r profile route; do
printf "%-26s %s\n" "$profile" "$route" printf "%-26s %s\n" "$profile" "$route"
done done
@@ -549,7 +550,7 @@ let
_ensure_app_routing _ensure_app_routing
route="$(_profile_route "$profile")" route="$(_profile_route "$profile")"
if [ -z "$route" ] || [ "$route" = "null" ]; then if [ -z "$route" ] || [ "$route" = "null" ]; then
echo "Unknown appRouting profile: $profile" echo "Unknown perAppRouting profile: $profile"
exit 1 exit 1
fi fi
@@ -558,28 +559,28 @@ let
exec "$@" exec "$@"
;; ;;
proxychains) proxychains)
if [ "$APP_ROUTING_PROXYCHAINS_ENABLED" != "1" ]; then if [ "$PER_APP_ROUTING_PROXYCHAINS_ENABLED" != "1" ]; then
echo "Profile '$profile' uses route=proxychains, but appRouting.proxychains.enable is false." echo "Profile '$profile' uses route=proxychains, but perAppRouting.proxychains.enable is false."
exit 1 exit 1
fi fi
exec ${proxychains4} ${proxychainsQuietArg}-f "$PROXYCHAINS_CONFIG" "$@" exec ${proxychains4} ${proxychainsQuietArg}-f "$PROXYCHAINS_CONFIG" "$@"
;; ;;
tun) tun)
_wrap_slice "$APP_TUN_SLICE_BASE" "$APP_ROUTING_TUN_ENABLED" \ _wrap_slice "$PER_APP_TUN_SLICE_BASE" "$PER_APP_ROUTING_TUN_ENABLED" \
"Profile '$profile' uses route=tun, but appRouting.backends.tun.enable is false." \ "Profile '$profile' uses route=tun, but singBox.tun.perApp.enable is false." \
"proxy-suite-app-tun.service" "$@" "proxy-suite-per-app-tun.service" "$@"
;; ;;
tproxy) tproxy)
_check_no_global_proxy tproxy _check_no_global_proxy tproxy
_wrap_slice "$APP_TPROXY_SLICE_BASE" "$APP_ROUTING_TPROXY_ENABLED" \ _wrap_slice "$PER_APP_TPROXY_SLICE_BASE" "$PER_APP_ROUTING_TPROXY_ENABLED" \
"Profile '$profile' uses route=tproxy, but appRouting.backends.tproxy.enable is false." \ "Profile '$profile' uses route=tproxy, but singBox.tproxy.perApp.enable is false." \
"proxy-suite-app-tproxy.service" "$@" "proxy-suite-per-app-tproxy.service" "$@"
;; ;;
zapret) zapret)
_check_no_global_proxy zapret _check_no_global_proxy zapret
_wrap_slice "$APP_ZAPRET_SLICE_BASE" "$APP_ROUTING_ZAPRET_ENABLED" \ _wrap_slice "$PER_APP_ZAPRET_SLICE_BASE" "$PER_APP_ROUTING_ZAPRET_ENABLED" \
"Profile '$profile' uses route=zapret, but the app-zapret backend or zapret service is not enabled." \ "Profile '$profile' uses route=zapret, but zapret.perApp.enable or zapret.enable is false." \
"proxy-suite-app-zapret.service" "$@" "proxy-suite-per-app-zapret.service" "$@"
;; ;;
*) *)
echo "Route backend '$route' is not implemented in this build." echo "Route backend '$route' is not implemented in this build."
@@ -629,23 +630,23 @@ let
in in
{ {
inherit inherit
appTunSliceName perAppTunSliceName
appTproxySliceName perAppTproxySliceName
appZapretSliceName perAppZapretSliceName
appTunUpScript perAppTunUpScript
appTunDownScript perAppTunDownScript
appTproxyUpScript perAppTproxyUpScript
appTproxyDownScript perAppTproxyDownScript
appTunUserRuleStart perAppTunUserRuleStart
appTunUserRuleStop perAppTunUserRuleStop
appTproxyUserRuleStart perAppTproxyUserRuleStart
appTproxyUserRuleStop perAppTproxyUserRuleStop
appZapretUserRuleStart perAppZapretUserRuleStart
appZapretUserRuleStop perAppZapretUserRuleStop
mkAnchorService mkAnchorService
effectiveAppRoutingProfiles effectivePerAppRoutingProfiles
effectiveAppRoutingProfileNames effectivePerAppRoutingProfileNames
appRoutingProfilesFile perAppRoutingProfilesFile
proxychainsConfigFile proxychainsConfigFile
proxychainsQuietArg proxychainsQuietArg
hasProxychainsProfiles hasProxychainsProfiles
+2 -2
View File
@@ -6,13 +6,13 @@ let
userControlPolkitRules = userControlPolkitRules =
lib.optionalString userControlCfg.perApp.enable '' lib.optionalString userControlCfg.perApp.enable ''
if (unit.indexOf("proxy-suite-app-") === 0) { if (unit.indexOf("proxy-suite-per-app-") === 0) {
return polkit.Result.YES; return polkit.Result.YES;
} }
'' ''
+ lib.optionalString userControlCfg.global.enable '' + lib.optionalString userControlCfg.global.enable ''
if ((unit.indexOf("proxy-suite-") === 0 && if ((unit.indexOf("proxy-suite-") === 0 &&
unit.indexOf("proxy-suite-app-") !== 0) || unit.indexOf("proxy-suite-per-app-") !== 0) ||
unit === "zapret-discord-youtube.service") { unit === "zapret-discord-youtube.service") {
return polkit.Result.YES; return polkit.Result.YES;
} }
+11 -9
View File
@@ -3,7 +3,7 @@
lib, lib,
pkgs, pkgs,
singBoxCfg, singBoxCfg,
appRoutingTun, perAppRoutingTun,
jq, jq,
python3, python3,
singBox, singBox,
@@ -12,10 +12,12 @@
fetchSubscriptionPy, fetchSubscriptionPy,
tproxyFile, tproxyFile,
tunFile, tunFile,
appTunFile, perAppTunFile,
}: }:
let let
globalTproxy = singBoxCfg.tproxy;
mkSubscriptionUrlSource = mkSubscriptionUrlSource =
sub: sub:
if sub.urlFile != null then if sub.urlFile != null then
@@ -171,7 +173,7 @@ let
name = "proxy-suite-start-socks"; name = "proxy-suite-start-socks";
runtimeDir = "/run/proxy-suite-socks"; runtimeDir = "/run/proxy-suite-socks";
configFile = tproxyFile; configFile = tproxyFile;
routingMark = singBoxCfg.proxyMark; routingMark = globalTproxy.proxyMark;
}; };
startTun = mkStartScript { startTun = mkStartScript {
@@ -181,13 +183,13 @@ let
routingMark = null; routingMark = null;
}; };
startAppTun = mkStartScript { startPerAppTun = mkStartScript {
name = "proxy-suite-start-app-tun"; name = "proxy-suite-start-per-app-tun";
runtimeDir = "/run/proxy-suite-app-tun"; runtimeDir = "/run/proxy-suite-per-app-tun";
configFile = appTunFile; configFile = perAppTunFile;
# Only needed when TProxy mode is enabled; otherwise avoid tagging app TUN # Only needed when TProxy mode is enabled; otherwise avoid tagging app TUN
# proxy outbounds with a host-global mark. # 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). # 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); subscriptionTagsList = lib.concatStringsSep " " (map (sub: lib.escapeShellArg sub.tag) singBoxCfg.subscriptions);
in in
{ {
inherit startSocks startTun startAppTun; inherit startSocks startTun startPerAppTun;
inherit subscriptionUpdateScript hasSubscriptions subscriptionTagsList; inherit subscriptionUpdateScript hasSubscriptions subscriptionTagsList;
} }
+29 -29
View File
@@ -4,13 +4,13 @@
pkgs, pkgs,
cfg, cfg,
zapret, zapret,
appZapretRulesFile, perAppZapretRulesFile,
nft, nft,
}: }:
let let
zapretCfg = cfg.zapret; zapretCfg = cfg.zapret;
appRoutingZapret = cfg.appRouting.backends.zapret; perAppZapretCfg = zapretCfg.perApp;
iptables = "${pkgs.iptables}/bin/iptables"; iptables = "${pkgs.iptables}/bin/iptables";
@@ -76,10 +76,10 @@ let
filterMark: filterMark:
pkgs.writeText "proxy-suite-zapret-global-bypass.sh" '' pkgs.writeText "proxy-suite-zapret-global-bypass.sh" ''
zapret_custom_firewall_nft() { 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 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 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 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 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"; pidDir = "/run/proxy-suite-zapret";
gameFilter = zapretCfg.gameFilter; gameFilter = zapretCfg.gameFilter;
forceDisableFilterMark = true; 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 { perAppZapretPackage = mkDerivedZapretPackage {
packageName = "proxy-suite-app-zapret-${selectedConfigName}"; packageName = "proxy-suite-per-app-zapret-${selectedConfigName}";
pidDir = "/run/proxy-suite-app-zapret"; pidDir = "/run/proxy-suite-per-app-zapret";
gameFilter = "all"; gameFilter = "all";
filterMark = appRoutingZapret.filterMark; filterMark = perAppZapretCfg.filterMark;
qnum = appRoutingZapret.qnum; qnum = perAppZapretCfg.qnum;
modeFilter = "none"; modeFilter = "none";
desyncMark = 134217728; desyncMark = 134217728;
desyncMarkPostnat = 67108864; desyncMarkPostnat = 67108864;
nftTable = "proxy_suite_app_zapret"; nftTable = "proxy_suite_per_app_zapret";
}; };
zapretCommonPreStart = package: '' zapretCommonPreStart = package: ''
@@ -253,17 +253,17 @@ let
"PATH=${lib.makeBinPath runtimeDeps}" "PATH=${lib.makeBinPath runtimeDeps}"
]; ];
globalZapretEnv = mkZapretEnv globalZapretPackage; 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 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
${nft} -f ${appZapretRulesFile} ${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 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: '' exemptStart = lib.concatMapStrings (cidr: ''
@@ -294,7 +294,7 @@ in
environment.systemPackages = environment.systemPackages =
lib.mkBefore ( lib.mkBefore (
[ globalZapretPackage ] ++ lib.optionals appRoutingZapret.enable [ appZapretPackage ] [ globalZapretPackage ] ++ lib.optionals perAppZapretCfg.enable [ perAppZapretPackage ]
); );
systemd.services.zapret-discord-youtube = { systemd.services.zapret-discord-youtube = {
@@ -313,23 +313,23 @@ in
}; };
}; };
systemd.services.proxy-suite-app-zapret = lib.mkIf appRoutingZapret.enable { systemd.services.proxy-suite-per-app-zapret = lib.mkIf perAppZapretCfg.enable {
description = "proxy-suite app-routing zapret backend"; description = "proxy-suite per-app-routing zapret backend";
after = [ "network.target" ]; after = [ "network.target" ];
conflicts = [ conflicts = [
"proxy-suite-tproxy.service" "proxy-suite-tproxy.service"
"proxy-suite-tun.service" "proxy-suite-tun.service"
]; ];
preStart = zapretCommonPreStart appZapretPackage; preStart = zapretCommonPreStart perAppZapretPackage;
serviceConfig = { serviceConfig = {
Type = "oneshot"; Type = "oneshot";
RemainAfterExit = true; RemainAfterExit = true;
RuntimeDirectory = "proxy-suite-app-zapret"; RuntimeDirectory = "proxy-suite-per-app-zapret";
ExecStartPre = "${appZapretMarkUpScript}"; ExecStartPre = "${perAppZapretMarkUpScript}";
ExecStart = "${appZapretPackage}/opt/zapret/init.d/sysv/zapret start"; ExecStart = "${perAppZapretPackage}/opt/zapret/init.d/sysv/zapret start";
ExecStop = "${appZapretPackage}/opt/zapret/init.d/sysv/zapret stop"; ExecStop = "${perAppZapretPackage}/opt/zapret/init.d/sysv/zapret stop";
ExecStopPost = "${appZapretMarkDownScript}"; ExecStopPost = "${perAppZapretMarkDownScript}";
Environment = appZapretEnv; Environment = perAppZapretEnv;
}; };
}; };
+500 -319
View File
File diff suppressed because it is too large. Load diff