feat: add configurable PID1-owned DoH forwarding

Default managed SOCKS hostname DNS to verified HTTPS with literal-IP bootstrap and no plaintext fallback. Supervise the loopback forwarder with private anchored readiness and preserve foreign markers. Include configuration, deployment, documentation and offline checks.

Co-authored-by: GPT-6 <gpt-6@openai.com>
This commit is contained in:
ZeusinaandGPT-6 Bot committed 2026-10-13 14:32:58 +03:00
1 parent ea886e5bc8
commit 367d956ae3
20 files changed
+2383 -37

No files matched your search

+59 -9
View File
@@ -14,6 +14,9 @@ esac
[ "${#ss_password}" -ge 32 ] || { echo 'error: target secret must be at least 32 hex characters' >&2; exit 64; }
# Validate before creating configs or launching any service. Errors never echo input.
/usr/local/bin/zapret-autotune target-config-check || exit 64
dns_mode=$(/usr/local/bin/zapret-autotune dns-forwarder mode) || exit 64
dns_address=$(/usr/local/bin/zapret-autotune dns-forwarder address) || exit 64
case "$dns_mode" in doh|native) ;; *) exit 64 ;; esac
config_dir=/run/zapret-autotune-ss
awk '$2 == "/run/zapret-autotune-ss" && $3 == "tmpfs" { found=1 } END { exit !found }' /proc/mounts || {
echo 'error: target Shadowsocks config directory must be a dedicated tmpfs' >&2; exit 78;
@@ -29,6 +32,9 @@ chmod 0600 "$config_dir/server.json" "$config_dir/local.json"
unset ss_password
server_pid=
dns_pid=
dns_anchor_dir=0
dns_anchor_file=0
local_pid=
monitor_pid=
monitor_sid=
@@ -39,7 +45,7 @@ cooldown_pid=
# descendant signalling is reserved for SS shutdown and forced watchdog cleanup.
. /opt/managed-target-supervision.sh
pause() {
sleep "$1" </dev/null >/dev/null 2>&1 & cooldown_pid=$!
sleep "$1" 8<&- 9<&- </dev/null >/dev/null 2>&1 & cooldown_pid=$!
wait "$cooldown_pid"
cooldown_pid=
}
@@ -82,7 +88,7 @@ bounded_wait() {
rm -f "$config_dir/join-timeout" "$config_dir/join-ready"
# Fresh interpreter: a forked shell can inherit a deferred EXIT/TERM trap
# context and never run its cancellation handler during PID 1 shutdown.
/bin/sh /opt/managed-target-supervision.sh --watchdog "$child" "$seconds" "$config_dir" </dev/null >/dev/null 2>&1 &
/bin/sh /opt/managed-target-supervision.sh --watchdog "$child" "$seconds" "$config_dir" 8<&- 9<&- </dev/null >/dev/null 2>&1 &
watchdog=$!
# A fast-exiting child must not cancel the watchdog before its trap exists.
# Fail closed if exec/setup fails before acknowledgement; never wait forever.
@@ -146,6 +152,16 @@ cleanup_cycle() {
done
server_pid=
local_pid=
# DNS remains available through monitor rollback, zapret stop and SS joins.
if [ -n "$dns_pid" ]; then
kill -TERM "$dns_pid" 2>/dev/null || true
bounded_wait "$dns_pid" 5
[ "$join_timed_out" -eq 0 ] || { echo 'error: DNS join timeout' >&2; cleanup_code=78; }
/usr/local/bin/zapret-autotune dns-forwarder readiness-cleanup "$dns_pid" || cleanup_code=78
dns_pid=
fi
if [ "$dns_anchor_file" -eq 1 ]; then exec 9<&-; dns_anchor_file=0; fi
if [ "$dns_anchor_dir" -eq 1 ]; then exec 8<&-; dns_anchor_dir=0; fi
return "$cleanup_code"
}
cleanup() {
@@ -164,23 +180,57 @@ trap cleanup EXIT
trap 'exit 0' INT TERM
# Never invoke /opt/entrypoint.sh: zapret must start AFTER reconciliation.
dns_alive() {
[ "$dns_mode" = native ] || { [ -n "$dns_pid" ] && kill -0 "$dns_pid" 2>/dev/null; }
}
start_dns() {
[ "$dns_mode" = doh ] || return 0
exec 9<&-
exec 8<"$config_dir"
dns_anchor_dir=1
# Unknown readiness is evidence, not debris. Never delete it to start.
if [ -e "$config_dir/dns-ready" ] || [ -L "$config_dir/dns-ready" ]; then
echo 'error: target DNS readiness already exists' >&2
return 78
fi
/usr/local/bin/zapret-autotune dns-forwarder serve &
dns_pid=$!
dns_deadline=$(($(date +%s) + 5))
while :; do
dns_alive || { echo 'error: target DNS child exited' >&2; return 75; }
if [ -s "$config_dir/dns-ready" ]; then
[ -f "$config_dir/dns-ready" ] && [ ! -L "$config_dir/dns-ready" ] &&
[ "$(stat -c '%u:%a' "$config_dir/dns-ready")" = "$(id -u):600" ] || return 78
# Retain inode/directory anchors in PID 1 even after child SIGKILL.
exec 9<"$config_dir/dns-ready"
dns_anchor_file=1
/usr/local/bin/zapret-autotune dns-forwarder readiness-check "$dns_pid" || return 78
dns_alive || return 75
return 0
fi
[ "$(date +%s)" -lt "$dns_deadline" ] || { echo 'error: target DNS readiness timeout' >&2; return 75; }
pause 1
done
}
run_cycle() {
start_dns || return $?
set -- ss-server -c "$config_dir/server.json"
# DNS is not a secret. Native -d avoids assuming support for a JSON DNS key.
[ -z "${SS_DNS_SERVER-}" ] || set -- "$@" -d "$SS_DNS_SERVER"
"$@" &
[ -z "$dns_address" ] || set -- "$@" -d "$dns_address"
"$@" 8<&- 9<&- &
server_pid=$!
ss-local -c "$config_dir/local.json" &
ss-local -c "$config_dir/local.json" 8<&- 9<&- &
local_pid=$!
# Bounded readiness, no network probe. Managed commands verify socket ownership.
attempt=0
until awk '$2 == "00000000:0438" && $4 == "0A" { found=1 } END { exit !found }' /proc/net/tcp; do
{ kill -0 "$server_pid" && kill -0 "$local_pid"; } || return 75
{ dns_alive && kill -0 "$server_pid" && kill -0 "$local_pid"; } || return 75
attempt=$((attempt + 1))
[ "$attempt" -lt 30 ] || { echo 'error: target SOCKS listener timeout' >&2; return 75; }
pause 1
done
dns_alive || return 75
# Reject unsafe operator requests before init/reconcile can mutate owned state.
timeout -k 5 45 /usr/local/bin/zapret-autotune managed experimental-request-check || exit 78
if [ ! -e "$ZAPRET_CONFIG" ]; then
@@ -201,9 +251,9 @@ fi
# A replacement SS child can fail during reconciliation/activation. Do not
# fork a monitor we already need to cancel: an early TERM can race interpreter
# startup before its cancellation handler exists. Clean this cycle directly.
{ kill -0 "$server_pid" 2>/dev/null && kill -0 "$local_pid" 2>/dev/null; } || return 75
{ dns_alive && kill -0 "$server_pid" 2>/dev/null && kill -0 "$local_pid" 2>/dev/null; } || return 75
rm -f "$config_dir/monitor-ready"
setsid /bin/sh /opt/managed-target-supervision.sh --monitor "$config_dir" /usr/local/bin/zapret-autotune managed monitor &
setsid /bin/sh /opt/managed-target-supervision.sh --monitor "$config_dir" /usr/local/bin/zapret-autotune managed monitor 8<&- 9<&- &
monitor_pid=$!
monitor_ack_deadline=$(($(date +%s) + 5))
while [ ! -e "$config_dir/monitor-ready" ]; do
@@ -217,7 +267,7 @@ if proc_identity "$monitor_pid" && [ "$proc_sid" != "$monitor_sid" ]; then
monitor_sid=
exit 78
fi
while kill -0 "$server_pid" && kill -0 "$local_pid" && kill -0 "$monitor_pid"; do
while dns_alive && kill -0 "$server_pid" && kill -0 "$local_pid" && kill -0 "$monitor_pid"; do
pause 1
done
if ! kill -0 "$monitor_pid" 2>/dev/null; then