some simplifications and improvements
This commit is contained in:
7 files changed
+296
-534
No files matched your search
@@ -47,7 +47,7 @@ in
|
||||
cfg
|
||||
;
|
||||
inherit (configs) tproxyFile tunFile appTunFile;
|
||||
inherit (nftr) nftablesRulesFile appTproxyRulesFile appZapretRulesFile ip nft;
|
||||
inherit (nftr) nftablesRulesFile appTproxyRulesFile appZapretRulesFile appTunChainFile ip nft;
|
||||
}
|
||||
))
|
||||
|
||||
|
||||
@@ -11,15 +11,8 @@ let
|
||||
artp = cfg.appRouting.backends.tproxy;
|
||||
arz = cfg.appRouting.backends.zapret;
|
||||
|
||||
mkLocalSubnetLines = subnets: lib.concatMapStrings (cidr: ''
|
||||
ip daddr ${cidr} tcp dport != 53 return
|
||||
ip daddr ${cidr} udp dport != 53 return
|
||||
'') subnets;
|
||||
|
||||
tproxyLocalSubnetLines = mkLocalSubnetLines sb.tproxy.localSubnets;
|
||||
appTproxyLocalSubnetLines = mkLocalSubnetLines artp.localSubnets;
|
||||
|
||||
nftablesRulesFile = pkgs.writeText "proxy-suite-tproxy.nft" ''
|
||||
# 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,
|
||||
@@ -31,7 +24,18 @@ let
|
||||
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;
|
||||
|
||||
tproxyLocalSubnetLines = mkLocalSubnetLines sb.tproxy.localSubnets;
|
||||
appTproxyLocalSubnetLines = mkLocalSubnetLines artp.localSubnets;
|
||||
|
||||
nftablesRulesFile = pkgs.writeText "proxy-suite-tproxy.nft" ''
|
||||
${reservedIpBlock}
|
||||
table ip singbox {
|
||||
chain prerouting {
|
||||
type filter hook prerouting priority mangle; policy accept;
|
||||
@@ -55,18 +59,7 @@ ${lib.optionalString art.enable " meta mark ${toString art.fwmark}
|
||||
'';
|
||||
|
||||
appTproxyRulesFile = pkgs.writeText "proxy-suite-app-tproxy.nft" ''
|
||||
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
|
||||
}
|
||||
|
||||
${reservedIpBlock}
|
||||
table ip proxy_suite_app_tproxy {
|
||||
chain prerouting {
|
||||
type filter hook prerouting priority mangle; policy accept;
|
||||
@@ -104,10 +97,25 @@ ${lib.optionalString art.enable " meta mark ${toString art.fwmark}
|
||||
}
|
||||
'';
|
||||
|
||||
appTunChainFile = pkgs.writeText "proxy-suite-app-tun-chain.nft" ''
|
||||
${reservedIpBlock}
|
||||
table inet proxy_suite_app_tun {
|
||||
chain output {
|
||||
type route hook output priority mangle; policy accept;
|
||||
ip daddr $RESERVED_IP return
|
||||
${lib.concatMapStrings (cidr: ''
|
||||
ip daddr ${cidr} return
|
||||
'') art.localSubnets}
|
||||
ct mark ${toString art.fwmark} meta mark set ${toString art.fwmark}
|
||||
meta mark ${toString art.fwmark} return
|
||||
}
|
||||
}
|
||||
'';
|
||||
|
||||
ip = "${pkgs.iproute2}/bin/ip";
|
||||
nft = "${pkgs.nftables}/bin/nft";
|
||||
|
||||
in
|
||||
{
|
||||
inherit nftablesRulesFile appTproxyRulesFile appZapretRulesFile ip nft;
|
||||
inherit nftablesRulesFile appTproxyRulesFile appZapretRulesFile appTunChainFile ip nft;
|
||||
}
|
||||
@@ -549,8 +549,7 @@ in
|
||||
type = types.port;
|
||||
default = 1081;
|
||||
description = ''
|
||||
Listen port for the local sing-box TProxy inbound.
|
||||
Only relevant when singBox.tproxy.enable = true.
|
||||
Listen port for the local sing-box TProxy inbound. Used by TProxy mode.
|
||||
'';
|
||||
example = 1081;
|
||||
};
|
||||
@@ -560,8 +559,7 @@ in
|
||||
default = 1;
|
||||
description = ''
|
||||
Netfilter mark set on packets intercepted by TProxy so policy routing
|
||||
can send them to the local loopback route table.
|
||||
Only relevant when singBox.tproxy.enable = true.
|
||||
can send them to the local loopback route table. Used by TProxy mode.
|
||||
'';
|
||||
example = 1;
|
||||
};
|
||||
@@ -571,8 +569,7 @@ in
|
||||
default = 2;
|
||||
description = ''
|
||||
Netfilter mark set on sing-box's own outbound packets so they bypass
|
||||
TProxy re-interception loops.
|
||||
Only relevant when singBox.tproxy.enable = true.
|
||||
TProxy re-interception loops. Used by TProxy mode.
|
||||
'';
|
||||
example = 2;
|
||||
};
|
||||
@@ -582,8 +579,7 @@ in
|
||||
default = 100;
|
||||
description = ''
|
||||
Policy routing table used to redirect TProxy-marked packets to the
|
||||
loopback interface.
|
||||
Only relevant when singBox.tproxy.enable = true.
|
||||
loopback interface. Used by TProxy mode.
|
||||
'';
|
||||
example = 100;
|
||||
};
|
||||
@@ -655,7 +651,6 @@ in
|
||||
description = ''
|
||||
Whether to start proxy-suite-tun automatically during boot by
|
||||
attaching it to multi-user.target.
|
||||
Only relevant when singBox.tun.enable = true.
|
||||
Cannot be enabled together with singBox.tproxy.autostart.
|
||||
'';
|
||||
example = true;
|
||||
@@ -664,30 +659,21 @@ in
|
||||
interface = mkOption {
|
||||
type = types.str;
|
||||
default = "singtun0";
|
||||
description = ''
|
||||
Name of the TUN interface created by proxy-suite-tun.
|
||||
Only relevant when singBox.tun.enable = true.
|
||||
'';
|
||||
description = "Name of the TUN interface created by proxy-suite-tun.";
|
||||
example = "singtun0";
|
||||
};
|
||||
|
||||
address = mkOption {
|
||||
type = types.str;
|
||||
default = "172.19.0.1/30";
|
||||
description = ''
|
||||
Address assigned to the TUN interface in CIDR notation.
|
||||
Only relevant when singBox.tun.enable = true.
|
||||
'';
|
||||
description = "Address assigned to the TUN interface in CIDR notation.";
|
||||
example = "172.19.0.1/30";
|
||||
};
|
||||
|
||||
mtu = mkOption {
|
||||
type = types.int;
|
||||
default = 1400;
|
||||
description = ''
|
||||
MTU for the TUN interface created by proxy-suite-tun.
|
||||
Only relevant when singBox.tun.enable = true.
|
||||
'';
|
||||
description = "MTU for the TUN interface created by proxy-suite-tun.";
|
||||
example = 1400;
|
||||
};
|
||||
};
|
||||
@@ -701,7 +687,6 @@ in
|
||||
description = ''
|
||||
Whether to start proxy-suite-tproxy automatically during boot by
|
||||
attaching it to multi-user.target.
|
||||
Only relevant when singBox.tproxy.enable = true.
|
||||
Cannot be enabled together with singBox.tun.autostart.
|
||||
'';
|
||||
example = true;
|
||||
@@ -712,7 +697,6 @@ in
|
||||
default = [ "192.168.0.0/16" ];
|
||||
description = ''
|
||||
Subnets whose traffic bypasses TProxy interception, except DNS (port 53).
|
||||
Only relevant when singBox.tproxy.enable = true.
|
||||
|
||||
Typically this should include your LAN subnet(s). VM bridge networks
|
||||
should usually go here too, or use zapret.cidrExemption for
|
||||
@@ -808,15 +792,8 @@ in
|
||||
};
|
||||
};
|
||||
|
||||
# Per-outbound and per-action routing rules.
|
||||
# These are evaluated before the global proxy/direct/block lists above.
|
||||
# Use this for fine-grained control: route specific services to specific
|
||||
# servers, or override the default for particular domains/IPs/geos.
|
||||
#
|
||||
# Alternatively, attach routing directly to an outbound via outbound.routing.*
|
||||
# which is equivalent to adding an entry here with outbound = <that tag>.
|
||||
#
|
||||
# Order within this list is preserved (first matching rule wins in sing-box).
|
||||
# Per-outbound routing rules evaluated before global proxy/direct/block lists.
|
||||
# Equivalent to setting outbound.routing.* directly on each outbound definition.
|
||||
rules = mkOption {
|
||||
type = types.listOf routingRuleType;
|
||||
default = [ ];
|
||||
@@ -1112,21 +1089,14 @@ in
|
||||
configName = mkOption {
|
||||
type = types.str;
|
||||
default = "general(ALT)";
|
||||
description = ''
|
||||
zapret strategy preset name passed through to the generated zapret
|
||||
configuration.
|
||||
Only relevant when zapret.enable = true.
|
||||
'';
|
||||
description = "zapret strategy preset name passed through to the generated zapret configuration.";
|
||||
example = "general(ALT)";
|
||||
};
|
||||
|
||||
gameFilter = mkOption {
|
||||
type = types.str;
|
||||
default = "null";
|
||||
description = ''
|
||||
zapret game traffic filter mode: "all", "tcp", "udp", or "null" to disable.
|
||||
Only relevant when zapret.enable = true.
|
||||
'';
|
||||
description = ''zapret game traffic filter mode: "all", "tcp", "udp", or "null" to disable.'';
|
||||
example = "null";
|
||||
};
|
||||
|
||||
@@ -1234,8 +1204,6 @@ in
|
||||
];
|
||||
description = ''
|
||||
Subnets to exempt from zapret's NFQUEUE mangle rules.
|
||||
Only relevant when zapret.enable = true and cidrExemption.enable = true.
|
||||
|
||||
Useful when a VM (libvirt, etc.) is behind NAT and zapret
|
||||
would corrupt its traffic through the host's nftables.
|
||||
'';
|
||||
@@ -1249,10 +1217,7 @@ in
|
||||
pollInterval = mkOption {
|
||||
type = types.int;
|
||||
default = 5;
|
||||
description = ''
|
||||
Status polling interval in seconds for the tray application.
|
||||
Only relevant when tray.enable = true.
|
||||
'';
|
||||
description = "Status polling interval in seconds for the tray application.";
|
||||
example = 5;
|
||||
};
|
||||
|
||||
@@ -1260,9 +1225,8 @@ in
|
||||
type = types.bool;
|
||||
default = true;
|
||||
description = ''
|
||||
Whether to install an XDG autostart entry for the tray application for
|
||||
graphical users.
|
||||
Only relevant when tray.enable = true.
|
||||
Whether to install an XDG autostart entry for the tray application
|
||||
for graphical users.
|
||||
'';
|
||||
example = true;
|
||||
};
|
||||
@@ -1274,20 +1238,14 @@ in
|
||||
port = mkOption {
|
||||
type = types.port;
|
||||
default = 1076;
|
||||
description = ''
|
||||
Listen port for the tg-ws-proxy service.
|
||||
Only relevant when tgWsProxy.enable = true.
|
||||
'';
|
||||
description = "Listen port for the tg-ws-proxy service.";
|
||||
example = 1076;
|
||||
};
|
||||
|
||||
host = mkOption {
|
||||
type = types.str;
|
||||
default = "127.0.0.1";
|
||||
description = ''
|
||||
Address to bind tg-ws-proxy to.
|
||||
Only relevant when tgWsProxy.enable = true.
|
||||
'';
|
||||
description = "Address to bind tg-ws-proxy to.";
|
||||
example = "127.0.0.1";
|
||||
};
|
||||
|
||||
@@ -1319,10 +1277,7 @@ in
|
||||
dcIps = mkOption {
|
||||
type = types.attrsOf types.str;
|
||||
default = { };
|
||||
description = ''
|
||||
Map of Telegram DC ID to IP address for relay.
|
||||
Only relevant when tgWsProxy.enable = true.
|
||||
'';
|
||||
description = "Map of Telegram DC ID to IP address for relay.";
|
||||
example = {
|
||||
"2" = "149.154.167.220";
|
||||
"4" = "149.154.167.220";
|
||||
|
||||
+149
-300
@@ -10,6 +10,7 @@
|
||||
nftablesRulesFile,
|
||||
appTproxyRulesFile,
|
||||
appZapretRulesFile,
|
||||
appTunChainFile,
|
||||
ip,
|
||||
nft,
|
||||
}:
|
||||
@@ -325,31 +326,6 @@ let
|
||||
appTunSliceName = "proxy-suite-app-tun.slice";
|
||||
appTproxySliceName = "proxy-suite-app-tproxy.slice";
|
||||
appZapretSliceName = "proxy-suite-app-zapret.slice";
|
||||
appTunChainFile = pkgs.writeText "proxy-suite-app-tun-chain.nft" ''
|
||||
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
|
||||
}
|
||||
|
||||
table inet proxy_suite_app_tun {
|
||||
chain output {
|
||||
type route hook output priority mangle; policy accept;
|
||||
ip daddr $RESERVED_IP return
|
||||
${lib.concatMapStrings (cidr: ''
|
||||
ip daddr ${cidr} return
|
||||
'') art.localSubnets}
|
||||
ct mark ${toString art.fwmark} meta mark set ${toString art.fwmark}
|
||||
meta mark ${toString art.fwmark} return
|
||||
}
|
||||
}
|
||||
'';
|
||||
appTunWaitForInterface = pkgs.writeShellScript "proxy-suite-app-tun-wait-for-interface" ''
|
||||
set -euo pipefail
|
||||
for _ in $(${seqBin} 1 50); do
|
||||
@@ -375,11 +351,21 @@ ${lib.concatMapStrings (cidr: ''
|
||||
${ip} route del default dev ${lib.escapeShellArg art.interface} table ${toString art.routeTable} 2>/dev/null || true
|
||||
${ip} rule del fwmark ${toString art.fwmark} table ${toString art.routeTable} 2>/dev/null || true
|
||||
'';
|
||||
appTunUserRuleStart =
|
||||
pkgs.writeShellScript "proxy-suite-app-tun-user-start" ''
|
||||
# Generate a "start" script that adds a per-user cgroup nftables mark rule.
|
||||
mkUserRuleStart =
|
||||
{
|
||||
name,
|
||||
nftFamily,
|
||||
nftTable,
|
||||
nftChain,
|
||||
sliceName,
|
||||
sliceLabel,
|
||||
markRule,
|
||||
}:
|
||||
pkgs.writeShellScript "proxy-suite-${name}-user-start" ''
|
||||
set -euo pipefail
|
||||
uid="$1"
|
||||
rule_comment_prefix="proxy-suite-app-tun-user-$uid"
|
||||
rule_comment_prefix="proxy-suite-${name}-user-$uid"
|
||||
mark_comment="$rule_comment_prefix-mark"
|
||||
cgroup_root="/sys/fs/cgroup/user.slice/user-$uid.slice/user@$uid.service"
|
||||
if ! [ -d "$cgroup_root" ]; then
|
||||
@@ -387,43 +373,100 @@ ${lib.concatMapStrings (cidr: ''
|
||||
exit 1
|
||||
fi
|
||||
|
||||
cgroup_dir=$(${findBin} "$cgroup_root" -type d -name ${lib.escapeShellArg appTunSliceName} | ${headBin} -n1 || true)
|
||||
cgroup_dir=$(${findBin} "$cgroup_root" -type d -name ${lib.escapeShellArg sliceName} | ${headBin} -n1 || true)
|
||||
if [ -z "$cgroup_dir" ]; then
|
||||
echo "proxy-suite: app TUN slice cgroup does not exist for uid $uid under $cgroup_root" >&2
|
||||
echo "proxy-suite: ${sliceLabel} slice cgroup does not exist for uid $uid under $cgroup_root" >&2
|
||||
exit 1
|
||||
fi
|
||||
cgroup_path=''${cgroup_dir#/sys/fs/cgroup/}
|
||||
cgroup_level=$(printf '%s' "$cgroup_path" | ${awk} -F/ '{ print NF }')
|
||||
|
||||
handles=$(${nft} -a list chain inet proxy_suite_app_tun output 2>/dev/null \
|
||||
handles=$(${nft} -a list chain ${nftFamily} ${nftTable} ${nftChain} 2>/dev/null \
|
||||
| ${grepBin} -F "comment \"$rule_comment_prefix" \
|
||||
| ${awk} '{ print $NF }' || true)
|
||||
if [ -n "$handles" ]; then
|
||||
while IFS= read -r handle; do
|
||||
[ -n "$handle" ] || continue
|
||||
${nft} delete rule inet proxy_suite_app_tun output handle "$handle" || true
|
||||
${nft} delete rule ${nftFamily} ${nftTable} ${nftChain} handle "$handle" || true
|
||||
done <<< "$handles"
|
||||
fi
|
||||
|
||||
printf '%s\n' \
|
||||
"add rule inet proxy_suite_app_tun output socket cgroupv2 level $cgroup_level \"$cgroup_path\" meta mark set ${toString art.fwmark} ct mark set ${toString art.fwmark} comment \"$mark_comment\"" \
|
||||
"add rule ${nftFamily} ${nftTable} ${nftChain} socket cgroupv2 level $cgroup_level \"$cgroup_path\" ${markRule} comment \"$mark_comment\"" \
|
||||
| ${nft} -f -
|
||||
'';
|
||||
appTunUserRuleStop =
|
||||
pkgs.writeShellScript "proxy-suite-app-tun-user-stop" ''
|
||||
|
||||
# Generate a "stop" script that removes all per-user cgroup nftables mark rules.
|
||||
mkUserRuleStop =
|
||||
{
|
||||
name,
|
||||
nftFamily,
|
||||
nftTable,
|
||||
nftChain,
|
||||
}:
|
||||
pkgs.writeShellScript "proxy-suite-${name}-user-stop" ''
|
||||
set -euo pipefail
|
||||
uid="$1"
|
||||
rule_comment_prefix="proxy-suite-app-tun-user-$uid"
|
||||
handles=$(${nft} -a list chain inet proxy_suite_app_tun output 2>/dev/null \
|
||||
rule_comment_prefix="proxy-suite-${name}-user-$uid"
|
||||
handles=$(${nft} -a list chain ${nftFamily} ${nftTable} ${nftChain} 2>/dev/null \
|
||||
| ${grepBin} -F "comment \"$rule_comment_prefix" \
|
||||
| ${awk} '{ print $NF }' || true)
|
||||
if [ -n "$handles" ]; then
|
||||
while IFS= read -r handle; do
|
||||
[ -n "$handle" ] || continue
|
||||
${nft} delete rule inet proxy_suite_app_tun output handle "$handle" || true
|
||||
${nft} delete rule ${nftFamily} ${nftTable} ${nftChain} handle "$handle" || true
|
||||
done <<< "$handles"
|
||||
fi
|
||||
'';
|
||||
|
||||
appTunUserRuleStart = mkUserRuleStart {
|
||||
name = "app-tun";
|
||||
nftFamily = "inet";
|
||||
nftTable = "proxy_suite_app_tun";
|
||||
nftChain = "output";
|
||||
sliceName = appTunSliceName;
|
||||
sliceLabel = "app TUN";
|
||||
markRule = "meta mark set ${toString art.fwmark} ct mark set ${toString art.fwmark}";
|
||||
};
|
||||
appTunUserRuleStop = mkUserRuleStop {
|
||||
name = "app-tun";
|
||||
nftFamily = "inet";
|
||||
nftTable = "proxy_suite_app_tun";
|
||||
nftChain = "output";
|
||||
};
|
||||
|
||||
appTproxyUserRuleStart = mkUserRuleStart {
|
||||
name = "app-tproxy";
|
||||
nftFamily = "ip";
|
||||
nftTable = "proxy_suite_app_tproxy";
|
||||
nftChain = "output";
|
||||
sliceName = appTproxySliceName;
|
||||
sliceLabel = "app TProxy";
|
||||
markRule = "meta mark set ${toString artp.fwmark} ct mark set ${toString artp.fwmark}";
|
||||
};
|
||||
appTproxyUserRuleStop = mkUserRuleStop {
|
||||
name = "app-tproxy";
|
||||
nftFamily = "ip";
|
||||
nftTable = "proxy_suite_app_tproxy";
|
||||
nftChain = "output";
|
||||
};
|
||||
|
||||
appZapretUserRuleStart = mkUserRuleStart {
|
||||
name = "app-zapret";
|
||||
nftFamily = "inet";
|
||||
nftTable = "proxy_suite_app_zapret_mark";
|
||||
nftChain = "output";
|
||||
sliceName = appZapretSliceName;
|
||||
sliceLabel = "app zapret";
|
||||
markRule = "meta mark set meta mark or ${toString arz.filterMark} ct mark set ct mark or ${toString arz.filterMark}";
|
||||
};
|
||||
appZapretUserRuleStop = mkUserRuleStop {
|
||||
name = "app-zapret";
|
||||
nftFamily = "inet";
|
||||
nftTable = "proxy_suite_app_zapret_mark";
|
||||
nftChain = "output";
|
||||
};
|
||||
|
||||
appTproxyUpScript = pkgs.writeShellScript "proxy-suite-app-tproxy-up" ''
|
||||
set -euo pipefail
|
||||
${nft} delete table ip proxy_suite_app_tproxy 2>/dev/null || true
|
||||
@@ -437,104 +480,6 @@ ${lib.concatMapStrings (cidr: ''
|
||||
${ip} route del local default dev lo table ${toString artp.routeTable} 2>/dev/null || true
|
||||
${ip} rule del fwmark ${toString artp.fwmark} table ${toString artp.routeTable} 2>/dev/null || true
|
||||
'';
|
||||
appTproxyUserRuleStart =
|
||||
pkgs.writeShellScript "proxy-suite-app-tproxy-user-start" ''
|
||||
set -euo pipefail
|
||||
uid="$1"
|
||||
rule_comment_prefix="proxy-suite-app-tproxy-user-$uid"
|
||||
mark_comment="$rule_comment_prefix-mark"
|
||||
cgroup_root="/sys/fs/cgroup/user.slice/user-$uid.slice/user@$uid.service"
|
||||
if ! [ -d "$cgroup_root" ]; then
|
||||
echo "proxy-suite: user cgroup root does not exist for uid $uid: $cgroup_root" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
cgroup_dir=$(${findBin} "$cgroup_root" -type d -name ${lib.escapeShellArg appTproxySliceName} | ${headBin} -n1 || true)
|
||||
if [ -z "$cgroup_dir" ]; then
|
||||
echo "proxy-suite: app TProxy slice cgroup does not exist for uid $uid under $cgroup_root" >&2
|
||||
exit 1
|
||||
fi
|
||||
cgroup_path=''${cgroup_dir#/sys/fs/cgroup/}
|
||||
cgroup_level=$(printf '%s' "$cgroup_path" | ${awk} -F/ '{ print NF }')
|
||||
|
||||
handles=$(${nft} -a list chain ip proxy_suite_app_tproxy output 2>/dev/null \
|
||||
| ${grepBin} -F "comment \"$rule_comment_prefix" \
|
||||
| ${awk} '{ print $NF }' || true)
|
||||
if [ -n "$handles" ]; then
|
||||
while IFS= read -r handle; do
|
||||
[ -n "$handle" ] || continue
|
||||
${nft} delete rule ip proxy_suite_app_tproxy output handle "$handle" || true
|
||||
done <<< "$handles"
|
||||
fi
|
||||
|
||||
printf '%s\n' \
|
||||
"add rule ip proxy_suite_app_tproxy output socket cgroupv2 level $cgroup_level \"$cgroup_path\" meta mark set ${toString artp.fwmark} ct mark set ${toString artp.fwmark} comment \"$mark_comment\"" \
|
||||
| ${nft} -f -
|
||||
'';
|
||||
appTproxyUserRuleStop =
|
||||
pkgs.writeShellScript "proxy-suite-app-tproxy-user-stop" ''
|
||||
set -euo pipefail
|
||||
uid="$1"
|
||||
rule_comment_prefix="proxy-suite-app-tproxy-user-$uid"
|
||||
handles=$(${nft} -a list chain ip proxy_suite_app_tproxy output 2>/dev/null \
|
||||
| ${grepBin} -F "comment \"$rule_comment_prefix" \
|
||||
| ${awk} '{ print $NF }' || true)
|
||||
if [ -n "$handles" ]; then
|
||||
while IFS= read -r handle; do
|
||||
[ -n "$handle" ] || continue
|
||||
${nft} delete rule ip proxy_suite_app_tproxy output handle "$handle" || true
|
||||
done <<< "$handles"
|
||||
fi
|
||||
'';
|
||||
appZapretUserRuleStart =
|
||||
pkgs.writeShellScript "proxy-suite-app-zapret-user-start" ''
|
||||
set -euo pipefail
|
||||
uid="$1"
|
||||
rule_comment_prefix="proxy-suite-app-zapret-user-$uid"
|
||||
mark_comment="$rule_comment_prefix-mark"
|
||||
cgroup_root="/sys/fs/cgroup/user.slice/user-$uid.slice/user@$uid.service"
|
||||
if ! [ -d "$cgroup_root" ]; then
|
||||
echo "proxy-suite: user cgroup root does not exist for uid $uid: $cgroup_root" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
cgroup_dir=$(${findBin} "$cgroup_root" -type d -name ${lib.escapeShellArg appZapretSliceName} | ${headBin} -n1 || true)
|
||||
if [ -z "$cgroup_dir" ]; then
|
||||
echo "proxy-suite: app zapret slice cgroup does not exist for uid $uid under $cgroup_root" >&2
|
||||
exit 1
|
||||
fi
|
||||
cgroup_path=''${cgroup_dir#/sys/fs/cgroup/}
|
||||
cgroup_level=$(printf '%s' "$cgroup_path" | ${awk} -F/ '{ print NF }')
|
||||
|
||||
handles=$(${nft} -a list chain inet proxy_suite_app_zapret_mark output 2>/dev/null \
|
||||
| ${grepBin} -F "comment \"$rule_comment_prefix" \
|
||||
| ${awk} '{ print $NF }' || true)
|
||||
if [ -n "$handles" ]; then
|
||||
while IFS= read -r handle; do
|
||||
[ -n "$handle" ] || continue
|
||||
${nft} delete rule inet proxy_suite_app_zapret_mark output handle "$handle" || true
|
||||
done <<< "$handles"
|
||||
fi
|
||||
|
||||
printf '%s\n' \
|
||||
"add rule inet proxy_suite_app_zapret_mark output socket cgroupv2 level $cgroup_level \"$cgroup_path\" meta mark set meta mark or ${toString arz.filterMark} ct mark set ct mark or ${toString arz.filterMark} comment \"$mark_comment\"" \
|
||||
| ${nft} -f -
|
||||
'';
|
||||
appZapretUserRuleStop =
|
||||
pkgs.writeShellScript "proxy-suite-app-zapret-user-stop" ''
|
||||
set -euo pipefail
|
||||
uid="$1"
|
||||
rule_comment_prefix="proxy-suite-app-zapret-user-$uid"
|
||||
handles=$(${nft} -a list chain inet proxy_suite_app_zapret_mark output 2>/dev/null \
|
||||
| ${grepBin} -F "comment \"$rule_comment_prefix" \
|
||||
| ${awk} '{ print $NF }' || true)
|
||||
if [ -n "$handles" ]; then
|
||||
while IFS= read -r handle; do
|
||||
[ -n "$handle" ] || continue
|
||||
${nft} delete rule inet proxy_suite_app_zapret_mark output handle "$handle" || true
|
||||
done <<< "$handles"
|
||||
fi
|
||||
'';
|
||||
|
||||
proxyCtl = pkgs.writeShellApplication {
|
||||
name = "proxy-ctl";
|
||||
@@ -560,11 +505,8 @@ ${lib.concatMapStrings (cidr: ''
|
||||
APP_ROUTING_PROFILES_FILE="${appRoutingProfilesFile}"
|
||||
PROXYCHAINS_CONFIG="${proxychainsConfigFile}"
|
||||
APP_TUN_SLICE_BASE="proxy-suite-app-tun"
|
||||
APP_TUN_ANCHOR_UNIT="proxy-suite-app-tun-anchor.service"
|
||||
APP_TPROXY_SLICE_BASE="proxy-suite-app-tproxy"
|
||||
APP_TPROXY_ANCHOR_UNIT="proxy-suite-app-tproxy-anchor.service"
|
||||
APP_ZAPRET_SLICE_BASE="proxy-suite-app-zapret"
|
||||
APP_ZAPRET_ANCHOR_UNIT="proxy-suite-app-zapret-anchor.service"
|
||||
|
||||
ALL_SERVICES=(
|
||||
proxy-suite-socks
|
||||
@@ -645,34 +587,56 @@ ${lib.concatMapStrings (cidr: ''
|
||||
${jq} -r --arg name "$profile" '.[] | select(.name == $name) | .route' "$APP_ROUTING_PROFILES_FILE"
|
||||
}
|
||||
|
||||
_tun_scope_running() {
|
||||
${systemctl} --user list-units --type=scope --state=running --plain --no-legend 'proxy-suite-app-tun-*' \
|
||||
_scope_running() {
|
||||
${systemctl} --user list-units --type=scope --state=running --plain --no-legend "$1-*" \
|
||||
| ${grepBin} -q .
|
||||
}
|
||||
|
||||
_any_app_tun_user_active() {
|
||||
${systemctl} list-units --type=service --state=active --plain --no-legend 'proxy-suite-app-tun-user@*.service' \
|
||||
_any_user_active() {
|
||||
${systemctl} list-units --type=service --state=active --plain --no-legend "$1*.service" \
|
||||
| ${grepBin} -q .
|
||||
}
|
||||
|
||||
_tproxy_scope_running() {
|
||||
${systemctl} --user list-units --type=scope --state=running --plain --no-legend 'proxy-suite-app-tproxy-*' \
|
||||
| ${grepBin} -q .
|
||||
_check_no_global_proxy() {
|
||||
if ${systemctl} is-active --quiet proxy-suite-tun.service; then
|
||||
echo "Global proxy-suite-tun.service is active. Stop it before using route=$1 profiles."
|
||||
exit 1
|
||||
fi
|
||||
if ${systemctl} is-active --quiet proxy-suite-tproxy.service; then
|
||||
echo "Global proxy-suite-tproxy.service is active. Stop it before using route=$1 profiles."
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
_any_app_tproxy_user_active() {
|
||||
${systemctl} list-units --type=service --state=active --plain --no-legend 'proxy-suite-app-tproxy-user@*.service' \
|
||||
| ${grepBin} -q .
|
||||
}
|
||||
# _wrap_slice <slice_base> <enabled_var> <disabled_msg> <backend_svc> [cmd...]
|
||||
_wrap_slice() {
|
||||
local slice_base="$1" enabled="$2" disabled_msg="$3" backend_svc="$4"
|
||||
shift 4
|
||||
if [ "$enabled" != "1" ]; then
|
||||
echo "$disabled_msg"
|
||||
exit 1
|
||||
fi
|
||||
local uid; uid="$(${idBin} -u)"
|
||||
local scope_unit="$slice_base-''${profile}-$$"
|
||||
local anchor_unit="$slice_base-anchor.service"
|
||||
local user_svc="$slice_base-user@$uid.service"
|
||||
|
||||
_zapret_scope_running() {
|
||||
${systemctl} --user list-units --type=scope --state=running --plain --no-legend 'proxy-suite-app-zapret-*' \
|
||||
| ${grepBin} -q .
|
||||
}
|
||||
${systemctl} --user start "$anchor_unit"
|
||||
${systemctl} start "$backend_svc"
|
||||
${systemctl} start "$user_svc"
|
||||
|
||||
_any_app_zapret_user_active() {
|
||||
${systemctl} list-units --type=service --state=active --plain --no-legend 'proxy-suite-app-zapret-user@*.service' \
|
||||
| ${grepBin} -q .
|
||||
local status=0
|
||||
${systemdRun} --user --scope --quiet --collect --same-dir \
|
||||
--slice="$slice_base" --unit="$scope_unit" "$@" || status=$?
|
||||
|
||||
if ! _scope_running "$slice_base"; then
|
||||
${systemctl} stop "$user_svc" || true
|
||||
${systemctl} --user stop "$anchor_unit" || true
|
||||
if ! _any_user_active "$slice_base-user@"; then
|
||||
${systemctl} stop "$backend_svc" || true
|
||||
fi
|
||||
fi
|
||||
exit "$status"
|
||||
}
|
||||
|
||||
cmd="''${1:-status}"
|
||||
@@ -825,116 +789,21 @@ ${lib.concatMapStrings (cidr: ''
|
||||
exec ${proxychains4} ${proxychainsQuietArg}-f "$PROXYCHAINS_CONFIG" "$@"
|
||||
;;
|
||||
tun)
|
||||
if [ "$APP_ROUTING_TUN_ENABLED" != "1" ]; then
|
||||
echo "Profile '$profile' uses route=tun, but appRouting.backends.tun.enable is false."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
uid="$(${idBin} -u)"
|
||||
scope_unit="proxy-suite-app-tun-$profile-$$"
|
||||
|
||||
${systemctl} --user start "$APP_TUN_ANCHOR_UNIT"
|
||||
${systemctl} start proxy-suite-app-tun.service
|
||||
${systemctl} start "proxy-suite-app-tun-user@$uid.service"
|
||||
|
||||
if ${systemdRun} --user --scope --quiet --collect --same-dir \
|
||||
--slice="$APP_TUN_SLICE_BASE" \
|
||||
--unit="$scope_unit" \
|
||||
"$@"; then
|
||||
status=0
|
||||
else
|
||||
status=$?
|
||||
fi
|
||||
|
||||
if ! _tun_scope_running; then
|
||||
${systemctl} stop "proxy-suite-app-tun-user@$uid.service" || true
|
||||
${systemctl} --user stop "$APP_TUN_ANCHOR_UNIT" || true
|
||||
if ! _any_app_tun_user_active; then
|
||||
${systemctl} stop proxy-suite-app-tun.service || true
|
||||
fi
|
||||
fi
|
||||
|
||||
exit "$status"
|
||||
_wrap_slice "$APP_TUN_SLICE_BASE" "$APP_ROUTING_TUN_ENABLED" \
|
||||
"Profile '$profile' uses route=tun, but appRouting.backends.tun.enable is false." \
|
||||
"proxy-suite-app-tun.service" "$@"
|
||||
;;
|
||||
tproxy)
|
||||
if [ "$APP_ROUTING_TPROXY_ENABLED" != "1" ]; then
|
||||
echo "Profile '$profile' uses route=tproxy, but appRouting.backends.tproxy.enable is false."
|
||||
exit 1
|
||||
fi
|
||||
if ${systemctl} is-active --quiet proxy-suite-tun.service; then
|
||||
echo "Global proxy-suite-tun.service is active. Stop it before using route=tproxy profiles."
|
||||
exit 1
|
||||
fi
|
||||
if ${systemctl} is-active --quiet proxy-suite-tproxy.service; then
|
||||
echo "Global proxy-suite-tproxy.service is active. Stop it before using route=tproxy profiles."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
uid="$(${idBin} -u)"
|
||||
scope_unit="proxy-suite-app-tproxy-$profile-$$"
|
||||
|
||||
${systemctl} --user start "$APP_TPROXY_ANCHOR_UNIT"
|
||||
${systemctl} start proxy-suite-app-tproxy.service
|
||||
${systemctl} start "proxy-suite-app-tproxy-user@$uid.service"
|
||||
|
||||
if ${systemdRun} --user --scope --quiet --collect --same-dir \
|
||||
--slice="$APP_TPROXY_SLICE_BASE" \
|
||||
--unit="$scope_unit" \
|
||||
"$@"; then
|
||||
status=0
|
||||
else
|
||||
status=$?
|
||||
fi
|
||||
|
||||
if ! _tproxy_scope_running; then
|
||||
${systemctl} stop "proxy-suite-app-tproxy-user@$uid.service" || true
|
||||
${systemctl} --user stop "$APP_TPROXY_ANCHOR_UNIT" || true
|
||||
if ! _any_app_tproxy_user_active; then
|
||||
${systemctl} stop proxy-suite-app-tproxy.service || true
|
||||
fi
|
||||
fi
|
||||
|
||||
exit "$status"
|
||||
_check_no_global_proxy tproxy
|
||||
_wrap_slice "$APP_TPROXY_SLICE_BASE" "$APP_ROUTING_TPROXY_ENABLED" \
|
||||
"Profile '$profile' uses route=tproxy, but appRouting.backends.tproxy.enable is false." \
|
||||
"proxy-suite-app-tproxy.service" "$@"
|
||||
;;
|
||||
zapret)
|
||||
if [ "$APP_ROUTING_ZAPRET_ENABLED" != "1" ]; then
|
||||
echo "Profile '$profile' uses route=zapret, but the app-zapret backend or zapret service is not enabled."
|
||||
exit 1
|
||||
fi
|
||||
if ${systemctl} is-active --quiet proxy-suite-tun.service; then
|
||||
echo "Global proxy-suite-tun.service is active. Stop it before using route=zapret profiles."
|
||||
exit 1
|
||||
fi
|
||||
if ${systemctl} is-active --quiet proxy-suite-tproxy.service; then
|
||||
echo "Global proxy-suite-tproxy.service is active. Stop it before using route=zapret profiles."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
uid="$(${idBin} -u)"
|
||||
scope_unit="proxy-suite-app-zapret-$profile-$$"
|
||||
|
||||
${systemctl} --user start "$APP_ZAPRET_ANCHOR_UNIT"
|
||||
${systemctl} start proxy-suite-app-zapret.service
|
||||
${systemctl} start "proxy-suite-app-zapret-user@$uid.service"
|
||||
|
||||
if ${systemdRun} --user --scope --quiet --collect --same-dir \
|
||||
--slice="$APP_ZAPRET_SLICE_BASE" \
|
||||
--unit="$scope_unit" \
|
||||
"$@"; then
|
||||
status=0
|
||||
else
|
||||
status=$?
|
||||
fi
|
||||
|
||||
if ! _zapret_scope_running; then
|
||||
${systemctl} stop "proxy-suite-app-zapret-user@$uid.service" || true
|
||||
${systemctl} --user stop "$APP_ZAPRET_ANCHOR_UNIT" || true
|
||||
if ! _any_app_zapret_user_active; then
|
||||
${systemctl} stop proxy-suite-app-zapret.service || true
|
||||
fi
|
||||
fi
|
||||
|
||||
exit "$status"
|
||||
_check_no_global_proxy zapret
|
||||
_wrap_slice "$APP_ZAPRET_SLICE_BASE" "$APP_ROUTING_ZAPRET_ENABLED" \
|
||||
"Profile '$profile' uses route=zapret, but the app-zapret backend or zapret service is not enabled." \
|
||||
"proxy-suite-app-zapret.service" "$@"
|
||||
;;
|
||||
*)
|
||||
echo "Route backend '$route' is not implemented in this build."
|
||||
@@ -982,6 +851,17 @@ ${lib.concatMapStrings (cidr: ''
|
||||
'';
|
||||
};
|
||||
|
||||
mkAnchorService = sliceName: desc: {
|
||||
description = desc;
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
RemainAfterExit = true;
|
||||
Slice = sliceName;
|
||||
ExecStart = "${pkgs.coreutils}/bin/true";
|
||||
ExecStop = "${pkgs.coreutils}/bin/true";
|
||||
};
|
||||
};
|
||||
|
||||
in
|
||||
{
|
||||
environment.systemPackages = [ proxyCtl ];
|
||||
@@ -1013,40 +893,16 @@ in
|
||||
|
||||
systemd.user.services =
|
||||
lib.optionalAttrs art.enable {
|
||||
proxy-suite-app-tun-anchor = {
|
||||
description = "Anchor service for proxy-suite app TUN slice";
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
RemainAfterExit = true;
|
||||
Slice = appTunSliceName;
|
||||
ExecStart = "${pkgs.coreutils}/bin/true";
|
||||
ExecStop = "${pkgs.coreutils}/bin/true";
|
||||
};
|
||||
};
|
||||
proxy-suite-app-tun-anchor =
|
||||
mkAnchorService appTunSliceName "Anchor service for proxy-suite app TUN slice";
|
||||
}
|
||||
// lib.optionalAttrs artp.enable {
|
||||
proxy-suite-app-tproxy-anchor = {
|
||||
description = "Anchor service for proxy-suite app TProxy slice";
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
RemainAfterExit = true;
|
||||
Slice = appTproxySliceName;
|
||||
ExecStart = "${pkgs.coreutils}/bin/true";
|
||||
ExecStop = "${pkgs.coreutils}/bin/true";
|
||||
};
|
||||
};
|
||||
proxy-suite-app-tproxy-anchor =
|
||||
mkAnchorService appTproxySliceName "Anchor service for proxy-suite app TProxy slice";
|
||||
}
|
||||
// lib.optionalAttrs (arz.enable && cfg.zapret.enable) {
|
||||
proxy-suite-app-zapret-anchor = {
|
||||
description = "Anchor service for proxy-suite app zapret slice";
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
RemainAfterExit = true;
|
||||
Slice = appZapretSliceName;
|
||||
ExecStart = "${pkgs.coreutils}/bin/true";
|
||||
ExecStop = "${pkgs.coreutils}/bin/true";
|
||||
};
|
||||
};
|
||||
proxy-suite-app-zapret-anchor =
|
||||
mkAnchorService appZapretSliceName "Anchor service for proxy-suite app zapret slice";
|
||||
};
|
||||
|
||||
assertions = [
|
||||
@@ -1226,7 +1082,6 @@ in
|
||||
]) sb.subscriptions;
|
||||
|
||||
systemd.services = {
|
||||
# Always-on: SOCKS5/HTTP mixed inbound, also ready for TProxy interception.
|
||||
proxy-suite-socks = {
|
||||
description = "sing-box proxy client (SOCKS + TProxy-ready)";
|
||||
after = [ "network.target" ];
|
||||
@@ -1241,9 +1096,6 @@ in
|
||||
};
|
||||
}
|
||||
// lib.optionalAttrs sb.tproxy.enable {
|
||||
# Opt-in transparent proxy. Start/stop with:
|
||||
# systemctl start proxy-suite-tproxy
|
||||
# systemctl stop proxy-suite-tproxy
|
||||
proxy-suite-tproxy = {
|
||||
description = "sing-box TProxy – nftables rules and policy routing";
|
||||
after = [
|
||||
@@ -1274,9 +1126,6 @@ in
|
||||
};
|
||||
}
|
||||
// lib.optionalAttrs sb.tun.enable {
|
||||
# Opt-in TUN mode (full tunnel, no nftables needed). Start/stop with:
|
||||
# systemctl start proxy-suite-tun
|
||||
# systemctl stop proxy-suite-tun
|
||||
proxy-suite-tun = {
|
||||
description = "sing-box TUN proxy client";
|
||||
after = [ "network-online.target" ];
|
||||
|
||||
Reference in new issue
Block a user