feat: 打卡记录拉取/缺卡统计工具(iEPMS token直连, 无需签名)
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
"""mitmproxy addon: append every HTTP flow to a JSONL file.
|
||||
|
||||
Usage:
|
||||
mitmdump -s tools/capture/dump_flows.py --set dump_path=data/captures/flows.jsonl
|
||||
"""
|
||||
|
||||
import base64
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
|
||||
from mitmproxy import ctx, http
|
||||
|
||||
|
||||
def _body(message: http.Message | None) -> dict | None:
|
||||
if message is None:
|
||||
return None
|
||||
raw = message.get_content(strict=False) or b""
|
||||
try:
|
||||
return {"text": raw.decode("utf-8"), "size": len(raw)}
|
||||
except UnicodeDecodeError:
|
||||
return {"b64": base64.b64encode(raw).decode(), "size": len(raw)}
|
||||
|
||||
|
||||
class DumpFlows:
|
||||
def load(self, loader):
|
||||
loader.add_option("dump_path", str, "data/captures/flows.jsonl", "JSONL output path")
|
||||
|
||||
def response(self, flow: http.HTTPFlow):
|
||||
self._write(flow)
|
||||
|
||||
def error(self, flow: http.HTTPFlow):
|
||||
self._write(flow)
|
||||
|
||||
def _write(self, flow: http.HTTPFlow):
|
||||
req, resp = flow.request, flow.response
|
||||
record = {
|
||||
"ts": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(req.timestamp_start)),
|
||||
"method": req.method,
|
||||
"url": req.pretty_url,
|
||||
"req_headers": list(req.headers.items(multi=True)),
|
||||
"req_body": _body(req),
|
||||
"status": resp.status_code if resp else None,
|
||||
"resp_headers": list(resp.headers.items(multi=True)) if resp else None,
|
||||
"resp_body": _body(resp),
|
||||
"error": str(flow.error) if flow.error else None,
|
||||
}
|
||||
path = ctx.options.dump_path
|
||||
os.makedirs(os.path.dirname(path) or ".", exist_ok=True)
|
||||
with open(path, "a", encoding="utf-8") as f:
|
||||
f.write(json.dumps(record, ensure_ascii=False) + "\n")
|
||||
|
||||
|
||||
addons = [DumpFlows()]
|
||||
Reference in New Issue
Block a user