per-app tun

This commit is contained in:
FUFSoB committed 2026-04-14 18:17:27 +05:00
1 parent 181f3355c1
commit 14557eba82
6 files changed
+630 -50

No files matched your search

+72 -37
View File
@@ -9,14 +9,36 @@
let
sb = cfg.singBox;
art = cfg.appRouting.backends.tun;
direct = rules.direct;
mkDnsServer =
tag: upstream: detour: {
inherit tag detour;
tag: upstream: detour:
{
inherit tag;
type = upstream.type;
server = upstream.address;
server_port = upstream.port;
}
// lib.optionalAttrs (detour != null) { inherit detour; };
mkDnsConfig =
{ localDetour ? null }:
{
servers = [
(mkDnsServer "remote" sb.dns.remote "proxy")
(mkDnsServer "local" sb.dns.local localDetour)
];
rules =
lib.optional (builtins.elem "google" sb.routing.proxy.geosites) {
rule_set = [ "geosite-google" ];
server = "remote";
}
++ lib.optional (direct.geosites != [ ]) {
rule_set = map (s: "geosite-${s}") direct.geosites;
server = "local";
};
final = if sb.proxyByDefault then "remote" else "local";
};
clashApiBlock = lib.optionalAttrs (sb.selection != "first") {
@@ -27,42 +49,30 @@ let
};
};
dnsConfig = {
servers = [
(mkDnsServer "remote" sb.dns.remote "proxy")
(
# In TUN mode, routing DNS through direct can conflict with auto_redirect.
# In TProxy/mixed mode, direct is fine for local traffic.
mkDnsServer "local" sb.dns.local "direct"
)
];
rules =
lib.optional (builtins.elem "google" sb.routing.proxy.geosites) {
rule_set = [ "geosite-google" ];
server = "remote";
}
++ lib.optional (direct.geosites != [ ]) {
rule_set = map (s: "geosite-${s}") direct.geosites;
server = "local";
};
final = if sb.proxyByDefault then "remote" else "local";
};
dnsConfigTun = dnsConfig // {
servers = map (s: if s.tag == "local" then s // { detour = "proxy"; } else s) dnsConfig.servers;
};
mkConfig =
{
enableMixed ? false,
enableTProxy ? false,
enableTun ? false,
tunInterface ? sb.tun.interface,
tunAddress ? sb.tun.address,
tunMtu ? sb.tun.mtu,
tunAutoRoute ? true,
tunAutoRedirect ? true,
tunStrictRoute ? true,
forceLocalDnsViaProxy ? false,
useOutboundRoutingMark ? false,
enableClashApi ? true,
}:
{
log.level = "warn";
dns = if enableTun then dnsConfigTun else dnsConfig;
# Keep "local" DNS on the direct path by default, but avoid explicit
# detour="direct" because sing-box rejects detouring to an empty
# direct outbound. Global TUN can force local DNS through proxy.
dns = mkDnsConfig {
localDetour = if forceLocalDnsViaProxy then "proxy" else null;
};
inbounds =
lib.optional enableMixed {
@@ -80,12 +90,12 @@ let
++ lib.optional enableTun {
type = "tun";
tag = "tun-in";
interface_name = sb.tun.interface;
address = [ sb.tun.address ];
mtu = sb.tun.mtu;
auto_route = true;
auto_redirect = true;
strict_route = true;
interface_name = tunInterface;
address = [ tunAddress ];
mtu = tunMtu;
auto_route = tunAutoRoute;
auto_redirect = tunAutoRedirect;
strict_route = tunStrictRoute;
stack = "mixed";
};
@@ -110,24 +120,49 @@ let
rules = rules.routingRules;
final = if sb.proxyByDefault then "proxy" else "direct";
}
// lib.optionalAttrs enableTun { auto_detect_interface = true; };
// lib.optionalAttrs (enableTun && tunAutoRoute) { auto_detect_interface = true; };
}
// clashApiBlock;
// lib.optionalAttrs enableClashApi clashApiBlock;
tproxyTemplate = mkConfig {
enableMixed = true;
enableTProxy = true;
useOutboundRoutingMark = true;
enableClashApi = true;
};
tunTemplate = mkConfig {
enableTun = true;
tunInterface = sb.tun.interface;
tunAddress = sb.tun.address;
tunMtu = sb.tun.mtu;
tunAutoRoute = true;
tunAutoRedirect = true;
tunStrictRoute = true;
forceLocalDnsViaProxy = true;
enableClashApi = false;
};
appTunTemplate = mkConfig {
enableTun = true;
tunInterface = art.interface;
tunAddress = art.address;
tunMtu = art.mtu;
tunAutoRoute = false;
tunAutoRedirect = false;
tunStrictRoute = false;
forceLocalDnsViaProxy = false;
# App TUN can be used alongside TProxy; mark direct egress so TProxy output
# rules do not re-intercept sing-box's own packets.
useOutboundRoutingMark = sb.tproxy.enable;
enableClashApi = false;
};
tproxyFile = pkgs.writeText "proxy-suite-tproxy-template.json" (builtins.toJSON tproxyTemplate);
tunFile = pkgs.writeText "proxy-suite-tun-template.json" (builtins.toJSON tunTemplate);
appTunFile = pkgs.writeText "proxy-suite-app-tun-template.json" (builtins.toJSON appTunTemplate);
in
{
inherit tproxyFile tunFile;
inherit tproxyFile tunFile appTunFile;
}
+1 -1
View File
@@ -49,7 +49,7 @@ in
pkgs
cfg
;
inherit (configs) tproxyFile tunFile;
inherit (configs) tproxyFile tunFile appTunFile;
inherit (nftr) nftablesRulesFile ip nft;
}
))
+2 -1
View File
@@ -7,6 +7,7 @@
let
sb = cfg.singBox;
art = cfg.appRouting.backends.tun;
localSubnetLines = lib.concatMapStrings (cidr: ''
ip daddr ${cidr} tcp dport != 53 return
@@ -42,7 +43,7 @@ let
ip daddr $RESERVED_IP return
${localSubnetLines}
meta mark ${toString sb.proxyMark} return
ip protocol tcp meta mark set ${toString sb.fwmark}
${lib.optionalString art.enable " meta mark ${toString art.fwmark} return\n"} ip protocol tcp meta mark set ${toString sb.fwmark}
ip protocol udp meta mark set ${toString sb.fwmark}
}
}
+96 -7
View File
@@ -299,6 +299,7 @@ let
type = types.enum [
"direct"
"proxychains"
"tun"
];
default = "proxychains";
description = ''
@@ -307,6 +308,8 @@ let
- "direct": run the command unchanged.
- "proxychains": run the command through proxychains-ng using the
local proxy-suite mixed SOCKS endpoint.
- "tun": launch the command in the dedicated app-routing TUN slice so
only that app's traffic is policy-routed into the app TUN backend.
Additional route backends may be added in the future.
'';
@@ -547,8 +550,8 @@ in
Whether traffic that does not match any explicit routing rule should
go through the proxy or go direct.
This affects both sing-box route.final and the default DNS resolver
choice in the generated config.
This affects sing-box route.final and dns.final in the generated
config.
'';
example = true;
};
@@ -565,9 +568,10 @@ in
DNS upstream used for the built-in "local" resolver role.
This resolver is also used as sing-box route.default_domain_resolver.
The module keeps detour policy automatic: in mixed/TProxy mode,
"local" uses direct; in TUN mode, it is forced through the proxy to
avoid auto_redirect conflicts.
The module keeps detour policy automatic: in mixed/TProxy mode and
app-routing TUN mode, "local" stays on the direct path (without an
explicit detour); in global TUN mode, it is forced through the proxy to avoid
auto_redirect conflicts.
'';
example = {
type = "tcp";
@@ -815,9 +819,11 @@ in
Current curated defaults:
- `proxychains`: route = "proxychains"
- `tun`: route = "tun" when appRouting.backends.tun.enable = true
This makes `proxy-ctl wrap proxychains -- <command>` available
without defining the profile manually.
without defining the profile manually, and similarly exposes
`proxy-ctl wrap tun -- <command>` when the app TUN backend is enabled.
'';
example = true;
};
@@ -832,9 +838,12 @@ in
- "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
proxychains-based wrapping depends on the local proxy-suite mixed
proxy listener provided by sing-box.
proxy listener provided by sing-box. The "tun" route depends on
appRouting.backends.tun.enable = true.
When createDefaultProfiles = true, curated defaults are added on top
of this list unless a user-defined profile already uses the same
@@ -876,6 +885,86 @@ in
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"
];
};
};
};
userControl = {
group = mkOption {
type = types.strMatching "^[a-z_][a-z0-9_-]*$";
default = "proxy-suite";
description = ''
Local group allowed to start and stop app-routing backend units via
polkit. Add desktop users who should be able to run
`proxy-ctl wrap ...` for route = "tun" profiles to this group.
'';
example = "proxy-suite";
};
};
};
zapret = {
+264 -2
View File
@@ -6,6 +6,7 @@
cfg,
tproxyFile,
tunFile,
appTunFile,
nftablesRulesFile,
ip,
nft,
@@ -15,6 +16,7 @@ let
sb = cfg.singBox;
t = cfg.tgWsProxy;
ar = cfg.appRouting;
art = ar.backends.tun;
builtinTags = [
"proxy"
"direct"
@@ -31,6 +33,16 @@ let
python3 = "${pkgs.python3}/bin/python3";
singBox = "${pkgs.sing-box}/bin/sing-box";
proxychains4 = "${pkgs.proxychains-ng}/bin/proxychains4";
systemdRun = "${pkgs.systemd}/bin/systemd-run";
systemctl = "${pkgs.systemd}/bin/systemctl";
journalctl = "${pkgs.systemd}/bin/journalctl";
idBin = "${pkgs.coreutils}/bin/id";
grepBin = "${pkgs.gnugrep}/bin/grep";
awk = "${pkgs.gawk}/bin/awk";
sleepBin = "${pkgs.coreutils}/bin/sleep";
headBin = "${pkgs.coreutils}/bin/head";
seqBin = "${pkgs.coreutils}/bin/seq";
findBin = "${pkgs.findutils}/bin/find";
fetchSubscriptionPy = ../../scripts/fetch-subscription.py;
@@ -192,6 +204,15 @@ let
routingMark = null;
};
startAppTun = mkStartScript {
name = "proxy-suite-start-app-tun";
runtimeDir = "/run/proxy-suite-app-tun";
configFile = appTunFile;
# Only needed when TProxy mode is enabled; otherwise avoid tagging app TUN
# proxy outbounds with a host-global mark.
routingMark = if sb.tproxy.enable then sb.proxyMark else null;
};
clashApi = "http://127.0.0.1:${toString sb.clashApiPort}";
selection = sb.selection;
@@ -244,6 +265,11 @@ let
name = "proxychains";
route = "proxychains";
}
] ++ lib.optionals art.enable [
{
name = "tun";
route = "tun";
}
];
effectiveAppRoutingProfiles =
ar.profiles
@@ -264,13 +290,118 @@ let
'';
proxychainsQuietArg = lib.optionalString ar.proxychains.quiet "-q ";
hasProxychainsProfiles = builtins.any (profile: profile.route == "proxychains") effectiveAppRoutingProfiles;
hasTunProfiles = builtins.any (profile: profile.route == "tun") effectiveAppRoutingProfiles;
appTunSliceName = "proxy-suite-app-tun.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
if ${ip} link show dev ${lib.escapeShellArg art.interface} >/dev/null 2>&1; then
exit 0
fi
${sleepBin} 0.1
done
echo "proxy-suite: app TUN interface ${art.interface} did not appear in time" >&2
exit 1
'';
appTunUpScript = pkgs.writeShellScript "proxy-suite-app-tun-up" ''
set -euo pipefail
${nft} delete table inet proxy_suite_app_tun 2>/dev/null || true
${nft} -f ${appTunChainFile}
${appTunWaitForInterface}
${ip} route replace default dev ${lib.escapeShellArg art.interface} table ${toString art.routeTable}
${ip} rule add fwmark ${toString art.fwmark} table ${toString art.routeTable} 2>/dev/null || true
'';
appTunDownScript = pkgs.writeShellScript "proxy-suite-app-tun-down" ''
set -euo pipefail
${nft} delete table inet proxy_suite_app_tun 2>/dev/null || true
${ip} route del default dev ${lib.escapeShellArg 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" ''
set -euo pipefail
uid="$1"
rule_comment_prefix="proxy-suite-app-tun-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 appTunSliceName} | ${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
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 \
| ${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
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\"" \
| ${nft} -f -
'';
appTunUserRuleStop =
pkgs.writeShellScript "proxy-suite-app-tun-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 \
| ${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
done <<< "$handles"
fi
'';
proxyCtl = pkgs.writeShellApplication {
name = "proxy-ctl";
runtimeInputs = with pkgs; [
coreutils
curl
gawk
gnugrep
jq
proxychains-ng
systemd
];
text = ''
# Embedded at build time from module config
@@ -279,8 +410,11 @@ let
SUB_TAGS=(${subscriptionTagsList})
APP_ROUTING_ENABLED="${if ar.enable then "1" else "0"}"
APP_ROUTING_PROXYCHAINS_ENABLED="${if ar.proxychains.enable then "1" else "0"}"
APP_ROUTING_TUN_ENABLED="${if art.enable then "1" else "0"}"
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"
ALL_SERVICES=(
proxy-suite-socks
@@ -331,6 +465,16 @@ let
${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-*' \
| ${grepBin} -q .
}
_any_app_tun_user_active() {
${systemctl} list-units --type=service --state=active --plain --no-legend 'proxy-suite-app-tun-user@*.service' \
| ${grepBin} -q .
}
cmd="''${1:-status}"
shift || true
@@ -447,6 +591,38 @@ let
fi
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"
;;
*)
echo "Route backend '$route' is not implemented in this build."
exit 1
@@ -498,7 +674,45 @@ in
environment.systemPackages = [ proxyCtl ];
# nftables must be on for TProxy to work.
networking.nftables.enable = lib.mkIf sb.tproxy.enable (lib.mkDefault true);
networking.nftables.enable = lib.mkIf (sb.tproxy.enable || art.enable) (lib.mkDefault true);
users.groups = lib.mkIf art.enable {
"${ar.userControl.group}" = { };
};
security.polkit.enable = lib.mkIf art.enable true;
security.polkit.extraConfig = lib.mkIf art.enable (lib.mkAfter ''
polkit.addRule(function(action, subject) {
if (!subject.isInGroup("${ar.userControl.group}")) {
return null;
}
if (action.id !== "org.freedesktop.systemd1.manage-units") {
return null;
}
var unit = action.lookup("unit");
if (unit === "proxy-suite-app-tun.service" ||
unit.indexOf("proxy-suite-app-tun-user@") === 0) {
return polkit.Result.YES;
}
return null;
});
'');
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";
};
};
};
assertions = [
{
@@ -530,13 +744,33 @@ in
message = "proxy-suite: appRouting.proxychains.enable requires appRouting.enable = true";
}
{
assertion = builtins.length appRoutingProfileNames == builtins.length (lib.unique appRoutingProfileNames);
assertion = builtins.length effectiveAppRoutingProfileNames == builtins.length (lib.unique effectiveAppRoutingProfileNames);
message = "proxy-suite: appRouting profile names must be unique";
}
{
assertion = !hasProxychainsProfiles || ar.proxychains.enable;
message = "proxy-suite: route=proxychains in appRouting.profiles requires appRouting.proxychains.enable = true";
}
{
assertion = !art.enable || ar.enable;
message = "proxy-suite: appRouting.backends.tun.enable requires appRouting.enable = true";
}
{
assertion = !(hasTunProfiles && !art.enable);
message = "proxy-suite: route=tun in appRouting profiles requires appRouting.backends.tun.enable = true";
}
{
assertion = !(art.enable && sb.tproxy.enable && art.fwmark == sb.fwmark);
message = "proxy-suite: appRouting.backends.tun.fwmark must differ from singBox.fwmark when singBox.tproxy.enable = true";
}
{
assertion = !(art.enable && sb.tproxy.enable && art.fwmark == sb.proxyMark);
message = "proxy-suite: appRouting.backends.tun.fwmark must differ from singBox.proxyMark when singBox.tproxy.enable = true";
}
{
assertion = !(art.enable && sb.tproxy.enable && art.routeTable == sb.routeTable);
message = "proxy-suite: appRouting.backends.tun.routeTable must differ from singBox.routeTable when singBox.tproxy.enable = true";
}
{
assertion =
!t.enable
@@ -640,6 +874,34 @@ in
};
};
}
// lib.optionalAttrs art.enable {
proxy-suite-app-tun = {
description = "sing-box app-routing TUN backend";
after = [ "network-online.target" ];
wants = [ "network-online.target" ];
serviceConfig = {
ExecStart = "${startAppTun}";
ExecStartPost = "${appTunUpScript}";
ExecStopPost = "${appTunDownScript}";
Restart = "on-failure";
RestartSec = 5;
RuntimeDirectory = "proxy-suite-app-tun";
StateDirectory = "proxy-suite";
};
};
"proxy-suite-app-tun-user@" = {
description = "Enable proxy-suite app TUN marking for user %i";
requires = [ "proxy-suite-app-tun.service" ];
after = [ "proxy-suite-app-tun.service" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = "${appTunUserRuleStart} %i";
ExecStop = "${appTunUserRuleStop} %i";
};
};
}
// lib.optionalAttrs hasSubscriptions {
proxy-suite-subscription-update = {
description = "Refresh proxy-suite subscription caches";