diff --git a/core/account_probe.py b/core/account_probe.py index b346410..d750b79 100644 --- a/core/account_probe.py +++ b/core/account_probe.py @@ -179,6 +179,56 @@ def probe_online_dumpsys(serials=None, workers=8): return out +def mask_matches(masked, full): + """MIUI 打码名(`锋哥苏***探访`)是否与完整名(`锋哥苏州百业探访`)匹配。 + + 按 `*` 分段、**按顺序**在完整名里找;没有 `*` 时按全等比较。 + ``` + mask_matches("锋哥苏***探访", "锋哥苏州百业探访") -> True + mask_matches("锋哥苏***探访", "周周苏州百业探访") -> False + ``` + """ + masked = (masked or "").strip() + full = (full or "").strip() + if not masked or not full: + return False + if "*" not in masked: + return masked == full + pos = 0 + for part in [p for p in re.split(r"\*+", masked) if p]: + i = full.find(part, pos) + if i < 0: + return False + pos = i + len(part) + return True + + +def remember_name(serial, name): + """把**完整昵称**记到 `device.cur_account_name`(广播下发成功后调用)。 + + 轮询接口会拿它和 dumpsys 的打码名比对:对得上就返回完整名, + 避免设备端每 2 秒轮询把广播推的完整昵称覆盖成 `***`。 + 必须在 Flask app context 里写库(worker 线程里自动推一个)。 + """ + name = (name or "").strip() + if not serial or not name: + return False + try: + from core import account_folder as _af + from core.models import Device, db + with _af._ctx(): + row = Device.query.get(serial) + if row is None: + return False + row.cur_account_name = name[:80] + row.cur_name_at = time.strftime("%Y-%m-%d %H:%M:%S") + db.session.commit() + return True + except Exception as e: + _log.warning(f"[{serial}] 记录完整账号名失败(不影响广播): {e}") + return False + + def push_account_name(serial, name=None): """把当前抖音账号推给设备端 Agent(素材页/身份页显示)。 @@ -207,6 +257,8 @@ def push_account_name(serial, name=None): ) ok = "broadcast completed" in (out or "").lower() if ok: + # 平台侧记住完整昵称:设备端轮询时用它盖掉 dumpsys 的打码名 + remember_name(serial, name) _log.info(f"[{serial}] 已下发当前账号: {name}") return True, f"已下发 {name}" _log.warning(f"[{serial}] 下发账号名失败: {(out or '')[:120]}") @@ -271,7 +323,13 @@ def cache_names(result): row = Device.query.get(serial) if not row: continue - row.cur_account_name = name[:80] + old = (row.cur_account_name or "").strip() + # dumpsys 探测到的是打码名:若与已缓存的完整名一致,**保留完整名** + # (否则设备端每次轮询都会把广播推的完整昵称覆盖成 `***`) + if "*" not in old and mask_matches(name, old): + pass + else: + row.cur_account_name = name[:80] row.cur_name_at = now db.session.commit() except Exception as e: diff --git a/scripts/tests/test_dumpsys_account.py b/scripts/tests/test_dumpsys_account.py index 3d6ab1a..dbf0c0d 100644 --- a/scripts/tests/test_dumpsys_account.py +++ b/scripts/tests/test_dumpsys_account.py @@ -62,6 +62,12 @@ check('使用 name_b64', _calls[0][10], "name_b64") check('base64 是标准编码', base64.b64decode(_calls[0][11]).decode("utf-8"), "锋哥苏***实录") +print('=== 4. 打码名 ↔ 完整名 匹配 ===') +check('前缀+后缀都对得上', ap.mask_matches('锋哥苏***探访', '锋哥苏州百业探访'), True) +check('前缀对、后缀不对 → 不匹配', ap.mask_matches('锋哥苏***探访', '锋哥苏州创业实录'), False) +check('没打码 → 全等', ap.mask_matches('同一名字', '同一名字'), True) +check('没打码但不是同一个 → 不匹配', ap.mask_matches('名字甲', '名字乙'), False) + print() if fails: print('❌ 失败 %d 项:%s' % (len(fails), ' / '.join(fails))) diff --git a/web/device_agent_api.py b/web/device_agent_api.py index 59cc4e2..7a848b1 100644 --- a/web/device_agent_api.py +++ b/web/device_agent_api.py @@ -154,8 +154,19 @@ def _account_cached(serial, fp, timeout=5): if hit and now - hit[0] < _ACCOUNT_CACHE_TTL: return hit[1] if serial: - from core.account_probe import probe_one_dumpsys + from core.account_probe import probe_one_dumpsys, mask_matches r = probe_one_dumpsys(serial, timeout=timeout) + # dumpsys 在 MIUI 上会把昵称打码;如果平台刚广播过完整昵称, + # 且打码名与它匹配,就返回完整名(否则设备端轮询会把完整名覆盖成 ***)。 + if not r.get("error") and r.get("masked"): + try: + from core.models import Device + row = Device.query.get(serial) + full = ((row.cur_account_name or "").strip() if row else "") + if full and "*" not in full and mask_matches(r.get("name"), full): + r = dict(r, name=full, masked=False, source="platform") + except Exception as e: + _log.warning("读取完整账号名失败(继续用打码名): %s", e) else: r = {"serial": serial, "name": "", "masked": False, "last_switch": "", "error": "无法识别设备(缺 serial)"}