type: 修复虎牙信封解析偏移类型
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
解析与改写 WUP 信封中的 cert、uid、session 等字段。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
@@ -34,7 +35,7 @@ DEFAULT_QURL_B64 = (
|
||||
)
|
||||
|
||||
|
||||
def _read_len_int(d: bytes, p: int) -> tuple[int, int]:
|
||||
def _read_len_int(d: bytes | bytearray, p: int) -> tuple[int, int]:
|
||||
dt = d[p] & 0x0F
|
||||
if dt == ZERO:
|
||||
return 0, p + 1
|
||||
@@ -47,7 +48,7 @@ def _read_len_int(d: bytes, p: int) -> tuple[int, int]:
|
||||
raise ValueError(f"长度int类型异常 {dt:#x}@{p}")
|
||||
|
||||
|
||||
def _skip_value(d: bytes, p: int, dt: int) -> int:
|
||||
def _skip_value(d: bytes | bytearray, p: int, dt: int) -> int:
|
||||
if dt == ZERO:
|
||||
return p
|
||||
if dt == INT8:
|
||||
@@ -122,7 +123,11 @@ class Envelope:
|
||||
if p.exists():
|
||||
return cls._load_from_path(p)
|
||||
# 尝试查找 evidence/cert_keycap.json
|
||||
candidate = Path(__file__).resolve().parent.parent.parent / "evidence" / "cert_keycap.json"
|
||||
candidate = (
|
||||
Path(__file__).resolve().parent.parent.parent
|
||||
/ "evidence"
|
||||
/ "cert_keycap.json"
|
||||
)
|
||||
if candidate.exists():
|
||||
try:
|
||||
return cls._load_from_path(candidate)
|
||||
@@ -134,7 +139,9 @@ class Envelope:
|
||||
def _load_from_path(cls, p: Path) -> "Envelope":
|
||||
if p.suffix == ".json":
|
||||
j = json.loads(p.read_text("utf-8"))
|
||||
q = next(e["data"] for e in j if e.get("type") == "qurl_done" and e.get("data"))
|
||||
q = next(
|
||||
e["data"] for e in j if e.get("type") == "qurl_done" and e.get("data")
|
||||
)
|
||||
return cls(base64.b64decode(q))
|
||||
return cls(p.read_bytes())
|
||||
|
||||
@@ -226,17 +233,21 @@ class Envelope:
|
||||
|
||||
@property
|
||||
def uid(self) -> int:
|
||||
assert self.uid_off is not None
|
||||
return struct.unpack_from(">Q", self.raw, self.uid_off)[0]
|
||||
|
||||
def patch_uid(self, uid: int) -> "Envelope":
|
||||
assert self.uid_off is not None
|
||||
struct.pack_into(">Q", self.raw, self.uid_off, uid)
|
||||
return self
|
||||
|
||||
@property
|
||||
def cert_b64(self) -> bytes:
|
||||
assert self.cert_off is not None
|
||||
return bytes(self.raw[self.cert_off : self.cert_off + self.cert_len])
|
||||
|
||||
def patch_cert(self, cert: bytes) -> "Envelope":
|
||||
assert self.cert_off is not None
|
||||
b64 = base64.b64encode(cert)
|
||||
if len(b64) != self.cert_len:
|
||||
raise ValueError(
|
||||
|
||||
Reference in New Issue
Block a user