fix: root actions in tui/gui & userControl

This commit is contained in:
fufsob committed 2026-09-15 20:02:43 +00:00
1 parent 2ce8da3f43
commit 9cdd6dada7
32 files changed
+665 -317

No files matched your search

+12 -6
View File
@@ -7,10 +7,13 @@
cfg,
proxyCtl,
autoProxyStateDir,
userControlAllows,
}:
let
apCfg = cfg.proxy.autoProxy;
# The autoProxy scope's group writes the directory: `proxy auto learn` queues there.
stateDirMode = if userControlAllows "autoProxy" then "0771" else "0751";
render = import ./autoproxy-render.nix { inherit pkgs; };
jqFile =
path:
@@ -52,7 +55,7 @@ let
echo "sing-box has no Clash API (selection = \"first\"?); nothing to sample"
exit 0
fi
install -d -m 0751 "$state_dir"
install -d -m ${stateDirMode} "$state_dir"
# Eleven snapshots a second apart: ten one-second deltas per connection.
lines=$(
@@ -86,7 +89,7 @@ let
echo "no probe listeners - is proxy-suite-socks running with autoProxy on?"
exit 0
fi
install -d -m 0751 "$state_dir"
install -d -m ${stateDirMode} "$state_dir"
# Timer runs and learn requests take turns on the state.
exec 9> "$state_dir/lock"
@@ -97,6 +100,8 @@ let
if [ -e "$requests.taking" ]; then
cat "$requests.taking" >> "$requests"
rm -f "$requests.taking"
# Made here by root: the group appends to it too.
chmod 0660 "$requests"
fi
if [ -e "$state" ] && ! jq -e 'type == "object"' "$state" > /dev/null 2>&1; then
@@ -392,16 +397,17 @@ let
${render} "$index" "$state"
'';
# Group: `proxy-ctl proxy auto list|queue` reads state.json. Set on every unit that
# declares the directory - systemd re-applies the ownership on each start.
# Group: `proxy-ctl proxy auto list|queue` reads state.json, `learn` queues requests.
# Set on every unit that declares the directory - systemd re-applies the ownership
# on each start.
stateDirConfig = {
StateDirectory = "proxy-suite/autoproxy";
# 0751 and a 027 umask: sing-box (proxy-suite) can open the rule-sets, which are
# made 0644, and nothing else.
StateDirectoryMode = "0751";
StateDirectoryMode = stateDirMode;
UMask = "0027";
}
// lib.optionalAttrs (cfg.userControl.allow != [ ]) { Group = cfg.userControl.group; };
// lib.optionalAttrs (userControlAllows "autoProxy") { Group = cfg.userControl.group; };
mkUnit = description: args: {
inherit description;
+6 -1
View File
@@ -192,7 +192,11 @@ let
# The cut-off networks no whitelisted name fixes go through the proxy outbound.
zapretCutoffProxyFallback =
zapretCutoffEnabled && zapretCfg.zapret2.cutoff.proxyFallback && hasAvailableOutbounds;
userControlEnabled = userControlCfg.allow != [ ];
userControlEnabled = userControlCfg.enable;
# Whether the group holds a scope; no scopes listed means all of them.
userControlAllows =
scope:
userControlEnabled && (userControlCfg.scopes == [ ] || builtins.elem scope userControlCfg.scopes);
constants = {
# Daemons (sing-box, XRay, the WARP tunnel, OpenSSH, tg-ws-proxy, wgcf) run as this
# user. Its group is not the userControl group: backend configs hold credentials. Start
@@ -333,6 +337,7 @@ in
zapretCutoffProxyFallback
userControlCfg
userControlEnabled
userControlAllows
sshProxyCfg
sshProxyOutboundTag
sshProxyOutboundEnabled
+1 -1
View File
@@ -36,7 +36,7 @@ in
};
openFirewall = bool true "Open every non-loopback listener port.";
shareLinks = bool true "Write client share links for `proxy-ctl inbounds link` (root and userControl.group only).";
shareLinks = bool true "Write client share links for `proxy-ctl inbounds link` (root, and userControl.group with the secrets scope).";
subscriptions = {
enable = mkEnableOption "" // {
+3 -2
View File
@@ -66,8 +66,9 @@ in
(rm "tgWsProxy.logBackups" "Use services.proxy-suite.tgWsProxy.log.keep.")
# userControl.
(rm "userControl.global.enable" ''Use services.proxy-suite.userControl.allow, which lists "global" by default.'')
(rm "userControl.perApp.enable" ''Use services.proxy-suite.userControl.allow, which lists "perApp" by default.'')
(rm "userControl.global.enable" ''Use services.proxy-suite.userControl.enable, with "services" in userControl.scopes.'')
(rm "userControl.perApp.enable" ''Use services.proxy-suite.userControl.enable, with "perApp" in userControl.scopes.'')
(rm "userControl.allow" "Use services.proxy-suite.userControl.enable, and userControl.scopes to limit it.")
]
++ lib.mapAttrsToList (
old: new: rm "zapret.${old}" "Use services.proxy-suite.zapret.zapret-discord-youtube.${new}."
+23 -10
View File
@@ -5,30 +5,43 @@ let
in
{
options.services.proxy-suite.userControl = {
enable = lib.mkEnableOption "passwordless `proxy-ctl` control for the members of userControl.group";
group = mkOption {
type = types.strMatching "^[a-z_][a-z0-9_-]*$";
default = "proxy-suite";
description = "Group whose members may run privileged `proxy-ctl` commands without a password.";
};
allow = mkOption {
scopes = mkOption {
type = types.listOf (
types.enum [
"global"
"services"
"perApp"
"routing"
"outbounds"
"secrets"
"autoProxy"
"zapret"
"stats"
]
);
default = [
"global"
"perApp"
];
default = [ ];
description = ''
What userControl.group may control without a password:
- "global": the global units (proxy, tun, tproxy, zapret, restart, subs update).
What userControl.group may do; empty allows every scope.
- "services": turn the proxy-suite units on and off (proxy, tun, tproxy, zapret, ssh, warp, tg, awg, inbounds), and `restart`.
- "perApp": the per-app backend units used by `proxy-ctl apps run`.
Empty grants no passwordless control.
- "routing": `proxy pin`, `proxy unpin` and `proxy mode`.
- "outbounds": add and remove runtime outbounds and subscriptions, and update subscriptions.
- "secrets": read share links, subscription URLs and the running configs.
- "autoProxy": read what autoProxy learned, and `proxy auto learn`.
- "zapret": edit zapret2's learned hosts, and `zapret cutoff probe`.
- "stats": `inbounds stats`.
'';
example = [ "global" ];
example = [
"services"
"routing"
];
};
};
}
@@ -84,6 +84,9 @@ let
(mkAssertion (cfg.userControl.group != derived.constants.serviceUser)
"proxy-suite: userControl.group must not be ${derived.constants.serviceUser}, the group that can read backend configs"
)
(mkAssertion (cfg.userControl.scopes == [ ] || cfg.userControl.enable)
"proxy-suite: userControl.scopes is set but userControl.enable is false, so the group gets nothing"
)
(requireEnabled (cfg.warp.enable && cfg.warp.asOutbound != null) proxyEnabled
"proxy-suite: warp.asOutbound requires proxy.enable = true"
)
+3 -5
View File
@@ -56,7 +56,7 @@ let
proxyInboundsNeedLocalProxy
proxyInboundsGuardPrivate
proxyInboundViaOutbounds
userControlEnabled
userControlAllows
;
# Tool paths – defined once here and passed into sub-modules as needed.
@@ -88,9 +88,7 @@ let
builders = import ./builders.nix { inherit lib pkgs; };
polkit = import ./polkit.nix {
inherit lib cfg userControlCfg;
};
polkit = import ./polkit.nix { inherit userControlCfg; };
scripts = import ./scripts.nix {
inherit
@@ -135,7 +133,7 @@ let
proxyInboundsNeedLocalProxy
proxyInboundsGuardPrivate
proxyInboundViaOutbounds
userControlEnabled
userControlAllows
builders
;
};
+36 -19
View File
@@ -62,6 +62,7 @@ let
perAppRoutingTproxy
userControlCfg
userControlEnabled
userControlAllows
perAppZapretEnabled
sshProxyOutboundEnabled
sshProxyUnitEnabled
@@ -129,6 +130,7 @@ let
inherit lib pkgs cfg;
inherit (control) proxyCtl;
inherit (constants) autoProxyStateDir;
inherit userControlAllows;
}
);
in
@@ -170,36 +172,51 @@ lib.mkMerge [
"${userControlCfg.group}" = { };
};
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.enable = lib.mkIf (cfg.enable && (userControlEnabled || cfg.gui.enable)) true;
# The GUI's "Retry as Root" and root toggle run proxy-ctl through pkexec: one
# admin password then covers the next few minutes, as sudo's does in the TUI.
security.polkit.extraConfig = lib.mkMerge [
(lib.mkIf (cfg.enable && cfg.gui.enable) (
lib.mkAfter ''
polkit.addRule(function(action, subject) {
if (action.id === "org.freedesktop.policykit.exec" &&
action.lookup("program") === "${control.proxyCtl}/bin/proxy-ctl") {
return polkit.Result.AUTH_ADMIN_KEEP;
}
return null;
}
});
''
))
(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}
if (action.id !== "org.freedesktop.systemd1.manage-units") {
return null;
}
var unit = action.lookup("unit");
${polkit.userControlPolkitRules}
return null;
});
''
);
});
''
))
];
# Spool dirs for outbounds and subscriptions added at runtime. Setgid so the
# files proxy-ctl drops here inherit the group; without userControl only root
# writes them. tmpfiles rather than the start scripts, so the group can add an
# files proxy-ctl drops here inherit the group; without the outbounds scope
# only root writes them. tmpfiles rather than the start scripts, so the group can add an
# outbound before the proxy has ever run.
systemd.tmpfiles.rules = lib.mkIf (cfg.enable && proxyEnabled) (
map
(
dir:
"d ${dir} ${
if userControlEnabled then "2770 root ${userControlCfg.group}" else "0700 root root"
if userControlAllows "outbounds" then "2770 root ${userControlCfg.group}" else "0700 root root"
} -"
)
[
+30 -21
View File
@@ -1,26 +1,35 @@
# Polkit rules granting proxy-suite group members passwordless control over services.
{
lib,
cfg,
userControlCfg,
}:
# Polkit rule body granting userControl group members passwordless control over the
# units of their scopes.
{ userControlCfg }:
let
userControlEnabled = userControlCfg.allow != [ ];
userControlPolkitRules =
lib.optionalString (builtins.elem "perApp" userControlCfg.allow) ''
if (unit.indexOf("proxy-suite-per-app-") === 0) {
return polkit.Result.YES;
}
''
+ lib.optionalString (builtins.elem "global" userControlCfg.allow) ''
if (unit.indexOf("proxy-suite-") === 0 &&
unit.indexOf("proxy-suite-per-app-") !== 0) {
return polkit.Result.YES;
}
'';
# Units a narrower scope starts; every other proxy-suite unit is "services".
scopeByUnitPrefix = {
"proxy-suite-per-app-" = "perApp";
"proxy-suite-outbound-pin@" = "routing";
"proxy-suite-outbound-unpin." = "routing";
"proxy-suite-route-mode@" = "routing";
"proxy-suite-outbound-reload." = "outbounds";
"proxy-suite-subscription-update." = "outbounds";
"proxy-suite-autoproxy-learn." = "autoProxy";
"proxy-suite-zapret2-cutoff." = "zapret";
"proxy-suite-inbound-stats." = "stats";
};
in
{
inherit userControlEnabled userControlPolkitRules;
userControlPolkitRules = ''
var scopeByUnitPrefix = ${builtins.toJSON scopeByUnitPrefix};
var scope = unit.indexOf("proxy-suite-") === 0 ? "services" : null;
for (var prefix in scopeByUnitPrefix) {
if (unit.indexOf(prefix) === 0) {
scope = scopeByUnitPrefix[prefix];
break;
}
}
// No scopes listed means all of them.
var scopes = ${builtins.toJSON userControlCfg.scopes};
if (scope !== null && (scopes.length === 0 || scopes.indexOf(scope) !== -1)) {
return polkit.Result.YES;
}
'';
}
@@ -8,7 +8,7 @@
proxyInboundsNeedLocalProxy,
proxyInboundViaOutbounds,
userControlCfg,
userControlEnabled,
userControlAllows,
localProxyAuth,
localProxyAuthEnabled,
localProxyAuthPasswordSource,
@@ -91,10 +91,10 @@ let
writeLinksBlock = lib.optionalString proxyInboundsCfg.shareLinks ''
${jq} -c '.links' <<< "$RENDERED" > "${linksFile}"
${lib.optionalString userControlEnabled ''
${lib.optionalString (userControlAllows "secrets") ''
${pkgs.coreutils}/bin/chgrp ${lib.escapeShellArg userControlCfg.group} "${linksFile}"
''}
chmod ${if userControlEnabled then "640" else "600"} "${linksFile}"
chmod ${if userControlAllows "secrets" then "640" else "600"} "${linksFile}"
'';
# One file per user for the web server, plus a token index for proxy-ctl. Filled aside
@@ -113,10 +113,10 @@ let
rm -rf "$SUB_DIR"
mv "$SUB_DIR.new" "$SUB_DIR"
${jq} -c '[.subscriptions[] | {user, token}]' <<< "$RENDERED" > "${subscriptionsFile}"
${lib.optionalString userControlEnabled ''
${lib.optionalString (userControlAllows "secrets") ''
${pkgs.coreutils}/bin/chgrp ${lib.escapeShellArg userControlCfg.group} "${subscriptionsFile}"
''}
chmod ${if userControlEnabled then "640" else "600"} "${subscriptionsFile}"
chmod ${if userControlAllows "secrets" then "640" else "600"} "${subscriptionsFile}"
'';
startInbounds = pkgs.writeShellScript "proxy-suite-inbounds" ''
@@ -177,8 +177,20 @@ let
done < <(${jq} -r '[.inbounds[].streamSettings.tlsSettings.certificates[]? | (.certificateFile, .keyFile) | strings]
| unique[]' "$RUNTIME_DIR/config.json.tmp")
${pkgs.coreutils}/bin/chgrp ${constants.serviceUser} "$RUNTIME_DIR/config.json.tmp"
chmod 640 "$RUNTIME_DIR/config.json.tmp"
# Read by XRay as ${constants.serviceUser}, and by the userControl group: the daemon
# as owner then, since a file has one group.
${
if userControlAllows "secrets" then
''
${pkgs.coreutils}/bin/chown ${constants.serviceUser}:${lib.escapeShellArg userControlCfg.group} "$RUNTIME_DIR/config.json.tmp"
chmod 440 "$RUNTIME_DIR/config.json.tmp"
''
else
''
${pkgs.coreutils}/bin/chgrp ${constants.serviceUser} "$RUNTIME_DIR/config.json.tmp"
chmod 640 "$RUNTIME_DIR/config.json.tmp"
''
}
mv "$RUNTIME_DIR/config.json.tmp" "$RUNTIME_DIR/config.json"
${writeLinksBlock}
@@ -213,11 +225,11 @@ let
path = ../inbound-stats-add.jq;
}
} "$file" > "$tmp"
${lib.optionalString userControlEnabled ''
${lib.optionalString (userControlAllows "stats") ''
${pkgs.coreutils}/bin/chgrp ${lib.escapeShellArg userControlCfg.group} "$tmp" ||
echo "proxy-suite: group ${userControlCfg.group} cannot read the stats; they stay root-only" >&2
''}
chmod ${if userControlEnabled then "640" else "600"} "$tmp"
chmod ${if userControlAllows "stats" then "640" else "600"} "$tmp"
mv -f "$tmp" "$file"
'';
in
+3 -2
View File
@@ -32,7 +32,7 @@
proxyInboundsNeedLocalProxy,
proxyInboundsGuardPrivate,
proxyInboundViaOutbounds,
userControlEnabled,
userControlAllows,
buildInboundPy,
proxyInboundsFile,
proxyInboundsSpecFile,
@@ -162,6 +162,7 @@ let
proxyCfg
perAppRoutingCfg
userControlCfg
userControlAllows
globalTproxy
xrayEnabled
hybridEnabled
@@ -199,7 +200,7 @@ let
proxyInboundsNeedLocalProxy
proxyInboundViaOutbounds
userControlCfg
userControlEnabled
userControlAllows
localProxyAuth
localProxyAuthEnabled
localProxyAuthPasswordSource
+27 -9
View File
@@ -5,6 +5,7 @@
proxyCfg,
perAppRoutingCfg,
userControlCfg,
userControlAllows,
globalTproxy,
xrayEnabled,
hybridEnabled,
@@ -41,6 +42,8 @@ let
autoProxyRender = import ../autoproxy-render.nix { inherit pkgs; };
runBackend = constants.runAsServiceUser pkgs constants.backendCaps;
userControlGroup = lib.escapeShellArg userControlCfg.group;
chgrp = "${pkgs.coreutils}/bin/chgrp";
routeModeBlacklistTail =
if pureXrayEnabled then
@@ -207,8 +210,9 @@ let
AUTOPROXY_RULES_JSON='[]'
${lib.optionalString enableAutoProxy ''
AUTOPROXY_DIR=${lib.escapeShellArg autoProxyStateDir}
# 0751: sing-box, running as ${constants.serviceUser}, reads the rule-sets inside.
install -d -m 0751 "$AUTOPROXY_DIR"
# 0751: sing-box, running as ${constants.serviceUser}, reads the rule-sets inside;
# 0771 with userControl's autoProxy scope, whose group queues learn requests there.
install -d -m ${if userControlAllows "autoProxy" then "0771" else "0751"} "$AUTOPROXY_DIR"
[ -s "$AUTOPROXY_DIR/state.json" ] || echo '{"domains":{},"hosts":{},"exits":{},"backlog":{}}' > "$AUTOPROXY_DIR/state.json"
# direct is always exit 0; state is keyed by tag, so shifting indices are
@@ -263,7 +267,7 @@ let
' <<< "$OUTBOUNDS_JSON" > "$ENDPOINTS_TMP"
# Where each proxy server is: not credentials, but not for every local user either.
${
if userControlCfg.allow != [ ] || localProxyAuthEnabled then
if userControlCfg.enable || localProxyAuthEnabled then
''
${pkgs.coreutils}/bin/chgrp ${lib.escapeShellArg userControlCfg.group} "$ENDPOINTS_TMP"
chmod 640 "$ENDPOINTS_TMP"
@@ -274,7 +278,7 @@ let
mv "$ENDPOINTS_TMP" "$RUNTIME_DIR/outbound-endpoints.json"
# proxy-ctl's share links: the URL each outbound was given, and its backend JSON.
# Credentials, so root only, whatever userControl allows.
# Credentials: root and the userControl group only.
SHARE_TMP="$RUNTIME_DIR/outbound-share.json.tmp"
(umask 077 && ${jq} -c --argjson sidecar "''${XRAY_OUTBOUNDS_JSON:-[]}" --arg collapsed "''${PROXY_TAG:-}" \
--argjson urls "$OUTBOUND_URLS_JSON" --argjson subs "$SUBSCRIPTION_URLS_JSON" '
@@ -285,7 +289,8 @@ let
| from_entries),
subscriptions: $subs}
' <<< "$OUTBOUNDS_JSON" > "$SHARE_TMP")
chmod 600 "$SHARE_TMP"
${lib.optionalString (userControlAllows "secrets") ''${chgrp} ${userControlGroup} "$SHARE_TMP"''}
chmod ${if userControlAllows "secrets" then "640" else "600"} "$SHARE_TMP"
mv "$SHARE_TMP" "$RUNTIME_DIR/outbound-share.json"
${lib.optionalString (!pureXrayEnabled) ''
@@ -313,7 +318,8 @@ let
# rule-set path and reloads the file when the probe renames a new one in.
if [ "$ROUTE_MODE" != all-bypass ]; then
CUTOFF_RULE_SET=${lib.escapeShellArg "${constants.zapret2CutoffDir}/proxy.json"}
install -d -m 0755 "$(dirname "$CUTOFF_RULE_SET")"
# The cutoff unit owns the directory's mode (the group asks for probes there).
mkdir -p "$(dirname "$CUTOFF_RULE_SET")"
[ -s "$CUTOFF_RULE_SET" ] || echo '{"version":1,"rules":[]}' > "$CUTOFF_RULE_SET"
chmod 644 "$CUTOFF_RULE_SET"
AUTOPROXY_RULE_SETS_JSON=$(${jq} -c --arg p "$CUTOFF_RULE_SET" \
@@ -353,11 +359,23 @@ let
"$RUNTIME_DIR/config.json" > "$RUNTIME_DIR/config.json.next"
mv "$RUNTIME_DIR/config.json.next" "$RUNTIME_DIR/config.json"
''}
# The backend runs as ${constants.serviceUser}: its configs are group-readable.
# Credentials, read by the backend running as ${constants.serviceUser}. With
# userControl its group reads them too; a file has one group, so the daemon
# reads as owner then.
for backend_config in "$RUNTIME_DIR/config.json" "$RUNTIME_DIR/xray-sidecar.json"; do
[ -e "$backend_config" ] || continue
${pkgs.coreutils}/bin/chgrp ${constants.serviceUser} "$backend_config"
chmod ${if enableLocalProxyAuth then "640" else "g+r"} "$backend_config"
${
if userControlAllows "secrets" then
''
${pkgs.coreutils}/bin/chown ${constants.serviceUser}:${userControlGroup} "$backend_config"
chmod 440 "$backend_config"
''
else
''
${chgrp} ${constants.serviceUser} "$backend_config"
chmod 640 "$backend_config"
''
}
done
FAKE_IP_CACHE=$(${jq} -r '.experimental.cache_file.path? // empty' "$RUNTIME_DIR/config.json")
if [ -n "$FAKE_IP_CACHE" ]; then
+6 -3
View File
@@ -16,7 +16,7 @@ let
zapretCfg = cfg.zapret;
perAppZapretCfg = cfg.perAppRouting.zapret;
cutoffCfg = zapretCfg.zapret2.cutoff;
inherit (import ./derived.nix { inherit lib cfg; }) constants;
inherit (import ./derived.nix { inherit lib cfg; }) constants userControlAllows;
# Global AmneziaWG profiles only: an outbound one leaves the host's routes alone.
awgServiceNames = map (name: "proxy-suite-awg-${name}.service") (
builtins.attrNames (lib.filterAttrs (_: profile: profile.asOutbound == null) cfg.amneziaWg.profiles)
@@ -71,10 +71,13 @@ let
if tunInterfaces != [ ] then runtime.mkCustomScript { inherit tunInterfaces; } else null;
};
# nfqws2 and proxy-ctl expect the list files to exist.
# nfqws2 and proxy-ctl expect the list files to exist. With userControl its group
# edits them: proxy-ctl renames a new list in, so the directories are what it writes.
mkPreStart = ''
${lib.getExe' pkgs.kmod "modprobe"} nfnetlink_queue 2>/dev/null || true
install -d -m 0755 ${runtime.stateDir} ${runtime.circularStateDir}
install -d -m ${
if userControlAllows "zapret" then "2775 -g ${lib.escapeShellArg cfg.userControl.group}" else "0755"
} ${runtime.stateDir} ${runtime.circularStateDir}
touch ${runtime.autoHostlistFile} ${runtime.userHostlistFile} ${runtime.excludeHostlistFile}
'';
+6 -1
View File
@@ -12,7 +12,7 @@
}:
let
inherit (import ../derived.nix { inherit lib cfg; }) constants;
inherit (import ../derived.nix { inherit lib cfg; }) constants userControlAllows;
z2k = zapret2Sources.z2k;
lists = "${z2k}/files/lists";
dir = constants.zapret2CutoffDir;
@@ -156,6 +156,11 @@ in
ExecStartPre = "${exempt}";
ExecStart = "${probe}";
ExecStopPost = "-${nft} delete table inet ${table}";
}
# The group's `proxy-ctl zapret cutoff probe` drops its `force` file here.
// lib.optionalAttrs (userControlAllows "zapret") {
Group = cfg.userControl.group;
StateDirectoryMode = "2775";
};
};