panrouter 5.2.0 → 5.3.0
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 +72 -4
package/package.json
CHANGED
package/pool-worker.mjs
CHANGED
|
@@ -118,7 +118,7 @@ function isPortOpen(port) {
|
|
|
118
118
|
});
|
|
119
119
|
}
|
|
120
120
|
|
|
121
|
-
// ─── 查找 cloudflared
|
|
121
|
+
// ─── 查找 & 自动安装 cloudflared ────────────────────────────────────────────
|
|
122
122
|
function findCloudflared() {
|
|
123
123
|
const candidates = ["cloudflared", "cloudflared.exe"];
|
|
124
124
|
for (const name of candidates) {
|
|
@@ -138,6 +138,56 @@ function findCloudflared() {
|
|
|
138
138
|
return null;
|
|
139
139
|
}
|
|
140
140
|
|
|
141
|
+
async function installCloudflared() {
|
|
142
|
+
log("正在自动安装 cloudflared...");
|
|
143
|
+
const platform = process.platform;
|
|
144
|
+
try {
|
|
145
|
+
if (platform === "win32") {
|
|
146
|
+
const dir = path.join(process.env.USERPROFILE || "C:/Users/default", "AppData", "Local", "cloudflared");
|
|
147
|
+
const exe = path.join(dir, "cloudflared.exe");
|
|
148
|
+
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
|
|
149
|
+
log("正在下载 cloudflared (Windows)...");
|
|
150
|
+
const url = "https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-windows-amd64.exe";
|
|
151
|
+
spawnSync("curl.exe", ["-L", "-o", exe, url], { stdio: "pipe", timeout: 120000 });
|
|
152
|
+
if (fs.existsSync(exe)) {
|
|
153
|
+
// 添加至用户 PATH(下次终端生效)
|
|
154
|
+
const currentPath = process.env.PATH || "";
|
|
155
|
+
if (!currentPath.includes(dir)) {
|
|
156
|
+
try {
|
|
157
|
+
spawnSync("powershell.exe", [
|
|
158
|
+
"-Command", `[Environment]::SetEnvironmentVariable("Path", [Environment]::GetEnvironmentVariable("Path","User") + ";${dir}", "User")`
|
|
159
|
+
], { stdio: "pipe", timeout: 10000 });
|
|
160
|
+
} catch {}
|
|
161
|
+
}
|
|
162
|
+
log("cloudflared 安装完成", "OK");
|
|
163
|
+
return exe;
|
|
164
|
+
}
|
|
165
|
+
} else if (platform === "darwin") {
|
|
166
|
+
log("正在通过 Homebrew 安装 cloudflared...");
|
|
167
|
+
spawnSync("brew", ["install", "cloudflare/cloudflare/cloudflared"], { stdio: "inherit", timeout: 120000 });
|
|
168
|
+
const r = spawnSync("which", ["cloudflared"], { stdio: "pipe", timeout: 5000 });
|
|
169
|
+
if (r.status === 0) { log("cloudflared 安装完成", "OK"); return "cloudflared"; }
|
|
170
|
+
} else {
|
|
171
|
+
// Linux / Termux
|
|
172
|
+
const isTermux = fs.existsSync("/data/data/com.termux");
|
|
173
|
+
if (isTermux) {
|
|
174
|
+
log("正在通过 pkg 安装 cloudflared (Termux)...");
|
|
175
|
+
spawnSync("pkg", ["install", "cloudflared", "-y"], { stdio: "inherit", timeout: 120000 });
|
|
176
|
+
} else {
|
|
177
|
+
log("正在下载 cloudflared (Linux)...");
|
|
178
|
+
const url = "https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64";
|
|
179
|
+
spawnSync("curl", ["-L", "-o", "/usr/local/bin/cloudflared", url], { stdio: "pipe", timeout: 120000 });
|
|
180
|
+
spawnSync("chmod", ["+x", "/usr/local/bin/cloudflared"], { stdio: "pipe", timeout: 5000 });
|
|
181
|
+
}
|
|
182
|
+
const r = spawnSync("which", ["cloudflared"], { stdio: "pipe", timeout: 5000 });
|
|
183
|
+
if (r.status === 0) { log("cloudflared 安装完成", "OK"); return "cloudflared"; }
|
|
184
|
+
}
|
|
185
|
+
} catch (e) {
|
|
186
|
+
log(`自动安装失败: ${e.message}`, "ERR");
|
|
187
|
+
}
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
|
|
141
191
|
// ─── 确保 server.mjs 在运行 ──────────────────────────────────────────────
|
|
142
192
|
async function ensureServer() {
|
|
143
193
|
if (await isPortOpen(SERVER_PORT)) {
|
|
@@ -176,12 +226,26 @@ async function ensureServer() {
|
|
|
176
226
|
function startTunnel() {
|
|
177
227
|
if (isShuttingDown) return;
|
|
178
228
|
|
|
179
|
-
|
|
229
|
+
let cfPath = findCloudflared();
|
|
180
230
|
if (!cfPath) {
|
|
181
|
-
log("未找到 cloudflared
|
|
231
|
+
log("未找到 cloudflared,尝试自动安装...");
|
|
232
|
+
// 异步安装,不阻塞隧道循环
|
|
233
|
+
installCloudflared().then((installed) => {
|
|
234
|
+
if (installed) {
|
|
235
|
+
log("安装成功,正在启动隧道...");
|
|
236
|
+
doStartTunnel(installed);
|
|
237
|
+
} else {
|
|
238
|
+
log("自动安装失败,请手动安装 cloudflared", "ERR");
|
|
239
|
+
}
|
|
240
|
+
});
|
|
182
241
|
return;
|
|
183
242
|
}
|
|
184
243
|
|
|
244
|
+
doStartTunnel(cfPath);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function doStartTunnel(cfPath) {
|
|
248
|
+
|
|
185
249
|
log("正在请求匿名公网入口...");
|
|
186
250
|
|
|
187
251
|
cfProcess = spawn(cfPath, ["tunnel", "--url", `http://127.0.0.1:${SERVER_PORT}`], {
|
|
@@ -222,7 +286,11 @@ function startTunnel() {
|
|
|
222
286
|
// 自动复活
|
|
223
287
|
if (!isShuttingDown) {
|
|
224
288
|
log("准备在 5 秒后自动重启隧道...", "WARN");
|
|
225
|
-
setTimeout(
|
|
289
|
+
setTimeout(() => {
|
|
290
|
+
const cf = findCloudflared();
|
|
291
|
+
if (cf) doStartTunnel(cf);
|
|
292
|
+
else startTunnel();
|
|
293
|
+
}, 5000);
|
|
226
294
|
}
|
|
227
295
|
});
|
|
228
296
|
|