]> git.ipfire.org Git - dbl.git/commitdiff
sources: Support other prefixes of the host format
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 22 Dec 2025 12:43:34 +0000 (12:43 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 22 Dec 2025 12:43:34 +0000 (12:43 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/dnsbl/sources.py

index 54b16560688cce5e6187cef32f1d58faea3fff82..a8269b96b5671a4c37db6a2be1422eedebd47926 100644 (file)
@@ -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):
                """