tt-help-cli-ycl 1.4.113 → 1.4.114
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/package.json +1 -1
- package/scripts/monitor-refresh-mini.mjs +161 -0
- package/src/clash/checker.js +18 -4
- package/src/clash/daemon.js +9 -0
- package/src/cli/refresh-mini.js +714 -0
- package/src/cli/refresh.js +28 -12
- package/src/cli/watchdog.js +5 -2
- package/src/lib/api-client.js +57 -42
- package/src/lib/api-interceptor.js +165 -49
- package/src/lib/args.js +86 -0
- package/src/lib/constants.js +22 -0
- package/src/main.js +3 -0
- package/src/npm-main.js +3 -0
- package/src/scraper/explore-core.js +24 -9
- package/src/watch/llm-config.js +14 -0
- package/src/watch/server.js +17 -0
- package/src/watchdog/process-manager.js +58 -1
- package/src/watchdog/reporter.js +1 -0
- package/src/watchdog/standalone-server.js +228 -0
package/package.json
CHANGED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* refresh-mini 运行状况监控(4222 / 5222)
|
|
3
|
+
*
|
|
4
|
+
* - 每 5 分钟采样:各端口 refresh-mini 进程 RSS + Edge 浏览器 RSS/进程数
|
|
5
|
+
* - 采样日志:../newdata/refresh-mini-monitor.log(JSONL)
|
|
6
|
+
* - 每天 08:00~08:10 自动生成报告(md 文件 + macOS 系统通知)
|
|
7
|
+
*/
|
|
8
|
+
import { execSync } from "child_process";
|
|
9
|
+
import fs from "fs";
|
|
10
|
+
|
|
11
|
+
const LOG_FILE =
|
|
12
|
+
"/Users/yinchengliang/soft/tiktok/newdata/refresh-mini-monitor.log";
|
|
13
|
+
const REPORT_DIR = "/Users/yinchengliang/soft/tiktok/newdata";
|
|
14
|
+
const INTERVAL_MS = 5 * 60 * 1000;
|
|
15
|
+
const PORTS = [4222, 5222];
|
|
16
|
+
|
|
17
|
+
function execSilent(cmd) {
|
|
18
|
+
try {
|
|
19
|
+
return execSync(cmd, { encoding: "utf8" }).trim();
|
|
20
|
+
} catch {
|
|
21
|
+
return "";
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** 采样一次:refresh-mini 进程 + Edge 浏览器进程 */
|
|
26
|
+
function sample() {
|
|
27
|
+
const ts = new Date();
|
|
28
|
+
const entry = { time: ts.toISOString(), ports: {} };
|
|
29
|
+
|
|
30
|
+
// ps ax -o pid=,rss=,command= :一行一个进程(macOS/Linux 兼容)
|
|
31
|
+
const psOut = execSilent("ps ax -o pid=,rss=,command=");
|
|
32
|
+
const lines = psOut ? psOut.split("\n") : [];
|
|
33
|
+
|
|
34
|
+
for (const port of PORTS) {
|
|
35
|
+
const rmList = [];
|
|
36
|
+
const edgeList = [];
|
|
37
|
+
for (const line of lines) {
|
|
38
|
+
const m = line.match(/^\s*(\d+)\s+(\d+)\s+(.*)$/);
|
|
39
|
+
if (!m) continue;
|
|
40
|
+
const [, pid, rssKb, cmd] = m;
|
|
41
|
+
const is5222 = /--port(=|\s+)5222/.test(cmd);
|
|
42
|
+
// 排除监控脚本自身(文件名含 refresh-mini)
|
|
43
|
+
if (
|
|
44
|
+
cmd.includes("refresh-mini") &&
|
|
45
|
+
!cmd.includes("monitor-refresh-mini")
|
|
46
|
+
) {
|
|
47
|
+
if ((port === 5222) === is5222) {
|
|
48
|
+
rmList.push({ pid: Number(pid), rssKb: Number(rssKb) });
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (cmd.includes(`remote-debugging-port=${port}`)) {
|
|
52
|
+
edgeList.push({ pid: Number(pid), rssKb: Number(rssKb) });
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const sum = (list) => list.reduce((a, b) => a + b.rssKb, 0);
|
|
56
|
+
entry.ports[port] = {
|
|
57
|
+
refreshMini: {
|
|
58
|
+
processes: rmList.length,
|
|
59
|
+
rssMb: +(sum(rmList) / 1024).toFixed(1),
|
|
60
|
+
pids: rmList.map((p) => p.pid),
|
|
61
|
+
},
|
|
62
|
+
edge: {
|
|
63
|
+
processes: edgeList.length,
|
|
64
|
+
rssMb: +(sum(edgeList) / 1024).toFixed(1),
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
fs.appendFileSync(LOG_FILE, JSON.stringify(entry) + "\n");
|
|
70
|
+
const parts = Object.entries(entry.ports).map(
|
|
71
|
+
([p, d]) =>
|
|
72
|
+
`${p}: rm ${d.refreshMini.processes}个/${d.refreshMini.rssMb}MB, Edge ${d.edge.processes}进程/${d.edge.rssMb}MB`,
|
|
73
|
+
);
|
|
74
|
+
console.log(`[${ts.toLocaleString("zh-CN")}] ${parts.join(" | ")}`);
|
|
75
|
+
return entry;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** 统计日志生成报告 */
|
|
79
|
+
function generateReport() {
|
|
80
|
+
let rows = [];
|
|
81
|
+
try {
|
|
82
|
+
rows = fs
|
|
83
|
+
.readFileSync(LOG_FILE, "utf8")
|
|
84
|
+
.split("\n")
|
|
85
|
+
.filter(Boolean)
|
|
86
|
+
.map((l) => JSON.parse(l));
|
|
87
|
+
} catch {
|
|
88
|
+
/* 无日志 */
|
|
89
|
+
}
|
|
90
|
+
if (rows.length === 0) return;
|
|
91
|
+
|
|
92
|
+
const fmt = (v) => (v == null ? "-" : `${v} MB`);
|
|
93
|
+
const stats = (arr) => {
|
|
94
|
+
const vals = arr.filter((v) => v != null);
|
|
95
|
+
if (vals.length === 0) return { min: "-", max: "-", avg: "-" };
|
|
96
|
+
return {
|
|
97
|
+
min: Math.min(...vals).toFixed(1),
|
|
98
|
+
max: Math.max(...vals).toFixed(1),
|
|
99
|
+
avg: (vals.reduce((a, b) => a + b, 0) / vals.length).toFixed(1),
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const lines = [
|
|
104
|
+
`# refresh-mini 运行状况报告`,
|
|
105
|
+
``,
|
|
106
|
+
`生成时间: ${new Date().toLocaleString("zh-CN")}`,
|
|
107
|
+
`采样频率: 5 分钟 | 采样点: ${rows.length} 个`,
|
|
108
|
+
``,
|
|
109
|
+
];
|
|
110
|
+
for (const port of PORTS) {
|
|
111
|
+
lines.push(`## 端口 ${port}`, ``);
|
|
112
|
+
const rmRss = rows.map((r) => r.ports?.[port]?.refreshMini?.rssMb ?? null);
|
|
113
|
+
const edgeRss = rows.map((r) => r.ports?.[port]?.edge?.rssMb ?? null);
|
|
114
|
+
const rmAlive = rows.filter(
|
|
115
|
+
(r) => (r.ports?.[port]?.refreshMini?.processes ?? 0) > 0,
|
|
116
|
+
).length;
|
|
117
|
+
const edgeAlive = rows.filter(
|
|
118
|
+
(r) => (r.ports?.[port]?.edge?.processes ?? 0) > 0,
|
|
119
|
+
).length;
|
|
120
|
+
const rmS = stats(rmRss);
|
|
121
|
+
const edgeS = stats(edgeRss);
|
|
122
|
+
const last = rows[rows.length - 1]?.ports?.[port] ?? {};
|
|
123
|
+
lines.push(
|
|
124
|
+
`- refresh-mini 进程:存活采样 ${rmAlive}/${rows.length} 次,内存 min/max/avg = ${fmt(rmS.min)} / ${fmt(rmS.max)} / ${fmt(rmS.avg)}`,
|
|
125
|
+
`- 当前:${last.refreshMini?.processes ?? 0} 个进程,${fmt(last.refreshMini?.rssMb)}(PID: ${(last.refreshMini?.pids ?? []).join(", ") || "无"})`,
|
|
126
|
+
`- Edge 浏览器:存活采样 ${edgeAlive}/${rows.length} 次,内存 min/max/avg = ${fmt(edgeS.min)} / ${fmt(edgeS.max)} / ${fmt(edgeS.avg)}`,
|
|
127
|
+
`- 当前:${last.edge?.processes ?? 0} 个进程,${fmt(last.edge?.rssMb)}`,
|
|
128
|
+
``,
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const now = new Date();
|
|
133
|
+
const file = `${REPORT_DIR}/refresh-mini-report-${now.toISOString().slice(0, 10)}.md`;
|
|
134
|
+
fs.writeFileSync(file, lines.join("\n"));
|
|
135
|
+
console.log(`\n=== 8 点报告已生成: ${file} ===`);
|
|
136
|
+
execSilent(
|
|
137
|
+
`osascript -e 'display notification "refresh-mini 运行报告已生成" with title "8点监控报告" subtitle "${file.split("/").pop()}"'`,
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// 每天 08:00~08:10 窗口内报告一次
|
|
142
|
+
let reportedToday = false;
|
|
143
|
+
function maybeReport() {
|
|
144
|
+
const now = new Date();
|
|
145
|
+
const key = now.toISOString().slice(0, 10);
|
|
146
|
+
if (now.getHours() === 8 && now.getMinutes() < 10) {
|
|
147
|
+
if (reportedToday !== key) {
|
|
148
|
+
reportedToday = key;
|
|
149
|
+
generateReport();
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
console.log(
|
|
155
|
+
`[监控] 启动于 ${new Date().toLocaleString("zh-CN")},每 5 分钟采样,每天 08:00 报告`,
|
|
156
|
+
);
|
|
157
|
+
sample();
|
|
158
|
+
setInterval(() => {
|
|
159
|
+
sample();
|
|
160
|
+
maybeReport();
|
|
161
|
+
}, INTERVAL_MS);
|
package/src/clash/checker.js
CHANGED
|
@@ -173,13 +173,22 @@ export async function requireClashOrExit(label = "") {
|
|
|
173
173
|
|
|
174
174
|
export function checkNodeHealth(url = CONFIG.testUrl) {
|
|
175
175
|
const { body, metaLine, error } = _curlViaProxy(url);
|
|
176
|
-
const { httpCode, timeTotal, sizeDownload } =
|
|
176
|
+
const { httpCode, timeTotal, sizeDownload, numRedirects } =
|
|
177
|
+
_parseCurlMeta(metaLine);
|
|
177
178
|
const title = _extractTitle(body);
|
|
178
179
|
return {
|
|
179
|
-
healthy: _isHealthyPage(
|
|
180
|
+
healthy: _isHealthyPage(
|
|
181
|
+
error,
|
|
182
|
+
httpCode,
|
|
183
|
+
sizeDownload,
|
|
184
|
+
title,
|
|
185
|
+
body,
|
|
186
|
+
numRedirects,
|
|
187
|
+
),
|
|
180
188
|
httpCode,
|
|
181
189
|
timeTotal,
|
|
182
190
|
sizeDownload,
|
|
191
|
+
numRedirects,
|
|
183
192
|
title,
|
|
184
193
|
error,
|
|
185
194
|
};
|
|
@@ -189,7 +198,7 @@ function _curlViaProxy(url) {
|
|
|
189
198
|
const args = [
|
|
190
199
|
"-s",
|
|
191
200
|
"-w",
|
|
192
|
-
"\n__CURL_META__%{http_code}|%{time_total}|%{size_download}",
|
|
201
|
+
"\n__CURL_META__%{http_code}|%{time_total}|%{size_download}|%{num_redirects}",
|
|
193
202
|
"--proxy",
|
|
194
203
|
CONFIG.proxyUrl,
|
|
195
204
|
"--max-time",
|
|
@@ -224,6 +233,7 @@ function _parseCurlMeta(line) {
|
|
|
224
233
|
httpCode: parseInt(p[0]) || 0,
|
|
225
234
|
timeTotal: parseFloat(p[1]) || 0,
|
|
226
235
|
sizeDownload: parseInt(p[2]) || 0,
|
|
236
|
+
numRedirects: parseInt(p[3]) || 0,
|
|
227
237
|
};
|
|
228
238
|
}
|
|
229
239
|
|
|
@@ -232,10 +242,14 @@ function _extractTitle(body) {
|
|
|
232
242
|
return m ? m[1].trim() : "";
|
|
233
243
|
}
|
|
234
244
|
|
|
235
|
-
function _isHealthyPage(error, httpCode, size, title, body) {
|
|
245
|
+
function _isHealthyPage(error, httpCode, size, title, body, numRedirects = 0) {
|
|
236
246
|
return (
|
|
237
247
|
!error &&
|
|
238
248
|
httpCode === 200 &&
|
|
249
|
+
// 发生跳转即不健康:探测 URL 正常时应直接 200。
|
|
250
|
+
// 跳转意味着出口地区/节点异常(如香港停运 302 → /hk/about,
|
|
251
|
+
// 该页 title 仍是 TikTok,旧版 title 规则无法识别)
|
|
252
|
+
numRedirects === 0 &&
|
|
239
253
|
size >= CONFIG.minPageSize &&
|
|
240
254
|
title.includes("TikTok") &&
|
|
241
255
|
!title.startsWith("About |") &&
|
package/src/clash/daemon.js
CHANGED
|
@@ -212,6 +212,7 @@ export class ClashDaemon {
|
|
|
212
212
|
if (!target) {
|
|
213
213
|
// 全部候选不可用,恢复原节点
|
|
214
214
|
await checker.switchToNode(node).catch(() => {});
|
|
215
|
+
await api.closeAllConnections().catch(() => {});
|
|
215
216
|
this._log(`✗ 恢复原节点 ${node}(原因: ${reason} — 候选全部不可用)`);
|
|
216
217
|
ipc.writeHealth({
|
|
217
218
|
node,
|
|
@@ -226,6 +227,9 @@ export class ClashDaemon {
|
|
|
226
227
|
this._log(
|
|
227
228
|
`⚠ 节点切换 [巡检异常: ${reason}]: ${node} → ${target} (${h2.timeTotal.toFixed(1)}s ${(h2.sizeDownload / 1024).toFixed(0)}KB)`,
|
|
228
229
|
);
|
|
230
|
+
// 切换后断开所有旧连接:mihomo 不主动掐旧连接,浏览器 keep-alive 会继续走旧节点
|
|
231
|
+
// 导致"时好时坏"(实测旧连接可存活 8+ 分钟),必须强制重建才即时生效
|
|
232
|
+
await api.closeAllConnections().catch(() => {});
|
|
229
233
|
ipc.writeHealth({
|
|
230
234
|
node: target,
|
|
231
235
|
healthy: h2.healthy,
|
|
@@ -312,6 +316,8 @@ export class ClashDaemon {
|
|
|
312
316
|
});
|
|
313
317
|
}
|
|
314
318
|
const h = checker.checkNodeHealth();
|
|
319
|
+
// 切换后断开所有旧连接,浏览器立即重建到新节点(同巡检切换)
|
|
320
|
+
await api.closeAllConnections().catch(() => {});
|
|
315
321
|
this._log(
|
|
316
322
|
`🔄 看门狗指令: 切换完成 (${h.timeTotal.toFixed(1)}s ${(h.sizeDownload / 1024).toFixed(0)}KB, ${h.healthy ? "健康" : "异常"})`,
|
|
317
323
|
);
|
|
@@ -366,6 +372,7 @@ export class ClashDaemon {
|
|
|
366
372
|
}
|
|
367
373
|
if (!target) {
|
|
368
374
|
await checker.switchToNode(node).catch(() => {});
|
|
375
|
+
await api.closeAllConnections().catch(() => {});
|
|
369
376
|
this._log(
|
|
370
377
|
`✗ failover [${reason}] — 候选全部不可用,恢复原节点 ${node}`,
|
|
371
378
|
);
|
|
@@ -378,6 +385,8 @@ export class ClashDaemon {
|
|
|
378
385
|
});
|
|
379
386
|
}
|
|
380
387
|
this._log(`⚠ 节点切换 [${reason}]: ${node} → ${target}`);
|
|
388
|
+
// 切换后断开所有旧连接,浏览器立即重建到新节点(同巡检切换)
|
|
389
|
+
await api.closeAllConnections().catch(() => {});
|
|
381
390
|
ipc.writeHealth({
|
|
382
391
|
node: target,
|
|
383
392
|
healthy: h2.healthy,
|