From: Michael Tremer Date: Mon, 22 Dec 2025 12:43:34 +0000 (+0000) Subject: sources: Support other prefixes of the host format X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7addd04cd7234e2e769d6be0bc85b7f77b121527;p=dbl.git sources: Support other prefixes of the host format Signed-off-by: Michael Tremer --- diff --git a/src/dnsbl/sources.py b/src/dnsbl/sources.py index 54b1656..a8269b9 100644 --- a/src/dnsbl/sources.py +++ b/src/dnsbl/sources.py @@ -35,6 +35,12 @@ from .i18n import _ # Setup logging log = logging.getLogger(__name__) +HOST_PREFIXES = set(( + "0.0.0.0 ", + "127.0.0.1 ", + "::1 ", +)) + class Format(enum.Enum): PLAIN = 1 HOSTS = 2 @@ -325,7 +331,7 @@ class Source(sqlmodel.SQLModel, database.BackendMixin, table=True): return Format.ADBLOCKPLUS # Is this a hosts file? - elif line.startswith("0.0.0.0 "): + elif any(line.startswith(prefix) for prefix in HOST_PREFIXES): return Format.HOSTS # Check for a plain FQDN @@ -352,7 +358,15 @@ class Source(sqlmodel.SQLModel, database.BackendMixin, table=True): """ Parses a line of a hosts file. """ - return line.removeprefix("0.0.0.0 ") + # Strip any comments + line, _, comment = line.partition("#") + + for prefix in HOST_PREFIXES: + if line.startswith(prefix): + return line.removeprefix(prefix) + + # If none of the prefixes matched, we return the entire line + return line def _process_plain(self, line): """