fix: hardening

This commit is contained in:
FUFSoB committed 2026-10-05 22:44:09 +05:00
1 parent 017a947a18
commit a435942b4f
168 files changed
+9166 -1644

No files matched your search

+53 -21
View File
@@ -25,9 +25,14 @@ let
# Past TUN and TProxy. Setting a mark takes CAP_NET_ADMIN, which a rootless host lacks,
# and has nothing to capture its traffic anyway.
mark = if cfg.host.privileged then cfg.proxy.tproxy.proxyMark else null;
# The tunnel's own listener takes the hop login ($HOP_PASSWORD), from curl's stdin.
probe = port: url: ''
${pkgs.curl}/bin/curl -s --noproxy "" -x socks5h://127.0.0.1:${toString port} -m "$timeout" \
-o /dev/null ${url}'';
printf 'proxy-user = "${derived.constants.hopUser}:%s"\n' "$HOP_PASSWORD" | ${pkgs.curl}/bin/curl -K - -s --noproxy "" \
-x socks5h://127.0.0.1:${toString port} -m "$timeout" -o /dev/null ${url}'';
# direct-in takes the login the start drew ($DIRECT_AUTH), from curl's stdin: argv is public.
probeDirect = port: url: ''
printf 'proxy-user = "probe:%s"\n' "$DIRECT_AUTH" | ${pkgs.curl}/bin/curl -K - -s --noproxy "" \
-x socks5h://127.0.0.1:${toString port} -m "$timeout" -o /dev/null ${url}'';
# Some lines drop a share of fresh handshakes for good, and both engines retry on the
# same source port forever. A new process binds a new port, so the watchdog exits when
@@ -36,10 +41,9 @@ let
# gets three misses. sing-box's "direct-in" listener reaches Cloudflare through the uplink
# with the backend's mark (not urlTest.url, which is picked to be blocked here): when that
# fails too, the tunnel could not work from a new port either. wireproxy has no such
# listener (directPort = null), so it is restarted either way.
# listener (directPort = null), so it is restarted either way. The tag is $TUNNEL_TAG.
mkWatchdog =
{
tag,
command,
tunnelPort,
directPort,
@@ -57,13 +61,13 @@ let
kill -0 "$tunnel" 2>/dev/null || { wait "$tunnel"; exit 1; }
timeout=$(( healthy ? 10 : 4 ))
if ${probe tunnelPort "-f ${lib.escapeShellArg cfg.proxy.urlTest.url}"}; then
(( healthy )) || echo "proxy-suite: ${tag} is answering" >&2
(( healthy )) || echo "proxy-suite: $TUNNEL_TAG is answering" >&2
healthy=1 failures=0 uplink_down=0
continue
fi
${lib.optionalString (directPort != null) ''
if ! ${probe directPort "https://1.1.1.1/cdn-cgi/trace"}; then
(( uplink_down )) || echo "proxy-suite: the uplink is down; waiting for it before judging ${tag}" >&2
if ! ${probeDirect directPort "https://1.1.1.1/cdn-cgi/trace"}; then
(( uplink_down )) || echo "proxy-suite: the uplink is down; waiting for it before judging $TUNNEL_TAG" >&2
uplink_down=1 failures=0 since=$SECONDS
continue
fi
@@ -71,10 +75,10 @@ let
uplink_down=0
# proxy-suite-outbound-groups hears of the first miss: a failover group moves off it now.
if (( healthy && failures == 0 )) && [[ -d ${hintDir} ]]; then
${pkgs.coreutils}/bin/touch ${hintDir}/${lib.escapeShellArg tag} 2>/dev/null || true
${pkgs.coreutils}/bin/touch "${hintDir}/$TUNNEL_TAG" 2>/dev/null || true
fi
if (( healthy ? ++failures >= 3 : SECONDS - since >= 15 )); then
echo "proxy-suite: ${tag} is not answering; restarting the tunnel on a new source port" >&2
echo "proxy-suite: $TUNNEL_TAG is not answering; restarting the tunnel on a new source port" >&2
kill "$tunnel"
wait "$tunnel"
exit 1
@@ -88,7 +92,6 @@ let
# rather than baked into the store; the engine itself runs as ${serviceUser}.
singBoxEngine =
{
tag,
tunnelPort,
directPort,
endpoint,
@@ -96,15 +99,19 @@ let
}:
{
convert = ''
endpoint=$(${pkgs.python3}/bin/python3 ${scriptsDir}/warp_outbound.py --tag ${tag}${markArg "--routing-mark"}${
endpoint=$(${pkgs.python3}/bin/python3 ${scriptsDir}/warp_outbound.py --tag "$TUNNEL_TAG"${markArg "--routing-mark"}${
lib.optionalString (endpoint != null) " --endpoint ${lib.escapeShellArg endpoint}"
} < "$profile")
# direct-in gets past TProxy and the kill switch: a login drawn per start keeps other local
# users off it (read from the environment, as argv is public).
DIRECT_AUTH=$(${pkgs.coreutils}/bin/od -An -tx1 -N16 /dev/urandom | ${pkgs.coreutils}/bin/tr -d ' \n')
export DIRECT_AUTH
# From a pipe: the endpoint holds the private key, and argv is public.
(umask 027 && ${pkgs.jq}/bin/jq -n --slurpfile ep <(printf '%s' "$endpoint") '$ep[0] as $ep | {
(umask 027 && ${pkgs.jq}/bin/jq -n --arg tag "$TUNNEL_TAG" --slurpfile ep <(printf '%s' "$endpoint") '$ep[0] as $ep | {
log: {level: "warn"},
dns: {servers: [
${builtins.toJSON (dnsServer "local" cfg.proxy.dns.local)},
(${builtins.toJSON (dnsServer "remote" cfg.proxy.dns.remote)} + {detour: "${tag}", domain_resolver: "local"})
(${builtins.toJSON (dnsServer "remote" cfg.proxy.dns.remote)} + {detour: $tag, domain_resolver: "local"})
]},
route: {
default_domain_resolver: ${
@@ -119,11 +126,13 @@ let
)
},
rules: [{inbound: ["direct-in"], outbound: "direct"}],
final: "${tag}"
final: $tag
},
inbounds: [
{type: "socks", tag: "socks-in", listen: "127.0.0.1", listen_port: ${toString tunnelPort}},
{type: "socks", tag: "direct-in", listen: "127.0.0.1", listen_port: ${toString directPort}}
{type: "socks", tag: "socks-in", listen: "127.0.0.1", listen_port: ${toString tunnelPort},
users: [{username: "${derived.constants.hopUser}", password: $ENV.HOP_PASSWORD}]},
{type: "socks", tag: "direct-in", listen: "127.0.0.1", listen_port: ${toString directPort},
users: [{username: "probe", password: $ENV.DIRECT_AUTH}]}
],
outbounds: [{type: "direct", tag: "direct"${
lib.optionalString (mark != null) ", routing_mark: ${toString mark}"
@@ -154,13 +163,13 @@ let
# `endpoint`, a host:port in place of the profile's Endpoint, and `domainStrategy`, the
# address family it dials destinations over first.
#
# A template unit passes `tag` and `tunnelPort` as shell words ("$TUNNEL_TAG") that
# `profile` exports: the watchdog runs as a script of its own, after the exec.
# The tag comes from $TUNNEL_TAG, never spliced in (an attribute name can hold anything). A
# template unit passes `tag = null` and `tunnelPort = "$TUNNEL_PORT"`, both from `profile`.
mkTunnel =
{
description,
unit,
tag,
tag ? null,
profile,
tunnelPort,
directPort ? null,
@@ -176,7 +185,6 @@ let
let
chosen = (if engine == "userspace" then wireproxyEngine else singBoxEngine) {
inherit
tag
tunnelPort
directPort
endpoint
@@ -185,7 +193,14 @@ let
};
tunnelScript = pkgs.writeShellScript "proxy-suite-wg-tunnel" ''
set -euo pipefail
${lib.optionalString (tag != null) "TUNNEL_TAG=${lib.escapeShellArg tag}"}
${profile}
export TUNNEL_TAG
# The hop login (constants.ensureHopLogin): the backend dials the listener with it,
# and the watchdog probes it so. Through the environment alone: argv is public.
${derived.constants.ensureHopLogin pkgs}
HOP_PASSWORD=$(< ${lib.escapeShellArg derived.constants.hopLoginFile})
export HOP_PASSWORD
${chosen.convert}
${ifPrivileged ''
@@ -195,7 +210,7 @@ let
exec ${runAsServiceUser pkgs [ "net_admin" ]} ${
mkWatchdog {
inherit tag tunnelPort;
inherit tunnelPort;
inherit (chosen) command directPort;
}
}
@@ -212,9 +227,26 @@ let
ExecStart = if execArgs == "" then tunnelScript else "${tunnelScript}${execArgs}";
Restart = restart;
RestartSec = 2;
# A profile that fails for good backs off to a minute rather than going through its
# conversion as root every 2 s.
RestartSteps = 5;
RestartMaxDelaySec = 60;
RuntimeDirectory = runtimeDirectory;
RuntimeDirectoryMode = "0750";
UMask = "0077";
}
# Root only converts the profile; the engine gets net_admin alone. No ProtectHome: a
# configFile may sit under /home or /root.
// lib.optionalAttrs cfg.host.privileged {
NoNewPrivileges = true;
PrivateTmp = true;
ProtectSystem = "strict";
# The watchdog's hints go to proxy-suite-outbound-groups' directory, which comes and
# goes with that unit: a bind of the directory itself would go stale.
ReadWritePaths = [ derived.constants.runtimeDir ];
ProtectKernelTunables = true;
ProtectControlGroups = true;
RestrictSUIDSGID = true;
};
};
in