aboutsummaryrefslogtreecommitdiffstats
path: root/test/mitmproxy/capture.py
blob: 0fb6b70ffb34adb9ea4867a97cac49b1e8d631c4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import json
import os
import time

from mitmproxy import http


class CaptureAddon:
    def __init__(self):
        self.output_dir = os.environ.get("CAPTURE_DIR", "/captures")
        os.makedirs(self.output_dir, exist_ok=True)
        self.counter = 0

    def request(self, flow: http.HTTPFlow):
        if "api.anthropic.com" not in (flow.request.host or ""):
            return

        self.counter += 1
        flow.metadata["capture_id"] = self.counter

        body = None
        try:
            body = json.loads(flow.request.get_text())
        except Exception:
            body = flow.request.get_text()

        source = "unknown"
        ua = flow.request.headers.get("user-agent", "")
        if "claude-cli" in ua or "claude-code" in ua:
            source = "claude-code"
        elif "python" in ua.lower() or "httpx" in ua.lower():
            source = "claude-py"

        data = {
            "capture_id": self.counter,
            "source": source,
            "timestamp": time.time(),
            "method": flow.request.method,
            "url": flow.request.pretty_url,
            "headers": dict(flow.request.headers),
            "body": body,
        }

        path = os.path.join(self.output_dir, f"req_{self.counter:04d}.json")
        with open(path, "w") as f:
            json.dump(data, f, indent=2, default=str)

        print(f"[capture] #{self.counter} {source} {flow.request.method} {flow.request.pretty_url}")


addons = [CaptureAddon()]