From 794460146a3bf7d05ea0efe2375ffa0d85a4bac9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sun, 8 Sep 2024 05:02:52 +0200 Subject: [PATCH] Continued: - added "logging" of obfuscated blocked domain/instance names - if it has been deobfuscated, it will be removed --- blocks_empty.db | Bin 40960 -> 57344 bytes fba/commands.py | 16 ++++++++- fba/models/__init__.py | 1 + fba/models/obfuscation.py | 70 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 fba/models/obfuscation.py diff --git a/blocks_empty.db b/blocks_empty.db index 1bc4b2d1bd1b50770e447b2d5c7adb9bd16d6dc9..daea60bdb128ce57d407722a7cf6c087820d420c 100644 GIT binary patch delta 325 zcmZoTz|?Snd4jYcHv$n=gv< zGH(R0Dn~W@E6zEbft-OG8xOLva0)P*Ol+K74^%xDs2>a(`FPmHm6aJAlS>kla#Hh? z(n^by6H7Al^N^Ul&Oxq@A+8D`j!r(V3Q9;aN(vfWoJs|WB_*jvc}koit`Q*$e*Qol z>f@sWlu1lUNlj7W^z;jHb$1PdNKN8nlMV>-^mPo1RPc6<)PQT(W;d3}9VkU;qF`B3EJn delta 88 zcmZoTz}#?vX@ayM2Ll5G7ZAfh=tLc3c@73W?}@zp0Sug+b`1Oxe7*dNyxn|JoR@hc kcvU&=HVX int: logger.debug("row[]='%s'", type(row)) if row is None: logger.warning("Cannot deobfuscate block[blocked]='%s',blocker='%s',software='%s' - SKIPPED!", block["blocked"], blocker, software) + + if not obfuscation.is_added(block["blocked"]): + logger.debug("Invoking add(%s)", block["blocked"]) + obfuscation.add(block["blocked"]) + continue deobfuscated = deobfuscated + 1 @@ -416,6 +422,11 @@ def fetch_blocks(args: argparse.Namespace) -> int: logger.debug("row[]='%s'", type(row)) if row is None: logger.warning("Cannot deobfuscate block[blocked]='%s',blocker='%s',software='%s' - SKIPPED!", block["blocked"], blocker, software) + + if not obfuscation.is_added(block["blocked"]): + logger.debug("Invoking add(%s)", block["blocked"]) + obfuscation.add(block["blocked"]) + continue deobfuscated = deobfuscated + 1 @@ -1441,7 +1452,7 @@ def recheck_obfuscation(args: argparse.Namespace) -> int: logger.debug("blocked[%s]='%s',block[blocked]='%s'", type(blocked), blocked, block["blocked"]) if blocked is not None and blocked != block["blocked"]: - logger.debug("blocked='%s' was deobfuscated to blocked='%s'", block["blocked"], blocked) + logger.debug("block[blocked]='%s' was deobfuscated to blocked='%s'", block["blocked"], blocked) if blacklist.is_blacklisted(blocked): logger.debug("blocked='%s' is blacklisted - SKIPPED!", blocked) @@ -1452,6 +1463,9 @@ def recheck_obfuscation(args: argparse.Namespace) -> int: elif blocks.is_instance_blocked(row["domain"], blocked): logger.debug("blocked='%s' is already blocked by domain='%s' - SKIPPED!", blocked, row["domain"]) continue + elif obfuscation.is_added(block["blocked"]): + logger.debug("Deleting deobfuscated pattern block[blocked]='%s' ...", block["blocked"]) + obfuscation.delete(block["blocked"]) logger.debug("block[block_level]='%s' - BEFORE!", block["block_level"]) block["block_level"] = blocks.alias_block_level(block["block_level"]) diff --git a/fba/models/__init__.py b/fba/models/__init__.py index f72332a..bd24729 100644 --- a/fba/models/__init__.py +++ b/fba/models/__init__.py @@ -17,5 +17,6 @@ __all__ = [ 'blocks', 'error_log', 'instances', + 'obfuscation', 'sources', ] diff --git a/fba/models/obfuscation.py b/fba/models/obfuscation.py new file mode 100644 index 0000000..cdbb19b --- /dev/null +++ b/fba/models/obfuscation.py @@ -0,0 +1,70 @@ +# Fedi API Block - An aggregator for fetching blocking data from fediverse nodes +# Copyright (C) 2023 Free Software Foundation +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +import logging + +import time + +from fba import database + +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + +def is_added(pattern: str) -> bool: + logger.debug("pattern='%s' - CALLED!", pattern) + if not isinstance(pattern, str): + raise ValueError(f"pattern[]='{type(pattern)}' is not of type 'str'") + elif pattern == "": + raise ValueError("Parametern 'pattern' is an empty string") + + database.cursor.execute("SELECT * FROM obfuscation WHERE pattern=? LIMIT 1", [pattern]) + + row = database.cursor.fetchone() + logger.debug("row[]='%s'", row) + + added = (row is not None) + + logger.debug("added='%s' - EXIT!", added) + return added + +def add(pattern: str) -> None: + logger.debug("pattern='%s' - CALLED!", pattern) + if not isinstance(pattern, str): + raise ValueError(f"pattern[]='{type(pattern)}' is not of type 'str'") + elif pattern == "": + raise ValueError("Parametern 'pattern' is an empty string") + elif is_added(pattern): + raise Exception(f"pattern='{pattern}' is already added but function was invoked") + + database.cursor.execute("INSERT INTO obfuscation (pattern, added) VALUES (?, ?)", ( + pattern, + time.time() + )) + + logger.debug("EXIT!") + +def delete (pattern: str) -> None: + logger.debug("pattern='%s' - CALLED!", pattern) + if not isinstance(pattern, str): + raise ValueError(f"pattern[]='{type(pattern)}' is not of type 'str'") + elif pattern == "": + raise ValueError("Parametern 'pattern' is an empty string") + elif not is_added(pattern): + raise Exception(f"pattern='{pattern}' is not added but function was invoked") + + database.cursor.execute("DELETE FROM obfuscation WHERE pattern=? LIMIT 1", [pattern]) + + logger.debug("EXIT!") -- 2.39.5