]> git.ipfire.org Git - dbl.git/commitdiff
sources: Support parsing hosts files
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 10 Dec 2025 12:41:13 +0000 (12:41 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 10 Dec 2025 12:41:13 +0000 (12:41 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/dnsbl/sources.py

index 54d460474a05de5ea0ef9e18a047a2e0c43d3021..fa5d6d4bd4d7e0376d8efe83f957ceb6877be316 100644 (file)
@@ -35,7 +35,8 @@ log = logging.getLogger(__name__)
 
 class Format(enum.Enum):
        PLAIN = 1
-       ADBLOCKPLUS = 2
+       HOSTS = 2
+       ADBLOCKPLUS = 3
 
 
 class Sources(object):
@@ -193,6 +194,9 @@ class Source(sqlmodel.SQLModel, database.BackendMixin, table=True):
                                                        case Format.ADBLOCKPLUS:
                                                                domain = self._process_adblockplus(line)
 
+                                                       case Format.HOSTS:
+                                                               domain = self._process_hosts(line)
+
                                                        case Format.PLAIN:
                                                                domain = line
 
@@ -256,6 +260,10 @@ class Source(sqlmodel.SQLModel, database.BackendMixin, table=True):
                if line == "[Adblock Plus]":
                        return Format.ADBLOCKPLUS
 
+               # Is this a hosts file?
+               elif line.startswith("0.0.0.0 "):
+                       return Format.HOSTS
+
                # Check for a plain FQDN
                elif util.is_fqdn(line):
                        return Format.PLAIN
@@ -276,6 +284,12 @@ class Source(sqlmodel.SQLModel, database.BackendMixin, table=True):
 
                        return domain
 
+       def _process_hosts(self, line):
+               """
+                       Parses a line of a hosts file.
+               """
+               return line.removeprefix("0.0.0.0 ")
+
        def add_domain(self, name):
                """
                        Adds or updates a domain.