nomadro/apps/miniprogram/weapp/scripts/_audit_sanitize_links.py
eric 3e5b7c2098 Monorepo super-optimize: src layout, workspace scripts, docs.
Collapse portal under src/, unify pnpm workspace/scripts, thin VIP topic re-exports, drop orphan vip scaffolds and one-shot scripts, ignore xhs/release, document root commands.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-01 00:34:04 +08:00

260 lines
8.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
"""Sanitize display copy for platform audit; keep originals in *Origin fields.
Origin values are stored as ``__o:<base64-utf8>`` so packaged apps do not
ship cleartext audit-sensitive keywords. Decode:
python -c "from scripts._audit_sanitize_links import decode_origin; print(decode_origin('__o:...'))"
"""
from __future__ import annotations
import base64
import json
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
PATHS = [
ROOT / "src" / "data" / "links.json",
ROOT / "tt" / "src" / "data" / "links.json",
]
ORIGIN_PREFIX = "__o:"
GROUP_TITLE = {
"proxy": "跨境网络工具",
"resi-ip": "企业固定出口 IP",
"resi-vps": "跨境专用服务器",
}
CATEGORY_TITLE = {
"live": "直播效率工具",
}
LINK_COPY = {
"okx": ("OKX 全球账户", "跨境数字账户与收款工具"),
"safepal": ("SafePal 支付卡", "跨境消费与法币结算工具(邀请码见官网)"),
"paypal": ("PayPal", "跨境收款与佣金结算"),
"jms": (None, "跨境网络加速服务"),
"ykk": (None, "跨境网络加速服务"),
"iproyal": (None, "企业级固定出口 IP"),
"proxy-cheap": ("出口 IP · Cheap", "企业级固定出口 IP"),
"smartproxy": ("出口 IP · Smart", "企业级固定出口 IP"),
"lisa": (None, "跨境专用云主机"),
"voyracloud": (None, "跨境专用云主机"),
"3x-ui": (None, "开源网络面板 / 仪表盘"),
"mihomo": ("Mihomo 配置终端", "开源网络规则与配置文档"),
"nowpayments": (None, "跨境多币种结算收款"),
"tampermonkey": (None, "油猴脚本,直播效率辅助"),
"cloud-phone": ("云端安卓环境", "用开源硬件打造云端安卓环境"),
"smmfollows": (None, "增长辅助平台"),
}
# Longer phrases first
PHRASE_REPLACEMENTS = [
("静态住宅 IP", "企业固定出口 IP"),
("加密货币交易所", "跨境数字账户工具"),
("科学上网", "跨境网络"),
("加密货币", "跨境数字资产"),
("无人直播", "直播效率"),
("云手机集群", "云端设备阵列"),
("云手机", "云端安卓环境"),
("刷粉刷量", "增长辅助"),
("刷量网站", "增长服务平台"),
("刷量", "增长辅助"),
("地理套利", "地理优势运营"),
("U卡", "支付卡"),
("网络代理", "网络加速"),
("代理软件", "网络规则工具"),
("住宅 IP", "固定出口 IP"),
("交易所", "全球账户平台"),
]
def encode_origin(text: str) -> str:
if not isinstance(text, str):
return text
if text.startswith(ORIGIN_PREFIX):
return text
raw = decode_origin(text) if False else text # noqa: keep clear API
return ORIGIN_PREFIX + base64.b64encode(text.encode("utf-8")).decode("ascii")
def decode_origin(text: str) -> str:
if not isinstance(text, str):
return text
if text.startswith(ORIGIN_PREFIX):
return base64.b64decode(text[len(ORIGIN_PREFIX) :].encode("ascii")).decode("utf-8")
return text
def set_origin(obj: dict, field: str, plain: str) -> None:
key = field + "Origin"
existing = obj.get(key)
if existing:
# already have origin — keep decoded-then-reencoded form
obj[key] = encode_origin(decode_origin(existing))
return
obj[key] = encode_origin(plain)
def patch_category(obj: dict) -> int:
changed = 0
cid = obj.get("id")
if cid in CATEGORY_TITLE:
new_t = CATEGORY_TITLE[cid]
old = decode_origin(obj.get("titleOrigin", "")) or obj.get("title", "")
if obj.get("title") != new_t:
set_origin(obj, "title", old if old else obj.get("title", ""))
obj["title"] = new_t
changed += 1
return changed
def patch_group(obj: dict) -> int:
changed = 0
gid = obj.get("id")
if gid in GROUP_TITLE:
new_t = GROUP_TITLE[gid]
old = decode_origin(obj.get("titleOrigin", "")) or obj.get("title", "")
if obj.get("title") != new_t:
set_origin(obj, "title", old if old else obj.get("title", ""))
obj["title"] = new_t
changed += 1
return changed
def patch_link(obj: dict) -> int:
changed = 0
lid = obj.get("id")
if lid in LINK_COPY:
nt, ns = LINK_COPY[lid]
if nt is not None and obj.get("title") != nt:
set_origin(obj, "title", decode_origin(obj.get("titleOrigin", "")) or obj.get("title", ""))
obj["title"] = nt
changed += 1
if ns is not None and obj.get("subtitle") != ns:
set_origin(
obj,
"subtitle",
decode_origin(obj.get("subtitleOrigin", "")) or obj.get("subtitle", ""),
)
obj["subtitle"] = ns
changed += 1
for field in ("title", "subtitle"):
val = obj.get(field)
if not isinstance(val, str):
continue
new_val = val
for a, b in PHRASE_REPLACEMENTS:
if a in new_val:
new_val = new_val.replace(a, b)
if new_val != val:
set_origin(obj, field, decode_origin(obj.get(field + "Origin", "")) or val)
obj[field] = new_val
changed += 1
return changed
def encode_all_origins(node) -> int:
"""Re-encode any plaintext *Origin fields to __o:base64."""
n = 0
if isinstance(node, dict):
for k, v in list(node.items()):
if k.endswith("Origin") and isinstance(v, str) and not v.startswith(ORIGIN_PREFIX):
node[k] = encode_origin(v)
n += 1
else:
n += encode_all_origins(v)
elif isinstance(node, list):
for item in node:
n += encode_all_origins(item)
return n
def walk(node, *, is_category: bool = False) -> int:
total = 0
if isinstance(node, dict):
groups = node.get("groups")
links = node.get("links")
if isinstance(groups, list) and "emoji" in node:
total += patch_category(node)
for g in groups:
total += walk(g)
elif isinstance(links, list):
total += patch_group(node)
for link in links:
if isinstance(link, dict):
total += patch_link(link)
else:
for v in node.values():
total += walk(v)
elif isinstance(node, list):
for item in node:
total += walk(item)
return total
def display_text_without_origins(data: dict) -> str:
def scrub(o):
if isinstance(o, dict):
return {
k: scrub(v)
for k, v in o.items()
if not k.endswith("Origin") and k != "_auditNote"
}
if isinstance(o, list):
return [scrub(x) for x in o]
return o
return json.dumps(scrub(data), ensure_ascii=False)
def main() -> None:
for p in PATHS:
if not p.exists():
print(f"SKIP missing {p}")
continue
data = json.loads(p.read_text(encoding="utf-8"))
data["_auditNote"] = (
"titleOrigin/subtitleOrigin 为过审前原文(__o:base64),界面只读 title/subtitle;"
"勿在 UI 渲染 *Origin。XHS 导出时会剥离 Origin。"
)
n = walk(data)
n += encode_all_origins(data)
p.write_text(
json.dumps(data, ensure_ascii=False, indent=2) + "\n",
encoding="utf-8",
)
print(f"{p}: patches={n}")
primary = PATHS[0]
data = json.loads(primary.read_text(encoding="utf-8"))
disp = display_text_without_origins(data)
for bad in [
"科学上网",
"加密货币",
"交易所",
"U卡",
"翻墙",
"机场",
"代理服务",
"住宅 IP",
"无人直播",
]:
mark = "STILL" if bad in disp else "OK"
print(f"{mark}: {bad}")
# sample decode
for c in data["categories"]:
if c.get("id") == "studio":
for g in c.get("groups", []):
if g.get("id") == "proxy":
print(
"proxy origin decoded:",
decode_origin(g.get("titleOrigin", "")),
)
if __name__ == "__main__":
main()