Upstream's cutoff.nix and cutoff-probe.template.sh: z2k-detect (built from z2k's pinned source for Windows, in zapret2-extras) finds the networks this line cuts after ~16 KB, and with z2k's own strategies a whitelisted name per network; z2k-tcp16.lua inside winws2 reads the maps (Z2K_TCP16_*, their digest in the environment so a new map restarts winws2); what no name fixes goes to the zapret-cutoff rule-set for the proxy (cutoff.proxyFallback). Upstream keeps zapret2 off the probe with an nft cgroup rule. Here z2k-detect is patched to dial from a local port range (Z2K_TCP16_LOCAL_PORTS, 44000-47999, below Windows' dynamic range) and winws2 gets --wf-raw-filter leaving that range alone; under the TUN the probe and the service's own lookups of the line's network go direct. Rounds as upstream's timer: 5 min after start, every 10 min, probing when the line's network (ipinfo.io and ifconfig.co agreeing) changed or a day passed. Request::ZapretCutoff, proxy-ctl zapret cutoff [probe], a card on the DPI page. Verified live on a line that cuts (25 networks, a name for each), the Windows z2k-detect under Wine, winws2 --dry-run and --wf-save with the filter. gui-shot's scroll takes a count. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
261 lines
12 KiB
Bash
Executable File
261 lines
12 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# zapret2's layers beyond the strategies, as upstream's runtime.nix loads them into nfqws2
|
|
# (winws2 here: the same engine, the same Lua): z2k's Lua (a site's working strategy kept
|
|
# across restarts, and its own detectors) and its lists and fake payloads; proxy-suite's own
|
|
# Lua (the extended detector, the per-site key by public suffix, the fail stamp, the
|
|
# strategy log); and z2k's strategies, made by z2k's own generator, as upstream's
|
|
# z2k-profiles.template.sh runs it at build time.
|
|
#
|
|
# What upstream fills in from the configuration at build time is left as a token here and
|
|
# filled in by the service (ps_core::zapret2): @HSUF@ and @ESUF@ (the user's sites and
|
|
# exclusions), @AUTO@ @EXCLUDE@ @FAILS@ @TIME@ @INSEQ@ @RETRANS@ @LEARNEXTRA@ (the
|
|
# learner's arguments), @STALLWATCH@; and the paths, @Z2K@ (z2k's files) and @ROOT@ (the
|
|
# tree its generator wrote), which winws2 reads where the component is installed.
|
|
#
|
|
# tools/build-zapret2-extras.sh OUT_DIR (needs git, bash, gawk, python3, nix, go;
|
|
# Z2K_DETECT_GOOS=linux: z2k-detect for here)
|
|
set -euo pipefail
|
|
. "$(dirname "$0")/lib.sh"
|
|
OUT=$(realpath -m "${1:?output directory}")
|
|
HERE=$(realpath "$(dirname "$0")")
|
|
UPSTREAM=$(realpath "$HERE/../../modules/proxy-suite/zapret2")
|
|
Z2K_COMMIT=7beb9754d65a2cafd9c1d26d382bcb61d453d5b3
|
|
WORK=$(mktemp -d)
|
|
trap 'rm -rf "$WORK"' EXIT
|
|
|
|
clone_commit https://github.com/necronicle/z2k "$Z2K_COMMIT" "$WORK/z2k"
|
|
Z=$WORK/z2k
|
|
rm -rf "$OUT" && mkdir -p "$OUT/z2k/files" "$OUT/lua"
|
|
|
|
# --- z2k's files the strategies and Lua read --------------------------------------------
|
|
cp -r "$Z/files/lua" "$Z/files/lists" "$OUT/z2k/files/"
|
|
[ -d "$Z/files/fake" ] && cp -r "$Z/files/fake" "$OUT/z2k/files/"
|
|
cp "$Z/LICENSE" "$OUT/z2k/LICENSE"
|
|
# Its QUIC silence detector, as upstream's sources.nix patches it: rkn_quic's flows too, and
|
|
# a flow answered past udp_in replies only.
|
|
q=$OUT/z2k/files/lua/z2k-quic-silence.lua
|
|
grep -qF 'local Z2K_QUIC_POOLS = { yt_quic = true, gv_quic = true }' "$q" || { echo "z2k-quic-silence.lua changed"; exit 1; }
|
|
sed -i 's/local Z2K_QUIC_POOLS = { yt_quic = true, gv_quic = true }/local Z2K_QUIC_POOLS = { yt_quic = true, gv_quic = true, rkn_quic = true }/' "$q"
|
|
python3 -I - "$q" <<'PY'
|
|
import sys
|
|
p = sys.argv[1]
|
|
s = open(p).read()
|
|
old = " if not desync.outgoing then\n crec.z2k_quic_answered = true\n"
|
|
new = " 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"
|
|
if old not in s:
|
|
sys.exit("z2k-quic-silence.lua: the answer check changed")
|
|
open(p, "w").write(s.replace(old, new))
|
|
PY
|
|
|
|
# --- the 16 KB cutoff probe: z2k-detect, as upstream's pkgs/z2k-detect.nix builds it --------
|
|
# Its connections leave from the local ports the service names (Z2K_TCP16_LOCAL_PORTS), which
|
|
# winws2's filter leaves out, as upstream's nft rule keeps zapret2 off the probe's cgroup.
|
|
t=$Z/z2k-detect/internal/tcp16/tcp16.go
|
|
grep -qF 'raw, err := d.DialContext(ctx, "tcp", addr)' "$t" || { echo "z2k-detect's tcp16 dial changed"; exit 1; }
|
|
sed -i 's|raw, err := d.DialContext(ctx, "tcp", addr)|raw, err := dialFrom(ctx, \&d, addr)|' "$t"
|
|
cat >"$Z/z2k-detect/internal/tcp16/localport.go" <<'GO'
|
|
package tcp16
|
|
|
|
// Proxy Suite for Windows: the probe's connections from a range of local ports
|
|
// (Z2K_TCP16_LOCAL_PORTS, "lo-hi") that the DPI bypass leaves alone. A port taken
|
|
// (or in TIME_WAIT) is passed over for the next.
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"sync/atomic"
|
|
)
|
|
|
|
var portLo, portHi = func() (int, int) {
|
|
lo, hi, ok := strings.Cut(os.Getenv("Z2K_TCP16_LOCAL_PORTS"), "-")
|
|
a, e1 := strconv.Atoi(lo)
|
|
b, e2 := strconv.Atoi(hi)
|
|
if !ok || e1 != nil || e2 != nil || a < 1 || b > 65535 || a > b {
|
|
return 0, 0
|
|
}
|
|
return a, b
|
|
}()
|
|
|
|
var nextPort atomic.Uint32
|
|
|
|
func dialFrom(ctx context.Context, d *net.Dialer, addr string) (net.Conn, error) {
|
|
if portLo == 0 {
|
|
return d.DialContext(ctx, "tcp", addr)
|
|
}
|
|
n := uint32(portHi - portLo + 1)
|
|
var err error
|
|
for i := 0; i < 64; i++ {
|
|
dd := *d
|
|
dd.LocalAddr = &net.TCPAddr{Port: portLo + int(nextPort.Add(1)%n)}
|
|
c, e := dd.DialContext(ctx, "tcp", addr)
|
|
if e == nil {
|
|
return c, nil
|
|
}
|
|
err = e
|
|
var se *os.SyscallError
|
|
if !errors.As(e, &se) || se.Syscall != "bind" {
|
|
return nil, e
|
|
}
|
|
}
|
|
return nil, err
|
|
}
|
|
GO
|
|
(
|
|
cd "$Z/z2k-detect"
|
|
export GOTOOLCHAIN=local GOFLAGS=-mod=mod CGO_ENABLED=0 GOOS=${Z2K_DETECT_GOOS:-windows} GOARCH=amd64
|
|
ext=; [ "$GOOS" = windows ] && ext=.exe
|
|
go build -trimpath -ldflags "-s -w" -o "$OUT/z2k-detect$ext" ./cmd/z2k-detect
|
|
)
|
|
|
|
# --- proxy-suite's own Lua --------------------------------------------------------------
|
|
cp "$UPSTREAM/detect.lua" "$UPSTREAM/persist-every.lua" "$UPSTREAM/strategy-log.lua" "$OUT/lua/"
|
|
# The suffix list's path from the environment: it is where the component is installed.
|
|
grep -q '"@suffixes@"' "$UPSTREAM/public-suffix-hostkey.lua" || { echo "public-suffix-hostkey.lua changed"; exit 1; }
|
|
sed 's|"@suffixes@"|os.getenv("PROXY_SUITE_PUBLIC_SUFFIXES")|' "$UPSTREAM/public-suffix-hostkey.lua" >"$OUT/lua/public-suffix-hostkey.lua"
|
|
cat >"$OUT/lua/fail-stamp.lua" <<'LUA'
|
|
-- The state layer undoes a rotation when the host succeeded within 30 s, unless a failure
|
|
-- came later; only z2k's own detector stamps that failure. Without the stamp any success on
|
|
-- a busy host (all of googlevideo.com) undoes every rotation. (upstream's runtime.nix)
|
|
local count = automate_failure_counter
|
|
function automate_failure_counter(hrec, crec, fails, maxtime)
|
|
if not (crec and crec.failure) then
|
|
hrec.z2k_last_fail_ts = type(clock_getfloattime) == "function" and clock_getfloattime() or os.time()
|
|
end
|
|
return count(hrec, crec, fails, maxtime)
|
|
end
|
|
LUA
|
|
psl=$(nix build --no-link --print-out-paths --inputs-from "$HERE/.." nixpkgs#publicsuffix-list | head -1)
|
|
python3 -I "$UPSTREAM/public-suffixes.py" "$psl/share/publicsuffix/public_suffix_list.dat" >"$OUT/public_suffixes.txt"
|
|
|
|
# --- z2k's strategies, by its generator ---------------------------------------------------
|
|
root=$WORK/root
|
|
mkdir -p "$root/lists" "$root/lua" "$WORK/conf"
|
|
cp -r "$Z/files/lists/extra_strats" "$root/"
|
|
chmod -R u+w "$root"
|
|
cp "$root/extra_strats/TCP/RKN/Discord.txt" "$root/extra_strats/TCP_Discord.txt"
|
|
cp "$Z/files/lists/cf_extra_check_ips.txt" "$root/lists/"
|
|
: >"$root/lists/discovered-domains.txt"
|
|
mkdir -p "$root/state"
|
|
echo 1 >"$root/state/tcp16.flag"
|
|
ln -s "$Z"/files/lua/*.lua "$root/lua/"
|
|
(
|
|
export ZAPRET2_DIR="$root" CONFIG_DIR="$WORK/conf" LISTS_DIR="$root/lists"
|
|
cd "$Z"
|
|
bash -c '
|
|
. lib/utils.sh && . lib/config.sh && . lib/strategies.sh && . lib/config_official.sh || exit 1
|
|
print_info() { echo "$*" >&2; }
|
|
print_success() { echo "$*" >&2; }
|
|
print_warning() { echo "$*" >&2; }
|
|
print_error() { echo "$*" >&2; }
|
|
create_base_config >&2 &&
|
|
generate_strategies_conf strats_new2.txt "$STRATEGIES_CONF" >&2 &&
|
|
generate_quic_strategies_conf quic_strats.ini "$QUIC_STRATEGIES_CONF" >&2 &&
|
|
create_default_strategy_files >&2 &&
|
|
generate_nfqws2_opt_from_strategies
|
|
'
|
|
) >"$WORK/generated" 2>"$WORK/generator.log" || { tail -20 "$WORK/generator.log"; exit 1; }
|
|
sed -n '/^NFQWS2_OPT="/,/^"$/p' "$WORK/generated" | sed '1s/^NFQWS2_OPT="//; $d' | tr '\n' ' ' >"$WORK/raw"
|
|
grep -q -- '--lua-desync=circular' "$WORK/raw" || { echo "z2k generator produced no profiles"; exit 1; }
|
|
|
|
# As upstream's z2k-profiles.template.sh: its learned-host list becomes ours, its whitelist
|
|
# takes our exclusions, circular's pinned detector args go (autoHostlist fills them), and
|
|
# learning is a last profile of its own; with the extended detector, ps_learn's.
|
|
profiles() {
|
|
local learn=$1 watch=$2
|
|
awk -v disc="--hostlist=$root/lists/discovered-domains.txt" \
|
|
-v wl="--hostlist-exclude=$root/lists/whitelist.txt" \
|
|
-v yt="--hostlist-exclude=$root/extra_strats/TCP/YT/List.txt --hostlist-exclude=$root/extra_strats/TCP/YT_GV/List.txt" \
|
|
-v hsuf=" @HSUF@" -v esuf=" @ESUF@" \
|
|
-v learn="$learn" -v wlfile="$root/lists/whitelist.txt" -v watch="$watch" '
|
|
{
|
|
for (i = 1; i <= NF; i++) {
|
|
t = $i
|
|
if (t == "--new") {
|
|
prof++
|
|
} else if (t ~ /^--filter-tcp=/ && !(prof in ports)) {
|
|
ports[prof] = substr(t, 14)
|
|
}
|
|
pre = ""
|
|
if (t ~ /^--(in-range|out-range|payload|lua-desync)=/) {
|
|
if (watch != "" && seen && prof == learner && !watched++) pre = watch " "
|
|
filtered[prof] = 1
|
|
}
|
|
if (t == disc) {
|
|
t = "<HOSTLIST_NOAUTO>" hsuf
|
|
if (!seen++) {
|
|
t = t " " yt
|
|
learner = prof
|
|
if (prof in filtered) { print "z2k general TLS profile: hostlist after its instances" > "/dev/stderr"; exit 1 }
|
|
}
|
|
} else if (t == wl) {
|
|
t = t esuf
|
|
} else if (t ~ /^--lua-desync=circular:/) {
|
|
n = split(substr(t, 23), p, ":")
|
|
t = "--lua-desync=circular"
|
|
for (j = 1; j <= n; j++)
|
|
if (p[j] !~ /^(retrans|maxseq|inseq)=/ && p[j] != "reset") t = t ":" p[j]
|
|
}
|
|
printf "%s%s%s", (i > 1 ? " " : ""), pre, t
|
|
}
|
|
}
|
|
END {
|
|
if (!seen || !(learner in ports)) { print "z2k general TLS profile not found" > "/dev/stderr"; exit 1 }
|
|
if (watch != "" && !watched) { print "z2k general TLS profile has no instances" > "/dev/stderr"; exit 1 }
|
|
if (learn == "")
|
|
printf " --new --filter-tcp=%s --filter-l7=tls %s%s <HOSTLIST>%s", ports[learner], wl, esuf, hsuf
|
|
else {
|
|
sub(/:exclude=/, ":exclude=" wlfile ",", learn)
|
|
printf " --new --filter-tcp=%s --filter-l7=tls --in-range=-s65536 --out-range=-s32768 --payload=all --lua-desync=ps_learn:%s", ports[learner], learn
|
|
}
|
|
}' "$WORK/raw" >"$WORK/body"
|
|
# rkn_quic: z2k's YouTube QUIC profile over the sites rkn_tcp handles, right after it.
|
|
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 <HOSTLIST_NOAUTO> @HSUF@" '
|
|
{
|
|
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 } }' "$WORK/body"
|
|
}
|
|
profiles "" "" >"$WORK/profiles.plain"
|
|
profiles "auto=@AUTO@:exclude=@EXCLUDE@:fails=@FAILS@:time=@TIME@:inseq=@INSEQ@:retrans=@RETRANS@:win=32@LEARNEXTRA@" "@STALLWATCH@" >"$WORK/profiles.learn"
|
|
awk -v z="$Z" '
|
|
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]] }
|
|
' "$Z/files/S99zapret2.new" >"$WORK/blobs"
|
|
grep -q -- '--blob=' "$WORK/blobs" || { echo "z2k blob registrations not found"; exit 1; }
|
|
|
|
# The paths as tokens; the tree the generator wrote, shipped.
|
|
for f in profiles.plain profiles.learn blobs; do
|
|
sed -e "s|$root|@ROOT@|g" -e "s|$Z|@Z2K@|g" "$WORK/$f" >"$OUT/z2k/$f"
|
|
if grep -q "$WORK" "$OUT/z2k/$f"; then echo "$f still names the build's directory"; exit 1; fi
|
|
done
|
|
# The blobs it names, from z2k's tree, wherever they are in it.
|
|
for b in $(grep -o '@Z2K@/[^ ]*' "$OUT/z2k/blobs" | sed 's|@Z2K@/||'); do
|
|
mkdir -p "$OUT/z2k/$(dirname "$b")" && cp "$Z/$b" "$OUT/z2k/$b"
|
|
done
|
|
cp -rL "$root" "$OUT/z2k/root"
|
|
rm -rf "$OUT/z2k/root/lua"
|
|
# Its ports, from its generator's configuration.
|
|
gen=$(cat "$Z/lib/config_official.sh")
|
|
for name in NFQWS2_PORTS_TCP NFQWS2_PORTS_UDP; do
|
|
printf '%s=%s\n' "$name" "$(sed -n "s/^$name=\"\\(.*\\)\"/\\1/p" "$Z/lib/config_official.sh" | head -1)"
|
|
done >"$OUT/z2k/ports"
|
|
grep -q '^NFQWS2_PORTS_TCP=.\+' "$OUT/z2k/ports" || { echo "z2k's ports not found"; exit 1; }
|
|
echo "$Z2K_COMMIT" >"$OUT/z2k/.commit"
|
|
echo "2026.10.28+z2k.${Z2K_COMMIT:0:7}" >"$OUT/.version"
|
|
echo "zapret2-extras $(cat "$OUT/.version") in $OUT"
|