From 2452d6fbe1936d8f94b5a670eef21235fa321a05 Mon Sep 17 00:00:00 2001 From: FUFSoB Date: Sun, 4 Oct 2026 17:31:20 +0500 Subject: [PATCH] fix: better logs and zapret2 --- README.md | 2 +- docs/usage/control.md | 6 ++-- modules/proxy-suite/zapret2/sources.nix | 35 ++++++++++++++----- .../zapret2/z2k-profiles.template.sh | 26 ++++++++++++++ nix/checks/zapret2.nix | 23 ++++++++++++ pkgs/proxy-ctl.nix | 4 +-- pkgs/proxy-ctl/proxy_ctl.py | 31 +++++++++++----- pkgs/proxy-ctl/test_proxy_ctl.py | 21 ++++++++--- 8 files changed, 121 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index b21cd9c..03d9eed 100644 --- a/README.md +++ b/README.md @@ -139,7 +139,7 @@ Changes and secrets need root or the userControl group. status [--json] services and routing mode (--tray: deprecated) restart restart running services logs [unit...] follow logs (default: all proxy-suite units); in a - terminal, in less: Ctrl-C scrolls back, F follows + terminal, in lnav: scroll back, G follows, q quits where how a domain is routed right now proxy [status|on|off] local proxy diff --git a/docs/usage/control.md b/docs/usage/control.md index f9329ab..1fef2b7 100644 --- a/docs/usage/control.md +++ b/docs/usage/control.md @@ -109,8 +109,10 @@ proxy-ctl logs # follow the logs of every proxy-suite service, with t proxy-ctl restart # restart what is running ``` -In a terminal, `proxy-ctl logs` follows in `less`: Ctrl-C stops following so you can scroll -back, `F` follows again and `q` quits. +In a terminal, `proxy-ctl logs` follows in [`lnav`](https://lnav.org): it follows while at +the bottom, scrolling back pauses it, `G` follows again, `e`/`E` jump between errors, `/` +searches, and `q` or Ctrl-C quits. `:enable-word-wrap` wraps long lines, and lnav remembers +it for the next time. Piped, it is plain `journalctl -f`. ## See also diff --git a/modules/proxy-suite/zapret2/sources.nix b/modules/proxy-suite/zapret2/sources.nix index 982e900..c912652 100644 --- a/modules/proxy-suite/zapret2/sources.nix +++ b/modules/proxy-suite/zapret2/sources.nix @@ -106,18 +106,37 @@ let src = sources.z2k; generator = builtins.readFile "${src}/lib/config_official.sh"; ports = name: between "config_official.sh" "\n${name}=\"" "\"" generator; + # Its silence detector, the only one that sees a dead QUIC flow (it sends less, not + # more), knows the YouTube pools alone; rkn_quic (z2k-profiles.template.sh) joins them. + # It took any reply for an answer, but DPI that lets the server's first flight through + # and drops the rest leaves a flow dead with a reply or two (every Discord host): such + # a flow neither failed nor, past udp_in, succeeded, and rotation never moved. A flow + # is answered now past udp_in replies, as the success detector judges it. + quicSilence = pkgs.writeText "z2k-quic-silence.lua" ( + lib.pipe (builtins.readFile "${src}/files/lua/z2k-quic-silence.lua") [ + (patch "z2k-quic-silence.lua" "local Z2K_QUIC_POOLS = { yt_quic = true, gv_quic = true }" + "local Z2K_QUIC_POOLS = { yt_quic = true, gv_quic = true, rkn_quic = true }" + ) + (patch "z2k-quic-silence.lua" + " if not desync.outgoing then\n crec.z2k_quic_answered = true\n" + " if not desync.outgoing then\n if (pos_get(desync, 'n') or 0) > (tonumber(desync.arg.udp_in) or 1) then crec.z2k_quic_answered = true end\n" + ) + ] + ); in { # Its S99zapret2 order. z2k-range-rand resolves the ranges its strategies # write (repeats=6-10); without it nfqws2 sends each fake once. - luaInit = map (name: "${src}/files/lua/${name}.lua") [ - "z2k-alert" - "z2k-quic-silence" - "z2k-tcp16" - "z2k-fooling-ext" - "z2k-range-rand" - "z2k-modern-core" - ]; + luaInit = + map (name: if name == "z2k-quic-silence" then quicSilence else "${src}/files/lua/${name}.lua") + [ + "z2k-alert" + "z2k-quic-silence" + "z2k-tcp16" + "z2k-fooling-ext" + "z2k-range-rand" + "z2k-modern-core" + ]; # As its S99zapret2 runs nfqws2: a connection without SNI (some TVs and apps, a # SYN before the ClientHello its syndata strategies act on) takes the name last # seen on that IP, so it still meets its site's profile and strategy. diff --git a/modules/proxy-suite/zapret2/z2k-profiles.template.sh b/modules/proxy-suite/zapret2/z2k-profiles.template.sh index 53a792c..c34a21a 100644 --- a/modules/proxy-suite/zapret2/z2k-profiles.template.sh +++ b/modules/proxy-suite/zapret2/z2k-profiles.template.sh @@ -70,6 +70,32 @@ awk -v disc="--hostlist=$root/lists/discovered-domains.txt" \ printf " --new --filter-tcp=%s --filter-l7=tls %s%s %s", ports[learner], wl, esuf, hsuf }' raw >body +# QUIC to blocked sites: z2k rotates QUIC strategies over YouTube's list alone, and DPI +# drops QUIC to the rest by name (every Discord host), so a browser stalls on each +# HTTP/3 attempt before it falls back to TCP. rkn_quic is its YouTube QUIC profile over +# the sites rkn_tcp handles, right after it: YouTube's hosts still meet yt_quic first. +# Its udp_in is 4: there DPI lets the server's first flight (two packets from Cloudflare) +# through before it drops the flow, which at udp_in=1 counted as a success and undid +# every failure counted; a real handshake brings back more. +awk -v yt="--hostlist=$root/extra_strats/UDP/YT/List.txt" \ + -v rkn="--hostlist=$root/extra_strats/TCP/RKN/List.txt --hostlist=$root/extra_strats/TCP_Discord.txt $hostlistSuffix" ' + { + n = split($0, p, / --new /) + for (i = 1; i <= n; i++) { + printf "%s%s", (i > 1 ? " --new " : ""), p[i] + if (p[i] ~ /key=yt_quic:/ && (k = index(p[i], yt))) { + q = substr(p[i], 1, k - 1) rkn substr(p[i], k + length(yt)) + gsub(/key=yt_quic:/, "key=rkn_quic:", q) + if (!sub(/:udp_in=1:/, ":udp_in=4:", q)) { print "z2k YouTube QUIC udp_in=1 not found" > "/dev/stderr"; exit 1 } + printf " --new %s", q + found++ + } + } + } + END { if (found != 1) { print "z2k YouTube QUIC profile not found" > "/dev/stderr"; exit 1 } } +' body >body.quic +mv body.quic body + awk -v z=@z2k@ ' match($0, /^([A-Z0-9_]+_BLOB)="\$ZAPRET_BASE\/([^"]+)"/, m) { file[m[1]] = m[2] } match($0, /--blob=([a-z0-9_]+):@\$([A-Z0-9_]+_BLOB)/, m) { printf "--blob=%s:@%s/%s ", m[1], z, file[m[2]] } diff --git a/nix/checks/zapret2.nix b/nix/checks/zapret2.nix index 48a2dab..dd8250d 100644 --- a/nix/checks/zapret2.nix +++ b/nix/checks/zapret2.nix @@ -360,6 +360,29 @@ in jq -e '.cf_extra == .rkn_tcp' "${z2kRuntime}/strategies.json" jq -e '.http_rkn["1"] == ["http_methodeol:payload=http_req:dir=out"]' "${z2kRuntime}/strategies.json" jq -e 'has("yt_tcp") and has("gv_tcp") and has("yt_quic")' "${z2kRuntime}/strategies.json" + # QUIC to the sites rkn_tcp handles gets YouTube's QUIC strategies, after yt_quic, + # and the silence detector that sees dead QUIC flows counts its failures. + jq -e '.rkn_quic == .yt_quic' "${z2kRuntime}/strategies.json" + grep -qE -- "key=yt_quic:[^']* --new [^']*/extra_strats/TCP/RKN/List.txt --hostlist=[^ ]+/extra_strats/TCP_Discord.txt --filter-udp=443 --filter-l7=quic [^']*key=rkn_quic:" "${z2kRuntime}/config" + QS=$(grep -oE '/nix/store/[^ ]+-z2k-quic-silence\.lua' "${z2kRuntime}/config") + grep -qF 'rkn_quic = true' "$QS" + # Its first flight is no success, nor an answer to the silence detector: rotation moves. + grep -qE -- "key=rkn_quic:" "${z2kRuntime}/config" + grep -oE -- "--lua-desync=circular:[^ ]*key=rkn_quic:[^ ]*" "${z2kRuntime}/config" | grep -qF ':udp_in=4:' + grep -oE -- "--lua-desync=circular:[^ ]*key=yt_quic:[^ ]*" "${z2kRuntime}/config" | grep -qF ':udp_in=1:' + QS="$QS" lua -e ' + function standard_failure_detector() return "standard" end + function pos_get(desync) return desync.n end + dofile(os.getenv("QS")) + local function reply(key, udp_in, n) + local crec = {} + z2k_fail_quic_silence({ dis = { udp = true }, outgoing = false, n = n, arg = { key = key, udp_in = udp_in } }, crec) + return crec.z2k_quic_answered == true + end + assert(not reply("rkn_quic", "4", 2) and reply("rkn_quic", "4", 5)) + assert(not reply("yt_quic", "1", 1) and reply("yt_quic", "1", 2)) + assert(z2k_fail_quic_silence({ dis = { udp = true }, arg = { key = "rkn_tcp" } }, {}) == "standard") + ' jq -e 'keys == ["circular_1_1", "circular_3_1"]' "${globalRuntime}/strategies.json" if grep -qF 'strategy=' "${z2kRuntime}/strategies.json"; then exit 1; fi diff --git a/pkgs/proxy-ctl.nix b/pkgs/proxy-ctl.nix index 601721c..056f667 100644 --- a/pkgs/proxy-ctl.nix +++ b/pkgs/proxy-ctl.nix @@ -184,8 +184,8 @@ let pkgs.bash pkgs.curl pkgs.fzf - # `proxy-ctl logs` follows in less. - pkgs.less + # `proxy-ctl logs` follows in lnav. + pkgs.lnav pkgs.proxychains-ng pkgs.qrencode pkgs.systemd diff --git a/pkgs/proxy-ctl/proxy_ctl.py b/pkgs/proxy-ctl/proxy_ctl.py index e56ef1a..c99a96c 100644 --- a/pkgs/proxy-ctl/proxy_ctl.py +++ b/pkgs/proxy-ctl/proxy_ctl.py @@ -60,7 +60,7 @@ Changes and secrets need root or the userControl group. status [--json] services and routing mode (--tray: deprecated) restart restart running services logs [unit...] follow logs (default: all proxy-suite units); in a - terminal, in less: Ctrl-C scrolls back, F follows + terminal, in lnav: scroll back, G follows, q quits where how a domain is routed right now proxy [status|on|off] local proxy @@ -5265,16 +5265,29 @@ def cmd_logs(*units): "journalctl", ["-f", "-n", str(LOGS_BACKLOG), *(f"--unit={u}" for u in units or ["proxy-suite-*"])], ) - pager = shutil.which("less") - if not (pager and sys.stdin.isatty() and sys.stdout.isatty()): + viewer = _log_viewer() + if not (viewer and sys.stdin.isatty() and sys.stdout.isatty()): _exec(argv) - sys.exit(_follow_in_pager(argv, pager)) + sys.exit(_follow_in_pager(argv, viewer)) -def _follow_in_pager(argv, pager): - """argv's output in less, following it: Ctrl-C stops to scroll back, F follows again, q quits. +def _log_viewer(): + """The argv that follows logs on its stdin in a terminal: lnav, or less where it is missing. - argv runs in a session of its own, so Ctrl-C reaches less alone. + lnav follows while at the bottom: scrolling back pauses it, G follows again, q or Ctrl-C + quits. less starts following (+F): Ctrl-C stops to scroll back, F follows again, q quits. + """ + lnav = shutil.which("lnav") + if lnav: + return [lnav, "-q"] + less = shutil.which("less") + return [less, "-R", "-M", "+F"] if less else None + + +def _follow_in_pager(argv, viewer): + """argv's output in viewer, the argv of a pager that reads its stdin. + + argv runs in a session of its own, so Ctrl-C reaches the viewer alone. """ sys.stdout.flush() old = signal.signal(signal.SIGINT, signal.SIG_IGN) @@ -5285,14 +5298,14 @@ def _follow_in_pager(argv, pager): stdout=subprocess.PIPE, stderr=subprocess.STDOUT, start_new_session=True, - # journalctl colors only a terminal; less -R shows them. + # journalctl colors only a terminal; lnav and less -R show them. env={**os.environ, "SYSTEMD_COLORS": "1"}, ) except OSError as e: signal.signal(signal.SIGINT, old) die(f"proxy-ctl: {argv[0]}: {e.strerror}", 127) try: - return subprocess.call([pager, "-R", "-M", "+F"], stdin=source.stdout) + return subprocess.call(viewer, stdin=source.stdout) finally: source.stdout.close() source.terminate() diff --git a/pkgs/proxy-ctl/test_proxy_ctl.py b/pkgs/proxy-ctl/test_proxy_ctl.py index 6a4d6a2..8afe7cb 100644 --- a/pkgs/proxy-ctl/test_proxy_ctl.py +++ b/pkgs/proxy-ctl/test_proxy_ctl.py @@ -710,18 +710,29 @@ class ServiceManagerTest(EnvTest): raise SystemExit(0) self.patch("_exec", exec_) - self.patch("_follow_in_pager", lambda argv, pager: ran.append(("less", argv)) or 0) - self.patch("shutil", mock.Mock(which=lambda name: f"/bin/{name}")) + self.patch("_follow_in_pager", lambda argv, viewer: ran.append((viewer, argv)) or 0) + found = {"lnav", "less"} + self.patch("shutil", mock.Mock(which=lambda name: f"/bin/{name}" if name in found else None)) # Piped (run() captures stdout): journalctl itself, with a backlog. run(ctl.cmd_logs) self.assertEqual(ran[-1], ("exec", ["journalctl", "-f", "-n", "1000", "--unit=proxy-suite-*"])) - # In a terminal: through less, one --unit per named unit. + # In a terminal: through lnav, one --unit per named unit. terminal = mock.Mock(isatty=lambda: True) with mock.patch.object(ctl.sys, "stdin", terminal), mock.patch.object(ctl.sys, "stdout", terminal): with self.assertRaises(SystemExit) as done: ctl.cmd_logs("a", "b") - self.assertEqual(done.exception.code, 0) - self.assertEqual(ran[-1], ("less", ["journalctl", "-f", "-n", "1000", "--unit=a", "--unit=b"])) + self.assertEqual(done.exception.code, 0) + self.assertEqual(ran[-1], (["/bin/lnav", "-q"], ["journalctl", "-f", "-n", "1000", "--unit=a", "--unit=b"])) + # Without lnav: less, already following. + found.discard("lnav") + with self.assertRaises(SystemExit): + ctl.cmd_logs() + self.assertEqual(ran[-1][0], ["/bin/less", "-R", "-M", "+F"]) + # Without either: journalctl itself. + found.clear() + with self.assertRaises(SystemExit): + ctl.cmd_logs() + self.assertEqual(ran[-1][0], "exec") def test_kill_switch_lifts_only_on_purpose(self): seen = self.calls()