fix: hardening and fixes

This commit is contained in:
fufsob committed 2026-09-15 08:56:33 +00:00
1 parent aa19042ee5
commit a82417a2bd
32 files changed
+619 -256

No files matched your search

+9
View File
@@ -134,6 +134,15 @@ let
in
lib.mkMerge [
autoProxyUnits
{
# See constants.serviceUser.
users.users.${constants.serviceUser} = {
isSystemUser = true;
group = constants.serviceUser;
description = "proxy-suite daemons";
};
users.groups.${constants.serviceUser} = { };
}
{
environment.systemPackages = [
control.proxyCtl
@@ -29,6 +29,8 @@ let
subscriptionsFile = "${runtimeDir}/subscriptions.json";
subsCfg = proxyInboundsCfg.subscriptions;
xray = "${proxyInboundsCfg.package}/bin/xray";
# Only listeners: no marks, no transparent sockets.
runXray = constants.runAsServiceUser pkgs [ "net_bind_service" ];
needsLocalProxyAuth = proxyInboundsNeedLocalProxy && localProxyAuthEnabled;
@@ -144,13 +146,39 @@ let
(.outbounds[] | select(.tag == "proxy") | .settings.servers[0].users)
= [{user:$user,pass:$password}]
else . end' \
${proxyInboundsFile} > "$RUNTIME_DIR/config.json"
chmod 600 "$RUNTIME_DIR/config.json"
${proxyInboundsFile} > "$RUNTIME_DIR/config.json.tmp"
# XRay runs as ${constants.serviceUser}. A certificate or key it cannot read (an ACME
# key, say) is copied in, which means a renewal needs a restart; give the user read
# access to keep XRay's own reload.
CERT_DIR="$RUNTIME_DIR/tls"
rm -rf "$CERT_DIR"
index=0
while IFS= read -r source; do
[ -n "$source" ] || continue
if ! ${runXray} ${pkgs.coreutils}/bin/test -r "$source"; then
[ -d "$CERT_DIR" ] || install -d -m 0750 -g ${constants.serviceUser} "$CERT_DIR"
index=$((index + 1))
copy="$CERT_DIR/$index-$(basename "$source")"
install -m 0640 -g ${constants.serviceUser} "$source" "$copy"
echo "proxy-suite: ${constants.serviceUser} cannot read $source; XRay uses a copy until the next restart" >&2
${jq} --arg from "$source" --arg to "$copy" '
(.inbounds[].streamSettings.tlsSettings.certificates[]? | (.certificateFile, .keyFile)
| select(. == $from)) = $to
' "$RUNTIME_DIR/config.json.tmp" > "$RUNTIME_DIR/config.json.next"
mv "$RUNTIME_DIR/config.json.next" "$RUNTIME_DIR/config.json.tmp"
fi
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"
mv "$RUNTIME_DIR/config.json.tmp" "$RUNTIME_DIR/config.json"
${writeLinksBlock}
${writeSubscriptionsBlock}
exec ${xray} run -c "$RUNTIME_DIR/config.json"
exec ${runXray} ${xray} run -c "$RUNTIME_DIR/config.json"
'';
# Adds XRay's per-user counters to the daily totals, read and reset in one call.
@@ -5,6 +5,7 @@
proxyCfg,
sshProxyCfg,
warpCfg,
constants,
pureXrayEnabled,
hybridEnabled,
collapseNamedOutbounds,
@@ -305,9 +306,6 @@ let
server_port = sshProxyCfg.server.port;
user = sshProxyCfg.server.user;
}
// lib.optionalAttrs (sshProxyCfg.identityFile != null) {
private_key_path = sshProxyCfg.identityFile;
}
// lib.optionalAttrs (sshProxyCfg.hostKey != [ ]) {
host_key = sshProxyCfg.hostKey;
}
@@ -355,6 +353,13 @@ let
''
# outbound: ${sshProxyTag} (${if pureXrayEnabled then "OpenSSH SOCKS5 listener" else "native SSH"})
OB_JSON=$(cat "${jsonFile}")
${lib.optionalString (!pureXrayEnabled && sshProxyCfg.identityFile != null) ''
# sing-box opens the key as ${constants.serviceUser}, which cannot read a key in a
# home directory: it gets a copy only root and its group can read.
SSH_IDENTITY="$RUNTIME_DIR/ssh-identity"
install -m 0640 -g ${constants.serviceUser} ${lib.escapeShellArg sshProxyCfg.identityFile} "$SSH_IDENTITY"
OB_JSON=$(${jq} --arg key "$SSH_IDENTITY" '.private_key_path = $key' <<< "$OB_JSON")
''}
${hostKeyFileBlock}
${
if hybridEnabled then
+1
View File
@@ -111,6 +111,7 @@ let
proxyCfg
sshProxyCfg
warpCfg
constants
pureXrayEnabled
hybridEnabled
collapseNamedOutbounds
+14 -8
View File
@@ -40,6 +40,7 @@ let
;
autoProxyRender = import ../autoproxy-render.nix { inherit pkgs; };
runBackend = constants.runAsServiceUser pkgs constants.backendCaps;
routeModeBlacklistTail =
if pureXrayEnabled then
@@ -205,7 +206,8 @@ let
AUTOPROXY_RULES_JSON='[]'
${lib.optionalString enableAutoProxy ''
AUTOPROXY_DIR=${lib.escapeShellArg autoProxyStateDir}
install -d -m 0750 "$AUTOPROXY_DIR"
# 0751: sing-box, running as ${constants.serviceUser}, reads the rule-sets inside.
install -d -m 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
@@ -312,6 +314,7 @@ let
CUTOFF_RULE_SET=${lib.escapeShellArg "${constants.zapret2CutoffDir}/proxy.json"}
install -d -m 0755 "$(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" \
'. + [{type: "local", format: "source", tag: "zapret-cutoff", path: $p}]' <<< "$AUTOPROXY_RULE_SETS_JSON")
AUTOPROXY_RULES_JSON=$(${jq} -c '. + [{rule_set: ["zapret-cutoff"], outbound: "proxy"}]' <<< "$AUTOPROXY_RULES_JSON")
@@ -340,9 +343,12 @@ let
--arg xray_dns_remote_client ${lib.escapeShellArg proxyCfg.dns.remote.address} \
-f "$BACKEND_JQ_FILTER" \
"${configFile}" > "$RUNTIME_DIR/config.json"
${lib.optionalString enableLocalProxyAuth ''
chmod 600 "$RUNTIME_DIR/config.json"
''}
# The backend runs as ${constants.serviceUser}: its configs are group-readable.
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"
done
${
if hybridEnabled then
@@ -358,7 +364,7 @@ let
trap _proxy_suite_cleanup_xray_sidecar EXIT
trap 'exit 143' INT TERM
${xray} run -c "$RUNTIME_DIR/xray-sidecar.json" &
${runBackend} ${xray} run -c "$RUNTIME_DIR/xray-sidecar.json" &
XRAY_SIDECAR_PID="$!"
${pkgs.coreutils}/bin/sleep 0.2
if ! kill -0 "$XRAY_SIDECAR_PID" 2>/dev/null; then
@@ -367,7 +373,7 @@ let
exit "$XRAY_SIDECAR_STATUS"
fi
${singBox} run -c "$RUNTIME_DIR/config.json" &
${runBackend} ${singBox} run -c "$RUNTIME_DIR/config.json" &
SING_BOX_PID="$!"
# Either one going away breaks the outbounds: exit, and let systemd restart both.
SING_BOX_STATUS=0
@@ -375,11 +381,11 @@ let
exit "$(( SING_BOX_STATUS == 0 ? 1 : SING_BOX_STATUS ))"
fi
exec ${singBox} run -c "$RUNTIME_DIR/config.json"
exec ${runBackend} ${singBox} run -c "$RUNTIME_DIR/config.json"
''
else
''
exec ${backendBin} run -c "$RUNTIME_DIR/config.json"
exec ${runBackend} ${backendBin} run -c "$RUNTIME_DIR/config.json"
''
}
'';