7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1 | """ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 2 | A mitmproxy script that introduces certain request failures in a deterministic |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 3 | way. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 4 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 5 | Used mainly for Matrix style requests. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 6 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 7 | To run execute it with mitmproxy: |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 8 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 9 | >>> mitmproxy -s failures.py` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 10 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 11 | """ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 12 | import time |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 13 | import json |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 14 | import random |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 15 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 16 | from mitmproxy import http |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 17 | from mitmproxy.script import concurrent |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 18 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 19 | REQUEST_COUNT = 0 |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 20 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 21 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 22 | def timeout(flow): |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 23 | timeout = 60 if "sync" in flow.request.pretty_url else 30 |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 24 | time.sleep(timeout) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 25 | return None |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 26 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 27 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 28 | # A map holding our failure modes. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 29 | # The keys are just descriptive names for the failure mode while the values |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 30 | # hold a tuple containing a function that may or may not create a failure and |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 31 | # the probability weight at which rate this failure should be triggered. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 32 | # |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 33 | # The method should return an http.Response if it should modify the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 34 | # response or None if the response should be passed as is. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 35 | FAILURES = { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 36 | "Success": (lambda x: None, 50), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 37 | "Gateway error": |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 38 | (lambda _: http.Response.make(500, b"Gateway error"), 20), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 39 | "Limit exceeded": (lambda _: http.Response.make( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 40 | 429, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 41 | json.dumps({ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 42 | "errcode": "M_LIMIT_EXCEEDED", |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 43 | "error": "Too many requests", |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 44 | "retry_after_ms": 2000 |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 45 | })), 20), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 46 | "Timeout error": (timeout, 10) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 47 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 48 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 49 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 50 | @concurrent |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 51 | def request(flow): |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 52 | global FAILURES |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 53 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 54 | weights = [weight for (_, weight) in FAILURES.values()] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 55 | failure = random.choices(list(FAILURES), weights=weights)[0] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 56 | failure_func, _ = FAILURES[failure] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 57 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 58 | response = failure_func(flow) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 59 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 60 | if response: |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 61 | flow.response = response |