#!/bin/sh set -eu umask 077 [ "$#" -eq 0 ] || { echo 'error: target entrypoint accepts no arguments' >&2; exit 64; } # Credentials never enter argv or logs. Docker supplies SS_PASSWORD via env; # clear the exported value before starting any child processes. ss_password=${SS_PASSWORD-} unset SS_PASSWORD case "$ss_password" in ''|*[!0123456789abcdefABCDEF]*) echo 'error: target secret must contain only hexadecimal characters' >&2; exit 64 ;; 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 generic_mode=0 [ -z "${ZAPRET_AUTOTUNE_RESOURCE_CONFIG-}" ] || generic_mode=1 qualification_mode=0 qualification_attempted=0 if [ -n "${ZAPRET_AUTOTUNE_QUALIFY_CONFIG-}${ZAPRET_AUTOTUNE_STRATEGY_CONFIG-}" ]; then [ -n "${ZAPRET_AUTOTUNE_QUALIFY_CONFIG-}" ] && [ -n "${ZAPRET_AUTOTUNE_STRATEGY_CONFIG-}" ] || { echo 'error: isolated qualification requires both config and strategy paths' >&2; exit 64; } qualification_mode=1 generic_mode=1 echo 'INTRUSIVE ISOLATED qualification target: do not route working traffic here' >&2 fi case "${ZAPRET_AUTOTUNE_RESOURCE_APPLY-0}" in 0|'') ;; 1) [ "$generic_mode" -eq 1 ] && [ "$qualification_mode" -eq 0 ] && [ -n "${ZAPRET_AUTOTUNE_RESOURCE_CONFIG-}" ] || { echo 'error: resource apply requires generic config and excludes qualification' >&2; exit 64; } ;; *) echo 'error: invalid resource apply flag' >&2; exit 64 ;; esac 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; } chmod 0700 "$config_dir" cat > "$config_dir/server.json" < "$config_dir/local.json" </dev/null || waited_code=$? child_joined=1 } pause() { sleep "$1" 8<&- 9<&- /dev/null 2>&1 & cooldown_pid=$! wait "$cooldown_pid" cooldown_pid= } cleanup_session() { [ -n "$monitor_sid" ] || return 0 # The verified SID survives leader death and Go's separate worker PGIDs. # Never sweep all namespace children: Docker exec peers/SS/helpers are not # in this session. Detached sysv daemons are reconciled/stopped separately. for session_signal in TERM KILL; do signal_session "$monitor_sid" "$session_signal" || return 78 session_deadline=$(($(date +%s) + 3)) if [ "$generic_mode" -eq 1 ] && [ -n "$generic_cleanup_deadline" ] && [ "$session_deadline" -gt "$generic_cleanup_deadline" ]; then session_deadline=$generic_cleanup_deadline fi while :; do signal_session "$monitor_sid" CHECK || return 78 if [ "$session_live" -eq 0 ]; then monitor_sid= return 0 fi [ "$(date +%s)" -lt "$session_deadline" ] || break pause 1 done done echo 'error: owned monitor session survived cleanup' >&2 return 78 } capture_monitor_session() { [ -z "$monitor_sid" ] || return 0 if [ -e "$config_dir/monitor-ready" ]; then { read -r candidate_sid < "$config_dir/monitor-ready"; } 2>/dev/null || { echo 'controller-startup: ack=read_failed' >&2; return 78; } case "$candidate_sid" in ''|*[!0-9]*) echo 'controller-startup: ack=invalid_pid' >&2; return 78 ;; esac [ "$candidate_sid" = "$monitor_pid" ] && [ "$candidate_sid" -gt 1 ] && [ "$candidate_sid" != "$$" ] || { echo 'controller-startup: ack=identity_mismatch' >&2; return 78; } monitor_sid=$candidate_sid elif proc_identity "$monitor_pid" && [ "$proc_sid" = "$monitor_pid" ] && [ "$proc_sid" -gt 1 ]; then monitor_sid=$proc_sid fi } # Wait with a watchdog: never let an unresponsive child exceed Docker's grace. bounded_wait() { child=$1 seconds=$2 if [ "$generic_mode" -eq 1 ]; then cleanup_remaining # Reserve ACK setup (5s) and post-KILL reaping (2s) before granting wait. if [ "$cleanup_left" -le 7 ]; then signal_tree "$child" KILL cleanup_reap "$child" "$(($(date +%s) + 2))" waited_code=${waited_code-137} join_timed_out=1 return 0 fi cleanup_wait_left=$((cleanup_left - 7)) [ "$seconds" -le "$cleanup_wait_left" ] || seconds=$cleanup_wait_left fi 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" 8<&- 9<&- /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. ack_deadline=$(($(date +%s) + 5)) if [ "$generic_mode" -eq 1 ] && [ "$ack_deadline" -gt "$generic_cleanup_deadline" ]; then ack_deadline=$generic_cleanup_deadline fi while [ ! -e "$config_dir/join-ready" ]; do if ! kill -0 "$watchdog" 2>/dev/null || [ "$(date +%s)" -ge "$ack_deadline" ]; then : > "$config_dir/join-timeout" signal_tree "$watchdog" KILL signal_tree "$child" KILL break fi done waited_code=0 if [ "$generic_mode" -eq 1 ]; then cleanup_join_deadline=$(($(date +%s) + seconds)) [ "$cleanup_join_deadline" -le "$generic_cleanup_deadline" ] || cleanup_join_deadline=$generic_cleanup_deadline cleanup_reap "$child" "$cleanup_join_deadline" if [ "$child_joined" -eq 0 ]; then : > "$config_dir/join-timeout" signal_tree "$child" KILL cleanup_reap "$child" "$(($(date +%s) + 2))" fi joined_service=$child_joined else wait "$child" 2>/dev/null || waited_code=$? fi kill -TERM "$watchdog" 2>/dev/null || true if [ "$generic_mode" -eq 1 ]; then service_waited_code=$waited_code cleanup_reap "$watchdog" "$(($(date +%s) + 2))" if [ "$child_joined" -eq 0 ]; then signal_tree "$watchdog" KILL cleanup_reap "$watchdog" "$(($(date +%s) + 2))" fi child_joined=$joined_service waited_code=$service_waited_code else wait "$watchdog" 2>/dev/null || true fi join_timed_out=0 [ ! -e "$config_dir/join-timeout" ] || join_timed_out=1 } cleanup_cycle() { if [ "$generic_mode" -eq 1 ] && [ -z "$generic_cleanup_deadline" ]; then generic_cleanup_deadline=$(($(date +%s) + 80)) fi cleanup_code=0 # Let the monitor cancel/join scans and restore committed bytes BEFORE stop. if [ -n "$monitor_pid" ]; then capture_monitor_session || cleanup_code=78 kill -TERM "$monitor_pid" 2>/dev/null || true if [ "$qualification_mode" -eq 1 ]; then bounded_wait "$monitor_pid" 75; else bounded_wait "$monitor_pid" 45; fi # Cancellation must restore safely even when SS failure requested retry. if [ "$qualification_mode" -eq 1 ]; then # Exit 78 is an outcome only with a producer-owned completed-cleanup # ACK and a fresh baseline/worker/nft check, while SS+DNS still live. cleanup_command 10 /usr/local/bin/zapret-autotune qualification-completion-check "$monitor_pid" "$waited_code" || cleanup_code=78 else case "$waited_code" in 0|143) ;; *) echo "error: monitor cleanup status $waited_code" >&2; cleanup_code=78 ;; esac fi [ "$join_timed_out" -eq 0 ] || { echo 'error: monitor join timeout' >&2; cleanup_code=78; } # SIGTERM may have arrived before the launch acknowledgement was read. capture_monitor_session || cleanup_code=78 monitor_pid= fi # Join/sweep scans even if the monitor was already reaped after SIGKILL. # A failed sweep vetoes reconciliation and all daemon script sourcing. if ! cleanup_session; then cleanup_code=78 zapret_started=0 fi # Dedicated PID/network namespace: no host/other-target stop. if [ "$zapret_started" -eq 1 ]; then # Stop also sources config: clear trust before reconciliation, never # source unknown bytes left by a crashed monitor, even for shutdown. zapret_started=0 if cleanup_command 45 /usr/local/bin/zapret-autotune managed reconcile; then cleanup_command 30 /opt/zapret2/init.d/sysv/zapret2 stop || cleanup_code=78 else cleanup_code=78 fi fi for pid in "$local_pid" "$server_pid"; do [ -z "$pid" ] || signal_tree "$pid" TERM done ss_joined=1 for pid in "$local_pid" "$server_pid"; do if [ -n "$pid" ]; then bounded_wait "$pid" 5 if [ "$generic_mode" -eq 1 ] && [ "$child_joined" -eq 0 ]; then ss_joined=0; fi # Its original failure triggered supervision; only forced shutdown # is a cleanup failure, not the already-dead Shadowsocks status. [ "$join_timed_out" -eq 0 ] || { echo 'error: Shadowsocks join timeout' >&2; cleanup_code=78; } fi done server_pid= local_pid= # DNS remains available through monitor rollback, zapret stop and SS joins. if [ -n "$dns_pid" ] && [ "$ss_joined" -eq 1 ]; 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; } if [ "$generic_mode" -eq 1 ] && [ "$child_joined" -eq 0 ]; then cleanup_code=78 elif [ "$generic_mode" -eq 1 ]; then cleanup_command 5 /usr/local/bin/zapret-autotune dns-forwarder readiness-cleanup "$dns_pid" || cleanup_code=78 else /usr/local/bin/zapret-autotune dns-forwarder readiness-cleanup "$dns_pid" || cleanup_code=78 fi if [ "$generic_mode" -eq 0 ] || [ "$child_joined" -eq 1 ]; then dns_pid=; fi fi if [ -z "$dns_pid" ]; then 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 fi if [ "$generic_mode" -eq 1 ]; then [ "$cleanup_code" -eq 0 ] || generic_cleanup_failed=1 generic_cleanup_deadline= fi return "$cleanup_code" } cleanup() { final_code=$? trap - EXIT # A caught (not ignored) signal lets watchdog children install TERM traps. trap ':' INT TERM if [ -n "$cooldown_pid" ]; then kill "$cooldown_pid" 2>/dev/null || true wait "$cooldown_pid" 2>/dev/null || true fi cleanup_cycle || final_code=78 [ "$generic_cleanup_failed" -eq 0 ] || final_code=78 exit "$final_code" } 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 } launch_controller() { rm -f "$config_dir/monitor-ready" setsid /bin/sh /opt/managed-target-supervision.sh --monitor "$config_dir" /usr/local/bin/zapret-autotune "$@" 8<&- 9<&- & monitor_pid=$! monitor_ack_deadline=$(($(date +%s) + 5)) while [ ! -e "$config_dir/monitor-ready" ]; do if ! kill -0 "$monitor_pid" 2>/dev/null || [ "$(date +%s)" -ge "$monitor_ack_deadline" ]; then if ! kill -0 "$monitor_pid" 2>/dev/null; then echo 'controller-startup: launch=child_gone' >&2 else echo 'controller-startup: launch=ack_timeout' >&2 fi signal_tree "$monitor_pid" KILL return 78 fi done capture_monitor_session || return 78 if proc_identity "$monitor_pid" && [ "$proc_sid" != "$monitor_sid" ]; then echo 'controller-startup: launch=session_mismatch' >&2 monitor_sid= return 78 fi } run_cycle() { [ "$qualification_attempted" -eq 0 ] || return 78 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 "$dns_address" ] || set -- "$@" -d "$dns_address" "$@" 8<&- 9<&- & server_pid=$! 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 { 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. if [ "$generic_mode" -eq 0 ]; then timeout -k 5 45 /usr/local/bin/zapret-autotune managed experimental-request-check || return 78 fi if [ ! -e "$ZAPRET_CONFIG" ]; then timeout -k 5 45 /usr/local/bin/zapret-autotune managed init || return 78 fi # Never activate pending or unknown bytes. Any reconciliation failure is fatal, # not a transient (75) restart request. Reload restores known bytes first. timeout -k 5 45 /usr/local/bin/zapret-autotune managed reconcile || return 78 if [ "$qualification_mode" -eq 0 ]; then zapret_started=1 if [ "${ZAPRET_AUTOTUNE_RESOURCE_APPLY-0}" = 1 ]; then launch_controller managed resource-apply --resource-config "$ZAPRET_AUTOTUNE_RESOURCE_CONFIG" || return 78 while dns_alive && kill -0 "$server_pid" && kill -0 "$local_pid" && kill -0 "$monitor_pid"; do pause 1; done { dns_alive && kill -0 "$server_pid" && kill -0 "$local_pid"; } || return 75 apply_code=0 wait "$monitor_pid" || apply_code=$? echo "controller-startup: apply_wait_exit=$apply_code" >&2 monitor_pid= cleanup_session || { echo 'controller-startup: apply_cleanup=failed' >&2; return 78; } [ "$apply_code" -eq 0 ] || return 78 fi # Idempotent apply may have only verified the already committed config. timeout -k 5 30 /opt/zapret2/init.d/sysv/zapret2 start || return 78 fi # Private operator request, consumed only after a successful guarded transaction. # Never suppress qualification gates globally or race the owned monitor. if [ "$generic_mode" -eq 0 ] && { [ -e "${ZAPRET_CONFIG%/*}/.autotune-experimental-suffix-request" ] || [ -L "${ZAPRET_CONFIG%/*}/.autotune-experimental-suffix-request" ]; }; then timeout -k 5 120 /usr/local/bin/zapret-autotune managed apply --googlevideo-experiment native-circular --googlevideo-scope experimental_googlevideo_suffix --googlevideo-request startup || exit 78 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. { dns_alive && kill -0 "$server_pid" 2>/dev/null && kill -0 "$local_pid" 2>/dev/null; } || return 75 rm -f "$config_dir/monitor-ready" if [ "$qualification_mode" -eq 1 ]; then qualification_attempted=1 set -- qualify --config "$ZAPRET_AUTOTUNE_QUALIFY_CONFIG" --strategies "$ZAPRET_AUTOTUNE_STRATEGY_CONFIG" --isolated else set -- managed monitor fi launch_controller "$@" || return 78 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 code=0 wait "$monitor_pid" || code=$? if [ "$qualification_mode" -eq 1 ]; then if ! timeout -k 1 10 /usr/local/bin/zapret-autotune qualification-completion-check "$monitor_pid" "$code"; then generic_cleanup_failed=1 fi monitor_pid= # One finite batch only, including negative/blocked completion. No live # daemon and no automatic retest. Keep owned SS/DoH alive for inspection. echo "isolated qualification finished (status $code); target safe-idle" >&2 cleanup_session || return 78 while dns_alive && kill -0 "$server_pid" && kill -0 "$local_pid"; do pause 1; done return 78 fi monitor_pid= echo 'error: target managed monitor exited' >&2 case "$code" in 0|2|137|139) code=75 ;; 64|75|78) ;; *) code=78 ;; esac return "$code" fi echo 'error: target child process exited' >&2 return 75 } # Three starts per rolling six hours, including initial startup. PID 1 owns # the entire runtime cycle; cooldown is cancellable without any external manager. starts= backoff=15 while :; do now=$(date +%s) recent= count=0 for started in $starts; do if [ "$((now - started))" -lt 21600 ]; then recent="$recent $started" count=$((count + 1)) fi done if [ "$count" -ge 3 ]; then if [ "$generic_mode" -eq 1 ]; then echo 'BLOCKED: generic target restart budget; checks paused' >&2 pause 60 continue fi echo 'error: target restart budget exhausted' >&2; exit 78 fi starts="$recent $now" code=0 run_cycle || code=$? if ! cleanup_cycle; then if [ "$generic_mode" -eq 1 ]; then echo 'BLOCKED: generic target unsafe cleanup; operator repair/restart required' >&2 while :; do pause 60; done fi code=78 fi if [ "$generic_mode" -eq 1 ] && [ "$code" -eq 78 ]; then echo 'BLOCKED: generic target trust/startup failure; checks paused' >&2 pause 60 continue fi [ "$code" -eq 75 ] || exit "$code" # A long-running monitor may outlive the window; recheck at failure time. now=$(date +%s) retry_count=0 for started in $starts; do [ "$((now - started))" -ge 21600 ] || retry_count=$((retry_count + 1)) done if [ "$retry_count" -ge 3 ] && [ "$generic_mode" -eq 0 ]; then echo 'error: target restart budget exhausted' >&2; exit 78 fi pause "$backoff" [ "$backoff" -ge 60 ] || backoff=$((backoff * 2)) done