feat: zapret2 shows strategies applied
This commit is contained in:
8 files changed
+168
-14
No files matched your search
@@ -3618,6 +3618,54 @@ def _zapret_strategy_drop(domain):
|
||||
_replace_lines(path, keep)
|
||||
|
||||
|
||||
def _zapret_strategy_rows():
|
||||
"""state.tsv as (key, host, strategy, mode, sni): the strategy each profile's rotation
|
||||
settled on per domain. The host may carry a |4 or |6 address family."""
|
||||
rows = []
|
||||
for fields in _tsv(_zapret_auto_file("circular/state.tsv")):
|
||||
if len(fields) < 3 or fields[0].startswith("#") or not fields[2].isdigit():
|
||||
continue
|
||||
fields += [""] * (6 - len(fields))
|
||||
rows.append((fields[0], fields[1].split("|", 1)[0], fields[2], fields[4], fields[5]))
|
||||
return rows
|
||||
|
||||
|
||||
def _zapret_strategies_for(domain, rows=None):
|
||||
"""The rows for domain: circular keys them by the domain it rotates on, usually the apex."""
|
||||
return [r for r in (_zapret_strategy_rows() if rows is None else rows) if r[1] and (domain == r[1] or domain.endswith(f".{r[1]}"))]
|
||||
|
||||
|
||||
def _zapret_strategy_map():
|
||||
"""{key: {strategy: [desync instances]}} of the running global instance, or {}."""
|
||||
return read_json_or(env("ZAPRET_STRATEGIES_FILE"), {})
|
||||
|
||||
|
||||
def _zapret_strategy_specs(row, strategies):
|
||||
specs = strategies.get(row[0])
|
||||
specs = specs.get(row[2]) if isinstance(specs, dict) else None
|
||||
return [s for s in specs if isinstance(s, str)] if isinstance(specs, list) else []
|
||||
|
||||
|
||||
def _zapret_strategy_text(row, strategies):
|
||||
""""rkn_tcp #3 (fake + multisplit)": the profile's key, the strategy number, what it does."""
|
||||
key, _, n, mode, sni = row
|
||||
text = f"{key} #{n}"
|
||||
names = " + ".join(dict.fromkeys(s.split(":", 1)[0] for s in _zapret_strategy_specs(row, strategies)))
|
||||
if names:
|
||||
text += f" ({names})"
|
||||
if mode == "frozen":
|
||||
text += ", frozen"
|
||||
if sni:
|
||||
text += f", fake SNI {sni}"
|
||||
return text
|
||||
|
||||
|
||||
def _zapret_strategy_summary(domain, rows=None, strategies=None):
|
||||
"""Every strategy remembered for domain, "; "-joined, or ""."""
|
||||
strategies = _zapret_strategy_map() if strategies is None else strategies
|
||||
return "; ".join(_zapret_strategy_text(r, strategies) for r in _zapret_strategies_for(domain, rows))
|
||||
|
||||
|
||||
def _truncate(path):
|
||||
_replace_lines(path, lambda _: False)
|
||||
|
||||
@@ -3643,10 +3691,14 @@ def cmd_zapret_auto(verb="list", domain="", *_):
|
||||
if verb in ("add", "forget", "exclude", "unpin", "include") and not _zapret_auto_host(domain):
|
||||
usage(f"zapret auto {verb} <domain>")
|
||||
if verb == "list":
|
||||
if os.path.isfile(auto) and os.path.getsize(auto) > 0:
|
||||
sys.stdout.write(read_text(auto))
|
||||
else:
|
||||
hosts = [h for h in lines(read_text(auto)) if h] if os.path.isfile(auto) else []
|
||||
if not hosts:
|
||||
print("No hostnames learned yet.")
|
||||
rows, strategies = _zapret_strategy_rows(), _zapret_strategy_map()
|
||||
width = max(map(len, hosts), default=0)
|
||||
for host in hosts:
|
||||
summary = _zapret_strategy_summary(host, rows, strategies)
|
||||
print(f"{host:<{width}} {summary}" if summary else host)
|
||||
elif verb == "add":
|
||||
_zapret_auto_edit(user, domain, "add")
|
||||
print(f"Pinned {domain}: zapret treats it as blocked.")
|
||||
@@ -3924,6 +3976,12 @@ def cmd_where(domain="", *_):
|
||||
verdict = verdict or "direct, with the zapret bypass"
|
||||
else:
|
||||
_where_row("zapret", "not learned, not pinned, not excluded")
|
||||
# Remembered for hosts a source's own lists cover too, learned or not.
|
||||
strategies = _zapret_strategy_map()
|
||||
for row in _zapret_strategies_for(domain):
|
||||
_where_row("strategy", _zapret_strategy_text(row, strategies))
|
||||
for spec in _zapret_strategy_specs(row, strategies):
|
||||
_where_row("", f" --lua-desync={spec}")
|
||||
|
||||
if local:
|
||||
zapret = verdict == "direct, with the zapret bypass"
|
||||
|
||||
@@ -394,9 +394,11 @@ def zapret_rows(_):
|
||||
if ctl.env("ZAPRET_AUTO_ENABLED") != "1":
|
||||
return []
|
||||
rows = {}
|
||||
strategy_rows, strategies = ctl._zapret_strategy_rows(), ctl._zapret_strategy_map()
|
||||
for kind, name in ZAPRET_LISTS:
|
||||
for host in _lines(name):
|
||||
rows.setdefault(f"{kind}:{host}", {"key": f"{kind}:{host}", "host": host, "kind": kind})
|
||||
strategy = "" if kind == "excluded" else ctl._zapret_strategy_summary(host, strategy_rows, strategies)
|
||||
rows.setdefault(f"{kind}:{host}", {"key": f"{kind}:{host}", "host": host, "kind": kind, "strategy": strategy})
|
||||
return list(rows.values())
|
||||
|
||||
|
||||
@@ -840,7 +842,7 @@ TABS = [
|
||||
"zapret",
|
||||
"zapret",
|
||||
lambda s: "proxy-suite-zapret" in s or ctl.env("ZAPRET_AUTO_ENABLED") == "1",
|
||||
[("host", "Host"), ("kind", "List")],
|
||||
[("host", "Host"), ("kind", "List"), ("strategy", "Strategy")],
|
||||
zapret_rows,
|
||||
summary=zapret_summary,
|
||||
actions=[
|
||||
|
||||
@@ -1222,6 +1222,32 @@ class ZapretAutoTest(EnvTest):
|
||||
ok(ctl.cmd_zapret_auto, "forget", "2001:db8::1")
|
||||
self.assertIn("zapret auto add <domain>", run(ctl.cmd_zapret_auto, "add", "1.2.3")[2])
|
||||
|
||||
def test_list_names_the_strategy_remembered_for_the_apex(self):
|
||||
os.environ.update(ZAPRET_AUTO_ENABLED="1", ZAPRET_STATE_DIR=self.dir)
|
||||
self.write("zapret-hosts-auto.txt", "www.blocked.example\nnew.example\n")
|
||||
self.write(
|
||||
"circular/state.tsv",
|
||||
"# key\thost\tstrategy\tts\tmode\tsni\n"
|
||||
"rkn_tcp\tblocked.example\t3\t1\tfrozen\t\n"
|
||||
"http_rkn\tblocked.example|4\t2\t1\tauto\thcaptcha.com\n",
|
||||
)
|
||||
os.environ["ZAPRET_STRATEGIES_FILE"] = self.write(
|
||||
"strategies.json",
|
||||
{"rkn_tcp": {"3": ["fake:blob=x:repeats=6", "multisplit:pos=1", "fake:blob=y"]}},
|
||||
)
|
||||
out = run(ctl.cmd_zapret_auto, "list")[1].splitlines()
|
||||
self.assertEqual(
|
||||
out,
|
||||
[
|
||||
"www.blocked.example rkn_tcp #3 (fake + multisplit), frozen; http_rkn #2, fake SNI hcaptcha.com",
|
||||
"new.example",
|
||||
],
|
||||
)
|
||||
# A missing or unreadable map still names the profile and the number.
|
||||
os.environ["ZAPRET_STRATEGIES_FILE"] = self.path("gone.json")
|
||||
self.assertIn("rkn_tcp #3, frozen", run(ctl.cmd_zapret_auto, "list")[1])
|
||||
self.assertEqual(ctl._zapret_strategy_summary("notblocked.example"), "")
|
||||
|
||||
|
||||
class InboundsTest(EnvTest):
|
||||
def test_stats(self):
|
||||
|
||||
Reference in new issue
Block a user