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