diff --git a/README.md b/README.md index 2c09023..6f301bb 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,8 @@ A group without a verb shows its status or list. zapret auto add|forget|exclude pin, forget, or never learn a host (sudo) zapret auto clear forget learned hosts and strategies (sudo) + zapret cutoff [status] networks this line cuts at 16 KB, and their names + zapret cutoff probe probe this line again now (sudo) awg [list] AmneziaWG profiles and their state awg on | off [profile] | restart [profile] diff --git a/docs/options/index.md b/docs/options/index.md index 035bb87..7e5e9aa 100644 --- a/docs/options/index.md +++ b/docs/options/index.md @@ -288,6 +288,10 @@ services.proxy-suite = { udpOut = 4; }; blobs = { }; + cutoff = { + enable = true; + proxyFallback = true; + }; domains = [ ]; excludeDomains = [ ]; ipv6 = false; diff --git a/docs/options/zapret.md b/docs/options/zapret.md index 6963e92..4fe13fb 100644 --- a/docs/options/zapret.md +++ b/docs/options/zapret.md @@ -46,6 +46,9 @@ Part of the [proxy-suite options reference](./index.md). - [udpIn](#services-proxy-suite-zapret-zapret2-autohostlist-udpin) - [udpOut](#services-proxy-suite-zapret-zapret2-autohostlist-udpout) - [blobs](#services-proxy-suite-zapret-zapret2-blobs) + - cutoff + - [enable](#services-proxy-suite-zapret-zapret2-cutoff-enable) + - [proxyFallback](#services-proxy-suite-zapret-zapret2-cutoff-proxyfallback) - [domains](#services-proxy-suite-zapret-zapret2-domains) - [excludeDomains](#services-proxy-suite-zapret-zapret2-excludedomains) - [ipv6](#services-proxy-suite-zapret-zapret2-ipv6) @@ -709,6 +712,48 @@ attribute set of string } ``` + +## services\.proxy-suite\.zapret\.zapret2\.cutoff\.enable + +Some lines let TLS to certain hosting networks handshake and then cut it at 12-34 KB, +which no strategy fixes\. z2k’s probe checks this line against ~110 known targets when +its network changes and once a day, and for each cut-off network searches a whitelisted +name that nfqws2 then puts into a fake ClientHello: thousands of short TLS connections to +foreign hosting IPs per full run, sent directly and never touched by zapret2\. State and +maps live in /var/lib/proxy-suite/zapret2/cutoff; ` proxy-ctl zapret cutoff ` shows them\. + +*Type:* +boolean + +*Default:* + +```nix +true +``` + +*Example:* + +```nix +true +``` + + +## services\.proxy-suite\.zapret\.zapret2\.cutoff\.proxyFallback + +Route the cut-off networks no name fixes through the proxy outbound\. Needs the sing-box +backend with an outbound and a route mode other than all-bypass, and only applies to +traffic the backend sees by address (TUN, TProxy, per-app routing, clients that dial IPs); +explicit direct rules still win\. + +*Type:* +boolean + +*Default:* + +```nix +true +``` + ## services\.proxy-suite\.zapret\.zapret2\.domains diff --git a/modules/proxy-suite/derived.nix b/modules/proxy-suite/derived.nix index 579a070..699f896 100644 --- a/modules/proxy-suite/derived.nix +++ b/modules/proxy-suite/derived.nix @@ -122,9 +122,19 @@ let collapseNamedOutbounds = selectionMode == "first"; clashApiEnabled = (singBoxEnabled || hybridEnabled) && selectionMode != "first"; perAppZapretEnabled = perAppZapretCfg.enable; + zapretCutoffEnabled = + zapretEngine == "zapret2" + && (zapretCfg.enable || perAppZapretEnabled) + && zapretCfg.zapret2.cutoff.enable; + # The cut-off networks no whitelisted name fixes go through the proxy outbound. + zapretCutoffProxyFallback = + zapretCutoffEnabled && zapretCfg.zapret2.cutoff.proxyFallback && hasAvailableOutbounds; userControlEnabled = userControlCfg.allow != [ ]; constants = { zapret2StateDir = "/var/lib/proxy-suite/zapret2"; + zapret2CutoffDir = "/var/lib/proxy-suite/zapret2/cutoff"; + # Conntrack bit on the cutoff probe's own connections, which zapret2 leaves alone. + zapret2CutoffProbeCtMark = 33554432; # 0x2000000 # NFQUEUE of the global zapret instance, per engine. Both are the engine's own # default, which proxy-suite never overrides; the per-app instance opens a @@ -203,6 +213,8 @@ in zapretEngine perAppZapretCfg perAppZapretEnabled + zapretCutoffEnabled + zapretCutoffProxyFallback userControlCfg userControlEnabled sshProxyCfg diff --git a/modules/proxy-suite/options/zapret2.nix b/modules/proxy-suite/options/zapret2.nix index 82be6d5..8f66f3f 100644 --- a/modules/proxy-suite/options/zapret2.nix +++ b/modules/proxy-suite/options/zapret2.nix @@ -122,5 +122,30 @@ in description = "Log why hosts are or are not learned to zapret-hosts-auto-debug.log."; }; }; + + cutoff = { + enable = mkEnableOption "the 16 KB cutoff probe" // { + default = true; + description = '' + Some lines let TLS to certain hosting networks handshake and then cut it at 12-34 KB, + which no strategy fixes. z2k's probe checks this line against ~110 known targets when + its network changes and once a day, and for each cut-off network searches a whitelisted + name that nfqws2 then puts into a fake ClientHello: thousands of short TLS connections to + foreign hosting IPs per full run, sent directly and never touched by zapret2. State and + maps live in /var/lib/proxy-suite/zapret2/cutoff; `proxy-ctl zapret cutoff` shows them. + ''; + }; + + proxyFallback = mkOption { + type = types.bool; + default = true; + description = '' + Route the cut-off networks no name fixes through the proxy outbound. Needs the sing-box + backend with an outbound and a route mode other than all-bypass, and only applies to + traffic the backend sees by address (TUN, TProxy, per-app routing, clients that dial IPs); + explicit direct rules still win. + ''; + }; + }; }; } diff --git a/modules/proxy-suite/service/context.nix b/modules/proxy-suite/service/context.nix index 452ee15..629acf4 100644 --- a/modules/proxy-suite/service/context.nix +++ b/modules/proxy-suite/service/context.nix @@ -37,6 +37,8 @@ let perAppRoutingTproxy perAppZapretCfg zapretEngine + zapretCutoffEnabled + zapretCutoffProxyFallback userControlCfg selectionMode builtinTags @@ -98,6 +100,7 @@ let selectionMode collapseNamedOutbounds constants + zapretCutoffProxyFallback ; inherit jq @@ -163,6 +166,7 @@ let perAppRoutingTproxy perAppZapretCfg zapretEngine + zapretCutoffEnabled constants selectionMode userControlCfg diff --git a/modules/proxy-suite/service/control.nix b/modules/proxy-suite/service/control.nix index 9f698af..759055c 100644 --- a/modules/proxy-suite/service/control.nix +++ b/modules/proxy-suite/service/control.nix @@ -8,6 +8,7 @@ perAppRoutingTproxy, perAppZapretCfg, zapretEngine, + zapretCutoffEnabled, constants, selectionMode, userControlCfg, @@ -44,6 +45,7 @@ perAppRoutingZapretEnabled = if perAppZapretCfg.enable then "1" else "0"; zapretAutoEnabled = if zapretEngine == "zapret2" then "1" else "0"; zapretStateDir = constants.zapret2StateDir; + zapretCutoffEnabled = if zapretCutoffEnabled then "1" else "0"; priorityOutboundFile = constants.priorityOutboundFile; outboundInventoryFile = constants.outboundInventoryFile; runtimeOutboundsDir = constants.runtimeOutboundsDir; diff --git a/modules/proxy-suite/service/scripts.nix b/modules/proxy-suite/service/scripts.nix index 6e1e09a..766d93f 100644 --- a/modules/proxy-suite/service/scripts.nix +++ b/modules/proxy-suite/service/scripts.nix @@ -14,6 +14,7 @@ selectionMode, collapseNamedOutbounds, constants, + zapretCutoffProxyFallback, jq, python3, singBox, @@ -159,6 +160,7 @@ let hybridEnabled pureXrayEnabled constants + zapretCutoffProxyFallback jq singBox xray diff --git a/modules/proxy-suite/service/start-scripts.nix b/modules/proxy-suite/service/start-scripts.nix index 5213bcc..c554fcc 100644 --- a/modules/proxy-suite/service/start-scripts.nix +++ b/modules/proxy-suite/service/start-scripts.nix @@ -10,6 +10,7 @@ hybridEnabled, pureXrayEnabled, constants, + zapretCutoffProxyFallback, jq, singBox, xray, @@ -143,6 +144,7 @@ let xraySidecarBasePort ? xraySidecarBasePorts.socks, xrayDnsBridgePort ? xrayDnsBridgePorts.socks, enableAutoProxy ? false, + enableZapretCutoff ? false, }: pkgs.writeShellScript name '' set -euo pipefail @@ -234,6 +236,20 @@ let fi ''} + ${lib.optionalString enableZapretCutoff '' + # zapret2's cutoff probe lists the prefixes of cut-off networks no whitelisted + # name gets through; the proxy carries those. sing-box refuses a missing + # rule-set path and reloads the file when the probe renames a new one in. + if [ "$ROUTE_MODE" != all-bypass ]; then + CUTOFF_RULE_SET=${lib.escapeShellArg "${constants.zapret2CutoffDir}/proxy.json"} + install -d -m 0755 "$(dirname "$CUTOFF_RULE_SET")" + [ -s "$CUTOFF_RULE_SET" ] || echo '{"version":1,"rules":[]}' > "$CUTOFF_RULE_SET" + AUTOPROXY_RULE_SETS_JSON=$(${jq} -c --arg p "$CUTOFF_RULE_SET" \ + '. + [{type: "local", format: "source", tag: "zapret-cutoff", path: $p}]' <<< "$AUTOPROXY_RULE_SETS_JSON") + AUTOPROXY_RULES_JSON=$(${jq} -c '. + [{rule_set: ["zapret-cutoff"], outbound: "proxy"}]' <<< "$AUTOPROXY_RULES_JSON") + fi + ''} + ${jq} \ --argjson obs "$OUTBOUNDS_JSON" \ --argjson probe_inbounds "$PROBE_INBOUNDS_JSON" \ @@ -309,6 +325,7 @@ let xrayDnsBridgePort = xrayDnsBridgePorts.socks; # Only the socks unit: relayed traffic reaches it, and the prober needs one home. enableAutoProxy = proxyCfg.autoProxy.enable && !pureXrayEnabled; + enableZapretCutoff = zapretCutoffProxyFallback && !pureXrayEnabled; }; startTun = mkStartScript { diff --git a/modules/proxy-suite/zapret2.nix b/modules/proxy-suite/zapret2.nix index fd1f3f5..ca599cb 100644 --- a/modules/proxy-suite/zapret2.nix +++ b/modules/proxy-suite/zapret2.nix @@ -15,6 +15,7 @@ let zapretCfg = cfg.zapret; perAppZapretCfg = cfg.perAppRouting.zapret; + cutoffCfg = zapretCfg.zapret2.cutoff; inherit (import ./derived.nix { inherit lib cfg; }) constants; awgServiceNames = map (name: "proxy-suite-awg-${name}.service") ( builtins.attrNames cfg.amneziaWg.profiles @@ -46,10 +47,11 @@ let let exemptCidrs = lib.optionals zapretCfg.cidrExemption.enable zapretCfg.cidrExemption.cidrs; in - if perAppZapretCfg.enable || tunInterfaces != [ ] || exemptCidrs != [ ] then + if perAppZapretCfg.enable || tunInterfaces != [ ] || exemptCidrs != [ ] || cutoffCfg.enable then runtime.mkCustomScript { inherit tunInterfaces exemptCidrs; excludeMark = if perAppZapretCfg.enable then perAppZapretCfg.filterMark else null; + probeCtMark = if cutoffCfg.enable then constants.zapret2CutoffProbeCtMark else null; } else null; @@ -75,6 +77,16 @@ let touch ${runtime.autoHostlistFile} ${runtime.userHostlistFile} ${runtime.excludeHostlistFile} ''; + cutoff = import ./zapret2/cutoff.nix { + inherit + lib + pkgs + cfg + zapret2Sources + nft + ; + }; + perAppZapretMarkUpScript = pkgs.writeShellScript "proxy-suite-per-app-zapret2-mark-up" '' set -euo pipefail ${nft} delete table inet proxy_suite_per_app_zapret_mark 2>/dev/null || true @@ -89,6 +101,9 @@ in { environment.systemPackages = lib.mkBefore [ runtime.package ]; + systemd.services.proxy-suite-zapret2-cutoff = lib.mkIf cutoffCfg.enable cutoff.service; + systemd.timers.proxy-suite-zapret2-cutoff = lib.mkIf cutoffCfg.enable cutoff.timer; + systemd.services.zapret-discord-youtube = lib.mkIf zapretCfg.enable (mkOneshotService { description = "zapret2 DPI bypass"; after = [ "network-online.target" ]; diff --git a/modules/proxy-suite/zapret2/cutoff.nix b/modules/proxy-suite/zapret2/cutoff.nix new file mode 100644 index 0000000..2fbd8de --- /dev/null +++ b/modules/proxy-suite/zapret2/cutoff.nix @@ -0,0 +1,169 @@ +# 16 KB cutoff: some lines let TLS to certain hosting networks handshake and then +# kill the connection at 12-34 KB, which no strategy rotation fixes. z2k-detect +# probes which networks (AS) this line cuts and finds a whitelisted name per network +# that z2k-tcp16.lua puts into a fake ClientHello; the networks no name fixes go to +# a sing-box rule-set that sends them through the proxy. +{ + lib, + pkgs, + cfg, + zapret2Sources, + nft, +}: + +let + inherit (import ../derived.nix { inherit lib cfg; }) constants; + z2k = zapret2Sources.z2k; + lists = "${z2k}/files/lists"; + dir = constants.zapret2CutoffDir; + unit = "proxy-suite-zapret2-cutoff"; + table = "proxy_suite_cutoff_probe"; + + z2kDetect = pkgs.buildGoModule { + pname = "z2k-detect"; + version = "0-unstable-${z2k.shortRev or "local"}"; + src = "${z2k}/z2k-detect"; + vendorHash = "sha256-9Qv6O6/8MP9N44GPSDULEV/NcrHdEC3YZmIQmpaa1nE="; + subPackages = [ "cmd/z2k-detect" ]; + # Its tests open raw sockets and dial out. + doCheck = false; + meta.mainProgram = "z2k-detect"; + }; + + # $1 asn.txt, $2 sni.txt, $3 AS-to-prefix map; prints a sing-box source rule-set + # with the prefixes of every cut-off network that no whitelisted name fixes. + proxyRules = pkgs.writeShellScript "proxy-suite-zapret2-cutoff-rules" '' + set -euo pipefail + ${pkgs.gawk}/bin/awk -F'\t' ' + FILENAME == ARGV[1] { if ($1 ~ /^[0-9]+$/) cut[$1] = 1; next } + FILENAME == ARGV[2] { if ($1 ~ /^[0-9]+$/) named[$1] = 1; next } + ($1 in cut) && !($1 in named) { print $2 } + ' "$1" "$2" "$3" \ + | ${pkgs.jq}/bin/jq -R -s -c ' + split("\n") | map(select(. != "")) + | {version: 1, rules: (if length > 0 then [{ip_cidr: .}] else [] end)}' + ''; + + probe = pkgs.writeShellScript unit '' + set -euo pipefail + export PATH=${ + lib.makeBinPath [ + pkgs.coreutils + pkgs.curl + pkgs.diffutils + pkgs.gnugrep + pkgs.jq + pkgs.systemd + z2kDetect + ] + } + cd ${lib.escapeShellArg dir} + touch asn.txt sni.txt + force=0 + if [ -e force ]; then + force=1 + rm -f force + fi + now=$(date +%s) + + # The name map belongs to this line: probe again when its network changes, else + # once a day. No answer (offline, captive portal) is no reason to probe. + info=$(curl -sS --max-time 10 https://ipinfo.io/json 2>/dev/null || true) + egress=$(jq -r '((.org // "") | split(" ")[0]) as $as + | if ($as | test("^AS[0-9]+$")) then $as else (.ip // "") end' <<<"$info" 2>/dev/null || true) + if [ -z "$egress" ]; then + echo "no answer about this line's address; not probing" + exit 0 + fi + if [ "$force" = 0 ] && [ "$egress" = "$(cat egress 2>/dev/null || true)" ] && + [ $((now - $(cat ts 2>/dev/null || echo 0))) -lt 86400 ]; then + exit 0 + fi + echo "probing the line from $egress" + + rc=0 + z2k-detect tcp16 -targets ${lists}/tcp16_targets.txt -parallel 50 -asn-out asn.new || rc=$? + case "$rc" in + 1) + rc=0 + z2k-detect tcp16 -targets ${lists}/tcp16_targets.txt -scan ${lists}/sni_wl_candidates.txt \ + -per-asn -batch 5 -parallel 50 -sni-out sni.new || rc=$? + # 2: no network took any name; the list of cut-off networks still stands. + if [ "$rc" != 0 ] && [ "$rc" != 2 ]; then + rm -f asn.new sni.new + echo "name search failed ($rc); keeping the previous maps" + exit 0 + fi + [ -e sni.new ] || : >sni.new + ;; + 0) + : >asn.new + : >sni.new + ;; + *) + rm -f asn.new + echo "the probe did not run ($rc); keeping the previous maps" + exit 0 + ;; + esac + + changed=0 + { cmp -s asn.new asn.txt && cmp -s sni.new sni.txt; } || changed=1 + mv -f asn.new asn.txt + mv -f sni.new sni.txt + ${proxyRules} asn.txt sni.txt ${lists}/tcp16_nets.txt >proxy.json.tmp + mv -f proxy.json.tmp proxy.json + printf '%s\n' "$egress" >egress + printf '%s\n' "$now" >ts + echo "cut-off networks: $(grep -c '^[0-9]' asn.txt || true), with a name: $(grep -c '^[0-9]' sni.txt || true)" + + # 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 + fi + ''; + + # The probe measures this line, so its traffic leaves directly: proxyMark keeps it + # out of TUN/TProxy like the backend's own, and the conntrack bit keeps zapret2 from + # touching or learning it. + exempt = pkgs.writeShellScript "${unit}-exempt" '' + set -euo pipefail + ${nft} delete table inet ${table} 2>/dev/null || true + ${nft} -f - < ${lists}" - "${args "NFQWS_ARGS"} ${lists}" + "${withSniPick (args "NFQWS_ARGS")} ${lists}" ]; blobArgs = map (replaceStrings [ "@/opt/etc/nfqws2/" ] [ "@${src}/etc/nfqws2/" ]) ( filter (hasPrefix "--blob=") (words (quoted "NFQWS_BASE_ARGS")) @@ -116,6 +127,10 @@ let cp "$root/extra_strats/TCP/RKN/Discord.txt" "$root/extra_strats/TCP_Discord.txt" cp ${src}/files/lists/cf_extra_check_ips.txt "$root/lists/" : >"$root/lists/discovered-domains.txt" + # Its cutoff name step is emitted only on lines its probe flagged; ours is + # a no-op until the cutoff probe writes maps, so emit it unconditionally. + mkdir -p "$root/state" + echo 1 >"$root/state/tcp16.flag" # The generator wires its detectors only when their Lua files exist. ln -s ${src}/files/lua/*.lua "$root/lua/" diff --git a/nix/checks/repo.nix b/nix/checks/repo.nix index 41e62e0..ff8ae55 100644 --- a/nix/checks/repo.nix +++ b/nix/checks/repo.nix @@ -260,6 +260,18 @@ run list | grep -q 'No hostnames learned' test ! -s "$state/circular/state.tsv" + # The cutoff verdict names each cut-off network's way through. + mkdir -p "$state/cutoff" + printf '1789000000\n' > "$state/cutoff/ts" + printf 'AS12389\n' > "$state/cutoff/egress" + printf '24940\n14061\n' > "$state/cutoff/asn.txt" + printf '24940\t300.ya.ru\n' > "$state/cutoff/sni.txt" + cutoff=$(env ZAPRET_CUTOFF_ENABLED=1 ZAPRET_STATE_DIR="$state" bash "$proxy_ctl" zapret cutoff) + printf '%s\n' "$cutoff" | grep -q 'from AS12389$' + printf '%s\n' "$cutoff" | grep -qx 'Cutoff: 2 network(s)' + printf '%s\n' "$cutoff" | grep -qE '^ AS24940 +300\.ya\.ru$' + printf '%s\n' "$cutoff" | grep -qE '^ AS14061 +no name - proxy fallback$' + # Without the zapret2 engine there is nothing to inspect. ! env ZAPRET_AUTO_ENABLED=0 ZAPRET_STATE_DIR="$state" bash "$proxy_ctl" zapret auto list diff --git a/nix/checks/zapret2.nix b/nix/checks/zapret2.nix index f3367b7..4e56ba0 100644 --- a/nix/checks/zapret2.nix +++ b/nix/checks/zapret2.nix @@ -18,6 +18,7 @@ let zapret2Tuned zapret2NoAuto zapret2Z2k + zapret2NoFallback ; envValue = @@ -42,6 +43,8 @@ let tunedRuntime = runtimeDir zapret2Tuned globalService; noAutoRuntime = runtimeDir zapret2NoAuto globalService; z2kRuntime = runtimeDir zapret2Z2k globalService; + cutoffProbe = zapret2Global.config.systemd.services.proxy-suite-zapret2-cutoff.serviceConfig.ExecStart; + socksStart = fixture: fixture.config.systemd.services.proxy-suite-socks.serviceConfig.ExecStart; proxyCtlEnv = fixture: (mkProxyCtlDerived fixture).wrapperEnv; in @@ -76,6 +79,16 @@ in true ) + # The cutoff probe comes with zapret2, on a timer, and nfqws2 reads its maps. + ( + assert zapret2Global.config.systemd.timers ? proxy-suite-zapret2-cutoff; + assert !(zapretDiscordYoutubeGlobal.config.systemd.services ? proxy-suite-zapret2-cutoff); + assert (proxyCtlEnv zapret2Global).ZAPRET_CUTOFF_ENABLED == "1"; + assert + envValue zapret2Global globalService "Z2K_TCP16_ASN=" == "/var/lib/proxy-suite/zapret2/cutoff/asn.txt"; + true + ) + # `proxy-ctl zapret auto` only makes sense when something is learning. ( assert (proxyCtlEnv zapret2Global).ZAPRET_AUTO_ENABLED == "1"; @@ -140,6 +153,31 @@ in test "$(grep -oF -- '' "${z2kRuntime}/config" | wc -l)" = 1 grep -qF -- '/lists/whitelist.txt --hostlist-exclude=/var/lib/proxy-suite/zapret2/zapret-hosts-user-exclude.txt' "${z2kRuntime}/config" + # --- 16 KB cutoff --------------------------------------------------- + # The whitelisted-name step runs ahead of rotation, in both sources. + grep -qF -- '/files/lua/z2k-tcp16.lua' "${globalRuntime}/config" + grep -qF -- 'blob=z2k_ch:optional:repeats=8:tcp_ts=-1000 --lua-desync=circular:fails=2:time=300' "${globalRuntime}/config" + grep -qF -- 'blob=z2k_ch:optional:repeats=8:tcp_ts=-1000 --lua-desync=circular:fails=3:time=60:key=rkn_tcp' "${z2kRuntime}/config" + # zapret2 leaves the probe's own connections alone, in every chain. + test "$(grep -c 'ct mark and 0x2000000 != 0 return' "${globalRuntime}/init.d/sysv/custom.d/50-proxy-suite-custom.sh")" = 4 + + # Cut-off networks without a name become the proxy's rule-set; named ones stay with zapret2. + rules=$(grep -o '/nix/store/[^ ]*-proxy-suite-zapret2-cutoff-rules' ${cutoffProbe} | head -n1) + printf '# networks\n24940\n14061\n' >asn.txt + printf '24940\t300.ya.ru\n' >sni.txt + printf '# map\n24940\t5.9.0.0/16\n14061\t104.131.0.0/16\n14061\t2604:a880::/32\n7777\t1.2.0.0/16\n' >nets.txt + test "$("$rules" asn.txt sni.txt nets.txt)" = '{"version":1,"rules":[{"ip_cidr":["104.131.0.0/16","2604:a880::/32"]}]}' + printf '24940\t300.ya.ru\n14061\tad.adriver.ru\n' >sni.txt + test "$("$rules" asn.txt sni.txt nets.txt)" = '{"version":1,"rules":[]}' + + # The probe is built from z2k's source and knows its tcp16 mode. + detect=$(grep -o '/nix/store/[^:]*-z2k-detect-[^:/]*/bin' ${cutoffProbe} | head -n1) + "$detect/z2k-detect" tcp16 -h 2>/dev/null + + # 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 + # --- per-app instance ------------------------------------------------- # Wrapped apps opted in explicitly, so no hostlist gates them. grep -qx 'MODE_FILTER=none' "${perAppRuntime}/config" diff --git a/nix/checks/zapret2/fixtures.nix b/nix/checks/zapret2/fixtures.nix index 7c4b2e3..1424bcc 100644 --- a/nix/checks/zapret2/fixtures.nix +++ b/nix/checks/zapret2/fixtures.nix @@ -63,4 +63,9 @@ in enable = true; zapret2.strategySource = "z2k"; }; + + zapret2NoFallback = mkZapret2 { + enable = true; + zapret2.cutoff.proxyFallback = false; + }; } diff --git a/pkgs/proxy-ctl-lib.sh b/pkgs/proxy-ctl-lib.sh index 5d7671e..6daa0b3 100644 --- a/pkgs/proxy-ctl-lib.sh +++ b/pkgs/proxy-ctl-lib.sh @@ -51,6 +51,8 @@ A group without a verb shows its status or list. zapret auto add|forget|exclude pin, forget, or never learn a host (sudo) zapret auto clear forget learned hosts and strategies (sudo) + zapret cutoff [status] networks this line cuts at 16 KB, and their names + zapret cutoff probe probe this line again now (sudo) awg [list] AmneziaWG profiles and their state awg on | off [profile] | restart [profile] @@ -80,9 +82,11 @@ _complete_tree() { "proxy subs rm") _runtime_tags subscription ;; "proxy select") printf 'auto\n'; _outbound_tags ;; "proxy mode") printf '%s\n' default whitelist blacklist all-proxy all-bypass ;; - "proxy tun" | "proxy tproxy" | zapret | ssh) printf '%s\n' status on off ;; + "proxy tun" | "proxy tproxy" | ssh) printf '%s\n' status on off ;; + zapret) printf '%s\n' status on off auto cutoff ;; "proxy auto") printf '%s\n' list probe learn queue ;; "zapret auto") printf '%s\n' list add forget exclude clear ;; + "zapret cutoff") printf '%s\n' status probe ;; "zapret auto forget" | "zapret auto exclude") cat "$(_zapret_auto_file zapret-hosts-auto.txt)" ;; awg) printf '%s\n' list on off restart ;; "awg on" | "awg off" | "awg restart") printf '%s\n' "${AWG_PROFILES[@]}" ;; @@ -1070,6 +1074,11 @@ cmd_zapret() { cmd_zapret_auto "$@" return fi + if [ "${1:-}" = cutoff ]; then + shift + cmd_zapret_cutoff "$@" + return + fi _toggle zapret-discord-youtube zapret "$@" } @@ -1153,6 +1162,42 @@ cmd_zapret_auto() { esac } +# --- zapret cutoff ------------------------------------------------------------- +# +# The 16 KB cutoff probe's verdict: which networks this line cuts after the +# handshake, and the whitelisted name that gets each through. Networks without a +# name are what the proxy fallback routes. + +cmd_zapret_cutoff() { + local dir="${ZAPRET_STATE_DIR:-/var/lib/proxy-suite/zapret2}/cutoff" verb="${1:-status}" cut + [ "${ZAPRET_CUTOFF_ENABLED:-0}" = 1 ] || _die "The cutoff probe needs zapret.engine = \"zapret2\" with zapret2.cutoff.enable." + case "$verb" in + status) ;; + probe) + : >"$dir/force" 2>/dev/null || _die "Cannot write $dir - re-run with sudo." + echo "Probing this line; this takes a few minutes..." + systemctl start proxy-suite-zapret2-cutoff.service || + _die "The probe failed. Details: journalctl -u proxy-suite-zapret2-cutoff -n 30" + ;; + *) _usage "zapret cutoff [status|probe]" ;; + esac + if ! [ -s "$dir/ts" ]; then + echo "Not probed yet." + return + fi + echo "Probed: $(date -d "@$(cat "$dir/ts")" '+%F %R') from $(cat "$dir/egress" 2>/dev/null)" + cut=$(grep -c '^[0-9]' "$dir/asn.txt" 2>/dev/null || true) + if [ "${cut:-0}" = 0 ]; then + echo "Cutoff: none on this line" + return + fi + echo "Cutoff: ${cut} network(s)" + awk -F'\t' ' + FILENAME == ARGV[1] { if ($1 ~ /^[0-9]+$/) name[$1] = $2; next } + $1 ~ /^[0-9]+$/ { printf " AS%-8s %s\n", $1, ($1 in name ? name[$1] : "no name - proxy fallback") } + ' "$dir/sni.txt" "$dir/asn.txt" +} + # --- where ------------------------------------------------------------------- # # What the runtime state says about one host. Hostlists and autoProxy both key diff --git a/pkgs/proxy-ctl.nix b/pkgs/proxy-ctl.nix index f15f5ed..9525ead 100644 --- a/pkgs/proxy-ctl.nix +++ b/pkgs/proxy-ctl.nix @@ -26,6 +26,7 @@ inboundsSubscriptionsBaseUrl, zapretAutoEnabled, zapretStateDir, + zapretCutoffEnabled, priorityOutboundFile, outboundInventoryFile, runtimeOutboundsDir, @@ -63,6 +64,7 @@ let INBOUNDS_SUB_BASE_URL = inboundsSubscriptionsBaseUrl; ZAPRET_AUTO_ENABLED = zapretAutoEnabled; ZAPRET_STATE_DIR = zapretStateDir; + ZAPRET_CUTOFF_ENABLED = zapretCutoffEnabled; PRIORITY_OUTBOUND_FILE = priorityOutboundFile; OUTBOUND_INVENTORY_FILE = outboundInventoryFile; RUNTIME_OUTBOUNDS_DIR = runtimeOutboundsDir;