fix: zapret refusing remove ips learned
This commit is contained in:
2 files changed
+22
-1
No files matched your search
@@ -8,6 +8,7 @@ import datetime
|
|||||||
import getpass
|
import getpass
|
||||||
import http.client
|
import http.client
|
||||||
import importlib
|
import importlib
|
||||||
|
import ipaddress
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
@@ -2517,6 +2518,17 @@ def _truncate(path):
|
|||||||
_replace_lines(path, lambda _: False)
|
_replace_lines(path, lambda _: False)
|
||||||
|
|
||||||
|
|
||||||
|
def _zapret_auto_host(host):
|
||||||
|
"""A hostname, or an IP literal: nfqws2 learns those from bare-IP requests."""
|
||||||
|
if HOSTNAME.fullmatch(host):
|
||||||
|
return True
|
||||||
|
try:
|
||||||
|
ipaddress.ip_address(host)
|
||||||
|
except ValueError:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def cmd_zapret_auto(verb="list", domain="", *_):
|
def cmd_zapret_auto(verb="list", domain="", *_):
|
||||||
if env("ZAPRET_AUTO_ENABLED") != "1":
|
if env("ZAPRET_AUTO_ENABLED") != "1":
|
||||||
die('Learned hostlists need zapret.engine = "zapret2" with zapret.global.enable.')
|
die('Learned hostlists need zapret.engine = "zapret2" with zapret.global.enable.')
|
||||||
@@ -2524,7 +2536,7 @@ def cmd_zapret_auto(verb="list", domain="", *_):
|
|||||||
user = _zapret_auto_file("zapret-hosts-user.txt")
|
user = _zapret_auto_file("zapret-hosts-user.txt")
|
||||||
exclude = _zapret_auto_file("zapret-hosts-user-exclude.txt")
|
exclude = _zapret_auto_file("zapret-hosts-user-exclude.txt")
|
||||||
|
|
||||||
if verb in ("add", "forget", "exclude", "unpin", "include") and not HOSTNAME.fullmatch(domain):
|
if verb in ("add", "forget", "exclude", "unpin", "include") and not _zapret_auto_host(domain):
|
||||||
usage(f"zapret auto {verb} <domain>")
|
usage(f"zapret auto {verb} <domain>")
|
||||||
if verb == "list":
|
if verb == "list":
|
||||||
if os.path.isfile(auto) and os.path.getsize(auto) > 0:
|
if os.path.isfile(auto) and os.path.getsize(auto) > 0:
|
||||||
|
|||||||
@@ -767,6 +767,15 @@ class ZapretAutoTest(EnvTest):
|
|||||||
self.assertEqual(ctl.read_text(auto), "")
|
self.assertEqual(ctl.read_text(auto), "")
|
||||||
self.assertEqual(ctl.read_text(state), "")
|
self.assertEqual(ctl.read_text(state), "")
|
||||||
|
|
||||||
|
def test_exclude_takes_learned_ip_literals(self):
|
||||||
|
os.environ.update(ZAPRET_AUTO_ENABLED="1", ZAPRET_STATE_DIR=self.dir)
|
||||||
|
auto = self.write("zapret-hosts-auto.txt", "82.146.44.102\na.example\n")
|
||||||
|
ok(ctl.cmd_zapret_auto, "exclude", "82.146.44.102")
|
||||||
|
self.assertEqual(ctl.read_text(auto), "a.example\n")
|
||||||
|
self.assertEqual(ctl.read_text(self.path("zapret-hosts-user-exclude.txt")), "82.146.44.102\n")
|
||||||
|
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])
|
||||||
|
|
||||||
|
|
||||||
class InboundsTest(EnvTest):
|
class InboundsTest(EnvTest):
|
||||||
def test_stats(self):
|
def test_stats(self):
|
||||||
|
|||||||
Reference in new issue
Block a user