From f648b19cddaeef41d7b3316002d2f29555deb671 Mon Sep 17 00:00:00 2001 From: Frisk Date: Mon, 9 Dec 2024 20:45:54 +0100 Subject: [PATCH] Add a funny hook --- extensions/hooks/__init__.py | 3 +- extensions/hooks/distinguish_ips.py | 100 ++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 extensions/hooks/distinguish_ips.py diff --git a/extensions/hooks/__init__.py b/extensions/hooks/__init__.py index 2430e19..2aeb4ae 100644 --- a/extensions/hooks/__init__.py +++ b/extensions/hooks/__init__.py @@ -18,4 +18,5 @@ #import extensions.hooks.edit_alerts #import extensions.hooks.buttons #import extensions.hooks.talk_notify -#import extensions.hooks.db_connection \ No newline at end of file +#import extensions.hooks.db_connection +#import extensions.hooks.distinguish_ips \ No newline at end of file diff --git a/extensions/hooks/distinguish_ips.py b/extensions/hooks/distinguish_ips.py new file mode 100644 index 0000000..250b587 --- /dev/null +++ b/extensions/hooks/distinguish_ips.py @@ -0,0 +1,100 @@ +# This file is part of Recent changes Goat compatible Discord webhook (RcGcDw). +# +# RcGcDw is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# RcGcDw 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with RcGcDw. If not, see . + +from src.api.context import Context +from src.discord.message import DiscordMessage, DiscordMessageMetadata +from src.api.hook import post_hook +from src.configloader import settings +import ipaddress + +ip_distinguish = settings.get("hooks", {}).get("distinguish", {}) +tracked_ip_pool: list = [] +tracked_usernames: list = [] + +# Adds distinguishment for special IP addresses such as governmental ones or from private IP ranges +# { +# "hooks": { +# "distinguish": { +# "icon": "ICON URL", +# "emoji": "EMOJI", +# "disable_default": false, +# "additional_targets": [ +# "USERNAME or IP RANGE" +# ] +# } +# } +# } + +# From https://en.wikipedia.org/wiki/Wikipedia:Blocking_IP_addresses +default_ips = ["143.228.0.0/16", "143.231.0.0/16", "137.18.0.0/16", "12.185.56.0/29", "12.147.170.144/28", + "74.119.128.0/22", "2620:0:e20::/46", "156.33.0.0/16", "2620:0:8a0::/48", "2600:803:618::/48", + "165.119.0.0/16", "198.137.240.0/23", "204.68.207.0/24", "2620:10F:B000::/40", "149.101.0.0/16", + "2607:f330::/32", "65.165.132.0/24", "204.248.24.0/24", "216.81.80.0/20", "2600:400::/32", + "131.132.0.0/14", "131.136.0.0/14", "131.140.0.0/15", "192.197.82.0/24", "194.60.0.0/18", "138.162.0.0/16"] + +if ip_distinguish.get("disable_default", False): + tracked_ip_pool.extend(default_ips) + +for target in ip_distinguish.get("additional_targets", []): + try: + if "/" in target: + ipaddress.ip_network(target) + else: + ipaddress.ip_address(target) + except ValueError: + tracked_usernames.append(target) + + +def convert_ip(ip: str): + return ipaddress.ip_network(ip) if "/" in ip else ipaddress.ip_address(ip) + + +# Convert to networks for faster comparison later +tracked_ip_pool = [convert_ip(item) for item in tracked_ip_pool] + + +def hit_or_miss(username): + if username is None: + return False + try: + if ipaddress.ip_address(username).is_private: + return True + for network in tracked_ip_pool: + if ipaddress.ip_address(username) in network: + return True + except ValueError: + return username in tracked_usernames + + +def edit_message(message): + if message.message_type == "compact": + warning = ip_distinguish.get("emoji", "⚠️") + message.webhook_object["content"] = "{warning} {message} {warning}".format(warning=warning, + message=message.webhook_object[ + "content"]) + else: + message.embed["author"]["icon_url"] = ip_distinguish.get("emoji", + "https://icon-library.com/images/warning-icon-svg/warning-icon-svg-27.jpg") + + +@post_hook +def distinguish_hook(message: DiscordMessage, metadata: DiscordMessageMetadata, context: Context, change: dict): + if not ip_distinguish or context.event == "suppressed": + return + if hit_or_miss(change.get("user")): + edit_message(message) + if context.feed_type in ["recentchanges", "abuselog"] and change["ns"] in [2, 3, 202, 1200] and "/" not in change["title"]: + if hit_or_miss(change["title"].split(':', 1)[1]): + edit_message()