# 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()