formatting

This commit is contained in:
FUFSoB committed 2026-04-19 12:11:01 +05:00
1 parent 744a769e73
commit 4d6802e194
21 files changed
+843 -582

No files matched your search

+12 -3
View File
@@ -9,7 +9,12 @@
let
derived = import ./derived.nix { inherit lib cfg; };
inherit (derived) singBoxCfg globalTun globalTproxy clashApiEnabled;
inherit (derived)
singBoxCfg
globalTun
globalTproxy
clashApiEnabled
;
perAppTun = derived.perAppRoutingTun;
direct = rules.direct;
@@ -24,7 +29,9 @@ let
// lib.optionalAttrs (detour != null) { inherit detour; };
mkDnsConfig =
{ localDetour ? null }:
{
localDetour ? null,
}:
{
servers = [
(mkDnsServer "remote" singBoxCfg.dns.remote "proxy")
@@ -160,7 +167,9 @@ let
tproxyFile = pkgs.writeText "proxy-suite-tproxy-template.json" (builtins.toJSON tproxyTemplate);
tunFile = pkgs.writeText "proxy-suite-tun-template.json" (builtins.toJSON tunTemplate);
perAppTunFile = pkgs.writeText "proxy-suite-per-app-tun-template.json" (builtins.toJSON perAppTunTemplate);
perAppTunFile = pkgs.writeText "proxy-suite-per-app-tun-template.json" (
builtins.toJSON perAppTunTemplate
);
in
{
+14 -2
View File
@@ -48,7 +48,14 @@ in
cfg
;
inherit (configs) tproxyFile tunFile perAppTunFile;
inherit (nftr) nftablesRulesFile perAppTproxyRulesFile perAppZapretRulesFile perAppTunChainFile ip nft;
inherit (nftr)
nftablesRulesFile
perAppTproxyRulesFile
perAppZapretRulesFile
perAppTunChainFile
ip
nft
;
})
(lib.mkIf cfg.zapret.enable (
@@ -65,7 +72,12 @@ in
(lib.mkIf cfg.tgWsProxy.enable (
import ./tg-ws-proxy.nix {
inherit lib pkgs packages cfg;
inherit
lib
pkgs
packages
cfg
;
}
))
+8 -2
View File
@@ -14,7 +14,11 @@ let
userControlCfg = cfg.userControl;
selectionMode = singBoxCfg.selection;
builtinTags = [ "proxy" "direct" "block" ];
builtinTags = [
"proxy"
"direct"
"block"
];
outboundTags = map (ob: ob.tag) singBoxCfg.outbounds;
subscriptionTags = map (sub: sub.tag) singBoxCfg.subscriptions;
@@ -27,7 +31,9 @@ let
invalidRoutingTargets = lib.unique (
map (rule: rule.outbound) (
builtins.filter (rule: !builtins.elem rule.outbound (builtinTags ++ outboundTags)) singBoxCfg.routing.rules
builtins.filter (
rule: !builtins.elem rule.outbound (builtinTags ++ outboundTags)
) singBoxCfg.routing.rules
)
);
in
+86 -77
View File
@@ -14,103 +14,105 @@ let
# Shared across all three nftables rule files that do IPv4 routing.
reservedIpBlock = ''
define RESERVED_IP = {
10.0.0.0/8,
100.64.0.0/10,
127.0.0.0/8,
169.254.0.0/16,
172.16.0.0/12,
192.0.0.0/24,
224.0.0.0/4,
240.0.0.0/4,
255.255.255.255/32
}
define RESERVED_IP = {
10.0.0.0/8,
100.64.0.0/10,
127.0.0.0/8,
169.254.0.0/16,
172.16.0.0/12,
192.0.0.0/24,
224.0.0.0/4,
240.0.0.0/4,
255.255.255.255/32
}
'';
mkLocalSubnetLines = subnets: lib.concatMapStrings (cidr: ''
ip daddr ${cidr} tcp dport != 53 return
ip daddr ${cidr} udp dport != 53 return
'') subnets;
mkLocalSubnetLines =
subnets:
lib.concatMapStrings (cidr: ''
ip daddr ${cidr} tcp dport != 53 return
ip daddr ${cidr} udp dport != 53 return
'') subnets;
tproxyLocalSubnetLines = mkLocalSubnetLines globalTproxy.localSubnets;
perAppTproxyLocalSubnetLines = mkLocalSubnetLines perAppTproxy.localSubnets;
nftablesRulesFile = pkgs.writeText "proxy-suite-tproxy.nft" ''
${reservedIpBlock}
table ip singbox {
chain prerouting {
type filter hook prerouting priority mangle; policy accept;
ip daddr $RESERVED_IP return
# Packets re-entering via loopback after output marking should not
# be skipped just because the host has an RFC1918 source address.
iifname != "lo" ip saddr $RESERVED_IP return
${tproxyLocalSubnetLines}
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}
${reservedIpBlock}
table ip singbox {
chain prerouting {
type filter hook prerouting priority mangle; policy accept;
ip daddr $RESERVED_IP return
# Packets re-entering via loopback after output marking should not
# be skipped just because the host has an RFC1918 source address.
iifname != "lo" ip saddr $RESERVED_IP return
${tproxyLocalSubnetLines}
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 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}
}
}
chain output {
type route hook output priority mangle; policy accept;
ip daddr $RESERVED_IP return
${tproxyLocalSubnetLines}
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}
}
}
'';
perAppTproxyRulesFile = pkgs.writeText "proxy-suite-per-app-tproxy.nft" ''
${reservedIpBlock}
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
${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}
}
${reservedIpBlock}
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
${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
${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
chain output {
type route hook output priority mangle; policy accept;
ip daddr $RESERVED_IP 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
}
}
}
'';
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 zapretApp.filterMark} == ${toString zapretApp.filterMark} meta mark set meta mark or ${toString zapretApp.filterMark}
}
table inet proxy_suite_per_app_zapret_mark {
chain prerouting {
type filter hook prerouting priority -103; policy accept;
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 zapretApp.filterMark} == ${toString zapretApp.filterMark} meta mark set meta mark or ${toString zapretApp.filterMark}
meta mark and ${toString zapretApp.filterMark} == ${toString zapretApp.filterMark} return
}
}
chain output {
type route hook output priority -103; policy accept;
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
}
}
'';
perAppTunChainFile = pkgs.writeText "proxy-suite-per-app-tun-chain.nft" ''
${reservedIpBlock}
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
'') perAppTun.localSubnets}
ct mark ${toString perAppTun.fwmark} meta mark set ${toString perAppTun.fwmark}
meta mark ${toString perAppTun.fwmark} return
${reservedIpBlock}
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
'') perAppTun.localSubnets}
ct mark ${toString perAppTun.fwmark} meta mark set ${toString perAppTun.fwmark}
meta mark ${toString perAppTun.fwmark} return
}
}
}
'';
ip = "${pkgs.iproute2}/bin/ip";
@@ -118,5 +120,12 @@ ${lib.concatMapStrings (cidr: ''
in
{
inherit nftablesRulesFile perAppTproxyRulesFile perAppZapretRulesFile perAppTunChainFile ip nft;
inherit
nftablesRulesFile
perAppTproxyRulesFile
perAppZapretRulesFile
perAppTunChainFile
ip
nft
;
}
+13 -6
View File
@@ -55,7 +55,9 @@ let
zapretUserIps = if syncZapretDirectUserIps then zapretCfg.ipsetAll else [ ];
zapretCustomDomains =
if syncZapretDirectDomains then
lib.unique (lib.concatMap (rule: lib.optionals rule.enableDirectSync rule.domains) zapretCfg.hostlistRules)
lib.unique (
lib.concatMap (rule: lib.optionals rule.enableDirectSync rule.domains) zapretCfg.hostlistRules
)
else
[ ];
zapretExcludedDomains =
@@ -90,17 +92,22 @@ let
geoips = lib.unique (r.direct.geoips ++ lib.optional r.enableRuDirect "ru");
};
mkRule = field: tag: items:
lib.optional (items != [ ]) { ${field} = items; outbound = tag; };
mkDomainRule = mkRule "domain_suffix";
mkIPRule = mkRule "ip_cidr";
mkRule =
field: tag: items:
lib.optional (items != [ ]) {
${field} = items;
outbound = tag;
};
mkDomainRule = mkRule "domain_suffix";
mkIPRule = mkRule "ip_cidr";
mkRulesetRule = mkRule "rule_set";
# In "first" mode the start script renames the first outbound to "proxy",
# so any per-outbound routing tag that isn't direct/block/proxy must map
# to "proxy" instead of the original tag (which won't exist in sing-box).
resolveTag =
tag: if derived.collapseNamedOutbounds && !builtins.elem tag derived.builtinTags then "proxy" else tag;
tag:
if derived.collapseNamedOutbounds && !builtins.elem tag derived.builtinTags then "proxy" else tag;
# Collect per-outbound routing attached directly to outbound definitions.
perOutboundRules = lib.concatMap (
+154 -165
View File
@@ -37,128 +37,109 @@ let
;
featureAssertions = [
(requireAvailable
singBoxCfg.enable
(singBoxCfg.outbounds != [ ] || singBoxCfg.subscriptions != [ ])
"proxy-suite: at least one outbound or subscription is required when singBox.enable = true")
(uniqueValues
singBoxCfg.enable
outboundTags
"proxy-suite: outbound tags must be unique")
(uniqueValues
singBoxCfg.enable
subscriptionTags
"proxy-suite: subscription tags must be unique because they are used as cache keys and outbound tag prefixes")
(requireAvailable singBoxCfg.enable (
singBoxCfg.outbounds != [ ] || singBoxCfg.subscriptions != [ ]
) "proxy-suite: at least one outbound or subscription is required when singBox.enable = true")
(uniqueValues singBoxCfg.enable outboundTags "proxy-suite: outbound tags must be unique")
(uniqueValues singBoxCfg.enable subscriptionTags
"proxy-suite: subscription tags must be unique because they are used as cache keys and outbound tag prefixes"
)
(mkAssertion (
!singBoxCfg.enable || builtins.all (tag: !builtins.elem tag builtinTags) outboundTags
) "proxy-suite: outbound tags must not use reserved names: proxy, direct, block")
(mkAssertion (!singBoxCfg.enable || invalidRoutingTargets == [ ])
"proxy-suite: routing.rules reference unknown outbound tag(s): ${lib.concatStringsSep ", " invalidRoutingTargets}"
)
(requireEnabled globalTun.enable singBoxCfg.enable
"proxy-suite: singBox.tun.enable requires singBox.enable = true"
)
(requireEnabled globalTproxy.enable singBoxCfg.enable
"proxy-suite: singBox.tproxy.enable requires singBox.enable = true"
)
(requireEnabled globalTun.autostart globalTun.enable
"proxy-suite: singBox.tun.autostart requires singBox.tun.enable = true"
)
(requireEnabled globalTproxy.autostart globalTproxy.enable
"proxy-suite: singBox.tproxy.autostart requires singBox.tproxy.enable = true"
)
(mkAssertion
(!singBoxCfg.enable || builtins.all (tag: !builtins.elem tag builtinTags) outboundTags)
"proxy-suite: outbound tags must not use reserved names: proxy, direct, block")
(mkAssertion
(!singBoxCfg.enable || invalidRoutingTargets == [ ])
"proxy-suite: routing.rules reference unknown outbound tag(s): ${lib.concatStringsSep ", " invalidRoutingTargets}")
(requireEnabled globalTun.enable singBoxCfg.enable "proxy-suite: singBox.tun.enable requires singBox.enable = true")
(requireEnabled globalTproxy.enable singBoxCfg.enable "proxy-suite: singBox.tproxy.enable requires singBox.enable = true")
(requireEnabled globalTun.autostart globalTun.enable "proxy-suite: singBox.tun.autostart requires singBox.tun.enable = true")
(requireEnabled globalTproxy.autostart globalTproxy.enable "proxy-suite: singBox.tproxy.autostart requires singBox.tproxy.enable = true")
(mkAssertion
(! (globalTproxy.enable && globalTproxy.autostart && globalTun.enable && globalTun.autostart))
"proxy-suite: singBox.tproxy.autostart and singBox.tun.autostart cannot both be enabled at the same time")
(!(globalTproxy.enable && globalTproxy.autostart && globalTun.enable && globalTun.autostart))
"proxy-suite: singBox.tproxy.autostart and singBox.tun.autostart cannot both be enabled at the same time"
)
];
perAppRoutingAssertions = [
(requireAvailable
(perAppRoutingCfg.profiles != [ ])
perAppRoutingCfg.enable
"proxy-suite: perAppRouting.profiles requires perAppRouting.enable = true")
(requireEnabled
perAppRoutingCfg.proxychains.enable
perAppRoutingCfg.enable
"proxy-suite: perAppRouting.proxychains.enable requires perAppRouting.enable = true")
(requireEnabled
perAppRoutingCfg.proxychains.enable
singBoxCfg.enable
"proxy-suite: perAppRouting.proxychains.enable requires singBox.enable = true")
(uniqueValues
true
effectivePerAppRoutingProfileNames
"proxy-suite: perAppRouting profile names must be unique")
(requireAvailable
hasProxychainsProfiles
perAppRoutingCfg.proxychains.enable
"proxy-suite: route=proxychains in perAppRouting.profiles requires perAppRouting.proxychains.enable = true")
(requireAvailable
hasProxychainsProfiles
singBoxCfg.enable
"proxy-suite: route=proxychains in perAppRouting.profiles requires singBox.enable = true")
(requireEnabled
perAppRoutingTun.enable
perAppRoutingCfg.enable
"proxy-suite: singBox.tun.perApp.enable requires perAppRouting.enable = true")
(requireEnabled
perAppRoutingTun.enable
singBoxCfg.enable
"proxy-suite: singBox.tun.perApp.enable requires singBox.enable = true")
(requireAvailable
hasTunProfiles
perAppRoutingTun.enable
"proxy-suite: route=tun in perAppRouting.profiles requires singBox.tun.perApp.enable = true")
(requireAvailable
hasTunProfiles
singBoxCfg.enable
"proxy-suite: route=tun in perAppRouting.profiles requires singBox.enable = true")
(requireEnabled
perAppRoutingTproxy.enable
perAppRoutingCfg.enable
"proxy-suite: singBox.tproxy.perApp.enable requires perAppRouting.enable = true")
(requireEnabled
perAppRoutingTproxy.enable
singBoxCfg.enable
"proxy-suite: singBox.tproxy.perApp.enable requires singBox.enable = true")
(requireAvailable
hasTproxyProfiles
perAppRoutingTproxy.enable
"proxy-suite: route=tproxy in perAppRouting.profiles requires singBox.tproxy.perApp.enable = true")
(requireAvailable
hasTproxyProfiles
singBoxCfg.enable
"proxy-suite: route=tproxy in perAppRouting.profiles requires singBox.enable = true")
(requireEnabled
perAppZapretCfg.enable
perAppRoutingCfg.enable
"proxy-suite: zapret.perApp.enable requires perAppRouting.enable = true")
(requireEnabled
perAppZapretCfg.enable
cfg.zapret.enable
"proxy-suite: zapret.perApp.enable requires zapret.enable = true")
(requireAvailable
hasZapretProfiles
(perAppZapretCfg.enable && cfg.zapret.enable)
"proxy-suite: route=zapret in perAppRouting.profiles requires zapret.perApp.enable = true and zapret.enable = true")
(requireAvailable (perAppRoutingCfg.profiles != [ ]) perAppRoutingCfg.enable
"proxy-suite: perAppRouting.profiles requires perAppRouting.enable = true"
)
(requireEnabled perAppRoutingCfg.proxychains.enable perAppRoutingCfg.enable
"proxy-suite: perAppRouting.proxychains.enable requires perAppRouting.enable = true"
)
(requireEnabled perAppRoutingCfg.proxychains.enable singBoxCfg.enable
"proxy-suite: perAppRouting.proxychains.enable requires singBox.enable = true"
)
(uniqueValues true effectivePerAppRoutingProfileNames
"proxy-suite: perAppRouting profile names must be unique"
)
(requireAvailable hasProxychainsProfiles perAppRoutingCfg.proxychains.enable
"proxy-suite: route=proxychains in perAppRouting.profiles requires perAppRouting.proxychains.enable = true"
)
(requireAvailable hasProxychainsProfiles singBoxCfg.enable
"proxy-suite: route=proxychains in perAppRouting.profiles requires singBox.enable = true"
)
(requireEnabled perAppRoutingTun.enable perAppRoutingCfg.enable
"proxy-suite: singBox.tun.perApp.enable requires perAppRouting.enable = true"
)
(requireEnabled perAppRoutingTun.enable singBoxCfg.enable
"proxy-suite: singBox.tun.perApp.enable requires singBox.enable = true"
)
(requireAvailable hasTunProfiles perAppRoutingTun.enable
"proxy-suite: route=tun in perAppRouting.profiles requires singBox.tun.perApp.enable = true"
)
(requireAvailable hasTunProfiles singBoxCfg.enable
"proxy-suite: route=tun in perAppRouting.profiles requires singBox.enable = true"
)
(requireEnabled perAppRoutingTproxy.enable perAppRoutingCfg.enable
"proxy-suite: singBox.tproxy.perApp.enable requires perAppRouting.enable = true"
)
(requireEnabled perAppRoutingTproxy.enable singBoxCfg.enable
"proxy-suite: singBox.tproxy.perApp.enable requires singBox.enable = true"
)
(requireAvailable hasTproxyProfiles perAppRoutingTproxy.enable
"proxy-suite: route=tproxy in perAppRouting.profiles requires singBox.tproxy.perApp.enable = true"
)
(requireAvailable hasTproxyProfiles singBoxCfg.enable
"proxy-suite: route=tproxy in perAppRouting.profiles requires singBox.enable = true"
)
(requireEnabled perAppZapretCfg.enable perAppRoutingCfg.enable
"proxy-suite: zapret.perApp.enable requires perAppRouting.enable = true"
)
(requireEnabled perAppZapretCfg.enable cfg.zapret.enable
"proxy-suite: zapret.perApp.enable requires zapret.enable = true"
)
(requireAvailable hasZapretProfiles (perAppZapretCfg.enable && cfg.zapret.enable)
"proxy-suite: route=zapret in perAppRouting.profiles requires zapret.perApp.enable = true and zapret.enable = true"
)
];
collisionAssertions = map (
item:
notEqualWhen item.condition item.left item.right item.message
) [
collisionAssertions = map (item: notEqualWhen item.condition item.left item.right item.message) [
{
condition = perAppRoutingTun.enable && globalTproxy.enable;
left = perAppRoutingTun.fwmark;
right = globalTproxy.fwmark;
message =
"proxy-suite: singBox.tun.perApp.fwmark must differ from singBox.tproxy.fwmark when global TProxy is enabled";
message = "proxy-suite: singBox.tun.perApp.fwmark must differ from singBox.tproxy.fwmark when global TProxy is enabled";
}
{
condition = perAppRoutingTun.enable && globalTproxy.enable;
left = perAppRoutingTun.fwmark;
right = globalTproxy.proxyMark;
message =
"proxy-suite: singBox.tun.perApp.fwmark must differ from singBox.tproxy.proxyMark when global TProxy is enabled";
message = "proxy-suite: singBox.tun.perApp.fwmark must differ from singBox.tproxy.proxyMark when global TProxy is enabled";
}
{
condition = perAppRoutingTun.enable && globalTproxy.enable;
left = perAppRoutingTun.routeTable;
right = globalTproxy.routeTable;
message =
"proxy-suite: singBox.tun.perApp.routeTable must differ from singBox.tproxy.routeTable when global TProxy is enabled";
message = "proxy-suite: singBox.tun.perApp.routeTable must differ from singBox.tproxy.routeTable when global TProxy is enabled";
}
{
condition = perAppRoutingTproxy.enable;
@@ -216,77 +197,85 @@ let
}
];
forbiddenValueAssertions = map (
item:
forbiddenValues item.condition item.value item.disallowed item.message
) [
{
condition = perAppZapretCfg.enable;
value = perAppZapretCfg.filterMark;
disallowed = [ 536870912 1073741824 ];
message = "proxy-suite: zapret.perApp.filterMark must not use zapret internal desync mark bits";
}
{
condition = perAppZapretCfg.enable;
value = globalTproxy.fwmark;
disallowed = [ 67108864 134217728 ];
message = "proxy-suite: singBox.tproxy.fwmark must not use per-app-zapret internal desync mark bits";
}
{
condition = perAppZapretCfg.enable;
value = globalTproxy.proxyMark;
disallowed = [ 67108864 134217728 ];
message = "proxy-suite: singBox.tproxy.proxyMark must not use per-app-zapret internal desync mark bits";
}
{
condition = perAppRoutingTun.enable && perAppZapretCfg.enable;
value = perAppRoutingTun.fwmark;
disallowed = [ 67108864 134217728 ];
message = "proxy-suite: singBox.tun.perApp.fwmark must not use per-app-zapret internal desync mark bits";
}
{
condition = perAppRoutingTproxy.enable && perAppZapretCfg.enable;
value = perAppRoutingTproxy.fwmark;
disallowed = [ 67108864 134217728 ];
message = "proxy-suite: singBox.tproxy.perApp.fwmark must not use per-app-zapret internal desync mark bits";
}
{
condition = perAppZapretCfg.enable;
value = perAppZapretCfg.filterMark;
disallowed = [ 67108864 134217728 ];
message = "proxy-suite: zapret.perApp.filterMark must not use per-app-zapret internal desync mark bits";
}
];
forbiddenValueAssertions =
map (item: forbiddenValues item.condition item.value item.disallowed item.message)
[
{
condition = perAppZapretCfg.enable;
value = perAppZapretCfg.filterMark;
disallowed = [
536870912
1073741824
];
message = "proxy-suite: zapret.perApp.filterMark must not use zapret internal desync mark bits";
}
{
condition = perAppZapretCfg.enable;
value = globalTproxy.fwmark;
disallowed = [
67108864
134217728
];
message = "proxy-suite: singBox.tproxy.fwmark must not use per-app-zapret internal desync mark bits";
}
{
condition = perAppZapretCfg.enable;
value = globalTproxy.proxyMark;
disallowed = [
67108864
134217728
];
message = "proxy-suite: singBox.tproxy.proxyMark must not use per-app-zapret internal desync mark bits";
}
{
condition = perAppRoutingTun.enable && perAppZapretCfg.enable;
value = perAppRoutingTun.fwmark;
disallowed = [
67108864
134217728
];
message = "proxy-suite: singBox.tun.perApp.fwmark must not use per-app-zapret internal desync mark bits";
}
{
condition = perAppRoutingTproxy.enable && perAppZapretCfg.enable;
value = perAppRoutingTproxy.fwmark;
disallowed = [
67108864
134217728
];
message = "proxy-suite: singBox.tproxy.perApp.fwmark must not use per-app-zapret internal desync mark bits";
}
{
condition = perAppZapretCfg.enable;
value = perAppZapretCfg.filterMark;
disallowed = [
67108864
134217728
];
message = "proxy-suite: zapret.perApp.filterMark must not use per-app-zapret internal desync mark bits";
}
];
secretAssertions = [
(exactlyOneOf
tgWsProxyCfg.enable
[
tgWsProxyCfg.secret
tgWsProxyCfg.secretFile
]
"proxy-suite: tgWsProxy requires exactly one of secret or secretFile")
(exactlyOneOf tgWsProxyCfg.enable [
tgWsProxyCfg.secret
tgWsProxyCfg.secretFile
] "proxy-suite: tgWsProxy requires exactly one of secret or secretFile")
];
outboundAssertions = lib.concatMap (ob: [
(exactlyOneOf
singBoxCfg.enable
[
ob.urlFile
ob.url
ob.json
]
"proxy-suite: outbound '${ob.tag}': set exactly one of urlFile, url, or json")
(exactlyOneOf singBoxCfg.enable [
ob.urlFile
ob.url
ob.json
] "proxy-suite: outbound '${ob.tag}': set exactly one of urlFile, url, or json")
]) singBoxCfg.outbounds;
subscriptionAssertions = lib.concatMap (sub: [
(exactlyOneOf
singBoxCfg.enable
[
sub.urlFile
sub.url
]
"proxy-suite: subscription '${sub.tag}': set exactly one of urlFile or url")
(exactlyOneOf singBoxCfg.enable [
sub.urlFile
sub.url
] "proxy-suite: subscription '${sub.tag}': set exactly one of urlFile or url")
]) singBoxCfg.subscriptions;
in
featureAssertions
+15 -7
View File
@@ -3,21 +3,29 @@
rec {
mkAssertion = assertion: message: { inherit assertion message; };
requireEnabled = featureEnabled: dependencyEnabled: message:
requireEnabled =
featureEnabled: dependencyEnabled: message:
mkAssertion (!featureEnabled || dependencyEnabled) message;
requireAvailable = featureUsed: dependencyEnabled: message:
requireAvailable =
featureUsed: dependencyEnabled: message:
mkAssertion (!featureUsed || dependencyEnabled) message;
uniqueValues = condition: values: message:
uniqueValues =
condition: values: message:
mkAssertion (!condition || builtins.length values == builtins.length (lib.unique values)) message;
notEqualWhen = condition: left: right: message:
notEqualWhen =
condition: left: right: message:
mkAssertion (!(condition && left == right)) message;
forbiddenValues = condition: value: disallowed: message:
forbiddenValues =
condition: value: disallowed: message:
mkAssertion (!(condition && builtins.elem value disallowed)) message;
exactlyOneOf = condition: values: message:
mkAssertion (!condition || builtins.length (builtins.filter (value: value != null) values) == 1) message;
exactlyOneOf =
condition: values: message:
mkAssertion (
!condition || builtins.length (builtins.filter (value: value != null) values) == 1
) message;
}
+33 -26
View File
@@ -15,7 +15,14 @@ let
preStart ? null,
}:
{
inherit description after wantedBy wants requires conflicts;
inherit
description
after
wantedBy
wants
requires
conflicts
;
}
// lib.optionalAttrs (preStart != null) { inherit preStart; };
in
@@ -23,7 +30,9 @@ rec {
mkNamedUnits =
entries:
lib.listToAttrs (
map (entry: lib.nameValuePair entry.name entry.value) (builtins.filter (entry: entry.enable) entries)
map (entry: lib.nameValuePair entry.name entry.value) (
builtins.filter (entry: entry.enable) entries
)
);
mkRestartingService =
@@ -54,17 +63,16 @@ rec {
;
})
// {
serviceConfig =
{
ExecStart = execStart;
Restart = "on-failure";
RestartSec = 5;
RuntimeDirectory = runtimeDirectory;
}
// lib.optionalAttrs (stateDirectory != null) { StateDirectory = stateDirectory; }
// lib.optionalAttrs (execStartPost != null) { ExecStartPost = execStartPost; }
// lib.optionalAttrs (execStopPost != null) { ExecStopPost = execStopPost; }
// extraServiceConfig;
serviceConfig = {
ExecStart = execStart;
Restart = "on-failure";
RestartSec = 5;
RuntimeDirectory = runtimeDirectory;
}
// lib.optionalAttrs (stateDirectory != null) { StateDirectory = stateDirectory; }
// lib.optionalAttrs (execStartPost != null) { ExecStartPost = execStartPost; }
// lib.optionalAttrs (execStopPost != null) { ExecStopPost = execStopPost; }
// extraServiceConfig;
};
mkOneshotService =
@@ -97,19 +105,18 @@ rec {
;
})
// {
serviceConfig =
{
Type = "oneshot";
RemainAfterExit = true;
ExecStart = execStart;
}
// lib.optionalAttrs (execStop != null) { ExecStop = execStop; }
// lib.optionalAttrs (execStartPre != null) { ExecStartPre = execStartPre; }
// lib.optionalAttrs (execStartPost != null) { ExecStartPost = execStartPost; }
// lib.optionalAttrs (execStopPost != null) { ExecStopPost = execStopPost; }
// lib.optionalAttrs (runtimeDirectory != null) { RuntimeDirectory = runtimeDirectory; }
// lib.optionalAttrs (stateDirectory != null) { StateDirectory = stateDirectory; }
// extraServiceConfig;
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = execStart;
}
// lib.optionalAttrs (execStop != null) { ExecStop = execStop; }
// lib.optionalAttrs (execStartPre != null) { ExecStartPre = execStartPre; }
// lib.optionalAttrs (execStartPost != null) { ExecStartPost = execStartPost; }
// lib.optionalAttrs (execStopPost != null) { ExecStopPost = execStopPost; }
// lib.optionalAttrs (runtimeDirectory != null) { RuntimeDirectory = runtimeDirectory; }
// lib.optionalAttrs (stateDirectory != null) { StateDirectory = stateDirectory; }
// extraServiceConfig;
};
mkUserRuleService =
+34 -4
View File
@@ -55,17 +55,47 @@ let
};
scripts = import ./scripts.nix {
inherit lib pkgs singBoxCfg perAppRoutingTun selectionMode collapseNamedOutbounds hasSubscriptions;
inherit jq python3 singBox parserScriptsPythonPath buildOutboundPy fetchSubscriptionPy;
inherit
lib
pkgs
singBoxCfg
perAppRoutingTun
selectionMode
collapseNamedOutbounds
hasSubscriptions
;
inherit
jq
python3
singBox
parserScriptsPythonPath
buildOutboundPy
fetchSubscriptionPy
;
inherit tproxyFile tunFile perAppTunFile;
};
perAppRouting = import ./per-app-routing.nix {
inherit lib pkgs cfg singBoxCfg perAppRoutingCfg perAppRoutingTun perAppRoutingTproxy;
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
awk
grepBin
findBin
headBin
seqBin
sleepBin
;
};
control = import ./control.nix {
+26 -22
View File
@@ -217,20 +217,17 @@ let
{
enable = perAppRoutingTun.enable;
name = "${serviceNames.perAppTun}-anchor";
value =
mkAnchorService perAppRouting.perAppTunSliceName "Anchor service for proxy-suite app TUN slice";
value = mkAnchorService perAppRouting.perAppTunSliceName "Anchor service for proxy-suite app TUN slice";
}
{
enable = perAppRoutingTproxy.enable;
name = "${serviceNames.perAppTproxy}-anchor";
value =
mkAnchorService perAppRouting.perAppTproxySliceName "Anchor service for proxy-suite app TProxy slice";
value = mkAnchorService perAppRouting.perAppTproxySliceName "Anchor service for proxy-suite app TProxy slice";
}
{
enable = perAppZapretEnabled;
name = "${serviceNames.perAppZapret}-anchor";
value =
mkAnchorService perAppRouting.perAppZapretSliceName "Anchor service for proxy-suite app zapret slice";
value = mkAnchorService perAppRouting.perAppZapretSliceName "Anchor service for proxy-suite app zapret slice";
}
];
@@ -262,29 +259,36 @@ in
};
security.polkit.enable = lib.mkIf (cfg.enable && userControlEnabled) true;
security.polkit.extraConfig = lib.mkIf (cfg.enable && userControlEnabled) (lib.mkAfter ''
polkit.addRule(function(action, subject) {
if (!subject.isInGroup("${userControlCfg.group}")) {
security.polkit.extraConfig = lib.mkIf (cfg.enable && userControlEnabled) (
lib.mkAfter ''
polkit.addRule(function(action, subject) {
if (!subject.isInGroup("${userControlCfg.group}")) {
return null;
}
if (action.id !== "org.freedesktop.systemd1.manage-units") {
return null;
}
var unit = action.lookup("unit");
${polkit.userControlPolkitRules}
return null;
}
if (action.id !== "org.freedesktop.systemd1.manage-units") {
return null;
}
var unit = action.lookup("unit");
${polkit.userControlPolkitRules}
return null;
});
'');
});
''
);
systemd.user.services = mkNamedUnits userServiceEntries;
assertions = import ../service-assertions.nix {
inherit lib cfg derived;
tgWsProxyCfg = cfg.tgWsProxy;
inherit builtinTags outboundTags subscriptionTags invalidRoutingTargets;
inherit
builtinTags
outboundTags
subscriptionTags
invalidRoutingTargets
;
inherit (perAppRouting)
effectivePerAppRoutingProfileNames
hasProxychainsProfiles
@@ -58,7 +58,9 @@ let
);
effectivePerAppRoutingProfiles =
perAppRoutingCfg.profiles
++ builtins.filter (profile: !(builtins.elem profile.name perAppRoutingProfileNames)) defaultPerAppRoutingProfiles;
++ builtins.filter (
profile: !(builtins.elem profile.name perAppRoutingProfileNames)
) defaultPerAppRoutingProfiles;
effectivePerAppRoutingProfileNames = map (profile: profile.name) effectivePerAppRoutingProfiles;
perAppRoutingProfilesFile = pkgs.writeText "proxy-suite-per-app-routing-profiles.json" (
@@ -77,10 +79,16 @@ let
'';
proxychainsQuietArg = lib.optionalString perAppRoutingCfg.proxychains.quiet "-q ";
hasProxychainsProfiles = builtins.any (profile: profile.route == "proxychains") effectivePerAppRoutingProfiles;
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;
hasTproxyProfiles = builtins.any (
profile: profile.route == "tproxy"
) effectivePerAppRoutingProfiles;
hasZapretProfiles = builtins.any (
profile: profile.route == "zapret"
) effectivePerAppRoutingProfiles;
perAppTunWaitForInterface = pkgs.writeShellScript "proxy-suite-per-app-tun-wait-for-interface" ''
set -euo pipefail
+8 -2
View File
@@ -1,5 +1,9 @@
# Polkit rules granting proxy-suite group members passwordless control over services.
{ lib, cfg, userControlCfg }:
{
lib,
cfg,
userControlCfg,
}:
let
userControlEnabled = userControlCfg.global.enable || userControlCfg.perApp.enable;
@@ -18,4 +22,6 @@ let
}
'';
in
{ inherit userControlEnabled userControlPolkitRules; }
{
inherit userControlEnabled userControlPolkitRules;
}
+8 -5
View File
@@ -100,9 +100,11 @@ let
fi
if [ -f "$CACHE_FILE" ]; then
SUB_JSON=$(cat "$CACHE_FILE")
${lib.optionalString (routingMark != null) ''
SUB_JSON=$(${jq} --argjson m ${toString routingMark} 'map(. + {routing_mark: $m})' <<< "$SUB_JSON")
''}OUTBOUNDS_JSON=$(${jq} --argjson sub "$SUB_JSON" '. + $sub' <<< "$OUTBOUNDS_JSON")
${
lib.optionalString (routingMark != null) ''
SUB_JSON=$(${jq} --argjson m ${toString routingMark} 'map(. + {routing_mark: $m})' <<< "$SUB_JSON")
''
}OUTBOUNDS_JSON=$(${jq} --argjson sub "$SUB_JSON" '. + $sub' <<< "$OUTBOUNDS_JSON")
fi
'';
@@ -117,8 +119,9 @@ let
else
lib.concatMapStrings (ob: mkOutboundBlock ob routingMark ob.tag) singBoxCfg.outbounds;
subscriptionBlocks =
lib.concatMapStrings (sub: mkSubscriptionBlock sub routingMark) singBoxCfg.subscriptions;
subscriptionBlocks = lib.concatMapStrings (
sub: mkSubscriptionBlock sub routingMark
) singBoxCfg.subscriptions;
wrapperBlock =
if collapseNamedOutbounds then
+80 -54
View File
@@ -45,10 +45,7 @@ let
if upstreamPackages ? zapret then upstreamPackages.zapret else upstreamPackages.default;
mkOptionalHostlistFile =
fileName: entries:
if entries != [ ] then
pkgs.writeText fileName (lib.concatStringsSep "\n" entries + "\n")
else
null;
if entries != [ ] then pkgs.writeText fileName (lib.concatStringsSep "\n" entries + "\n") else null;
listGeneralFile = mkOptionalHostlistFile "proxy-suite-zapret-list-general-user.txt" zapretCfg.listGeneral;
listExcludeFile = mkOptionalHostlistFile "proxy-suite-zapret-list-exclude-user.txt" zapretCfg.listExclude;
@@ -160,15 +157,31 @@ let
}
${lib.concatMapStrings
({ name, file }: lib.optionalString (file != null) ''
append_list_file ${name} ${file}
'')
(
{ name, file }:
lib.optionalString (file != null) ''
append_list_file ${name} ${file}
''
)
[
{ name = "list-general-user.txt"; file = listGeneralFile; }
{ name = "list-exclude-user.txt"; file = listExcludeFile; }
{ name = "ipset-all.txt"; file = ipsetAllFile; }
{ name = "ipset-exclude-user.txt"; file = ipsetExcludeFile; }
]}
{
name = "list-general-user.txt";
file = listGeneralFile;
}
{
name = "list-exclude-user.txt";
file = listExcludeFile;
}
{
name = "ipset-all.txt";
file = ipsetAllFile;
}
{
name = "ipset-exclude-user.txt";
file = ipsetExcludeFile;
}
]
}
rm -f "$out/opt/zapret/hostlists/.game_filter.enabled"
${lib.optionalString (gameFilter != "null") ''
@@ -193,15 +206,28 @@ let
set_config_var FILTER_MARK ""
''}
${lib.concatMapStrings
({ var, value }: lib.optionalString (value != null) ''
hex=$(printf '0x%x' ${toString value})
set_config_var ${var} "$hex"
'')
(
{ var, value }:
lib.optionalString (value != null) ''
hex=$(printf '0x%x' ${toString value})
set_config_var ${var} "$hex"
''
)
[
{ var = "FILTER_MARK"; value = filterMark; }
{ var = "DESYNC_MARK"; value = desyncMark; }
{ var = "DESYNC_MARK_POSTNAT"; value = desyncMarkPostnat; }
]}
{
var = "FILTER_MARK";
value = filterMark;
}
{
var = "DESYNC_MARK";
value = desyncMark;
}
{
var = "DESYNC_MARK_POSTNAT";
value = desyncMarkPostnat;
}
]
}
${lib.optionalString (qnum != null) ''
set_config_var QNUM ${toString qnum}
''}
@@ -224,7 +250,8 @@ let
pidDir = "/run/proxy-suite-zapret";
gameFilter = zapretCfg.gameFilter;
forceDisableFilterMark = true;
customScript = if perAppZapretCfg.enable then mkGlobalBypassScript (toString perAppZapretCfg.filterMark) else null;
customScript =
if perAppZapretCfg.enable then mkGlobalBypassScript (toString perAppZapretCfg.filterMark) else null;
};
perAppZapretPackage = mkDerivedZapretPackage {
@@ -256,7 +283,7 @@ let
"PATH=${lib.makeBinPath runtimeDeps}"
];
globalZapretEnv = mkZapretEnv globalZapretPackage;
perAppZapretEnv = mkZapretEnv perAppZapretPackage;
perAppZapretEnv = mkZapretEnv perAppZapretPackage;
perAppZapretMarkUpScript = pkgs.writeShellScript "proxy-suite-per-app-zapret-mark-up" ''
set -euo pipefail
@@ -290,15 +317,16 @@ in
message = "proxy-suite: each zapret.hostlistRules entry must define at least one domain";
}
{
assertion = builtins.all (rule: rule.preset != null || rule.nfqwsArgs != [ ]) zapretCfg.hostlistRules;
assertion = builtins.all (
rule: rule.preset != null || rule.nfqwsArgs != [ ]
) zapretCfg.hostlistRules;
message = "proxy-suite: each zapret.hostlistRules entry must set preset, nfqwsArgs, or both";
}
];
environment.systemPackages =
lib.mkBefore (
[ globalZapretPackage ] ++ lib.optionals perAppZapretCfg.enable [ perAppZapretPackage ]
);
environment.systemPackages = lib.mkBefore (
[ globalZapretPackage ] ++ lib.optionals perAppZapretCfg.enable [ perAppZapretPackage ]
);
systemd.services.zapret-discord-youtube = mkOneshotService {
description = "zapret DPI bypass";
@@ -314,32 +342,30 @@ in
};
};
systemd.services.proxy-suite-per-app-zapret = lib.mkIf perAppZapretCfg.enable (
mkOneshotService {
description = "proxy-suite per-app-routing zapret backend";
after = [ "network.target" ];
conflicts = [
"proxy-suite-tproxy.service"
"proxy-suite-tun.service"
];
preStart = zapretCommonPreStart perAppZapretPackage;
runtimeDirectory = "proxy-suite-per-app-zapret";
execStart = "${perAppZapretPackage}/opt/zapret/init.d/sysv/zapret start";
execStop = "${perAppZapretPackage}/opt/zapret/init.d/sysv/zapret stop";
execStartPre = "${perAppZapretMarkUpScript}";
execStopPost = "${perAppZapretMarkDownScript}";
extraServiceConfig.Environment = perAppZapretEnv;
}
);
systemd.services.proxy-suite-per-app-zapret = lib.mkIf perAppZapretCfg.enable (mkOneshotService {
description = "proxy-suite per-app-routing zapret backend";
after = [ "network.target" ];
conflicts = [
"proxy-suite-tproxy.service"
"proxy-suite-tun.service"
];
preStart = zapretCommonPreStart perAppZapretPackage;
runtimeDirectory = "proxy-suite-per-app-zapret";
execStart = "${perAppZapretPackage}/opt/zapret/init.d/sysv/zapret start";
execStop = "${perAppZapretPackage}/opt/zapret/init.d/sysv/zapret stop";
execStartPre = "${perAppZapretMarkUpScript}";
execStopPost = "${perAppZapretMarkDownScript}";
extraServiceConfig.Environment = perAppZapretEnv;
});
systemd.services.proxy-suite-zapret-vm-exempt = lib.mkIf zapretCfg.cidrExemption.enable (
mkOneshotService {
description = "Exempt CIDRs from zapret NFQUEUE";
after = [ "zapret-discord-youtube.service" ];
wants = [ "zapret-discord-youtube.service" ];
wantedBy = [ "multi-user.target" ];
execStart = pkgs.writeShellScript "proxy-suite-zapret-vm-exempt-start" exemptStart;
execStop = pkgs.writeShellScript "proxy-suite-zapret-vm-exempt-stop" exemptStop;
}
);
systemd.services.proxy-suite-zapret-vm-exempt =
lib.mkIf zapretCfg.cidrExemption.enable
(mkOneshotService {
description = "Exempt CIDRs from zapret NFQUEUE";
after = [ "zapret-discord-youtube.service" ];
wants = [ "zapret-discord-youtube.service" ];
wantedBy = [ "multi-user.target" ];
execStart = pkgs.writeShellScript "proxy-suite-zapret-vm-exempt-start" exemptStart;
execStop = pkgs.writeShellScript "proxy-suite-zapret-vm-exempt-stop" exemptStop;
});
}