Add offline battle estimates and passive phone sample analysis
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
"""Passive mitmproxy addon for manually played battles; never modifies flows.
|
||||
|
||||
Run from the project root using mitmdump -s tools/capture_battle_proxy.py.
|
||||
Only the listed game service paths on dydab.com are recorded. The original
|
||||
HTTP exchange is forwarded by mitmproxy; this addon does not issue requests.
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import sys
|
||||
from urllib.parse import urlparse
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
sys.path.insert(0, str(ROOT))
|
||||
from ranch_assistant.api import DEFAULT_KEY, decode
|
||||
from ranch_assistant.debug import TraceLog
|
||||
|
||||
OPERATIONS = frozenset(("stageService/stageStart", "battleService/battleStart",
|
||||
"battleService/battleEnd", "battleService/recordBattleResult",
|
||||
"taskService/taskCompleteStep", "masterService/masterQueryDetail"))
|
||||
DIAGNOSTIC_OPERATIONS = frozenset(("heartBeatService/keepAlive", "stackService/stackCheckProcess"))
|
||||
|
||||
|
||||
class BattleCapture:
|
||||
def __init__(self, folder=None, key=None):
|
||||
self.trace = TraceLog(folder or ROOT / "local_state/battle_capture")
|
||||
self.key = key if key is not None else os.environ.get("MYRZX_BATTLE_CAPTURE_KEY", DEFAULT_KEY)
|
||||
self.trace.secrets.add(self.key)
|
||||
|
||||
def operation(self, flow):
|
||||
parsed = urlparse(flow.request.pretty_url)
|
||||
host = (parsed.hostname or "").lower()
|
||||
if host != "dydab.com" and not host.endswith(".dydab.com"):
|
||||
return None
|
||||
if self.trace.path.with_name("capture.paused").exists():
|
||||
return None
|
||||
operation = parsed.path.strip("/")
|
||||
return operation if operation in OPERATIONS | DIAGNOSTIC_OPERATIONS else None
|
||||
|
||||
def payload(self, message):
|
||||
raw = message.get_content()
|
||||
try:
|
||||
result = json.loads(raw)
|
||||
except (ValueError, TypeError, UnicodeError):
|
||||
result = decode(raw, self.key)
|
||||
if not isinstance(result, dict):
|
||||
raise ValueError("Game payload is not a JSON object")
|
||||
return result
|
||||
|
||||
def request(self, flow):
|
||||
operation = self.operation(flow)
|
||||
if operation is None:
|
||||
return
|
||||
self.record("request", operation, flow, flow.request)
|
||||
|
||||
def response(self, flow):
|
||||
operation = self.operation(flow)
|
||||
if operation is None or flow.response is None:
|
||||
return
|
||||
self.record("response", operation, flow, flow.response)
|
||||
|
||||
def record(self, kind, operation, flow, message):
|
||||
details = {"request_id": str(flow.id), "capture_origin": "phone_proxy",
|
||||
"url": flow.request.pretty_url.split("?", 1)[0]}
|
||||
if kind == "response":
|
||||
details["http_status"] = flow.response.status_code
|
||||
try:
|
||||
body = self.payload(message)
|
||||
except Exception as error:
|
||||
# Never write undecoded content, keys or exception text containing data.
|
||||
self.trace.emit("error", operation, **details, decoded=False,
|
||||
exception_type=type(error).__name__, message="Payload decoding failed; no raw body retained")
|
||||
else:
|
||||
if operation in DIAGNOSTIC_OPERATIONS:
|
||||
# Verify passive decoding before a battle without retaining unrelated data.
|
||||
self.trace.emit("diagnostic", operation, **details, direction=kind, decoded=True,
|
||||
resCode=body.get("resCode") if type(body.get("resCode")) is int else None)
|
||||
else:
|
||||
self.trace.emit(kind, operation, **details, decoded=True, body=body)
|
||||
|
||||
|
||||
addons = [BattleCapture()]
|
||||
Reference in New Issue
Block a user