panrouter 1.4.1 → 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 -15
- package/package.json +3 -3
- package/panrouter-tray.bat +26 -0
- package/daemon.mjs +0 -148
package/cli.mjs
CHANGED
|
@@ -141,30 +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
|
-
|
|
165
|
-
`--serverPath="${path.join(__dirname, "server.mjs")}"`,
|
|
166
|
-
`--trayPsPath="${path.join(__dirname, "tray-daemon.ps1")}"`,
|
|
167
|
-
], {
|
|
164
|
+
const child = spawn("cmd.exe", ["/c", batPath], {
|
|
165
|
+
cwd: __dirname,
|
|
168
166
|
stdio: "ignore",
|
|
169
167
|
windowsHide: true,
|
|
170
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,148 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Pan Router Daemon — 后台守护进程
|
|
5
|
-
*
|
|
6
|
-
* 用法 (由 cli.mjs --tray 调用):
|
|
7
|
-
* node daemon.mjs --serverPath="..." --trayPsPath="..."
|
|
8
|
-
*
|
|
9
|
-
* 架构 (参考 9Router):
|
|
10
|
-
* daemon.mjs (无窗口, 常驻)
|
|
11
|
-
* ├─ spawn server.mjs (hidden, UseShellExecute=true)
|
|
12
|
-
* └─ spawn powershell tray-daemon.ps1 (pipe, NOT detached)
|
|
13
|
-
* └─ NotifyIcon (右下角)
|
|
14
|
-
*
|
|
15
|
-
* 关键: PS 子进程不 detached, 保持与 daemon 的 session 连接,
|
|
16
|
-
* 否则失去 Window Station 无法创建 UI 通知区图标。
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
import { spawn, execSync } from "node:child_process";
|
|
20
|
-
import { createInterface } from "node:readline";
|
|
21
|
-
import { fileURLToPath } from "node:url";
|
|
22
|
-
import path from "node:path";
|
|
23
|
-
import fs from "node:fs";
|
|
24
|
-
import http from "node:http";
|
|
25
|
-
|
|
26
|
-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
27
|
-
|
|
28
|
-
// ─── 解析参数 ────────────────────────────────────
|
|
29
|
-
const serverPath = process.argv.find(a => a.startsWith("--serverPath="))?.split("=")[1];
|
|
30
|
-
const trayPsPath = process.argv.find(a => a.startsWith("--trayPsPath="))?.split("=")[1];
|
|
31
|
-
if (!serverPath || !trayPsPath) { process.exit(1); }
|
|
32
|
-
|
|
33
|
-
const appDir = path.dirname(serverPath);
|
|
34
|
-
const LOG = path.join(process.env.TEMP || "/tmp", "panrouter-daemon.log");
|
|
35
|
-
|
|
36
|
-
function log(msg) {
|
|
37
|
-
try { fs.appendFileSync(LOG, `${new Date().toISOString().slice(11,19)} ${msg}\n`); } catch {}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
log("=== PanRouter Daemon (v2) ===");
|
|
41
|
-
log(`serverPath=${serverPath}`);
|
|
42
|
-
|
|
43
|
-
// ─── 健康检查 ─────────────────────────────────────
|
|
44
|
-
function isOnline() {
|
|
45
|
-
return new Promise(rs => {
|
|
46
|
-
const req = http.get("http://127.0.0.1:50816/health", () => {});
|
|
47
|
-
req.on("response", () => { req.destroy(); rs(true); });
|
|
48
|
-
req.on("error", () => rs(false));
|
|
49
|
-
req.setTimeout(1500, () => { req.destroy(); rs(false); });
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// ─── 启动隐藏的 server.mjs ────────────────────────
|
|
54
|
-
const serverProcess = (() => {
|
|
55
|
-
// 先杀旧的
|
|
56
|
-
try {
|
|
57
|
-
const wmic = spawn("wmic", [
|
|
58
|
-
"process", "where", "name='node.exe'", "get", "ProcessId,CommandLine", "/format:csv"
|
|
59
|
-
], { stdio: ["ignore", "pipe", "ignore"] });
|
|
60
|
-
let out = "";
|
|
61
|
-
wmic.stdout.on("data", d => out += d.toString());
|
|
62
|
-
wmic.on("close", () => {
|
|
63
|
-
for (const line of out.split("\n")) {
|
|
64
|
-
if (line.includes("server.mjs")) {
|
|
65
|
-
const m = line.match(/(\d+),.*?server\.mjs/);
|
|
66
|
-
if (m) { try { process.kill(parseInt(m[1]), "SIGKILL"); } catch {} }
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
} catch {}
|
|
71
|
-
|
|
72
|
-
log("Starting server...");
|
|
73
|
-
// 用 node 直接 spawn 子进程, 避免 cmd /c 的额外 shell
|
|
74
|
-
const child = spawn(process.execPath, [serverPath], {
|
|
75
|
-
cwd: appDir,
|
|
76
|
-
stdio: "ignore",
|
|
77
|
-
windowsHide: true,
|
|
78
|
-
detached: true,
|
|
79
|
-
shell: false,
|
|
80
|
-
});
|
|
81
|
-
child.unref();
|
|
82
|
-
log(`Server started PID=${child.pid}`);
|
|
83
|
-
return child;
|
|
84
|
-
})();
|
|
85
|
-
|
|
86
|
-
// ─── 等服务器就绪 ─────────────────────────────────
|
|
87
|
-
(async () => {
|
|
88
|
-
let ready = false;
|
|
89
|
-
for (let i = 0; i < 20; i++) {
|
|
90
|
-
if (await isOnline()) { ready = true; break; }
|
|
91
|
-
await new Promise(r => setTimeout(r, 500));
|
|
92
|
-
}
|
|
93
|
-
log(`Server ready=${ready}`);
|
|
94
|
-
|
|
95
|
-
// ─── 启动 PS 托盘 (pipe, 不 detached!) ──────────
|
|
96
|
-
// 关键: 9Router 的做法 - PS 必须与 daemon 同 session
|
|
97
|
-
log("Starting PS tray...");
|
|
98
|
-
const psProcess = spawn("powershell.exe", [
|
|
99
|
-
"-NoProfile",
|
|
100
|
-
"-ExecutionPolicy", "Bypass",
|
|
101
|
-
"-WindowStyle", "Hidden",
|
|
102
|
-
"-File", trayPsPath,
|
|
103
|
-
], {
|
|
104
|
-
stdio: ["pipe", "pipe", "pipe"],
|
|
105
|
-
windowsHide: true,
|
|
106
|
-
shell: false,
|
|
107
|
-
// ⚠ 没有 detached: true — PS 进程保持子进程身份
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
// IPC 读取 PS 事件
|
|
111
|
-
createInterface({ input: psProcess.stdout }).on("line", (line) => {
|
|
112
|
-
try {
|
|
113
|
-
const evt = JSON.parse(line);
|
|
114
|
-
log(`PS event: ${evt.type} idx=${evt.index}`);
|
|
115
|
-
if (evt.type === "click") {
|
|
116
|
-
if (evt.index === 2) { // 退出
|
|
117
|
-
log("Exit from PS menu");
|
|
118
|
-
psSend({ action: "kill" });
|
|
119
|
-
setTimeout(() => process.exit(0), 500);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
} catch (e) { log(`PS parse err: ${e.message}`); }
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
psProcess.on("error", err => log(`PS error: ${err.message}`));
|
|
126
|
-
psProcess.stderr.on("data", d => log(`[ps] ${d.toString().trim()}`));
|
|
127
|
-
psProcess.on("exit", code => {
|
|
128
|
-
log(`PS exited code=${code}`);
|
|
129
|
-
process.exit(0);
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
// ─── 发送菜单配置到 PS ──────────────────────────
|
|
133
|
-
function psSend(cmd) {
|
|
134
|
-
if (psProcess?.stdin?.writable) {
|
|
135
|
-
psProcess.stdin.write(JSON.stringify(cmd) + "\n");
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
psSend({ action: "add-item", index: 0, title: "Pan Router - :50816", enabled: false });
|
|
139
|
-
psSend({ action: "add-item", index: 1, title: "─".repeat(19), enabled: false });
|
|
140
|
-
psSend({ action: "add-item", index: 2, title: "退出", enabled: true });
|
|
141
|
-
psSend({ action: "set-tooltip", text: "Pan Router | 端口 50816" });
|
|
142
|
-
|
|
143
|
-
log("Daemon running — keeping session alive");
|
|
144
|
-
process.stdin.resume(); // keep alive
|
|
145
|
-
})();
|
|
146
|
-
|
|
147
|
-
process.on("SIGTERM", () => { log("SIGTERM"); process.exit(0); });
|
|
148
|
-
process.on("SIGINT", () => { log("SIGINT"); process.exit(0); });
|