fix: zapret2 improvements
This commit is contained in:
24 files changed
+1350
-99
No files matched your search
@@ -57,6 +57,9 @@ let
|
||||
amneziaWgInboundsRuntime = import ./checks/amnezia-wg-inbound-runtime.nix {
|
||||
inherit pkgs proxySuiteModule;
|
||||
};
|
||||
zapret2Runtime = import ./checks/zapret2-runtime.nix {
|
||||
inherit pkgs proxySuiteModule;
|
||||
};
|
||||
in
|
||||
moduleSuiteChecks
|
||||
// {
|
||||
@@ -72,6 +75,7 @@ moduleSuiteChecks
|
||||
amneziawg-runtime = amneziaWgRuntime;
|
||||
proxy-inbounds-runtime = proxyInboundsRuntime;
|
||||
amneziawg-inbounds-runtime = amneziaWgInboundsRuntime;
|
||||
zapret2-runtime = zapret2Runtime;
|
||||
}
|
||||
// pythonChecks
|
||||
// repoChecks
|
||||
@@ -0,0 +1,184 @@
|
||||
# zapret2's learning and verdicts against a censor in the middle (detect.lua): a router
|
||||
# that blocks one site two connections out of three, cuts another after 16 KB, and drops
|
||||
# every connection to an address. The client must learn the first two, record that the
|
||||
# first works once it gets through, and hand the address to the proxy until it answers.
|
||||
# Public test ranges throughout: detect.lua never judges private addresses.
|
||||
{
|
||||
pkgs,
|
||||
proxySuiteModule,
|
||||
}:
|
||||
|
||||
let
|
||||
state = "/var/lib/proxy-suite/zapret2";
|
||||
direct = "/var/lib/proxy-suite/zapret2-direct";
|
||||
|
||||
cert = pkgs.runCommand "zapret2-runtime-cert" { nativeBuildInputs = [ pkgs.openssl ]; } ''
|
||||
mkdir -p "$out"
|
||||
openssl req -x509 -newkey rsa:2048 -nodes -days 3650 -subj /CN=fine.test \
|
||||
-addext "subjectAltName=DNS:fine.test,DNS:flaky.test,DNS:stall.test,DNS:blocked.test" \
|
||||
-keyout "$out/key.pem" -out "$out/cert.pem"
|
||||
'';
|
||||
# Big enough for the success detector (past 4 KB in) and, at /big, for the cutoff.
|
||||
site = pkgs.runCommand "zapret2-runtime-site" { } ''
|
||||
mkdir -p "$out"
|
||||
head -c 65536 /dev/zero | tr '\0' a >"$out/index.html"
|
||||
head -c 1048576 /dev/zero | tr '\0' b >"$out/big"
|
||||
'';
|
||||
|
||||
address = interface: addresses: {
|
||||
networking.interfaces.${interface}.ipv4.addresses = map (address: {
|
||||
inherit address;
|
||||
prefixLength = 24;
|
||||
}) addresses;
|
||||
};
|
||||
in
|
||||
pkgs.testers.runNixOSTest {
|
||||
name = "proxy-suite-zapret2-runtime";
|
||||
|
||||
nodes = {
|
||||
server = {
|
||||
imports = [
|
||||
(address "eth1" [
|
||||
"203.0.113.2"
|
||||
"203.0.113.3"
|
||||
])
|
||||
];
|
||||
virtualisation.vlans = [ 2 ];
|
||||
networking.useDHCP = false;
|
||||
networking.defaultGateway = "203.0.113.1";
|
||||
networking.firewall.allowedTCPPorts = [ 443 ];
|
||||
services.nginx = {
|
||||
enable = true;
|
||||
virtualHosts.default = {
|
||||
default = true;
|
||||
onlySSL = true;
|
||||
sslCertificate = "${cert}/cert.pem";
|
||||
sslCertificateKey = "${cert}/key.pem";
|
||||
root = site;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
router = {
|
||||
imports = [
|
||||
(address "eth1" [ "198.51.100.1" ])
|
||||
(address "eth2" [ "203.0.113.1" ])
|
||||
];
|
||||
virtualisation.vlans = [
|
||||
1
|
||||
2
|
||||
];
|
||||
networking.useDHCP = false;
|
||||
networking.firewall.enable = false;
|
||||
networking.nftables.enable = false;
|
||||
boot.kernel.sysctl."net.ipv4.ip_forward" = 1;
|
||||
environment.systemPackages = [ pkgs.iptables ];
|
||||
};
|
||||
|
||||
client = {
|
||||
imports = [
|
||||
proxySuiteModule
|
||||
(address "eth1" [ "198.51.100.2" ])
|
||||
];
|
||||
virtualisation.vlans = [ 1 ];
|
||||
networking.useDHCP = false;
|
||||
networking.defaultGateway = "198.51.100.1";
|
||||
networking.hosts."203.0.113.2" = [
|
||||
"fine.test"
|
||||
"flaky.test"
|
||||
"stall.test"
|
||||
];
|
||||
networking.hosts."203.0.113.3" = [ "blocked.test" ];
|
||||
environment.systemPackages = [ pkgs.curl ];
|
||||
services.proxy-suite = {
|
||||
enable = true;
|
||||
# An outbound to hand what zapret2 cannot fix to; nothing here dials it.
|
||||
proxy = {
|
||||
enable = true;
|
||||
backend = "sing-box";
|
||||
outbounds = [
|
||||
{
|
||||
tag = "primary";
|
||||
url = "http://192.0.2.1:8080";
|
||||
}
|
||||
];
|
||||
};
|
||||
zapret = {
|
||||
enable = true;
|
||||
engine = "zapret2";
|
||||
zapret2 = {
|
||||
strategySource = "z2k";
|
||||
# The probe dials the internet.
|
||||
cutoff.enable = false;
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
testScript = ''
|
||||
start_all()
|
||||
server.wait_for_unit("nginx.service")
|
||||
router.wait_for_unit("multi-user.target")
|
||||
|
||||
# The censor. Per connection, by the name in its ClientHello: flaky.test passes one
|
||||
# in three; stall.test is cut once 16 KB came back; 203.0.113.3 never answers.
|
||||
router.succeed(
|
||||
"iptables -t mangle -A FORWARD -p tcp --dport 443 -m connmark --mark 0 -m string --string flaky.test --algo bm"
|
||||
" -m statistic --mode nth --every 3 --packet 0 -j CONNMARK --set-mark 3",
|
||||
"iptables -t mangle -A FORWARD -p tcp --dport 443 -m connmark --mark 0 -m string --string flaky.test --algo bm"
|
||||
" -j CONNMARK --set-mark 2",
|
||||
"iptables -t mangle -A FORWARD -p tcp --dport 443 -m connmark --mark 0 -m string --string stall.test --algo bm"
|
||||
" -j CONNMARK --set-mark 4",
|
||||
"iptables -A FORWARD -m connmark --mark 2 -j DROP",
|
||||
"iptables -A FORWARD -p tcp --sport 443 -m connmark --mark 4"
|
||||
" -m connbytes --connbytes 16384: --connbytes-dir reply --connbytes-mode bytes -j DROP",
|
||||
"iptables -A FORWARD -d 203.0.113.3 -p tcp --syn -j DROP",
|
||||
# Byte counts for connbytes; the rules above loaded conntrack.
|
||||
"sysctl -w net.netfilter.nf_conntrack_acct=1",
|
||||
)
|
||||
|
||||
client.wait_for_unit("proxy-suite-zapret.service")
|
||||
client.wait_until_succeeds("curl -sk --max-time 5 -o /dev/null https://fine.test/")
|
||||
|
||||
def curl(host, path="/", seconds=8):
|
||||
client.execute(f"curl -sk --noproxy '*' --max-time {seconds} -o /dev/null https://{host}{path}")
|
||||
|
||||
def learned(host):
|
||||
return f"grep -qx {host} ${state}/zapret-hosts-auto.txt"
|
||||
|
||||
with subtest("a site blocked two times in three is learned, successes in between"):
|
||||
for _ in range(9):
|
||||
curl("flaky.test")
|
||||
client.wait_until_succeeds(learned("flaky.test"), timeout=60)
|
||||
|
||||
with subtest("a transfer cut after 16 KB is learned"):
|
||||
for _ in range(4):
|
||||
curl("stall.test", "/big", 12)
|
||||
client.wait_until_succeeds(learned("stall.test"), timeout=60)
|
||||
|
||||
with subtest("a working site is not"):
|
||||
for _ in range(4):
|
||||
curl("fine.test", "/big")
|
||||
client.fail(learned("fine.test"))
|
||||
|
||||
with subtest("an address that never answers goes to the proxy, until it does"):
|
||||
for _ in range(4):
|
||||
curl("blocked.test", "/", 5)
|
||||
client.wait_until_succeeds("grep -qP '^blocked\\t203\\.0\\.113\\.3\\tip' ${state}/verdicts.tsv", timeout=60)
|
||||
client.wait_until_succeeds("grep -qF 203.0.113.3/32 ${direct}/proxy.json", timeout=60)
|
||||
client.succeed("proxy-ctl zapret auto | grep -F 'blocked by address'")
|
||||
router.succeed("iptables -D FORWARD -d 203.0.113.3 -p tcp --syn -j DROP")
|
||||
curl("blocked.test")
|
||||
client.wait_until_succeeds("grep -qP '^reachable\\t203\\.0\\.113\\.3\\tip' ${state}/verdicts.tsv", timeout=60)
|
||||
client.wait_until_succeeds("! grep -qF 203.0.113.3 ${direct}/proxy.json", timeout=60)
|
||||
|
||||
with subtest("a learned site goes direct once a strategy gets through for it"):
|
||||
for _ in range(9):
|
||||
curl("flaky.test")
|
||||
client.wait_until_succeeds("grep -qP '^works\\tflaky\\.test\\ttcp' ${state}/verdicts.tsv", timeout=60)
|
||||
client.wait_until_succeeds("grep -qF '\"flaky.test\"' ${direct}/direct.json", timeout=60)
|
||||
# stall.test never got through: it keeps the proxy's route.
|
||||
client.fail("grep -qF stall.test ${direct}/direct.json")
|
||||
'';
|
||||
}
|
||||
+81
-25
@@ -189,7 +189,9 @@ in
|
||||
|
||||
# The NFQUEUE window must exceed the retransmission threshold.
|
||||
grep -qx 'NFQWS2_TCP_PKT_OUT=9' "${globalRuntime}/config"
|
||||
grep -qx 'NFQWS2_TCP_PKT_IN=15' "${globalRuntime}/config"
|
||||
# Wider with detect.lua learning, to see a transfer stall past the 16 KB cutoff.
|
||||
grep -qx 'NFQWS2_TCP_PKT_IN=32' "${globalRuntime}/config"
|
||||
grep -qx 'NFQWS2_TCP_PKT_IN=15' "${tunedRuntime}/config"
|
||||
|
||||
# Learning off means list mode, not "no lists".
|
||||
grep -qx 'MODE_FILTER=hostlist' "${noAutoRuntime}/config"
|
||||
@@ -236,7 +238,7 @@ in
|
||||
'
|
||||
# Every new host or switch reaches state.tsv, or a restart loses it; a z2k bump
|
||||
# that drops the setter must fail the start, not quietly lose switches again.
|
||||
grep -qE -- 'z2k-state-persist\.lua --lua-init=@[^ ]+-persist-every\.lua --lua-init=@[^ ]+-strategy-log\.lua' "${z2kRuntime}/config"
|
||||
grep -qE -- 'z2k-state-persist\.lua --lua-init=@[^ ]+-persist-every\.lua --lua-init=@[^ ]+-detect\.lua --lua-init=@[^ ]+-strategy-log\.lua' "${z2kRuntime}/config"
|
||||
EVERY=$(grep -oE '/nix/store/[^ ]+-persist-every\.lua' "${globalRuntime}/config") lua -e '
|
||||
local set, flushed, now = nil, 0, 1000
|
||||
local state = { rkn_tcp = { ["example.com"] = { strategy = 1 } } }
|
||||
@@ -286,6 +288,21 @@ in
|
||||
assert(key("1.2.3.4", "2", true) == "standard")
|
||||
assert(key("www.google.com", nil) == "standard")
|
||||
'
|
||||
# What nfqws2's learner and z2k's detector miss: flaky sites, stalled transfers,
|
||||
# unanswered ClientHellos, for learning and for rotation; and the verdicts the
|
||||
# proxy routes by: works, unfixable, blocked by address.
|
||||
mkdir -p detect
|
||||
touch detect/verdicts.tsv
|
||||
DETECT=$(grep -oE '/nix/store/[^ ]+-detect\.lua' "${z2kRuntime}/config") WORK="$PWD/detect" \
|
||||
PROXY_SUITE_ZAPRET2_VERDICTS="$PWD/detect/verdicts.tsv" lua ${./zapret2/detect-test.lua}
|
||||
# nfqws2 finds the verdicts file by its environment; the last profile watches
|
||||
# connections for addresses blocked outright.
|
||||
grep -qF '"PROXY_SUITE_ZAPRET2_VERDICTS=/var/lib/proxy-suite/zapret2/verdicts.tsv"' \
|
||||
<<<'${builtins.toJSON zapret2Z2k.config.systemd.services.proxy-suite-zapret.serviceConfig.Environment}'
|
||||
grep -qE -- " --new --filter-tcp=\* --out-range=<n2 --in-range=<n2 --payload=empty --lua-desync=ps_syn:fails=3:time=300:log'$" "${z2kRuntime}/config"
|
||||
# nfqws2-keenetic learns with detect.lua too: its own profile only reads the auto list.
|
||||
grep -qE -- " --new --filter-tcp=443 --filter-l7=tls --in-range=-s65536 --out-range=-s32768 --payload=all --lua-desync=ps_learn:auto=[^ ]+:exclude=[^ ]+/etc/nfqws2/lists/exclude.list,[^ ]+zapret-hosts-user-exclude.txt:" "${globalRuntime}/config"
|
||||
test "$(grep -oF -- '<HOSTLIST>' "${globalRuntime}/config" | wc -l)" = 0
|
||||
# Counted failures and switches, named by circular key and host; the switch
|
||||
# is the one that stuck, after the state layer's revert.
|
||||
LOG=$(grep -oE '/nix/store/[^ ]+-strategy-log\.lua' "${z2kRuntime}/config") lua -e '
|
||||
@@ -350,8 +367,15 @@ in
|
||||
# YouTube's hosts to yt_tcp/gv_tcp.
|
||||
grep -qF -- '/extra_strats/TCP_Discord.txt <HOSTLIST_NOAUTO> --hostlist-exclude=' "${z2kRuntime}/config"
|
||||
grep -qE -- '<HOSTLIST_NOAUTO> --hostlist-exclude=[^ ]+/extra_strats/TCP/YT/List.txt --hostlist-exclude=[^ ]+/extra_strats/TCP/YT_GV/List.txt --filter-tcp=' "${z2kRuntime}/config"
|
||||
test "$(grep -oF -- '<HOSTLIST>' "${z2kRuntime}/config" | wc -l)" = 1
|
||||
grep -qE -- " --new --filter-tcp=443,2053,2083,2087,2096,8443 --filter-l7=tls --hostlist-exclude=[^ ]+/lists/whitelist.txt --hostlist-exclude=/var/lib/proxy-suite/zapret2/zapret-hosts-user-exclude.txt <HOSTLIST>'$" "${z2kRuntime}/config"
|
||||
# nfqws2's own learner, without extendedDetection.
|
||||
test "$(grep -oF -- '<HOSTLIST>' "${z2kTunedRuntime}/config" | wc -l)" = 1
|
||||
grep -qE -- " --new --filter-tcp=443,2053,2083,2087,2096,8443,8444 --filter-l7=tls --hostlist-exclude=[^ ]+/lists/whitelist.txt --hostlist-exclude=/var/lib/proxy-suite/zapret2/zapret-hosts-user-exclude.txt <HOSTLIST> --new --filter-tcp=\* " "${z2kTunedRuntime}/config"
|
||||
grep -qx 'NFQWS2_TCP_PKT_IN=15' "${z2kTunedRuntime}/config"
|
||||
# detect.lua's in its place by default: same ports, the same auto list and excludes,
|
||||
# and the server's packets past the 16 KB cutoff.
|
||||
test "$(grep -oF -- '<HOSTLIST>' "${z2kRuntime}/config" | wc -l)" = 0
|
||||
grep -qE -- " --new --filter-tcp=443,2053,2083,2087,2096,8443 --filter-l7=tls --in-range=-s65536 --out-range=-s32768 --payload=all --lua-desync=ps_learn:auto=/var/lib/proxy-suite/zapret2/zapret-hosts-auto.txt:exclude=[^ ]+/lists/whitelist.txt,/var/lib/proxy-suite/zapret2/zapret-hosts-user-exclude.txt:fails=3:time=300:inseq=4096:retrans=3:win=32:log --new --filter-tcp=\* " "${z2kRuntime}/config"
|
||||
grep -qx 'NFQWS2_TCP_PKT_IN=32' "${z2kRuntime}/config"
|
||||
grep -qF -- '/lists/whitelist.txt --hostlist-exclude=/var/lib/proxy-suite/zapret2/zapret-hosts-user-exclude.txt' "${z2kRuntime}/config"
|
||||
|
||||
# proxy-ctl names state.tsv's rows by this map: keyed circulars by key, the
|
||||
@@ -431,19 +455,37 @@ in
|
||||
grep -qF 'tag: "zapret-hosts"' ${socksStart zapret2Global}
|
||||
grep -qF 'all-proxy | all-bypass) ;;' ${socksStart zapret2Global}
|
||||
if grep -qF 'zapret-hosts' ${socksStart zapretDiscordYoutubeGlobal}; then exit 1; fi
|
||||
# Pinned and learned hosts less the excluded ones; IP literals as single addresses.
|
||||
mkdir -p direct-lists empty-lists
|
||||
# What zapret2 cannot fix goes to the proxy, ahead of what it does.
|
||||
grep -qF 'tag: "zapret-unfixable"' ${socksStart zapret2Global}
|
||||
test "$(grep -n 'zapret-unfixable' ${socksStart zapret2Global} | head -n1 | cut -d: -f1)" -lt \
|
||||
"$(grep -n 'zapret-hosts' ${socksStart zapret2Global} | head -n1 | cut -d: -f1)"
|
||||
# Pinned hosts, and learned ones a strategy was seen working for, less the excluded;
|
||||
# IP literals as single addresses.
|
||||
mkdir -p direct-lists empty-lists out empty-out
|
||||
printf 'pinned.example\n# a comment\n\n' >direct-lists/zapret-hosts-user.txt
|
||||
printf 'Learned.Example\nexcluded.example\n203.0.113.7\n2001:db8::1\npinned.example\n' >direct-lists/zapret-hosts-auto.txt
|
||||
printf 'Learned.Example\nexcluded.example\n203.0.113.7\n2001:db8::1\npinned.example\nunseen.example\n' >direct-lists/zapret-hosts-auto.txt
|
||||
printf 'excluded.example\n' >direct-lists/zapret-hosts-user-exclude.txt
|
||||
${directSync} direct-lists direct.json
|
||||
test "$(cat direct.json)" = '{"version":1,"rules":[{"domain_suffix":["learned.example","pinned.example"]},{"ip_cidr":["2001:db8::1/128","203.0.113.7/32"]}]}'
|
||||
# Unchanged, the file stays as it was: sing-box reloads on every rename.
|
||||
inode=$(stat -c %i direct.json)
|
||||
${directSync} direct-lists direct.json
|
||||
test "$(stat -c %i direct.json)" = "$inode"
|
||||
${directSync} empty-lists empty.json
|
||||
test "$(cat empty.json)" = '{"version":1,"rules":[]}'
|
||||
{
|
||||
printf 'works\tlearned.example\ttcp\trkn_tcp\t1\nworks\texcluded.example\ttcp\trkn_tcp\t1\n'
|
||||
printf 'works\t203.0.113.7\ttcp\trkn_tcp\t1\n'
|
||||
printf 'stalls\tunseen.example\tcutoff\t\t1\nworks\tunseen.example\ttcp\trkn_tcp\t2\n'
|
||||
printf 'unfixable\tchat.example\ttcp\trkn_tcp\t2\nunfixable\tdiscord.com\tudp\trkn_quic\t2\n'
|
||||
printf 'works\tdiscord.com\ttcp\trkn_tcp\t2\n'
|
||||
printf 'blocked\t149.154.167.99\tip\tx\t3\nblocked\t198.51.100.1\tip\tx\t3\nreachable\t198.51.100.1\tip\t\t4\n'
|
||||
printf 'unfixable\tretried.example\ttcp\t\t5\nretry\tretried.example\ttcp\tproxy-ctl\t6\n'
|
||||
printf 'unfixable\tBAD NAME\ttcp\t\t7\n'
|
||||
} >direct-lists/verdicts.tsv
|
||||
${directSync} direct-lists out
|
||||
test "$(cat out/direct.json)" = '{"version":1,"rules":[{"domain_suffix":["learned.example","pinned.example"]},{"ip_cidr":["203.0.113.7/32"]}]}'
|
||||
# A site's TCP, or only its QUIC; addresses blocked outright.
|
||||
test "$(cat out/proxy.json)" = '{"version":1,"rules":[{"domain_suffix":["chat.example"]},{"network":["udp"],"port":[443],"domain_suffix":["discord.com"]},{"ip_cidr":["149.154.167.99/32"]}]}'
|
||||
# Unchanged, the files stay as they were: sing-box reloads on every rename.
|
||||
inode=$(stat -c %i out/direct.json)
|
||||
${directSync} direct-lists out
|
||||
test "$(stat -c %i out/direct.json)" = "$inode"
|
||||
${directSync} empty-lists empty-out
|
||||
test "$(cat empty-out/direct.json)" = '{"version":1,"rules":[]}'
|
||||
test "$(cat empty-out/proxy.json)" = '{"version":1,"rules":[]}'
|
||||
|
||||
# --- per-app instance -------------------------------------------------
|
||||
# Wrapped apps opted in explicitly, so no hostlist gates them.
|
||||
@@ -490,7 +532,9 @@ in
|
||||
opt=$(printf '%s' "$opt" | sed "s|/var/lib/proxy-suite/zapret2/|$HOSTLIST_BASE/|g")
|
||||
printf '%s\n' "$opt" | tr ' ' '\n' >"$2"
|
||||
|
||||
# --user is deliberately omitted: the sandbox is not root.
|
||||
# --user is deliberately omitted: the sandbox is not root. No globbing, as in
|
||||
# the launcher: --filter-tcp=* is nfqws2's.
|
||||
set -f
|
||||
"$base/nfq2/nfqws2" --dry-run --fwmark="$DESYNC_MARK" \
|
||||
--lua-init=@"$base/lua/zapret-lib.lua" \
|
||||
--lua-init=@"$base/lua/zapret-antidpi.lua" \
|
||||
@@ -499,26 +543,38 @@ in
|
||||
)
|
||||
|
||||
dry_run ${globalRuntime} global.args
|
||||
grep -qx -- "--hostlist-auto=$HOSTLIST_BASE/zapret-hosts-auto.txt" global.args
|
||||
grep -qx -- '--hostlist-auto-fail-threshold=3' global.args
|
||||
grep -qx -- '--hostlist-auto-fail-time=300' global.args
|
||||
# detect.lua learns; the auto list is read like the others.
|
||||
if grep -q -- '^--hostlist-auto=' global.args; then exit 1; fi
|
||||
grep -qx -- "--hostlist=$HOSTLIST_BASE/zapret-hosts-auto.txt" global.args
|
||||
grep -q -- '^--lua-desync=ps_learn:' global.args
|
||||
|
||||
# The supervised launcher execs nfqws2 with the command line the init script builds.
|
||||
printf '#!/bin/sh\nprintf "%%s\\n" "$@"\n' >fake-nfqws2
|
||||
chmod +x fake-nfqws2
|
||||
ZAPRET_RW=${globalRuntime} NFQWS2="$PWD/fake-nfqws2" ${daemonStart} >daemon.args
|
||||
head -n1 daemon.args | grep -qx -- '--user=root'
|
||||
tail -n +6 daemon.args | diff - global.args
|
||||
# dry_run moved the state files into the sandbox, detect.lua's arguments with them.
|
||||
tail -n +6 daemon.args | sed "s|/var/lib/proxy-suite/zapret2/|$HOSTLIST_BASE/|g" | diff - global.args
|
||||
|
||||
dry_run ${z2kRuntime} z2k.args
|
||||
test "$(grep -cx -- "--hostlist-auto=$HOSTLIST_BASE/zapret-hosts-auto.txt" z2k.args)" = 1
|
||||
# Nothing follows the learning profile, so no profile after it can lose to it.
|
||||
test "$(sed -n '/^--hostlist-auto=/,$p' z2k.args | grep -c -- '^--new$')" = 0
|
||||
if sed -n '/^--hostlist-auto=/,$p' z2k.args | grep -q -- '^--lua-desync='; then exit 1; fi
|
||||
# The learner is the last profile: nothing after it, and nfqws2's own autohostlist off.
|
||||
test "$(grep -cx -- "--hostlist-auto=$HOSTLIST_BASE/zapret-hosts-auto.txt" z2k.args)" = 0
|
||||
# Only ps_syn follows it, last: it takes connections before their data names them.
|
||||
test "$(sed -n '/^--lua-desync=ps_learn:/,$p' z2k.args | grep -c -- '^--new$')" = 1
|
||||
tail -n1 z2k.args | grep -q -- '^--lua-desync=ps_syn:'
|
||||
|
||||
# Extra ports and --debug are options nfqws2 takes.
|
||||
# Extra ports and --debug are options nfqws2 takes; nfqws2's own learner, tuned.
|
||||
dry_run ${tunedRuntime} tuned.args
|
||||
grep -qx -- "--hostlist-auto=$HOSTLIST_BASE/zapret-hosts-auto.txt" tuned.args
|
||||
grep -qx -- '--hostlist-auto-fail-threshold=5' tuned.args
|
||||
grep -qx -- '--hostlist-auto-fail-time=120' tuned.args
|
||||
dry_run ${z2kTunedRuntime} z2k-tuned.args >/dev/null
|
||||
test "$(grep -cx -- "--hostlist-auto=$HOSTLIST_BASE/zapret-hosts-auto.txt" z2k-tuned.args)" = 1
|
||||
# Nothing but ps_syn follows the learning profile, so no profile after it can lose
|
||||
# to it: ps_syn takes only what has no protocol yet, which the TLS learner never does.
|
||||
test "$(sed -n '/^--hostlist-auto=/,$p' z2k-tuned.args | grep -c -- '^--new$')" = 1
|
||||
test "$(sed -n '/^--hostlist-auto=/,$p' z2k-tuned.args | grep -c -- '^--lua-desync=')" = 1
|
||||
tail -n1 z2k-tuned.args | grep -q -- '^--lua-desync=ps_syn:'
|
||||
|
||||
touch "$out"
|
||||
'';
|
||||
|
||||
@@ -0,0 +1,236 @@
|
||||
-- detect.lua against stubs of the nfqws2 API it uses: timers fire when the test says.
|
||||
-- $DETECT: the module; $WORK: a writable directory; $PROXY_SUITE_ZAPRET2_VERDICTS in it.
|
||||
local work = os.getenv("WORK")
|
||||
local verdict_file = os.getenv("PROXY_SUITE_ZAPRET2_VERDICTS")
|
||||
local timers = {}
|
||||
function timer_set(name, func, _, _, data) timers[name] = { func = func, data = data } end
|
||||
local function fire(prefix)
|
||||
for name, t in pairs(timers) do
|
||||
if name:sub(1, #prefix) == prefix then
|
||||
timers[name] = nil
|
||||
t.func(name, t.data)
|
||||
end
|
||||
end
|
||||
end
|
||||
TH_FIN, TH_SYN, TH_RST, TH_ACK = 1, 2, 4, 16
|
||||
function ntop(ip) return ip end
|
||||
function dis_ipdst(dis) return dis.dst end
|
||||
function deepcopy(t) return t end
|
||||
function dis_reverse() end
|
||||
local resets = 0
|
||||
function rawsend_dissect() resets = resets + 1 end
|
||||
function is_retransmission(desync) return desync.retrans end
|
||||
function standard_failure_detector() return "standard" end
|
||||
function standard_hostkey(desync) return desync.track.hostname:gsub("^www%.", "") end
|
||||
function automate_failure_check(_, _, crec)
|
||||
crec.nocheck = true
|
||||
return false
|
||||
end
|
||||
function bitand(a, b)
|
||||
local r, bit = 0, 1
|
||||
while a > 0 and b > 0 do
|
||||
if a % 2 == 1 and b % 2 == 1 then r = r + bit end
|
||||
a, b, bit = math.floor(a / 2), math.floor(b / 2), bit * 2
|
||||
end
|
||||
return r
|
||||
end
|
||||
function pos_get(desync, mode) return desync.pos[mode] end
|
||||
function dis_timer_name(dis) return dis.flow end
|
||||
|
||||
-- rotation: what circular and z2k leave in place
|
||||
local counted = {}
|
||||
function automate_failure_counter(hrec, crec, fails)
|
||||
crec.failure = true
|
||||
hrec.failure_counter = (hrec.failure_counter or 0) + 1
|
||||
counted[#counted + 1] = hrec.failure_counter
|
||||
if hrec.failure_counter < fails then return false end
|
||||
hrec.failure_counter = nil
|
||||
return true
|
||||
end
|
||||
local hrec = { nstrategy = 1, ctstrategy = 50 }
|
||||
function automate_host_record() return hrec end
|
||||
function z2k_fail_tls_alert() return "z2k" end
|
||||
|
||||
dofile(os.getenv("DETECT"))
|
||||
|
||||
local auto = work .. "/auto.txt"
|
||||
local excl = work .. "/exclude.txt"
|
||||
local f = io.open(excl, "w")
|
||||
f:write("bank.example\n")
|
||||
f:close()
|
||||
io.open(auto, "w"):close()
|
||||
local arg = { auto = auto, exclude = excl, fails = "3", time = "300", inseq = "4096", win = "32", exclude_domains = "skip.example" }
|
||||
|
||||
local flows = 0
|
||||
local function flow(host)
|
||||
flows = flows + 1
|
||||
return { hostname = host, lua_state = {}, flow = "f" .. flows, pos = { reverse = { tcp = { winsize = 64 } } } }
|
||||
end
|
||||
local function packet(track, outgoing, opts)
|
||||
return {
|
||||
track = track,
|
||||
outgoing = outgoing,
|
||||
arg = arg,
|
||||
l7payload = opts.l7,
|
||||
pos = { n = opts.n or 1, s = opts.s or 1 },
|
||||
dis = { flow = track.flow, tcp = { th_flags = opts.flags or 16 }, payload = opts.payload or "" },
|
||||
}
|
||||
end
|
||||
local function hello(track) ps_learn(nil, packet(track, true, { l7 = "tls_client_hello", payload = "hello" })) end
|
||||
local function reply(track, s, data, n) ps_learn(nil, packet(track, false, { s = s, payload = data, n = n })) end
|
||||
local function record(len) return string.char(23, 3, 3, math.floor(len / 256), len % 256) end
|
||||
local function learned()
|
||||
local t = {}
|
||||
for line in io.lines(auto) do t[line] = true end
|
||||
return t
|
||||
end
|
||||
|
||||
-- A site blocked some of the time: a working connection between failures no longer
|
||||
-- clears them, and the third no-reply learns it.
|
||||
for i = 1, 3 do
|
||||
local t = flow("www.notion.so")
|
||||
hello(t)
|
||||
fire("psln_")
|
||||
if i == 2 then
|
||||
local ok = flow("www.notion.so")
|
||||
hello(ok)
|
||||
reply(ok, 1, record(3) .. "abc")
|
||||
fire("psln_")
|
||||
fire("psls_")
|
||||
end
|
||||
end
|
||||
assert(learned()["www.notion.so"], "flaky site learned")
|
||||
|
||||
-- The 16 KB cutoff: a record announced longer than what arrived, then silence.
|
||||
for _ = 1, 3 do
|
||||
local t = flow("signal.org")
|
||||
hello(t)
|
||||
reply(t, 1, record(16000) .. string.rep("x", 8000), 6)
|
||||
reply(t, 8006, string.rep("x", 4000), 9)
|
||||
fire("psln_")
|
||||
fire("psls_")
|
||||
end
|
||||
assert(learned()["signal.org"], "stalled site learned")
|
||||
do
|
||||
local found = false
|
||||
for line in io.lines(verdict_file) do
|
||||
if line:match("^stalls\tsignal%.org\tcutoff\t") then found = true end
|
||||
end
|
||||
assert(found, "learned from a cut transfer: it keeps the proxy's route")
|
||||
end
|
||||
|
||||
-- A whole record and silence is a finished reply; out of order bytes are put in order.
|
||||
for _ = 1, 3 do
|
||||
local t = flow("fine.example")
|
||||
hello(t)
|
||||
reply(t, 9, string.rep("y", 4), 3)
|
||||
reply(t, 1, record(7) .. "yyy", 2)
|
||||
fire("psln_")
|
||||
fire("psls_")
|
||||
end
|
||||
assert(not learned()["fine.example"], "finished transfers are no failure")
|
||||
|
||||
-- Past the queue window nothing more is seen: no verdict.
|
||||
for _ = 1, 3 do
|
||||
local t = flow("big.example")
|
||||
hello(t)
|
||||
reply(t, 1, record(60000) .. "z", 32)
|
||||
fire("psls_")
|
||||
end
|
||||
assert(not learned()["big.example"], "blind past the window")
|
||||
|
||||
-- A reset early in the reply counts; excluded sites never.
|
||||
for _ = 1, 3 do
|
||||
local t = flow("reset.example")
|
||||
hello(t)
|
||||
ps_learn(nil, packet(t, false, { flags = TH_RST, s = 1 }))
|
||||
for _, host in ipairs({ "www.bank.example", "skip.example" }) do
|
||||
local x = flow(host)
|
||||
hello(x)
|
||||
fire("psln_")
|
||||
end
|
||||
end
|
||||
local got = learned()
|
||||
assert(got["reset.example"] and not got["www.bank.example"] and not got["skip.example"])
|
||||
|
||||
-- A client that hangs up before the answer (a cancelled page) says nothing of the site.
|
||||
for _ = 1, 3 do
|
||||
local t = flow("cancelled.example")
|
||||
hello(t)
|
||||
ps_learn(nil, packet(t, true, { flags = TH_FIN + 16 }))
|
||||
fire("psln_")
|
||||
end
|
||||
assert(not learned()["cancelled.example"], "cancelled connections are no failure")
|
||||
|
||||
-- Rotation: a ClientHello with no reply counts a failure; three switch the strategy.
|
||||
-- A reply, or the detector's own verdict, in time leaves it be.
|
||||
local function tls(outgoing, payload, crec, flags)
|
||||
return z2k_fail_tls_alert({
|
||||
outgoing = outgoing,
|
||||
l7payload = outgoing and payload ~= "" and "tls_client_hello" or nil,
|
||||
track = {},
|
||||
arg = { fails = "3", time = "60" },
|
||||
dis = { flow = "r" .. tostring(crec), tcp = { th_flags = flags }, payload = payload },
|
||||
}, crec)
|
||||
end
|
||||
for _ = 1, 3 do
|
||||
local crec = {}
|
||||
assert(tls(true, "hello", crec) == "z2k")
|
||||
fire("psnr_")
|
||||
end
|
||||
assert(hrec.nstrategy == 2 and #counted == 3, "rotated after three silent ClientHellos")
|
||||
local answered, judged, cancelled = {}, { nocheck = true }, {}
|
||||
tls(true, "hello", answered)
|
||||
tls(false, "server hello", answered)
|
||||
tls(true, "hello", judged)
|
||||
tls(true, "hello", cancelled)
|
||||
tls(true, "", cancelled, TH_RST)
|
||||
fire("psnr_")
|
||||
assert(#counted == 3, "answered, judged or cancelled flows are no failure")
|
||||
assert(standard_failure_detector({ arg = {}, dis = { tcp = {} } }, {}) == "standard", "nfqws2's detector is wrapped too")
|
||||
|
||||
local function verdicts()
|
||||
local t = {}
|
||||
for line in io.lines(verdict_file) do
|
||||
local kind, name, proto = line:match("^(%w+)\t([^\t]+)\t([^\t]*)")
|
||||
t[name .. " " .. proto] = kind
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
-- Fast fail: a ClientHello retransmitted into silence gets the client a reset, and counts.
|
||||
local t = flow("slow.example")
|
||||
hello(t)
|
||||
for _ = 1, 3 do ps_learn(nil, { track = t, outgoing = true, arg = arg, retrans = true, pos = {}, dis = { flow = t.flow, tcp = { th_flags = 16 }, payload = "hello" } }) end
|
||||
assert(resets == 1 and t.lua_state.ps_learn.counted, "reset after three retransmissions")
|
||||
|
||||
-- works: a connection that got through, by the site key, with its protocol.
|
||||
local wrec = { ps_cycle = 4 }
|
||||
automate_failure_check({ arg = { key = "rkn_tcp" }, track = { hostname = "www.notion.so" }, dis = { tcp = {} } }, wrec, {})
|
||||
assert(verdicts()["notion.so tcp"] == "works" and wrec.ps_cycle == 0, "works recorded, the cycle count reset")
|
||||
|
||||
-- unfixable: a whole cycle of strategies without one success stops the rotation.
|
||||
local qrec = { nstrategy = 2, ctstrategy = 2 }
|
||||
autostate = { rkn_quic = { ["discord.com"] = qrec } }
|
||||
automate_failure_counter(qrec, {}, 1)
|
||||
assert(not qrec.final)
|
||||
automate_failure_counter(qrec, {}, 1)
|
||||
assert(qrec.final == 2 and verdicts()["discord.com udp"] == "unfixable", "unfixable after a full cycle, QUIC only")
|
||||
|
||||
-- Addresses: unanswered connections make one blocked, an answer clears it; local ones never.
|
||||
local function syn(ip, answered)
|
||||
local s = flow("")
|
||||
s.hostname = nil
|
||||
local pkt = function(outgoing, flags) return { track = s, outgoing = outgoing, arg = arg, pos = {}, dis = { flow = s.flow, dst = ip, tcp = { th_flags = flags } } } end
|
||||
ps_syn(nil, pkt(true, TH_SYN))
|
||||
if answered then ps_syn(nil, pkt(false, TH_SYN + TH_ACK)) end
|
||||
fire("pssy_")
|
||||
end
|
||||
for _ = 1, 3 do
|
||||
syn("149.154.167.99")
|
||||
syn("192.168.1.1")
|
||||
end
|
||||
assert(verdicts()["149.154.167.99 ip"] == "blocked" and not verdicts()["192.168.1.1 ip"], "blocked by address")
|
||||
syn("149.154.167.99", true)
|
||||
assert(verdicts()["149.154.167.99 ip"] == "reachable", "an answer clears it")
|
||||
print("detect.lua ok")
|
||||
@@ -57,6 +57,7 @@ in
|
||||
failTime = 120;
|
||||
retransThreshold = 4;
|
||||
debugLog = true;
|
||||
extendedDetection = false;
|
||||
};
|
||||
};
|
||||
};
|
||||
@@ -78,6 +79,7 @@ in
|
||||
ports.extraTcp = [ 8444 ];
|
||||
strategyLog = false;
|
||||
debug = true;
|
||||
autoHostlist.extendedDetection = false;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user