diff --git a/README.md b/README.md index 2f1f920..f281224 100755 --- a/README.md +++ b/README.md @@ -135,7 +135,8 @@ To do: - https://github.com/matrix-org/synapse/blob/master/docs/admin_api/delete_group.md - https://matrix-org.github.io/synapse/v1.38/admin_api/rooms.html#make-room-admin-api - https://matrix-org.github.io/synapse/latest/admin_api/server_notices.html -https://matrix-org.github.io/synapse/latest/usage/administration/admin_api/federation.html#destination-rooms +- https://matrix-org.github.io/synapse/latest/admin_api/event_reports.html +- https://matrix-org.github.io/synapse/latest/usage/administration/admin_api/federation.html#destination-rooms 2) Add fully automated (should just return a web link and decryption password) reporting functions for users: - Description of why the report was made (what happened), include key information - Any other usernames associated with that IP diff --git a/docs/server_functions.md b/docs/server_functions.md index 677ebef..c13d12e 100644 --- a/docs/server_functions.md +++ b/docs/server_functions.md @@ -11,9 +11,9 @@ This guide provides detailed steps for server-side operations that use the datab --- -### 1. Delete and Block Specific Media +100) **Delete and Block Specific Media.** -This command allows an admin to delete a specific media on their Matrix Synapse server and block it to prevent future accesses. +> This command allows an admin to delete a specific media on their Matrix Synapse server and block it to prevent future accesses. #### Process Flow: @@ -33,7 +33,7 @@ $ ssh matrix.perthchat.org "true > ...path to file..." $ ssh matrix.perthchat.org "chattr +i ...path to file..." ``` -### 2. Purge Remote Media Repository +101) **Purge Remote Media Repository** This command purges the remote media repository for a certain range of days. Process Flow: @@ -49,7 +49,7 @@ $ date --date '149 days ago' +%s $ curl -X POST --header "Authorization: Bearer ACCESS_TOKEN" '... Matrix Synapse purge endpoint ...' ``` -### 3. Prepare Database for Copying Events of Multiple Rooms +102) **Prepare Database for Copying Events of Multiple Rooms** This command prepares the PostgreSQL database to export events from multiple Matrix rooms. Process Flow: @@ -81,4 +81,20 @@ After copying the data, ensure to clean up the RAM disk: ```bash $ rm -r /matrix/postgres/data/ramdisk/* $ umount /matrix/postgres/data/ramdisk -``` \ No newline at end of file +``` + +103) **Show last 10 reported events.** + +> Gets the last 10 reported events using the event reports API and returns it. + + https://matrix-org.github.io/synapse/latest/admin_api/event_reports.html#show-reported-events + +104) **Paginate all reported events.** + +> Combines all the events into a large JSON and returns it. + +105) **Show details of a specific event report** + +> This API returns information about a specific event report. + + https://matrix-org.github.io/synapse/latest/admin_api/event_reports.html#show-details-of-a-specific-event-report diff --git a/moderation_tool.py b/moderation_tool.py index 74483b6..634c5a8 100755 --- a/moderation_tool.py +++ b/moderation_tool.py @@ -73,21 +73,22 @@ while pass_token == False: print("19) Check if user account exists.\t\t68) Block a room.") print("20) Shadow ban a user.\t\t\t\t69) Unblock a room.") print("21) Find a user by their 3PID.") - print("\n#### Server Commands ####\t\t\t\t\t#### Report Generation ####") - print("100) Delete and block a specific media.\t\t\t\t150) Generate user report.") - print("101) Purge remote media repository up to a certain date.\t151) Lookup homeserver admin contact details.") - print("102) Prepare database for copying events of multiple rooms.\t152) Send a test email (to yourself).") - print("\t\t\t\t\t\t\t\t153) Sent a test Matrix message (to yourself).") - print("#### rdlist - General ####\t\t\t\t\t154) Send test incident reports (to yourself).") - print("120) Block all rooms with specific rdlist tags.") + print("\n#### Server Commands ####\t\t\t\t\t#### ipinfo.io ####") + print("100) Delete and block a specific media.\t\t\t\t140) Analyse a users country of origin.") + print("101) Purge remote media repository up to a certain date.\t141) Analyse multiple users country of origin.") + print("102) Prepare database for copying events of multiple rooms.") + print("103) Show last 10 reported events.\t\t\t\t#### Report Generation ####") + print("104) Get all reported events.\t\t\t\t\t150) Generate user report.") + print("105) Get details of a reported event.\t\t\t\t151) Lookup homeserver admin contact details.") + print("\t\t\t\t\t\t\t\t152) Send a test email (to yourself).") + print("#### rdlist - General ####\t\t\t\t\t153) Send a test Matrix message (to yourself).") + print("120) Block all rooms with specific rdlist tags.\t\t\t154) Send test incident reports (to yourself).") print("121) Get rdlist tags for a room.") - print("\n#### rdlist - Recommended Tags ####\nFor rdlist rooms with recommended tags, the following actions are available:") + print("\n#### rdlist - Recommended Tags ####") + print("For rdlist rooms with recommended tags, the following actions are available:") print("130) Collect User Reports on local accounts in rdlist rooms.") print("131) Send Incident Reports on remote accounts in rdlist rooms.") print("132) Block/Purge all rdlist rooms.") - print("\n#### ipinfo.io ####") - print("140) Analyse a users country of origin.") - print("141) Analyse multiple users country of origin.") print("\nPlease enter a number from the above menu, or enter 'q' or 'e' to exit.\n") menu_input = input() if menu_input == "1": @@ -202,6 +203,15 @@ while pass_token == False: server_commands.purge_remote_media_repo() elif menu_input == "102": server_commands.prepare_database_copy_of_multiple_rooms() + elif menu_input == "103": + reported_events = server_commands.get_reported_events(10) + print(json.dumps(reported_events, indent=4, sort_keys=True)) + elif menu_input == "104": + all_reported_events = server_commands.paginate_reported_events() # Again assuming default values are set + print(json.dumps(all_reported_events, indent=4, sort_keys=True)) + elif menu_input == "105": + report_details = server_commands.get_event_report_details() + print(json.dumps(report_details, indent=4, sort_keys=True)) elif menu_input == "120": rdlist_commands.block_all_rooms_with_rdlist_tags(False,'','','') elif menu_input == "121": diff --git a/server_commands.py b/server_commands.py index 4ddfacf..0c89b4c 100644 --- a/server_commands.py +++ b/server_commands.py @@ -136,3 +136,70 @@ 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 get_reported_events(limit=100, _from=0, dir='b', user_id=None, room_id=None): + url = f"https://{hardcoded_variables.homeserver_url}/_synapse/admin/v1/event_reports" + + headers = { + "Content-Type": "application/json", + "Authorization": f"Bearer {hardcoded_variables.access_token}" + } + + params = { + 'limit': limit, + 'from': _from, + 'dir': dir + } + + if user_id: + params['user_id'] = user_id + + if room_id: + params['room_id'] = room_id + + response = requests.get(url, headers=headers, params=params) + + if response.status_code == 200: + return response.json() + else: + print(f"Error fetching reported events: {response.status_code}, {response.text}") + return None + +def paginate_reported_events(limit=100, dir='b', user_id=None, room_id=None): + _from = 0 + all_reports = [] + + while True: + reports = get_reported_events(limit=limit, _from=_from, dir=dir, user_id=user_id, room_id=room_id) + if not reports or "event_reports" not in reports: + break + + all_reports.extend(reports["event_reports"]) + + if "next_token" in reports: + _from = reports["next_token"] + else: + break + + return all_reports + +def get_event_report_details(preset_report_id=''): + if preset_report_id == '': + report_id = input("\nEnter the report_id of the report you wish to query (Example: 56): ") + elif preset_report_id != '': + report_id = preset_report_id + + url = f"https://{hardcoded_variables.homeserver_url}/_synapse/admin/v1/event_reports/{report_id}" + + headers = { + "Content-Type": "application/json", + "Authorization": f"Bearer {hardcoded_variables.access_token}" + } + + response = requests.get(url, headers=headers) + + if response.status_code == 200: + return response.json() + else: + print(f"Error fetching event report details: {response.status_code}, {response.text}") + return None