dsh-qqbot-muu 0.1.20 → 0.1.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +168 -154
- package/client/qqbot-settings.js +13401 -13380
- package/dist/config.js +573 -581
- package/dist/features/contextless-store.js +247 -247
- package/dist/features/four-source.js +338 -336
- package/dist/features/group-join-request.js +289 -287
- package/dist/features/local-models.js +1494 -1467
- package/dist/features/style-layer.js +501 -498
- package/dist/features/thinking-log.js +71 -69
- package/dist/gateway/bootstrap.js +1704 -1652
- package/dist/model/settings-reader.js +193 -102
- package/dist/shared/paths.js +128 -0
- package/dist/shared/settings-io.js +79 -83
- package/dshqqbot/.qqbot-extensions/inbound/00-vision.mjs +111 -0
- package/dshqqbot/.qqbot-extensions/inbound/aa-memory-recall.mjs +802 -794
- package/dshqqbot/.qqbot-extensions/inbound/az-feedback.mjs +113 -115
- package/dshqqbot/.qqbot-extensions/inbound/local-answer.mjs +689 -568
- package/dshqqbot/.qqbot-extensions/lib/local-llm.mjs +455 -444
- package/dshqqbot/.qqbot-extensions/lib/memory-agent.mjs +930 -928
- package/dshqqbot/.qqbot-extensions/lib/memory-core.mjs +537 -537
- package/dshqqbot/.qqbot-extensions/lib/memory-db.mjs +2476 -2245
- package/dshqqbot/.qqbot-extensions/lib/memory-hippo.mjs +18 -0
- package/dshqqbot/.qqbot-extensions/lib/memory.mjs +191 -186
- package/dshqqbot/.qqbot-extensions/lib/scope-util.mjs +84 -0
- package/dshqqbot/.qqbot-extensions/outbound/aa-sanitize.mjs +158 -154
- package/dshqqbot/.qqbot-extensions/outbound/memory-agent.mjs +121 -130
- package/dshqqbot/.qqbot-extensions/outbound/turn-memory.mjs +92 -101
- package/dshqqbot/.qqbot-extensions/tools/memory.mjs +178 -120
- package/dshqqbot/.qqbot-extensions/vendor/abogus/test-abogus.mjs +128 -126
- package/package.json +119 -118
- package/scripts/__pycache__/fan-mode.cpython-311.pyc +0 -0
- package/scripts/__pycache__/memory-audit.cpython-311.pyc +0 -0
- package/scripts/__pycache__/qq-ip-sync.cpython-311.pyc +0 -0
- package/scripts/__pycache__/qq-whitelist.cpython-311.pyc +0 -0
- package/scripts/ai-toolkit.py +607 -478
- package/scripts/chat-archive/__pycache__/build-sft-v4.cpython-311.pyc +0 -0
- package/scripts/chat-archive/__pycache__/build-sft.cpython-311.pyc +0 -0
- package/scripts/chat-archive/build-sft-v4.py +566 -0
- package/scripts/chat-archive/build-sft.py +390 -0
- package/scripts/chat-archive/eval-sft.py +155 -0
- package/scripts/chat-archive/extract-memory.py +143 -0
- package/scripts/chat-archive/inventory.py +182 -0
- package/scripts/chat-archive/merge-sft.py +114 -0
- package/scripts/chat-archive/qce-pull.py +217 -0
- package/scripts/chat-archive/qqheader.py +110 -0
- package/scripts/chat-archive/qqkey2.py +113 -0
- package/scripts/chat-archive/qqprobe.py +153 -0
- package/scripts/chat-archive/screen-collect.py +163 -0
- package/scripts/chat-archive/sqlcipher_probe.py +444 -0
- package/scripts/episode.py +529 -0
- package/scripts/fan-curve.py +218 -0
- package/scripts/fan-guard.cmd +8 -0
- package/scripts/fan-mode.py +380 -263
- package/scripts/gen-image.py +420 -413
- package/scripts/gen-video.py +186 -178
- package/scripts/hw-profile.py +175 -0
- package/scripts/media-enhance.py +402 -0
- package/scripts/memory-audit.py +581 -0
- package/scripts/migrate-scope.mjs +140 -139
- package/scripts/qq-connect-login.py +133 -0
- package/scripts/qq-ip-sync-hidden.vbs +5 -0
- package/scripts/qq-ip-sync.py +300 -0
- package/scripts/qq-whitelist.py +71 -0
- package/scripts/release-all.mjs +636 -509
- package/scripts/release-check.mjs +244 -226
- package/scripts/secure-room/index.html +334 -0
- package/scripts/secure-room/server.py +582 -0
- package/scripts/stickers/auto-tag.mjs +161 -0
- package/scripts/train/__pycache__/train-lora.cpython-311.pyc +0 -0
- package/scripts/train/gguf-quantize.py +92 -0
- package/scripts/train/gguf-to-hf.py +112 -0
- package/scripts/train/merge-lora.py +51 -0
- package/scripts/train/probe-mirrors.sh +12 -0
- package/scripts/train/probe-mirrors2.sh +20 -0
- package/scripts/train/setup-wsl.sh +23 -0
- package/scripts/train/setup-wsl2.sh +25 -0
- package/scripts/train/setup-wsl3.sh +22 -0
- package/scripts/train/setup-wsl4.sh +22 -0
- package/scripts/train/train-lora.py +319 -0
- package/dist/_backup_ctx_20261125_183653/dist/config.d.ts +0 -473
- package/dist/_backup_ctx_20261125_183653/dist/config.js +0 -450
- package/dist/_backup_ctx_20261125_183653/dist/features/contextless-trim.js +0 -304
- package/dist/_backup_ctx_20261125_183653/dist/session/session-manager.js +0 -1588
- package/dist/_backup_ctx_20261125_183653/dist/transport/inbound.js +0 -1567
- package/dist/_backup_ctx_20261125_183653/dist/transport/outbound.js +0 -647
- package/scripts/__pycache__/ai-mode.cpython-311.pyc +0 -0
- package/scripts/__pycache__/ai-toolkit.cpython-311.pyc +0 -0
- package/scripts/__pycache__/comfy_gen.cpython-311.pyc +0 -0
- package/scripts/__pycache__/gen-image.cpython-311.pyc +0 -0
- package/scripts/__pycache__/gen-video.cpython-311.pyc +0 -0
- package/scripts/__pycache__/model-registry.cpython-311.pyc +0 -0
- package/scripts/__pycache__/purge-standby.cpython-311.pyc +0 -0
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
"""qq-ip-sync.py — 机器人 IP 白名单自愈(流程化,不再人工扫码/点控制台)
|
|
4
|
+
|
|
5
|
+
背景(2026-12-03):
|
|
6
|
+
· 家用宽带重播后出口 IP 变了(113.200.174.4 -> .15),机器人开始报
|
|
7
|
+
「接口访问源IP不在白名单」,DSH 里那套 `qq-whitelist.py --headless`
|
|
8
|
+
还在用旧控制台地址(/qqbot/dashboard/ 已 302 回首页),永远 rc=20。
|
|
9
|
+
· 现在改成「先探针、再开浏览器」:gateway 能调通就什么都不做(秒级);
|
|
10
|
+
调不通才打开新控制台(q.qq.com/qqbot/dashboard/manage/<appId>?sec=dev-settings),
|
|
11
|
+
把当前 IPv4 加进白名单,最后再用 gateway 复核。
|
|
12
|
+
|
|
13
|
+
子命令:
|
|
14
|
+
--print-ip 只打印当前 IPv4(给 bootstrap 的漂移检测用)
|
|
15
|
+
--sync 完整自愈(默认行为)
|
|
16
|
+
--check 只探 gateway,通=0 不通=3
|
|
17
|
+
|
|
18
|
+
退出码:0 正常 / 3 需要人工(控制台要登录)/ 4 浏览器或控制台失败 / 5 无 IPv4
|
|
19
|
+
"""
|
|
20
|
+
from __future__ import annotations
|
|
21
|
+
|
|
22
|
+
import argparse
|
|
23
|
+
import json
|
|
24
|
+
import os
|
|
25
|
+
import subprocess
|
|
26
|
+
import sys
|
|
27
|
+
import time
|
|
28
|
+
from pathlib import Path
|
|
29
|
+
|
|
30
|
+
BASE = Path(os.environ.get("QQBOT_REPO_ROOT") or Path(__file__).resolve().parent.parent)
|
|
31
|
+
LOGS = BASE / "logs"
|
|
32
|
+
NET = BASE / "data" / "net"
|
|
33
|
+
PROFILE = BASE / "data" / "edge-profile"
|
|
34
|
+
LOCK = BASE / "data" / "whitelist-run.lock"
|
|
35
|
+
APPLIED = NET / "whitelist-applied.txt"
|
|
36
|
+
SETTINGS = BASE / "data" / "harness" / "qqbot-settings" / "desktop" / "im-qqbot.json"
|
|
37
|
+
IP_APIS = ["http://ipv4.icanhazip.com", "http://ip.3322.net", "https://ipv4.ip.sb/ip"]
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def log(tag: str, msg: str) -> None:
|
|
41
|
+
line = f"{time.strftime('%Y-%m-%d %H:%M:%S')} [{tag}] {msg}"
|
|
42
|
+
print(line, flush=True)
|
|
43
|
+
try:
|
|
44
|
+
LOGS.mkdir(parents=True, exist_ok=True)
|
|
45
|
+
with open(LOGS / "qq-ip-sync.log", "a", encoding="utf-8") as f:
|
|
46
|
+
f.write(line + "\n")
|
|
47
|
+
except Exception:
|
|
48
|
+
pass
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def creds() -> tuple[str, str]:
|
|
52
|
+
try:
|
|
53
|
+
raw = json.loads(SETTINGS.read_text("utf-8"))
|
|
54
|
+
except Exception:
|
|
55
|
+
return "", ""
|
|
56
|
+
found: dict = {}
|
|
57
|
+
|
|
58
|
+
def walk(o):
|
|
59
|
+
if isinstance(o, dict):
|
|
60
|
+
if "appId" in o and "appSecret" in o:
|
|
61
|
+
found.update(o)
|
|
62
|
+
for v in o.values():
|
|
63
|
+
walk(v)
|
|
64
|
+
elif isinstance(o, list):
|
|
65
|
+
for v in o:
|
|
66
|
+
walk(v)
|
|
67
|
+
|
|
68
|
+
walk(raw)
|
|
69
|
+
return str(found.get("appId") or ""), str(found.get("appSecret") or "")
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def current_ipv4() -> str:
|
|
73
|
+
import requests
|
|
74
|
+
for u in IP_APIS:
|
|
75
|
+
try:
|
|
76
|
+
t = requests.get(u, timeout=8).text.strip().splitlines()[0].strip()
|
|
77
|
+
if t.count(".") == 3 and all(x.isdigit() for x in t.split(".")):
|
|
78
|
+
return t
|
|
79
|
+
except Exception:
|
|
80
|
+
continue
|
|
81
|
+
return ""
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def token_of(app_id: str, secret: str) -> str:
|
|
85
|
+
import requests
|
|
86
|
+
r = requests.post("https://bots.qq.com/app/getAppAccessToken",
|
|
87
|
+
json={"appId": app_id, "clientSecret": secret}, timeout=15)
|
|
88
|
+
try:
|
|
89
|
+
return str(r.json().get("access_token") or "")
|
|
90
|
+
except Exception:
|
|
91
|
+
return ""
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def gateway_ok(app_id: str, token: str) -> tuple[bool, str]:
|
|
95
|
+
"""真正的判据:能调通 /gateway 就说明源 IP 已放行(401 只是 header 写法问题)"""
|
|
96
|
+
import requests
|
|
97
|
+
h = {"Authorization": "QQBot " + token, "X-Union-Appid": app_id}
|
|
98
|
+
try:
|
|
99
|
+
r = requests.get("https://api.sgroup.qq.com/gateway", headers=h, timeout=15)
|
|
100
|
+
if r.status_code == 200:
|
|
101
|
+
return True, "200"
|
|
102
|
+
if "白名单" in r.text or r.status_code == 401 and "源IP" in r.text:
|
|
103
|
+
return False, f"{r.status_code} {r.text[:120]}"
|
|
104
|
+
return False, f"{r.status_code} {r.text[:120]}"
|
|
105
|
+
except Exception as e:
|
|
106
|
+
return False, f"ERR {e}"
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def kill_stale_edge() -> None:
|
|
110
|
+
ps = ("Get-CimInstance Win32_Process -Filter \"Name='msedge.exe'\" | "
|
|
111
|
+
"Where-Object { $_.CommandLine -like '*edge-profile*' } | "
|
|
112
|
+
"ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue }")
|
|
113
|
+
subprocess.run(["powershell", "-NoProfile", "-Command", ps], capture_output=True)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def acquire_lock() -> bool:
|
|
117
|
+
try:
|
|
118
|
+
LOCK.parent.mkdir(parents=True, exist_ok=True)
|
|
119
|
+
if LOCK.exists():
|
|
120
|
+
try:
|
|
121
|
+
info = json.loads(LOCK.read_text("utf-8"))
|
|
122
|
+
except Exception:
|
|
123
|
+
info = {}
|
|
124
|
+
pid = int(info.get("pid") or 0)
|
|
125
|
+
if pid and pid != os.getpid():
|
|
126
|
+
try:
|
|
127
|
+
os.kill(pid, 0)
|
|
128
|
+
log("lock", f"已有白名单任务在跑(PID {pid}),本次退出")
|
|
129
|
+
return False
|
|
130
|
+
except OSError:
|
|
131
|
+
pass
|
|
132
|
+
LOCK.write_text(json.dumps({"pid": os.getpid(), "at": time.time()}), "utf-8")
|
|
133
|
+
return True
|
|
134
|
+
except Exception:
|
|
135
|
+
return True
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def release_lock() -> None:
|
|
139
|
+
try:
|
|
140
|
+
info = json.loads(LOCK.read_text("utf-8"))
|
|
141
|
+
if int(info.get("pid") or 0) == os.getpid():
|
|
142
|
+
LOCK.unlink()
|
|
143
|
+
except Exception:
|
|
144
|
+
pass
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def add_ip_via_console(app_id: str, ip: str, timeout_manual: int = 180) -> tuple[bool, str]:
|
|
148
|
+
"""打开控制台把 IP 加进白名单。返回 (成功, 说明)"""
|
|
149
|
+
# 2026-12-04:playwright 不是插件的必装依赖。别的用户机器上没装时,旧代码
|
|
150
|
+
# 会抛 ModuleNotFoundError → rc 非 0 → 插件每隔一段时间重试一次自愈,
|
|
151
|
+
# 日志里只有一堆 traceback。这里直接给可执行的手工方案(IP 已经算好了)。
|
|
152
|
+
try:
|
|
153
|
+
from playwright.sync_api import sync_playwright
|
|
154
|
+
except Exception:
|
|
155
|
+
return (False,
|
|
156
|
+
"未安装 playwright(pip install playwright && playwright install msedge),"
|
|
157
|
+
"白名单自愈不可用;请手动把 %s 加入后台白名单:"
|
|
158
|
+
"https://q.qq.com/qqbot/dashboard/manage/%s?sec=dev-settings" % (ip, app_id))
|
|
159
|
+
|
|
160
|
+
url = f"https://q.qq.com/qqbot/dashboard/manage/{app_id}?sec=dev-settings"
|
|
161
|
+
for attempt, headless in enumerate((True, False)):
|
|
162
|
+
kill_stale_edge()
|
|
163
|
+
time.sleep(4)
|
|
164
|
+
ctx = None
|
|
165
|
+
try:
|
|
166
|
+
with sync_playwright() as p:
|
|
167
|
+
ctx = p.chromium.launch_persistent_context(
|
|
168
|
+
str(PROFILE), channel="msedge", headless=headless,
|
|
169
|
+
viewport={"width": 1440, "height": 900}, locale="zh-CN", timezone_id="Asia/Shanghai",
|
|
170
|
+
args=["--disable-blink-features=AutomationControlled", "--no-first-run"],
|
|
171
|
+
)
|
|
172
|
+
pg = ctx.pages[0] if ctx.pages else ctx.new_page()
|
|
173
|
+
pg.goto(url, wait_until="domcontentloaded", timeout=45000)
|
|
174
|
+
time.sleep(6 if headless else 9)
|
|
175
|
+
body = pg.inner_text("body")
|
|
176
|
+
if "我的机器人" not in body and "开发设置" not in body:
|
|
177
|
+
if headless:
|
|
178
|
+
log("console", "未登录(headless),改用有窗口模式再试一次")
|
|
179
|
+
ctx.close()
|
|
180
|
+
continue
|
|
181
|
+
log("console", f"仍未登录,需要人工在窗口里点一次头像授权(最多等 {timeout_manual}s)")
|
|
182
|
+
try:
|
|
183
|
+
pg.locator("text=QQ登录").first.click(timeout=5000)
|
|
184
|
+
except Exception:
|
|
185
|
+
pass
|
|
186
|
+
deadline = time.time() + timeout_manual
|
|
187
|
+
while time.time() < deadline:
|
|
188
|
+
time.sleep(4)
|
|
189
|
+
try:
|
|
190
|
+
if "我的机器人" in pg.inner_text("body") or "开发设置" in pg.inner_text("body"):
|
|
191
|
+
break
|
|
192
|
+
except Exception:
|
|
193
|
+
pass
|
|
194
|
+
else:
|
|
195
|
+
ctx.close()
|
|
196
|
+
return False, "需要人工登录控制台"
|
|
197
|
+
pg.goto(url, wait_until="domcontentloaded", timeout=45000)
|
|
198
|
+
time.sleep(7)
|
|
199
|
+
# 已经在白名单里就不用重复添加
|
|
200
|
+
if ip in pg.inner_text("body"):
|
|
201
|
+
log("console", f"{ip} 已在白名单中")
|
|
202
|
+
ctx.close()
|
|
203
|
+
return True, "already"
|
|
204
|
+
pg.locator("text=添加 IP").first.click(timeout=8000)
|
|
205
|
+
time.sleep(2)
|
|
206
|
+
pg.locator("input[placeholder='请输入 IPv4']").first.fill(ip)
|
|
207
|
+
time.sleep(1)
|
|
208
|
+
btns = pg.locator("button", has_text="添加")
|
|
209
|
+
clicked = False
|
|
210
|
+
for i in range(btns.count() - 1, -1, -1):
|
|
211
|
+
try:
|
|
212
|
+
b = btns.nth(i)
|
|
213
|
+
if b.is_visible():
|
|
214
|
+
b.click(timeout=4000)
|
|
215
|
+
clicked = True
|
|
216
|
+
break
|
|
217
|
+
except Exception:
|
|
218
|
+
continue
|
|
219
|
+
if not clicked:
|
|
220
|
+
ctx.close()
|
|
221
|
+
return False, "找不到「添加」按钮"
|
|
222
|
+
time.sleep(5)
|
|
223
|
+
body = pg.inner_text("body")
|
|
224
|
+
ok = ip in body
|
|
225
|
+
try:
|
|
226
|
+
pg.screenshot(path=str(LOGS / f"ip-sync-{time.strftime('%H%M%S')}.png"))
|
|
227
|
+
except Exception:
|
|
228
|
+
pass
|
|
229
|
+
ctx.close()
|
|
230
|
+
return ok, "added" if ok else "提交后未在页面看到该 IP"
|
|
231
|
+
except Exception as e:
|
|
232
|
+
try:
|
|
233
|
+
ctx and ctx.close()
|
|
234
|
+
except Exception:
|
|
235
|
+
pass
|
|
236
|
+
log("console", f"第 {attempt + 1} 次失败: {str(e)[:140]}")
|
|
237
|
+
return False, "浏览器两次都失败"
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
def write_applied(ip: str) -> None:
|
|
241
|
+
try:
|
|
242
|
+
NET.mkdir(parents=True, exist_ok=True)
|
|
243
|
+
APPLIED.write_text(ip + "\n", "utf-8")
|
|
244
|
+
except Exception:
|
|
245
|
+
pass
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
def main() -> int:
|
|
249
|
+
ap = argparse.ArgumentParser()
|
|
250
|
+
ap.add_argument("--print-ip", action="store_true")
|
|
251
|
+
ap.add_argument("--check", action="store_true")
|
|
252
|
+
ap.add_argument("--sync", action="store_true")
|
|
253
|
+
ap.add_argument("--manual-wait", type=int, default=180)
|
|
254
|
+
a = ap.parse_args()
|
|
255
|
+
|
|
256
|
+
ip = current_ipv4()
|
|
257
|
+
if a.print_ip:
|
|
258
|
+
print(ip)
|
|
259
|
+
return 0 if ip else 5
|
|
260
|
+
if not ip:
|
|
261
|
+
log("ip", "取不到 IPv4(网络异常)")
|
|
262
|
+
return 5
|
|
263
|
+
|
|
264
|
+
app_id, secret = creds()
|
|
265
|
+
if not app_id or not secret:
|
|
266
|
+
log("cred", "读不到 appId/appSecret,跳过")
|
|
267
|
+
return 4
|
|
268
|
+
|
|
269
|
+
if not acquire_lock():
|
|
270
|
+
return 0
|
|
271
|
+
try:
|
|
272
|
+
token = token_of(app_id, secret)
|
|
273
|
+
if not token:
|
|
274
|
+
log("token", "getAppAccessToken 失败(凭据或网络问题)")
|
|
275
|
+
return 4
|
|
276
|
+
ok, detail = gateway_ok(app_id, token)
|
|
277
|
+
log("probe", f"ip={ip} gateway={'OK' if ok else 'FAIL'} {detail}")
|
|
278
|
+
if ok:
|
|
279
|
+
write_applied(ip)
|
|
280
|
+
return 0
|
|
281
|
+
if a.check:
|
|
282
|
+
return 3
|
|
283
|
+
log("sync", f"{ip} 不在白名单 -> 打开控制台添加")
|
|
284
|
+
done, why = add_ip_via_console(app_id, ip, a.manual_wait)
|
|
285
|
+
log("console", f"{'成功' if done else '失败'}:{why}")
|
|
286
|
+
if not done:
|
|
287
|
+
return 3 if "人工" in why else 4
|
|
288
|
+
token = token_of(app_id, secret)
|
|
289
|
+
ok2, detail2 = gateway_ok(app_id, token)
|
|
290
|
+
log("verify", f"gateway={'OK' if ok2 else 'FAIL'} {detail2}")
|
|
291
|
+
if ok2:
|
|
292
|
+
write_applied(ip)
|
|
293
|
+
return 0
|
|
294
|
+
return 4
|
|
295
|
+
finally:
|
|
296
|
+
release_lock()
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
if __name__ == "__main__":
|
|
300
|
+
sys.exit(main())
|
package/scripts/qq-whitelist.py
CHANGED
|
@@ -858,8 +858,62 @@ def _attach_dialogs(page) -> None:
|
|
|
858
858
|
pass
|
|
859
859
|
|
|
860
860
|
|
|
861
|
+
def _run_lock_path() -> Path:
|
|
862
|
+
return BASE / "data" / "whitelist-run.lock"
|
|
863
|
+
|
|
864
|
+
|
|
865
|
+
def _acquire_run_lock() -> bool:
|
|
866
|
+
"""单实例锁。
|
|
867
|
+
|
|
868
|
+
2026-12-03 实测事故:人工扫码窗口开着的时候,又跑了一次 --headless,
|
|
869
|
+
_open_context → kill_stale_edge 把**正在扫码的那个浏览器**当成残留杀掉,
|
|
870
|
+
于是扫码白扫、cookie 被覆盖、页面又回到登录卡。
|
|
871
|
+
所以这里保证同一时间只有一个进程能碰 edge-profile。
|
|
872
|
+
"""
|
|
873
|
+
p = _run_lock_path()
|
|
874
|
+
try:
|
|
875
|
+
p.parent.mkdir(parents=True, exist_ok=True)
|
|
876
|
+
if p.exists():
|
|
877
|
+
try:
|
|
878
|
+
info = json.loads(p.read_text("utf-8"))
|
|
879
|
+
except Exception:
|
|
880
|
+
info = {}
|
|
881
|
+
pid = int(info.get("pid") or 0)
|
|
882
|
+
if pid and pid != os.getpid():
|
|
883
|
+
alive = False
|
|
884
|
+
try:
|
|
885
|
+
os.kill(pid, 0)
|
|
886
|
+
alive = True
|
|
887
|
+
except OSError:
|
|
888
|
+
alive = False
|
|
889
|
+
if alive:
|
|
890
|
+
log("lock", f"另一个白名单任务正在运行(PID {pid}),本次退出,不要同时开两个", "ERROR")
|
|
891
|
+
return False
|
|
892
|
+
p.write_text(json.dumps({"pid": os.getpid(), "at": time.time()}), "utf-8")
|
|
893
|
+
return True
|
|
894
|
+
except Exception:
|
|
895
|
+
return True
|
|
896
|
+
|
|
897
|
+
|
|
898
|
+
def _release_run_lock() -> None:
|
|
899
|
+
try:
|
|
900
|
+
p = _run_lock_path()
|
|
901
|
+
if p.exists():
|
|
902
|
+
info = json.loads(p.read_text("utf-8"))
|
|
903
|
+
if int(info.get("pid") or 0) == os.getpid():
|
|
904
|
+
p.unlink()
|
|
905
|
+
except Exception:
|
|
906
|
+
pass
|
|
907
|
+
|
|
908
|
+
|
|
909
|
+
def _state_path() -> Path:
|
|
910
|
+
return BASE / "data" / "edge-profile-state.json"
|
|
911
|
+
|
|
912
|
+
|
|
861
913
|
def _open_context(p, headless: bool, slow_mo: int = 0):
|
|
862
914
|
PROFILE_DIR.mkdir(parents=True, exist_ok=True)
|
|
915
|
+
if not _acquire_run_lock():
|
|
916
|
+
raise RuntimeError("已有一个白名单任务在跑(正在扫码就别再开第二个),本次不启动浏览器")
|
|
863
917
|
kill_stale_edge()
|
|
864
918
|
ctx = p.chromium.launch_persistent_context(
|
|
865
919
|
str(PROFILE_DIR),
|
|
@@ -872,6 +926,17 @@ def _open_context(p, headless: bool, slow_mo: int = 0):
|
|
|
872
926
|
accept_downloads=False,
|
|
873
927
|
args=["--disable-blink-features=AutomationControlled", "--no-first-run"],
|
|
874
928
|
)
|
|
929
|
+
# profile 偶尔被别的进程覆盖(实测扫完码又被写成未登录),用独立快照兜底
|
|
930
|
+
try:
|
|
931
|
+
st = _state_path()
|
|
932
|
+
if st.exists():
|
|
933
|
+
snap = json.loads(st.read_text("utf-8"))
|
|
934
|
+
cookies = snap.get("cookies") or []
|
|
935
|
+
if cookies:
|
|
936
|
+
ctx.add_cookies(cookies)
|
|
937
|
+
log("login", f"已注入登录快照 cookie {len(cookies)} 条({st.name})")
|
|
938
|
+
except Exception as e:
|
|
939
|
+
log("login", f"注入登录快照失败(忽略): {e}", "WARN")
|
|
875
940
|
page = ctx.pages[0] if ctx.pages else ctx.new_page()
|
|
876
941
|
page.set_default_timeout(12000)
|
|
877
942
|
_attach_dialogs(page)
|
|
@@ -1278,6 +1343,11 @@ def do_login(wait_sec: int) -> int:
|
|
|
1278
1343
|
if state == LOGIN_LOGGED:
|
|
1279
1344
|
page.wait_for_timeout(2500)
|
|
1280
1345
|
take_shot(page, "login-ok")
|
|
1346
|
+
try:
|
|
1347
|
+
ctx.storage_state(path=str(_state_path()))
|
|
1348
|
+
log("login", f"登录快照已保存: {_state_path()}")
|
|
1349
|
+
except Exception as e:
|
|
1350
|
+
log("login", f"保存登录快照失败(不影响本次登录): {e}", "WARN")
|
|
1281
1351
|
log("login", f"LOGIN_OK 已登录({detail.get('summary')}),cookie 已保存到专用 profile")
|
|
1282
1352
|
return EXIT_OK
|
|
1283
1353
|
now = time.time()
|
|
@@ -1306,6 +1376,7 @@ def do_login(wait_sec: int) -> int:
|
|
|
1306
1376
|
ctx.close()
|
|
1307
1377
|
except Exception:
|
|
1308
1378
|
pass
|
|
1379
|
+
_release_run_lock()
|
|
1309
1380
|
|
|
1310
1381
|
|
|
1311
1382
|
# ---------------------------------------------------------------- 入口
|