fix: zapret2 fixes and xray update

This commit is contained in:
FUFSoB committed 2026-10-23 23:56:33 +05:00
1 parent 20f1bdd110
commit 564ea9e24f
11 files changed
+219 -41

No files matched your search

+8 -4
View File
@@ -162,7 +162,9 @@ in
Learn with proxy-suite's detector instead of nfqws2's. It also learns sites blocked
only some of the time (a working connection no longer clears the failures before it),
downloads that stall partway (the 16 KB cutoff), and requests that never get an
answer. Learned sites are logged with `strategyLog`. Only with the source's own
answer. It also follows learned sites past the first 4 KB a strategy is judged on:
one whose downloads keep stalling keeps the proxy's route (`zapret.directSync`).
Learned sites are logged with `strategyLog`. Only with the source's own
`profiles`, and for TLS: nfqws2-keenetic no longer learns plain HTTP sites.
'';
};
@@ -173,9 +175,11 @@ in
default = true;
description = ''
Work around ISPs that cut TLS to some hosting networks after about 16 KB, which no
strategy fixes. A daily probe finds the affected networks and a whitelisted name that
gets through for each. Only with `strategySource = "z2k"`. Each run makes thousands of
short direct connections. `proxy-ctl zapret cutoff` shows the results.
strategy fixes. A daily probe finds the affected networks, and with
`strategySource = "z2k"` (and its own `profiles`) a whitelisted name that gets
through for each, which z2k puts into a fake ClientHello. Otherwise no name is
searched for, and every affected network is left to `proxyFallback`. Each run makes
thousands of short direct connections. `proxy-ctl zapret cutoff` shows the results.
'';
};
@@ -10,6 +10,22 @@ for f in asn.txt sni.txt asn.new sni.new proxy.json proxy.json.tmp egress ts for
fi
done
touch asn.txt sni.txt
# The rule-set from the maps as they stand, rewritten only when it changes: sing-box
# reloads it on every rename.
rules() {
@proxyRules@ asn.txt sni.txt @lists@/tcp16_nets.txt >proxy.json.tmp
if cmp -s proxy.json.tmp proxy.json; then
rm -f proxy.json.tmp
else
mv -f proxy.json.tmp proxy.json
fi
}
# Names no profile puts in fix nothing: none kept from when one did, so every cut-off
# network goes to the proxy now, not after the next probe.
if [ @names@ = 0 ] && [ -s sni.txt ]; then
: >sni.txt
rules
fi
force=0
if [ -e requests/force ] || [ -L requests/force ]; then
force=1
@@ -59,8 +75,13 @@ z2k-detect tcp16 -targets @lists@/tcp16_targets.txt -parallel 50 -asn-out asn.ne
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=$?
# No profile puts a name in: the cut-off networks are all there is to know.
if [ @names@ = 0 ]; then
: >sni.new
else
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=$?
fi
# 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
@@ -84,8 +105,7 @@ 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
rules
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)"
+6
View File
@@ -33,6 +33,11 @@ let
# The z2k flake.lock holds, whatever `z2k` the consumer passes (pkgs/z2k-detect.nix).
z2kDetect = packages.z2k-detect;
# Only z2k's own profiles put the name in (z2k-tcp16.lua, ahead of rotation; ahead of
# the nfqws2-keenetic strategies it broke hosts, sources.nix). Elsewhere a name found
# fixes nothing: every cut-off network goes to the proxy, and no name is searched for.
namesApply = cfg.zapret.zapret2.strategySource == "z2k" && cfg.zapret.zapret2.profiles == null;
# $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" ''
@@ -59,6 +64,7 @@ let
z2kDetect
];
dir = lib.escapeShellArg dir;
names = if namesApply then "1" else "0";
inherit lists proxyRules;
restartUnits = lib.escapeShellArgs restartUnits;
}
+79 -18
View File
@@ -19,8 +19,9 @@
-- "kind<TAB>name<TAB>proto<TAB>note<TAB>time" (the last line per name and proto wins), for
-- direct-sync.template.sh and proxy-ctl:
-- - works <site> tcp|udp: a strategy got through; only then does a learned site go direct;
-- - stalls <host> cutoff: learned from transfers cut short (the 16 KB cutoff); such a site
-- keeps the proxy's route whatever "works" says, as success is judged on the first 4 KB;
-- - stalls <host> cutoff: learned from transfers cut short (the 16 KB cutoff), or learned
-- otherwise and cut short since (ps_watch); such a site keeps the proxy's route whatever
-- "works" says, as success is judged on the first 4 KB;
-- - unfixable <site> tcp|udp: every strategy failed in turn; the proxy carries it;
-- - blocked <ip> ip: connections to it go unanswered (blocked by address, ps_syn), and
-- reachable <ip> ip once one is answered again;
@@ -355,6 +356,30 @@ local function feed(st, data)
end
end
-- The server's next segment: fed in order, one that came early held until the gap fills.
local function receive(st, desync, payload)
local s = pos_get(desync, "s")
if s == st.next then
feed(st, payload)
st.next = s + #payload
while st.pending[st.next] do
local p = st.pending[st.next]
st.pending[st.next] = nil
feed(st, p)
st.next = st.next + #p
end
elseif s > st.next then
st.pending[s] = payload
end
end
-- The server went quiet inside a TLS record, while the window still showed its packets.
-- Past the NFQUEUE window nothing more is seen: no telling a stall from the rest.
local function cut_short(st, arg)
if st.done or st.notls or st.inpk >= (tonumber(arg.win) or 15) then return false end
return st.need > 0 or #st.hdr > 0 or next(st.pending) ~= nil
end
-- nfqws2's own fast fail: a reset to the client retransmitting into silence, so it
-- gives up (and tries again) instead of waiting out its timeouts.
local function reset_client(desync)
@@ -377,9 +402,7 @@ end
function ps_learn_stall(_, data)
local st = data.st
-- Past the NFQUEUE window nothing more is seen: no telling a stall from the rest.
if st.done or st.notls or st.inpk >= (tonumber(data.arg.win) or 15) then return end
if st.need > 0 or #st.hdr > 0 or next(st.pending) then
if cut_short(st, data.arg) then
failure(st, data.arg, "transfer stalled after " .. math.floor((st.next - 1) / 1024) .. " KB", true)
end
end
@@ -419,19 +442,7 @@ function ps_learn(_, desync)
end
if #payload > 0 then
st.answered = true
local s = pos_get(desync, "s")
if s == st.next then
feed(st, payload)
st.next = s + #payload
while st.pending[st.next] do
local p = st.pending[st.next]
st.pending[st.next] = nil
feed(st, p)
st.next = st.next + #p
end
elseif s > st.next then
st.pending[s] = payload
end
receive(st, desync, payload)
if st.name then
-- Replacing the timer restarts its countdown: it fires once the server goes quiet.
timer_set("psls_" .. st.name, ps_learn_stall, STALL_MS, true, { st = st, arg = desync.arg })
@@ -440,6 +451,56 @@ function ps_learn(_, desync)
if bitand(flags, TH_FIN) ~= 0 then st.done = true end
end
-- --- learned sites cut off ---------------------------------------------------------------
-- A learned site goes direct once a strategy gets it through, judged on the first 4 KB,
-- which the 16 KB cutoff lets through; a site learned for another failure (a ClientHello
-- retransmitted) carries no "stalls" verdict from ps_learn, which sees no listed site.
-- ps_watch, ahead of rotation in the profile that takes the learned sites, follows each
-- transfer on: one cut short `fails` times within `time` seconds is a "stalls" verdict,
-- and the site keeps the proxy's route.
function ps_watch_stall(_, data)
local st = data.st
if not cut_short(st, data.arg) then return end
st.done = true
local n, need = tally("cut " .. st.host, tonumber(data.arg.time) or 300), tonumber(data.arg.fails) or 3
if data.arg.log then
say(st.host .. ": transfer stalled after " .. math.floor((st.next - 1) / 1024) .. " KB (" .. n .. "/" .. need .. " to keep it on the proxy)")
end
if n >= need then
if status[st.host .. " cutoff"] ~= "stalls" then say(st.host .. ": its transfers are cut short, the proxy carries it") end
verdict("stalls", st.host, "cutoff", "transfers cut short")
end
end
function ps_watch(_, desync)
local track, dis = desync.track, desync.dis
if not (track and dis and dis.tcp) or not track.hostname or track.hostname_is_ip then return end
local st = track.lua_state.ps_watch
if not st then
st = { host = track.hostname:lower(), next = 1, need = 0, hdr = "", pending = {}, inpk = 0 }
track.lua_state.ps_watch = st
end
if st.done then return end
local flags = dis.tcp.th_flags
-- A client that hangs up mid-transfer (a cancelled page) says nothing of the site.
if bitand(flags, TH_RST) ~= 0 or (desync.outgoing and bitand(flags, TH_FIN) ~= 0) then
st.done = true
return
end
if desync.outgoing then return end
st.inpk = pos_get(desync, "n") or (st.inpk + 1)
local payload = dis.payload or ""
if #payload > 0 then
receive(st, desync, payload)
-- Named by the server's direction, the one it is reset by.
st.name = st.name or dis_timer_name(dis)
timer_set("psws_" .. st.name, ps_watch_stall, STALL_MS, true, { st = st, arg = desync.arg })
end
if bitand(flags, TH_FIN) ~= 0 then st.done = true end
end
-- --- addresses blocked outright ------------------------------------------------------------
-- No strategy helps where the connection itself goes unanswered: the address is blocked,
+23 -4
View File
@@ -70,11 +70,13 @@ let
# lib.trim drops string context: without it the source's list paths are no build input,
# so lazy trees never materialize them and the closure omits them.
# <STALLWATCH>: where the source's profile for learned sites takes stallWatch.
renderProfile =
profile:
lib.replaceStrings hostlistMarkers (map (
marker: marker + withSpaces staticListArgs
) hostlistMarkers) (lib.addContextFrom profile (lib.trim profile));
lib.replaceStrings (hostlistMarkers ++ [ "<STALLWATCH> " ]) (
map (marker: marker + withSpaces staticListArgs) hostlistMarkers
++ [ (lib.optionalString (stallWatch != "") (stallWatch + " ")) ]
) (lib.addContextFrom profile (lib.trim profile));
# detect.lua learns in place of nfqws2's autohostlist, in a last TLS profile of its
# own: z2k-profiles.template.sh puts it there, tailProfiles for nfqws2-keenetic.
@@ -97,8 +99,25 @@ let
)
);
# detect.lua's ps_watch, ahead of every other instance of the profile that takes the
# learned sites, so rotation (an orchestrator: it runs the instances after it) leaves it
# be; it sees the server's bytes past what rotation judges a success on, and the
# profile's filters are back at nfqws2's defaults after it.
stallWatch = lib.optionalString learnLua (
"--in-range=-s65536 --payload=all --lua-desync=ps_watch:"
+ lib.concatStringsSep ":" (
[
"fails=${toString autoCfg.failThreshold}"
"time=${toString autoCfg.failTime}"
"win=${toString packetWindow.tcpIn}"
]
++ lib.optional zapret2Cfg.strategyLog "log"
)
+ " --in-range=x --payload=all"
);
z2kGenerated = sources.z2k.mkProfiles {
inherit learnArgs;
inherit learnArgs stallWatch;
hostlistSuffix = withSpaces staticListArgs;
# z2k's category profiles filter on their own lists, not on a marker.
excludeSuffix = withSpaces ([ "--hostlist-exclude=${excludeHostlistFile}" ] ++ excludeDomainArgs);
+11 -2
View File
@@ -83,9 +83,12 @@ let
# No 16 KB cutoff name step: its fake ClientHello ahead of these strategies
# broke every host on a line where the strategies alone work. Its HTTP strategy
# has no strategy=N, so circular must not take http_req: it would skip it.
# <STALLWATCH>: it takes the learned sites (runtime.nix). ps_watch there makes
# circular the profile's second instance: keyed as nfqws2 named it before
# (<func>_<profile>_<nth instance>), its sites keep the strategies remembered.
"${
patch "NFQWS_ARGS" "${tcpPayload} ${circular}"
"--in-range=-s5556 ${tcpPayload},tls_server_hello,empty,unknown ${circular} --in-range=x ${tcpPayload}"
"<STALLWATCH> --in-range=-s5556 ${tcpPayload},tls_server_hello,empty,unknown ${circular}:key=circular_3_1 --in-range=x ${tcpPayload}"
(args "NFQWS_ARGS")
} <HOSTLIST> ${lists}"
];
@@ -166,6 +169,7 @@ let
hostlistSuffix,
excludeSuffix,
learnArgs ? "",
stallWatch ? "",
}:
pkgs.runCommand "proxy-suite-zapret2-z2k-profiles" {
nativeBuildInputs = with pkgs; [
@@ -176,7 +180,12 @@ let
gnugrep
gnused
];
inherit hostlistSuffix excludeSuffix learnArgs;
inherit
hostlistSuffix
excludeSuffix
learnArgs
stallWatch
;
} (fillTemplate ./z2k-profiles.template.sh { z2k = src; });
};
in
@@ -44,11 +44,14 @@ grep -q -- '--lua-desync=circular' raw || { echo "z2k generator produced no prof
# instead, in the same last place. It cannot ride along on the autohostlist profile:
# nfqws2 applies nothing there to a host in no list, Lua included. It writes the
# same auto list and reads the same excludes, which it checks itself.
#
# $stallWatch (detect.lua's ps_watch) goes first among the instances of the profile
# that takes the learned sites, ahead of its in-profile filters, which it resets after.
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="$hostlistSuffix" -v esuf="$excludeSuffix" \
-v learn="$learnArgs" -v wlfile="$root/lists/whitelist.txt" '
-v learn="$learnArgs" -v wlfile="$root/lists/whitelist.txt" -v watch="$stallWatch" '
{
for (i = 1; i <= NF; i++) {
t = $i
@@ -57,9 +60,18 @@ awk -v disc="--hostlist=$root/lists/discovered-domains.txt" \
} 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 (!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:/) {
@@ -68,11 +80,12 @@ awk -v disc="--hostlist=$root/lists/discovered-domains.txt" \
for (j = 1; j <= n; j++)
if (p[j] !~ /^(retrans|maxseq|inseq)=/ && p[j] != "reset") t = t ":" p[j]
}
printf "%s%s", (i > 1 ? " " : ""), t
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 {