From 63dc5bd6d821b2872cdb111aa15788e57b4022f7 Mon Sep 17 00:00:00 2001 From: PC-Admin Date: Mon, 24 Jul 2023 01:44:41 +0800 Subject: [PATCH] add function to automatically lookup a homeserver admins email address, either the easy way (MSC1929) or the hard way (Your domain registrars whois email) --- README.md | 7 ++++++ moderation_tool.py | 3 +++ server_commands.py | 53 +++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8e2ed76..101ed96 100755 --- a/README.md +++ b/README.md @@ -19,6 +19,13 @@ You can hard code the server URL, federation port and access token into the [har Your access token can be found in Element > Settings > Help & About, your user account must first be upgraded to a server admin. +This script also requires you to install the following PIP packages: +``` +pip3 install python-whois +pip3 install requests +pip3 install pyAesCrypt +``` + *** ## Upgrade user to 'server admin' diff --git a/moderation_tool.py b/moderation_tool.py index 041c531..290d58f 100755 --- a/moderation_tool.py +++ b/moderation_tool.py @@ -57,6 +57,7 @@ while pass_token == False: print("40) Delete and block a specific media.\t\t\t\t70) Generate user report.") print("41) Purge remote media repository up to a certain date.\t\t71) Decrypt user report .zip file.") print("42) Prepare database for copying events of multiple rooms.") + print("43) Lookup homeserver admin contact email.") print("\n#### rdlist ####") print("50) Block all rooms with specific rdlist tags.") print("51) Block all rooms with recommended rdlist tags.") @@ -139,6 +140,8 @@ while pass_token == False: server_commands.purge_remote_media_repo() elif menu_input == "42": server_commands.prepare_database_copy_of_multiple_rooms() + elif menu_input == "43": + server_commands.lookup_homeserver_admin_email('') elif menu_input == "50": rdlist_commands.block_all_rooms_with_rdlist_tags(False,'','','','','') elif menu_input == "51": diff --git a/server_commands.py b/server_commands.py index 05960dd..d212bcc 100644 --- a/server_commands.py +++ b/server_commands.py @@ -1,8 +1,10 @@ +import os import subprocess import csv import time -import os +import json +import whois import requests import datetime import hardcoded_variables @@ -136,3 +138,52 @@ def prepare_database_copy_of_multiple_rooms(): print(chown_command_process.stdout) print("\nThe sql query files have been generated, as postgres user in container run:\n# docker exec -it matrix-postgres /bin/bash\nbash-5.0$ export PGPASSWORD=your-db-password\nbash-5.0$ for f in /var/lib/postgresql/data/ramdisk/*/dump_room_data.sql; do psql --host=127.0.0.1 --port=5432 --username=synapse -w -f $f; done\n\nAfter copying the data to a cloud location law enforcement can access, clean up the ramdisk like so:\n# rm -r /matrix/postgres/data/ramdisk/*\n# umount /matrix/postgres/data/ramdisk") + +def lookup_homeserver_admin_email(preset_homeserver): + if preset_homeserver == '': + homeserver = input("\nEnter the base URL to collect the admin contact details (Example: matrix.org): ") + elif preset_homeserver != '': + homeserver = preset_homeserver + + # Check target homserver for MSC1929 support email + url = f"https://{homeserver}/.well-known/matrix/support" + try: + response = requests.get(url) + except requests.exceptions.RequestException as e: + print(f"Error: Unable to connect to server {homeserver}. Trying WHOIS data...") + response = None + + # If the request was successful, the status code will be 200 + if response and response.status_code == 200: + # Parse the response as JSON + data = json.loads(response.text) + + # Extract the emails from the admins field and remove duplicates + admin_emails = list({admin['email_address'] for admin in data['admins']}) + + print("Admin contact emails for " + homeserver + " are: " + str(admin_emails)) + + # Create a dictionary with homeserver as key and emails as value + email_dict = {homeserver: admin_emails} + + # Convert the dictionary to a JSON string and print it + email_json = json.dumps(email_dict, indent=4) + print("Admin contact emails for " + homeserver + " in JSON format: " + email_json) + + return email_dict, False + else: + print(f"Error: Unable to collect admin email from server {homeserver}") + print("Attempting to collect admin email from WHOIS data...") + + # Get WHOIS data + try: + w = whois.whois(homeserver) + if w.emails: + print("Admin contact email(s) for " + homeserver + " are: " + str(w.emails)) + return {homeserver: list(w.emails)}, True + else: + print(f"Error: Unable to collect admin email from WHOIS data for {homeserver}") + return None, False + except: + print(f"Error: Unable to collect WHOIS data for {homeserver}") + return None, False