panrouter 1.4.2 → 1.5.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/cli.mjs CHANGED
@@ -141,26 +141,29 @@ async function startServer() {
141
141
  // ─── 4. 以托盘模式启动 ──────────────────────────────────────────────────
142
142
 
143
143
  /**
144
- * 启动策略 (参考 9Router Windows 托盘):
144
+ * VBS 隐藏启动 server + PS tray (Windows 原生, 最可靠)
145
145
  *
146
- * cli.mjs ─spawn(detached)──→ node daemon.mjs (无窗口, 常驻)
147
- * ├─ cmd /c start /B → node server.mjs (隐藏)
148
- * └─ spawn(pipe) ───→ powershell tray-daemon.ps1
149
- * └─ NotifyIcon
146
+ * panrouter --tray
147
+ * └─ wscript.exe panrouter-tray.vbs (WScript.Shell.Run, 隐藏)
148
+ * ├─ node server.mjs (run 0 = 完全隐藏)
149
+ * └─ powershell tray-daemon.ps1 (run 0 = 完全隐藏)
150
+ * └─ NotifyIcon ✓
150
151
  *
151
- * 关键: PS 必须用 pipe 连接(不 detached), 保持与 daemon 的 Window Station
152
- * 连接, 否则无法创建通知区图标。
152
+ * WScript.Shell.Run 0 Windows 最可靠的隐藏启动方式,
153
+ * 不依赖 spawn 的 detached/Window Station 行为。
153
154
  */
154
155
  function startTray() {
155
- const daemonPath = path.join(__dirname, "daemon.mjs");
156
- if (!fs.existsSync(daemonPath)) {
157
- log("!!", "未找到 daemon.mjs", "red");
156
+ const vbsPath = path.join(__dirname, "panrouter-tray.vbs");
157
+
158
+ if (!fs.existsSync(vbsPath)) {
159
+ log("!!", "未找到 panrouter-tray.vbs", "red");
158
160
  process.exit(1);
159
161
  }
160
162
 
161
163
  log("..", "正在以托盘模式启动 Pan Router...", "yellow");
162
164
 
163
- const child = spawn(process.execPath, [daemonPath], {
165
+ // wscript //B = 批处理模式 (无窗口, 无交互)
166
+ const child = spawn("wscript.exe", ["//B", "//NoLogo", vbsPath], {
164
167
  stdio: "ignore",
165
168
  windowsHide: true,
166
169
  detached: true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "panrouter",
3
- "version": "1.4.2",
3
+ "version": "1.5.1",
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.vbs"
14
14
  ],
15
15
  "license": "MIT"
16
16
  }
@@ -0,0 +1,18 @@
1
+ ' Pan Router 隐藏启动器
2
+ ' 用 WScript.Shell.Run 的 0 (隐藏) 启动后台进程
3
+ ' Windows 原生, 最可靠的方式
4
+
5
+ Dim WshShell, FSO, ScriptDir
6
+ Set WshShell = CreateObject("WScript.Shell")
7
+ Set FSO = CreateObject("Scripting.FileSystemObject")
8
+
9
+ ScriptDir = FSO.GetParentFolderName(WScript.ScriptFullName)
10
+
11
+ ' 0 = 隐藏窗口, False = 不等待返回
12
+ WshShell.Run "node """ & ScriptDir & "\server.mjs""", 0, False
13
+
14
+ ' 等 3 秒让服务器启动
15
+ WScript.Sleep 3000
16
+
17
+ ' 启动 PS 托盘 (无窗口)
18
+ WshShell.Run "powershell -ExecutionPolicy Bypass -WindowStyle Hidden -STA -File """ & ScriptDir & "\tray-daemon.ps1""", 0, False
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); });