7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1 | """ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 2 | A mitmproxy script that blocks and removes well known Matrix server |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 3 | information. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 4 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 5 | There are two ways a Matrix server can trigger the client to reconfigure the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 6 | homeserver URL: |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 7 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 8 | 1. By responding to a `./well-known/matrix/client` request with a new |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 9 | homeserver URL. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 10 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 11 | 2. By including a new homeserver URL inside the `/login` response. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 12 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 13 | To run execute it with mitmproxy: |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 14 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 15 | >>> mitmproxy -s well-known-block.py` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 16 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 17 | """ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 18 | import json |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 19 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 20 | from mitmproxy import http |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 21 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 22 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 23 | def request(flow): |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 24 | if flow.request.path == "/.well-known/matrix/client": |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 25 | flow.response = http.HTTPResponse.make( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 26 | 404, # (optional) status code |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 27 | b"Not found", # (optional) content |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 28 | {"Content-Type": "text/html"} # (optional) headers |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 29 | ) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 30 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 31 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 32 | def response(flow: http.HTTPFlow): |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 33 | if flow.request.path == "/_matrix/client/r0/login": |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 34 | if flow.response.status_code == 200: |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 35 | body = json.loads(flow.response.content) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 36 | body.pop("well_known", None) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 37 | flow.response.text = json.dumps(body) |