From 006ac60b9a8316672165f3a29360f4580168e59b Mon Sep 17 00:00:00 2001 From: fufsob Date: Mon, 14 Sep 2026 04:37:17 +0000 Subject: [PATCH] fix: improve proxy-ctl --- README.md | 2 +- modules/proxy-suite/zapret2/cutoff.nix | 6 +++++- nix/checks/repo.nix | 8 +++++++- nix/checks/zapret2.nix | 4 ++++ pkgs/proxy-ctl-lib.sh | 20 ++++++++++++-------- 5 files changed, 29 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 6dea654..224b133 100644 --- a/README.md +++ b/README.md @@ -123,7 +123,7 @@ A group without a verb shows its status or list. proxy tun [status|on|off] global TUN mode proxy tproxy [status|on|off] global TProxy mode proxy auto [list] what autoProxy routed, and via which exit (sudo, or userControl) - proxy auto probe [/path] [--json] [--exits a,b | --via tag] + proxy auto probe [/path] [--json] [--keep-going] [--exits a,b | --via tag] find an exit that reaches a domain proxy auto learn probe now and route it if an exit works (sudo) proxy auto queue [count] destinations waiting to be probed (sudo, or userControl) diff --git a/modules/proxy-suite/zapret2/cutoff.nix b/modules/proxy-suite/zapret2/cutoff.nix index 2fbd8de..69cdedf 100644 --- a/modules/proxy-suite/zapret2/cutoff.nix +++ b/modules/proxy-suite/zapret2/cutoff.nix @@ -18,6 +18,10 @@ let dir = constants.zapret2CutoffDir; unit = "proxy-suite-zapret2-cutoff"; table = "proxy_suite_cutoff_probe"; + # try-restart fails on a unit that is not installed, so only name the ones built. + restartUnits = + lib.optional cfg.zapret.enable "zapret-discord-youtube.service" + ++ lib.optional cfg.perAppRouting.zapret.enable "proxy-suite-per-app-zapret.service"; z2kDetect = pkgs.buildGoModule { pname = "z2k-detect"; @@ -119,7 +123,7 @@ let # nfqws2 reads the maps once; learned strategies survive the restart. if [ "$changed" = 1 ]; then - systemctl try-restart zapret-discord-youtube.service proxy-suite-per-app-zapret.service + systemctl try-restart ${lib.escapeShellArgs restartUnits} fi ''; diff --git a/nix/checks/repo.nix b/nix/checks/repo.nix index ff8ae55..5d9f161 100644 --- a/nix/checks/repo.nix +++ b/nix/checks/repo.nix @@ -503,7 +503,8 @@ grep -q '"url":"https://example.test/robots.txt"' <<<"$out" # A working apex is never second-guessed by the fallback path. - out="$(probe_with '0|0.10|200|500|' '0|0.20|200|500|' '0|0.10|403|10|' '0|0.20|200|10|')" + # set -e: proxy-ctl runs under it, and $() does not inherit it. + out="$(set -e; probe_with '0|0.10|200|500|' '0|0.20|200|500|' '0|0.10|403|10|' '0|0.20|200|10|')" grep -q '"verdict":"ok"' <<<"$out" # --- walking more than two exits --- @@ -522,6 +523,11 @@ out="$(cmd_proxy_probe --json sekai.test)" jq -e '.verdict == "destination" and .exit == "de"' <<<"$out" > /dev/null + # --keep-going tries every exit but keeps the first that worked. + out="$(cmd_proxy_probe --json --keep-going --exits de,fi sekai.test)" + jq -e '.exit == "de" and [.exits[].tag] == ["direct", "de", "fi", "direct", "de", "fi"]' \ + <<<"$out" > /dev/null + # --exits restricts and orders the walk; direct is always tried first. out="$(cmd_proxy_probe --json --exits de sekai.test)" jq -e '[.exits[].tag] == ["direct", "de"]' <<<"$out" > /dev/null diff --git a/nix/checks/zapret2.nix b/nix/checks/zapret2.nix index 4e56ba0..47695d8 100644 --- a/nix/checks/zapret2.nix +++ b/nix/checks/zapret2.nix @@ -174,6 +174,10 @@ in detect=$(grep -o '/nix/store/[^:]*-z2k-detect-[^:/]*/bin' ${cutoffProbe} | head -n1) "$detect/z2k-detect" tcp16 -h 2>/dev/null + # New maps restart only the zapret units this config installs. + grep -qF 'try-restart zapret-discord-youtube.service' ${cutoffProbe} + if grep -qF 'proxy-suite-per-app-zapret.service' ${cutoffProbe}; then exit 1; fi + # The proxy carries what no name fixes, unless the fallback is off. grep -qF 'tag: "zapret-cutoff"' ${socksStart zapret2Global} if grep -qF 'zapret-cutoff' ${socksStart zapret2NoFallback}; then exit 1; fi diff --git a/pkgs/proxy-ctl-lib.sh b/pkgs/proxy-ctl-lib.sh index 6daa0b3..ca9d915 100644 --- a/pkgs/proxy-ctl-lib.sh +++ b/pkgs/proxy-ctl-lib.sh @@ -41,7 +41,7 @@ A group without a verb shows its status or list. proxy tun [status|on|off] global TUN mode proxy tproxy [status|on|off] global TProxy mode proxy auto [list] what autoProxy routed, and via which exit (sudo, or userControl) - proxy auto probe [/path] [--json] [--exits a,b | --via tag] + proxy auto probe [/path] [--json] [--keep-going] [--exits a,b | --via tag] find an exit that reaches a domain proxy auto learn probe now and route it if an exit works (sudo) proxy auto queue [count] destinations waiting to be probed (sudo, or userControl) @@ -762,11 +762,13 @@ _probe_fetch_exit() { } # Walks EXIT_TAGS/EXIT_URLS (direct first) and stops at the first exit that -# gets content. Sets WALK_VERDICT, WALK_EXIT, WALK_PATH, WALK_DIRECT, WALK_VIA +# gets content; PROBE_KEEP_GOING=1 still tries the rest, keeping that one. +# Sets WALK_VERDICT, WALK_EXIT, WALK_PATH, WALK_DIRECT, WALK_VIA # and WALK_ROWS ("tagpathresultjudgement" per request). _probe_walk() { local domain="$1" path direct result judgement i first_direct="" WALK_ROWS=() + WALK_VERDICT="" WALK_EXIT="" WALK_VIA="" for path in "$PROBE_PATH" ${PROBE_PATH_FALLBACK:+"$PROBE_PATH_FALLBACK"}; do @@ -774,26 +776,27 @@ _probe_walk() { judgement="$(_probe_exit_verdict "$direct")" WALK_ROWS+=("${EXIT_TAGS[0]}"$'\t'"$path"$'\t'"$direct"$'\t'"$judgement") [ -n "$first_direct" ] || first_direct="$direct" - if [ "$judgement" = ok ]; then + if [ "$judgement" = ok ] && [ -z "$WALK_VERDICT" ]; then WALK_VERDICT=ok WALK_PATH="$path" WALK_DIRECT="$direct" - return + [ "${PROBE_KEEP_GOING:-0}" = 1 ] || return 0 fi for ((i = 1; i < ${#EXIT_TAGS[@]}; i++)); do result="$(_probe_fetch_exit "$domain" "$path" "${EXIT_URLS[i]}")" judgement="$(_probe_exit_verdict "$result")" WALK_ROWS+=("${EXIT_TAGS[i]}"$'\t'"$path"$'\t'"$result"$'\t'"$judgement") - if [ "$judgement" = ok ]; then + if [ "$judgement" = ok ] && [ -z "$WALK_VERDICT" ]; then WALK_VERDICT="$(_probe_verdict "$direct" "$result")" WALK_EXIT="${EXIT_TAGS[i]}" WALK_PATH="$path" WALK_DIRECT="$direct" WALK_VIA="$result" - return + [ "${PROBE_KEEP_GOING:-0}" = 1 ] || return 0 fi done done + [ -z "$WALK_VERDICT" ] || return 0 WALK_PATH="$PROBE_PATH" WALK_DIRECT="$first_direct" if [ "$(_probe_exit_verdict "$first_direct")" = dead ]; then @@ -829,11 +832,12 @@ _probe_stand_in() { } cmd_proxy_probe() { - local json=0 domain="" want="" have_want=0 via="" row tag + local json=0 domain="" want="" have_want=0 via="" row tag PROBE_KEEP_GOING=0 local -a rows wanted while [ $# -gt 0 ]; do case "$1" in --json) json=1 ;; + --keep-going) PROBE_KEEP_GOING=1 ;; --exits) want="${2?--exits needs a comma-separated list of exit tags}" have_want=1 @@ -851,7 +855,7 @@ cmd_proxy_probe() { shift done - [ -n "$domain" ] || _usage "proxy auto probe [/path] [--json] [--exits a,b | --via tag]" + [ -n "$domain" ] || _usage "proxy auto probe [/path] [--json] [--keep-going] [--exits a,b | --via tag]" domain="${domain#*://}" if [[ "$domain" == */* ]]; then local PROBE_PATH="/${domain#*/}" PROBE_PATH_FALLBACK=""