Files
myrzx_bot/ranch_assistant/sweep_recovery.py
T

69 lines
3.3 KiB
Python

"""Read-only evidence shared by hidden-entry migration and legacy archival."""
import hashlib
import json
from pathlib import Path
def scan_trace(path, owner):
"""Called once per pending by the account worker, never by presentation."""
evidence = {"events": [], "files": [], "valid": False, "owner_read": False, "no_consumption": True}
if not isinstance(path, (str, Path)):
return evidence
path = Path(path)
role = None
safe = {"读取角色", "masterService/masterQueryDetail", "heartBeatService/keepAlive",
"渠道账号验证", "获取官方区服", "进入选中区服", "本地区服整理"}
def unique(pairs):
result = {}
for key, item in pairs:
if key in result:
raise ValueError("调试记录字段重复")
result[key] = item
return result
try:
for file in (path.with_name("network_debug.previous.jsonl"), path):
if not file.exists():
continue
raw = file.read_bytes()
evidence["files"].append({"name": file.name, "sha256": hashlib.sha256(raw).hexdigest()})
for line in raw.decode("utf-8").splitlines():
if not line.strip():
continue
event = json.loads(line, object_pairs_hook=unique)
if not isinstance(event, dict):
return evidence
# Client.enter_server emits this local signature diagnostic
# before entering the server; it dispatches no game request.
if (event.get("kind") == "info" and event.get("operation") == "进入参数检查"
and event.get("scope") == "local"):
continue
if event.get("kind") not in ("request", "response", "error"):
return evidence
evidence["events"].append(event)
operation, kind, body = event.get("operation"), event.get("kind"), event.get("body")
if operation in ("读取角色", "masterService/masterQueryDetail") and kind == "response":
role = str((body.get("master") or {}).get("id")) if isinstance(body, dict) else None
if role != owner or event.get("http_status", 200) != 200 or body.get("resCode", 0) not in (0, "0"):
evidence["no_consumption"] = False
else:
evidence["owner_read"] = True
elif operation not in safe:
# A missing role or an unrecognised operation cannot prove
# that no later consuming request was dispatched.
evidence["no_consumption"] = False
evidence["valid"] = bool(evidence["files"]) and role == owner
except (OSError, ValueError, TypeError, AttributeError):
pass
return evidence
def role_path_matches(sweep_path, session, owner, trace_path):
from .api import Session
from .sessions import identity
if not isinstance(session, Session) or str(session.role_id) != owner or not sweep_path:
return False
root = Path(sweep_path).parent
return (root.parent.name == "roles" and root.name == identity(session)
and root.resolve() == root.absolute() and isinstance(trace_path, (str, Path))
and Path(trace_path).parent.resolve() == root.parent.parent.resolve())