From: Michael Tremer Date: Wed, 10 Dec 2025 12:41:13 +0000 (+0000) Subject: sources: Support parsing hosts files X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7a126b45074bb7af078c4cfeb4936746a60a60c7;p=dbl.git sources: Support parsing hosts files Signed-off-by: Michael Tremer --- diff --git a/src/dnsbl/sources.py b/src/dnsbl/sources.py index 54d4604..fa5d6d4 100644 --- a/src/dnsbl/sources.py +++ b/src/dnsbl/sources.py @@ -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.