diff options
| author | Claude <claude@anthropic.com> | 2026-03-04 19:14:55 +0100 |
|---|---|---|
| committer | Claude <claude@anthropic.com> | 2026-03-04 19:14:55 +0100 |
| commit | 171c5b86ef05974426ba5c5d8547c8025977d1a2 (patch) | |
| tree | 2a1193e2bb81a6341e55d0b883a3fc33f77f8be1 /test/mitmproxy/capture.py | |
| parent | 9f14edf2b97286e02830d528038b32d5b31aaa0a (diff) | |
| parent | 0278c87f062a9ae7d617b92be22b175558a05086 (diff) | |
| download | gemini-py-main.tar.gz gemini-py-main.zip | |
Diffstat (limited to 'test/mitmproxy/capture.py')
| -rw-r--r-- | test/mitmproxy/capture.py | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/test/mitmproxy/capture.py b/test/mitmproxy/capture.py new file mode 100644 index 0000000..5ddd756 --- /dev/null +++ b/test/mitmproxy/capture.py @@ -0,0 +1,77 @@ +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 "googleapis.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 = flow.request.headers.get("x-capture-source") or ( + "gemini-cli" if "GeminiCLI" in flow.request.headers.get("user-agent", "") else "unknown" + ) + + 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}" + ) + + def response(self, flow: http.HTTPFlow): + if "googleapis.com" not in (flow.request.host or ""): + return + + capture_id = flow.metadata.get("capture_id") + if not capture_id: + return + + body = None + try: + body = json.loads(flow.response.get_text()) + except Exception: + body = flow.response.get_text()[:4000] + + path = os.path.join(self.output_dir, f"res_{capture_id:04d}.json") + with open(path, "w") as f: + json.dump( + { + "status": flow.response.status_code, + "headers": dict(flow.response.headers), + "body": body, + }, + f, + indent=2, + default=str, + ) + + +addons = [CaptureAddon()] |
