panrouter 1.4.2 → 1.5.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/cli.mjs +13 -11
- package/package.json +3 -3
- package/panrouter-tray.bat +26 -0
- package/daemon.mjs +0 -127
package/cli.mjs
CHANGED
|
@@ -141,26 +141,28 @@ async function startServer() {
|
|
|
141
141
|
// ─── 4. 以托盘模式启动 ──────────────────────────────────────────────────
|
|
142
142
|
|
|
143
143
|
/**
|
|
144
|
-
* 启动策略 (
|
|
144
|
+
* 启动策略 (Windows BAT 原生方式):
|
|
145
145
|
*
|
|
146
|
-
* cli.mjs ─spawn(detached)──→
|
|
147
|
-
* ├─
|
|
148
|
-
* └─
|
|
149
|
-
*
|
|
146
|
+
* cli.mjs ─spawn(detached)──→ cmd /c panrouter-tray.bat
|
|
147
|
+
* ├─ start /B → node server.mjs (后台隐藏)
|
|
148
|
+
* └─ start /B → powershell tray-daemon.ps1 (后台无窗口)
|
|
149
|
+
* └─ NotifyIcon ✓
|
|
150
150
|
*
|
|
151
|
-
*
|
|
152
|
-
*
|
|
151
|
+
* 用 cmd /c start /B 是 Windows 最原生的后台/隐藏启动方式,
|
|
152
|
+
* 不依赖 detached vs non-detached 的 Window Station 差异。
|
|
153
153
|
*/
|
|
154
154
|
function startTray() {
|
|
155
|
-
const
|
|
156
|
-
|
|
157
|
-
|
|
155
|
+
const batPath = path.join(__dirname, "panrouter-tray.bat");
|
|
156
|
+
|
|
157
|
+
if (!fs.existsSync(batPath)) {
|
|
158
|
+
log("!!", "未找到 panrouter-tray.bat", "red");
|
|
158
159
|
process.exit(1);
|
|
159
160
|
}
|
|
160
161
|
|
|
161
162
|
log("..", "正在以托盘模式启动 Pan Router...", "yellow");
|
|
162
163
|
|
|
163
|
-
const child = spawn(
|
|
164
|
+
const child = spawn("cmd.exe", ["/c", batPath], {
|
|
165
|
+
cwd: __dirname,
|
|
164
166
|
stdio: "ignore",
|
|
165
167
|
windowsHide: true,
|
|
166
168
|
detached: true,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "panrouter",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "让 Claude Code 免费使用 DeepSeek 等模型,无需 API Key",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -8,9 +8,9 @@
|
|
|
8
8
|
},
|
|
9
9
|
"files": [
|
|
10
10
|
"cli.mjs",
|
|
11
|
-
"daemon.mjs",
|
|
12
11
|
"server.mjs",
|
|
13
|
-
"tray-daemon.ps1"
|
|
12
|
+
"tray-daemon.ps1",
|
|
13
|
+
"panrouter-tray.bat"
|
|
14
14
|
],
|
|
15
15
|
"license": "MIT"
|
|
16
16
|
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
@echo off
|
|
2
|
+
rem Pan Router Tray Launcher (Windows 原生 BAT, 最可靠方式)
|
|
3
|
+
|
|
4
|
+
setlocal
|
|
5
|
+
set "SCRIPT_DIR=%~dp0"
|
|
6
|
+
|
|
7
|
+
rem 1. 清理旧 server.mjs 进程
|
|
8
|
+
for /f "tokens=2 delims=," %%a in ('wmic process where "name='node.exe'" get ProcessId^,CommandLine /format:csv 2^>nul ^| findstr "server.mjs"') do (
|
|
9
|
+
taskkill /f /pid %%a >nul 2>&1
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
rem 2. 后台隐藏启动 server.mjs (start /B = 不创建新窗口, 同一控制台后台)
|
|
13
|
+
start /B node "%SCRIPT_DIR%server.mjs"
|
|
14
|
+
|
|
15
|
+
rem 3. 等待服务器就绪 (最久 5 秒)
|
|
16
|
+
for /l %%i in (1,1,5) do (
|
|
17
|
+
>nul 2>&1 %WINDIR%\System32\curl.exe -s http://127.0.0.1:50816/health && goto :ready
|
|
18
|
+
>nul 2>&1 %WINDIR%\System32\timeout.exe /t 1 /nobreak
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
:ready
|
|
22
|
+
|
|
23
|
+
rem 4. 启动 PS 托盘 (无窗口后台)
|
|
24
|
+
start /B powershell -ExecutionPolicy Bypass -WindowStyle Hidden -STA -File "%SCRIPT_DIR%tray-daemon.ps1"
|
|
25
|
+
|
|
26
|
+
rem 5. BAT 立即结束 (不影响后台进程)
|
package/daemon.mjs
DELETED
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Pan Router Daemon — 后台守护进程
|
|
5
|
-
*
|
|
6
|
-
* 让 server.mjs + tray-daemon.ps1 在无窗口下运行。
|
|
7
|
-
* server.mjs 用 detached + windowsHide 启动。
|
|
8
|
-
* tray-daemon.ps1 用 pipe 连接 (不 detached — 否则失去 Window Station)。
|
|
9
|
-
*
|
|
10
|
-
* 用法 (由 cli.mjs --tray 调用):
|
|
11
|
-
* node daemon.mjs
|
|
12
|
-
*
|
|
13
|
-
* 所有路径自动从 __dirname 解析 (同包目录)。
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
import { spawn } from "node:child_process";
|
|
17
|
-
import { createInterface } from "node:readline";
|
|
18
|
-
import { fileURLToPath } from "node:url";
|
|
19
|
-
import path from "node:path";
|
|
20
|
-
import fs from "node:fs";
|
|
21
|
-
import http from "node:http";
|
|
22
|
-
|
|
23
|
-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
24
|
-
const appDir = __dirname;
|
|
25
|
-
const serverPath = path.join(appDir, "server.mjs");
|
|
26
|
-
const trayPsPath = path.join(appDir, "tray-daemon.ps1");
|
|
27
|
-
|
|
28
|
-
const LOG = path.join(process.env.TEMP || "/tmp", "panrouter-daemon.log");
|
|
29
|
-
function log(msg) {
|
|
30
|
-
try { fs.appendFileSync(LOG, `${new Date().toISOString().slice(11,19)} ${msg}\n`); } catch {}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
log("=== PanRouter Daemon (v3) ===");
|
|
34
|
-
log(`serverPath=${serverPath}`);
|
|
35
|
-
log(`trayPsPath=${trayPsPath}`);
|
|
36
|
-
|
|
37
|
-
// ─── 健康检查 ─────────────────────────────────────
|
|
38
|
-
function isOnline() {
|
|
39
|
-
return new Promise(rs => {
|
|
40
|
-
try {
|
|
41
|
-
const req = http.get("http://127.0.0.1:50816/health", () => {});
|
|
42
|
-
req.on("response", () => { req.destroy(); rs(true); });
|
|
43
|
-
req.on("error", () => rs(false));
|
|
44
|
-
req.setTimeout(1500, () => { req.destroy(); rs(false); });
|
|
45
|
-
} catch { rs(false); }
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// ─── 启动隐藏的 server.mjs ────────────────────────
|
|
50
|
-
log("Starting server...");
|
|
51
|
-
const serverProcess = spawn(process.execPath, [serverPath], {
|
|
52
|
-
cwd: appDir,
|
|
53
|
-
stdio: "ignore",
|
|
54
|
-
windowsHide: true,
|
|
55
|
-
detached: true,
|
|
56
|
-
shell: false,
|
|
57
|
-
});
|
|
58
|
-
serverProcess.on("error", (err) => log(`Server spawn ERROR: ${err.message}`));
|
|
59
|
-
serverProcess.unref();
|
|
60
|
-
log(`Server spawned PID=${serverProcess.pid || "(no pid)"}`);
|
|
61
|
-
|
|
62
|
-
// ─── 等服务器就绪后启动 PS 托盘 ──────────────────
|
|
63
|
-
(async () => {
|
|
64
|
-
let ready = false;
|
|
65
|
-
for (let i = 0; i < 20; i++) {
|
|
66
|
-
if (await isOnline()) { ready = true; break; }
|
|
67
|
-
await new Promise(r => setTimeout(r, 500));
|
|
68
|
-
}
|
|
69
|
-
log(`Server online=${ready}`);
|
|
70
|
-
|
|
71
|
-
if (!fs.existsSync(trayPsPath)) {
|
|
72
|
-
log(`ERROR: tray-daemon.ps1 not found at ${trayPsPath}`);
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// ─── 启动 PS 托盘 (pipe, 不 detached!) ──────────
|
|
77
|
-
// 9Router 的做法: PS 必须与 daemon 同 session 才能创建通知图标
|
|
78
|
-
log("Starting PS tray...");
|
|
79
|
-
const psProcess = spawn("powershell.exe", [
|
|
80
|
-
"-NoProfile",
|
|
81
|
-
"-ExecutionPolicy", "Bypass",
|
|
82
|
-
"-WindowStyle", "Hidden",
|
|
83
|
-
"-File", trayPsPath,
|
|
84
|
-
], {
|
|
85
|
-
stdio: ["pipe", "pipe", "pipe"],
|
|
86
|
-
windowsHide: true,
|
|
87
|
-
shell: false,
|
|
88
|
-
// ⚠ 没有 detached — PS 保持子进程身份才能访问 Window Station
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
psProcess.on("error", (err) => log(`PS spawn ERROR: ${err.message}`));
|
|
92
|
-
|
|
93
|
-
// 读取 PS 事件 (stdout)
|
|
94
|
-
createInterface({ input: psProcess.stdout }).on("line", (line) => {
|
|
95
|
-
try {
|
|
96
|
-
const evt = JSON.parse(line);
|
|
97
|
-
log(`PS: ${evt.type} idx=${evt.index}`);
|
|
98
|
-
if (evt.type === "click" && evt.index === 2) {
|
|
99
|
-
log("Exit from PS menu");
|
|
100
|
-
try { psProcess.stdin.write(JSON.stringify({ action: "kill" }) + "\n"); } catch {}
|
|
101
|
-
setTimeout(() => process.exit(0), 300);
|
|
102
|
-
}
|
|
103
|
-
} catch {}
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
psProcess.stderr.on("data", (d) => log(`[ps-err] ${d.toString().trim()}`));
|
|
107
|
-
psProcess.on("exit", (code) => {
|
|
108
|
-
log(`PS exited code=${code}`);
|
|
109
|
-
process.exit(0);
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
// 发送菜单配置
|
|
113
|
-
function psSend(cmd) {
|
|
114
|
-
try { psProcess.stdin.write(JSON.stringify(cmd) + "\n"); } catch {}
|
|
115
|
-
}
|
|
116
|
-
await new Promise(r => setTimeout(r, 500)); // 等 PS 就绪
|
|
117
|
-
psSend({ action: "add-item", index: 0, title: "Pan Router - :50816", enabled: false });
|
|
118
|
-
psSend({ action: "add-item", index: 1, title: "─".repeat(19), enabled: false });
|
|
119
|
-
psSend({ action: "add-item", index: 2, title: "退出", enabled: true });
|
|
120
|
-
psSend({ action: "set-tooltip", text: "Pan Router | 端口 50816" });
|
|
121
|
-
|
|
122
|
-
log("Daemon running — keeping session alive");
|
|
123
|
-
process.stdin.resume(); // keep alive
|
|
124
|
-
})();
|
|
125
|
-
|
|
126
|
-
process.on("SIGTERM", () => { log("SIGTERM"); process.exit(0); });
|
|
127
|
-
process.on("SIGINT", () => { log("SIGINT"); process.exit(0); });
|