panrouter 5.1.0 → 5.1.1
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/pool-worker.mjs +36 -2
package/package.json
CHANGED
package/pool-worker.mjs
CHANGED
|
@@ -44,11 +44,45 @@ function notifyHub(status, url = "") {
|
|
|
44
44
|
"x-secret-token": AUTH_SECRET,
|
|
45
45
|
},
|
|
46
46
|
});
|
|
47
|
-
req.on("error", () => {});
|
|
47
|
+
req.on("error", () => {});
|
|
48
48
|
req.write(payload);
|
|
49
49
|
req.end();
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
+
// 可等待的 notify — 用于首次上线确认
|
|
53
|
+
function notifyHubWait(status, url = "") {
|
|
54
|
+
return new Promise((resolve) => {
|
|
55
|
+
const payload = JSON.stringify({ nodeId: NODE_ID, status, url });
|
|
56
|
+
const req = https.request(MAIN_HUB_URL, {
|
|
57
|
+
method: "POST",
|
|
58
|
+
headers: {
|
|
59
|
+
"Content-Type": "application/json",
|
|
60
|
+
"Content-Length": Buffer.byteLength(payload),
|
|
61
|
+
"x-secret-token": AUTH_SECRET,
|
|
62
|
+
},
|
|
63
|
+
}, (res) => resolve(res.statusCode === 200));
|
|
64
|
+
req.on("error", () => resolve(false));
|
|
65
|
+
req.setTimeout(5000, () => { req.destroy(); resolve(false); });
|
|
66
|
+
req.write(payload);
|
|
67
|
+
req.end();
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// 首次上线确认 — 失败则重试,最多 5 次
|
|
72
|
+
async function confirmOnline(url, retries = 5) {
|
|
73
|
+
const ok = await notifyHubWait("online", url);
|
|
74
|
+
if (ok) {
|
|
75
|
+
log("主控已确认节点注册", "OK");
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
if (retries > 0) {
|
|
79
|
+
log(`注册尚未确认,3 秒后重试 (剩余 ${retries} 次)`, "INFO");
|
|
80
|
+
await new Promise((r) => setTimeout(r, 3000));
|
|
81
|
+
return confirmOnline(url, retries - 1);
|
|
82
|
+
}
|
|
83
|
+
log("注册确认已达最大重试次数", "WARN");
|
|
84
|
+
}
|
|
85
|
+
|
|
52
86
|
// ─── 检查端口 ────────────────────────────────────────────────────────────────
|
|
53
87
|
function isPortOpen(port) {
|
|
54
88
|
return new Promise((resolve) => {
|
|
@@ -138,7 +172,7 @@ function startTunnel() {
|
|
|
138
172
|
if (match && match[0] !== currentUrl) {
|
|
139
173
|
currentUrl = match[0];
|
|
140
174
|
log(`成功获取公网入口: ${currentUrl}`, "OK");
|
|
141
|
-
|
|
175
|
+
confirmOnline(currentUrl); // 确保主控注册成功(异步重试)
|
|
142
176
|
}
|
|
143
177
|
|
|
144
178
|
// 网络波动提示
|