panrouter 1.5.1 → 1.6.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 CHANGED
@@ -141,29 +141,28 @@ async function startServer() {
141
141
  // ─── 4. 以托盘模式启动 ──────────────────────────────────────────────────
142
142
 
143
143
  /**
144
- * VBS 隐藏启动 server + PS tray (Windows 原生, 最可靠)
144
+ * 启动 daemon (Node 常驻进程, 管理 server + PS tray)
145
145
  *
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 ✓
146
+ * cli.mjs ─→ node daemon.mjs (detached, hidden)
147
+ * ├─ 启动 server.mjs (子进程, detached, hidden)
148
+ * └─ 启动 powershell tray-daemon.ps1 (子进程, hidden)
149
+ * └─ 保持存活, 30s 健康检查
151
150
  *
152
- * WScript.Shell.Run 0 Windows 最可靠的隐藏启动方式,
153
- * 不依赖 spawndetached/Window Station 行为。
151
+ * key: process.execPath (绝对路径) 启动
152
+ * 避免 VBS/WScript/CMDPATH 查找问题
154
153
  */
155
154
  function startTray() {
156
- const vbsPath = path.join(__dirname, "panrouter-tray.vbs");
155
+ const daemonPath = path.join(__dirname, "daemon.mjs");
157
156
 
158
- if (!fs.existsSync(vbsPath)) {
159
- log("!!", "未找到 panrouter-tray.vbs", "red");
157
+ if (!fs.existsSync(daemonPath)) {
158
+ log("!!", "未找到 daemon.mjs", "red");
160
159
  process.exit(1);
161
160
  }
162
161
 
163
162
  log("..", "正在以托盘模式启动 Pan Router...", "yellow");
164
163
 
165
- // wscript //B = 批处理模式 (无窗口, 无交互)
166
- const child = spawn("wscript.exe", ["//B", "//NoLogo", vbsPath], {
164
+ const child = spawn(process.execPath, [daemonPath], {
165
+ cwd: __dirname,
167
166
  stdio: "ignore",
168
167
  windowsHide: true,
169
168
  detached: true,
@@ -172,7 +171,7 @@ function startTray() {
172
171
  child.unref();
173
172
 
174
173
  log("OK", "Pan Router 托盘已启动", "green");
175
- console.log(" 图标在任务栏右下角, 右键菜单可退出");
174
+ console.log(" 图标出现在右下角后, 即可运行 claude");
176
175
  }
177
176
 
178
177
  // ─── 主流程 ──────────────────────────────────────────────────────────────
package/daemon.mjs ADDED
@@ -0,0 +1,115 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * Pan Router Daemon (v4)
5
+ *
6
+ * 一个 Node 进程同时:
7
+ * 1. 隐藏启动 server.mjs(子进程)
8
+ * 2. 隐藏启动 tray-daemon.ps1(子进程)
9
+ * 3. 保持存活,守护两者
10
+ *
11
+ * 由 cli.mjs --tray 启动 (detached, 无窗口)。
12
+ */
13
+
14
+ import { spawn, execSync } from "node:child_process";
15
+ import { fileURLToPath } from "node:url";
16
+ import path from "node:path";
17
+ import fs from "node:fs";
18
+ import http from "node:http";
19
+
20
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
21
+ const serverPath = path.join(__dirname, "server.mjs");
22
+ const trayPsPath = path.join(__dirname, "tray-daemon.ps1");
23
+ const nodeExe = process.execPath; // ← 绝对路径!不依赖 PATH
24
+ const logPath = path.join(process.env.TEMP || "/tmp", "panrouter-daemon.log");
25
+
26
+ function log(msg) {
27
+ try { fs.appendFileSync(logPath, `${new Date().toISOString().slice(11,19)} ${msg}\n`); } catch {}
28
+ }
29
+
30
+ log("=== Daemon v4 ===");
31
+ log(`nodeExe=${nodeExe}`);
32
+ log(`serverPath=${serverPath}`);
33
+
34
+ // ─── 1. 杀旧 server ──────────────────────────────
35
+ try {
36
+ const out = execSync(
37
+ 'wmic process where "name=\'node.exe\'" get ProcessId,CommandLine /format:csv 2>nul',
38
+ { encoding: "utf8", windowsHide: true, timeout: 5000 }
39
+ );
40
+ for (const line of out.split("\n")) {
41
+ if (line.includes("server.mjs")) {
42
+ const m = line.match(/(\d+),.*?server\.mjs/);
43
+ if (m) { try { process.kill(parseInt(m[1]), "SIGKILL"); log(`Killed old server PID=${m[1]}`); } catch {} }
44
+ }
45
+ }
46
+ } catch {}
47
+
48
+ // ─── 2. 启动 server.mjs ──────────────────────────
49
+ // 关键: 用 process.execPath (绝对路径), 不用 "node" (可能找不到 PATH)
50
+ const server = spawn(nodeExe, [serverPath], {
51
+ cwd: __dirname,
52
+ stdio: "ignore",
53
+ windowsHide: true,
54
+ detached: true,
55
+ shell: false,
56
+ });
57
+ server.unref();
58
+ log(`Server spawned PID=${server.pid || "??"}`);
59
+
60
+ // ─── 3. 等就绪 ───────────────────────────────────
61
+ function isOnline() {
62
+ return new Promise(rs => {
63
+ try {
64
+ const req = http.get("http://127.0.0.1:50816/health", () => {});
65
+ req.on("response", () => { req.destroy(); rs(true); });
66
+ req.on("error", () => rs(false));
67
+ req.setTimeout(1500, () => { req.destroy(); rs(false); });
68
+ } catch { rs(false); }
69
+ });
70
+ }
71
+
72
+ (async () => {
73
+ let ready = false;
74
+ for (let i = 0; i < 20; i++) {
75
+ if (await isOnline()) { ready = true; break; }
76
+ await new Promise(r => setTimeout(r, 500));
77
+ }
78
+ log(`Server online=${ready}`);
79
+
80
+ // ─── 4. 启动 PS 托盘 ────────────────────────────
81
+ if (fs.existsSync(trayPsPath)) {
82
+ log("Starting PS tray...");
83
+ const ps = spawn("powershell.exe", [
84
+ "-NoProfile", "-ExecutionPolicy", "Bypass",
85
+ "-WindowStyle", "Hidden",
86
+ "-File", trayPsPath,
87
+ ], {
88
+ stdio: "ignore",
89
+ windowsHide: true,
90
+ shell: false,
91
+ });
92
+ ps.unref();
93
+ log(`PS spawned PID=${ps.pid || "??"}`);
94
+ } else {
95
+ log(`WARN: tray-daemon.ps1 not found at ${trayPsPath}`);
96
+ }
97
+
98
+ // ─── 5. 保持存活 ────────────────────────────────
99
+ log("Daemon running, keeping session alive");
100
+ process.stdin.resume();
101
+
102
+ // 定时检查 server, 挂了就重启
103
+ setInterval(() => {
104
+ isOnline().then(ok => {
105
+ if (!ok) {
106
+ log("Server offline, restarting...");
107
+ const s2 = spawn(nodeExe, [serverPath], {
108
+ cwd: __dirname, stdio: "ignore", windowsHide: true, detached: true, shell: false,
109
+ });
110
+ s2.unref();
111
+ log(`Server restarted PID=${s2.pid || "??"}`);
112
+ }
113
+ });
114
+ }, 30000);
115
+ })();
package/package.json CHANGED
@@ -1,16 +1,17 @@
1
1
  {
2
2
  "name": "panrouter",
3
- "version": "1.5.1",
3
+ "version": "1.5.2",
4
4
  "description": "让 Claude Code 免费使用 DeepSeek 等模型,无需 API Key",
5
5
  "type": "module",
6
6
  "bin": {
7
7
  "panrouter": "cli.mjs"
8
8
  },
9
+ "version": "1.6.0",
9
10
  "files": [
10
11
  "cli.mjs",
12
+ "daemon.mjs",
11
13
  "server.mjs",
12
- "tray-daemon.ps1",
13
- "panrouter-tray.vbs"
14
+ "tray-daemon.ps1"
14
15
  ],
15
16
  "license": "MIT"
16
17
  }
package/tray-daemon.ps1 CHANGED
@@ -1,28 +1,29 @@
1
1
  <#
2
2
  .SYNOPSIS
3
- Pan Router 托盘进程 NotifyIcon IPC 包装器
3
+ Pan Router 托盘进程 (仅托盘 + 健康检查, 不启动服务器)
4
4
 
5
- IPC: stdin JSON 命令, stdout JSON 事件
5
+ panrouter-tray.vbs 启动。
6
+ 服务器已经由 VBS 启动, PS 只负责:
7
+ - NotifyIcon (右下角)
8
+ - 右键菜单 (开机自启动 / 退出)
9
+ - 健康检查 + 自动重启
6
10
 
7
- 命令 (stdin):
8
- {"action":"add-item","index":0,"title":"...","enabled":true}
9
- {"action":"update-item","index":0,"title":"...","enabled":true}
10
- {"action":"set-tooltip","text":"..."}
11
- {"action":"kill"}
12
-
13
- 事件 (stdout):
14
- {"type":"started"}
15
- {"type":"click","index":0}
16
- {"type":"error","message":"..."}
11
+ 用法:
12
+ powershell -ExecutionPolicy Bypass -WindowStyle Hidden -STA -File tray-daemon.ps1
17
13
  #>
18
14
 
15
+ $scriptDir = Split-Path $MyInvocation.MyCommand.Path -Parent
16
+ $serverPath = Join-Path $scriptDir "server.mjs"
17
+ $logFile = "$env:TEMP\panrouter-tray.log"
18
+ $autostartName = "PanRouter"
19
+
20
+ function Write-Log($m) { "$(Get-Date -Format 'HH:mm:ss') $m" | Out-File -Append -Encoding utf8 $logFile }
21
+ Write-Log "=== PanRouter Tray ==="
22
+
19
23
  Add-Type -AssemblyName System.Windows.Forms
20
24
  Add-Type -AssemblyName System.Drawing
21
25
 
22
- $ErrorActionPreference = "Stop"
23
- [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
24
-
25
- # ─── 生成图标 (蓝色 P, 纯内存) ────────────────────
26
+ # ─── 生成图标 ──────────────────────────────────────
26
27
  $bmp = New-Object System.Drawing.Bitmap(16, 16)
27
28
  $g = [System.Drawing.Graphics]::FromImage($bmp)
28
29
  $g.SmoothingMode = 'HighQuality'
@@ -37,63 +38,139 @@ $hIcon = $bmp.GetHicon()
37
38
  $icon = [System.Drawing.Icon]::FromHandle($hIcon)
38
39
  $bmp.Dispose()
39
40
 
40
- # ─── NotifyIcon ──────────────────────────────────
41
+ # ─── NotifyIcon ────────────────────────────────────
41
42
  $notifyIcon = New-Object System.Windows.Forms.NotifyIcon
42
43
  $notifyIcon.Icon = $icon
43
44
  $notifyIcon.Text = "Pan Router | 端口 50816"
44
45
  $notifyIcon.Visible = $true
46
+ Write-Log "NotifyIcon visible"
45
47
 
46
- $menu = New-Object System.Windows.Forms.ContextMenuStrip
47
- $notifyIcon.ContextMenuStrip = $menu
48
- $items = @()
48
+ # ─── 健康检查 ──────────────────────────────────────
49
+ function Test-Online {
50
+ try {
51
+ $req = [System.Net.WebRequest]::Create("http://127.0.0.1:50816/health")
52
+ $req.Timeout = 1500
53
+ $resp = $req.GetResponse()
54
+ $resp.Close()
55
+ return $true
56
+ } catch { return $false }
57
+ }
49
58
 
50
- function Write-Event($obj) {
59
+ function Start-Server {
51
60
  try {
52
- [Console]::Out.WriteLine(($obj | ConvertTo-Json -Compress))
53
- [Console]::Out.Flush()
61
+ # 杀旧的 server.mjs 进程
62
+ $old = wmic process where "name='node.exe'" get ProcessId,CommandLine /format:csv 2>$null
63
+ foreach ($line in $old) {
64
+ if ($line -match '(\d+),.*?server\.mjs') {
65
+ Stop-Process -Id $Matches[1] -Force -ErrorAction SilentlyContinue
66
+ Write-Log "Killed old server PID=$($Matches[1])"
67
+ }
68
+ }
54
69
  } catch {}
70
+ try {
71
+ $p = Start-Process -FilePath "node" -ArgumentList "`"$serverPath`"" -WorkingDirectory $scriptDir -WindowStyle Hidden -PassThru -NoNewWindow
72
+ Write-Log "Server started PID=$($p.Id)"
73
+ } catch { Write-Log "Server start FAILED: $_" }
55
74
  }
56
75
 
57
- function Add-MenuItem($index, $title, $enabled) {
58
- $item = New-Object System.Windows.Forms.ToolStripMenuItem
59
- $item.Text = $title
60
- $item.Enabled = $enabled
61
- $idx = $index
62
- $item.Add_Click({ Write-Event @{type="click"; index=$idx} }.GetNewClosure())
63
- $menu.Items.Add($item) | Out-Null
64
- $items += $item
76
+ # ─── 开机自启动 ────────────────────────────────────
77
+ function Get-Autostart {
78
+ try { $val = Get-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run" -Name $autostartName -ErrorAction Stop; return $val.$autostartName -ne $null }
79
+ catch { return $false }
80
+ }
81
+ function Set-Autostart($enable) {
82
+ try {
83
+ if ($enable) {
84
+ reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Run" /v $autostartName /t REG_SZ /d "powershell -ExecutionPolicy Bypass -WindowStyle Hidden -STA -File `"$($MyInvocation.MyCommand.Path)`"" /f 2>&1 | Out-Null
85
+ Write-Log "Autostart ON"
86
+ } else {
87
+ reg delete "HKCU\Software\Microsoft\Windows\CurrentVersion\Run" /v $autostartName /f 2>&1 | Out-Null
88
+ Write-Log "Autostart OFF"
89
+ }
90
+ } catch { Write-Log "Autostart error: $_" }
65
91
  }
66
92
 
67
- function Set-Tooltip($text) {
68
- if ($text.Length -gt 63) { $text = $text.Substring(0, 63) }
69
- $notifyIcon.Text = $text
93
+ # ═══════════ 主流程 ═══════════
94
+
95
+ # 等待服务器就绪 (VBS 已启动 server, 但可能还没完全起来)
96
+ $ready = $false
97
+ for ($i = 0; $i -lt 20; $i++) {
98
+ Start-Sleep -Milliseconds 500
99
+ if (Test-Online) { $ready = $true; break }
100
+ }
101
+ Write-Log "Server online=$ready"
102
+ if ($ready) {
103
+ $notifyIcon.ShowBalloonTip(3000, "Pan Router", "服务器已就绪 ✓ (端口 50816)", [System.Windows.Forms.ToolTipIcon]::Info)
104
+ } else {
105
+ Write-Log "Server not online, starting ourselves..."
106
+ Start-Server
107
+ for ($i = 0; $i -lt 20; $i++) {
108
+ Start-Sleep -Milliseconds 500
109
+ if (Test-Online) { $ready = $true; break }
110
+ }
111
+ if ($ready) {
112
+ $notifyIcon.ShowBalloonTip(3000, "Pan Router", "服务器已就绪 ✓", [System.Windows.Forms.ToolTipIcon]::Info)
113
+ } else {
114
+ $notifyIcon.ShowBalloonTip(3000, "Pan Router", "服务器启动失败 ⚠", [System.Windows.Forms.ToolTipIcon]::Error)
115
+ }
70
116
  }
71
117
 
72
- # ─── stdin 轮询 (9Router 做法: 100ms Timer) ──────
73
- $timer = New-Object System.Windows.Forms.Timer
74
- $timer.Interval = 100
75
- $timer.Add_Tick({
76
- try {
77
- while ([Console]::In.Peek() -ne -1) {
78
- $line = [Console]::In.ReadLine()
79
- if ([string]::IsNullOrWhiteSpace($line)) { continue }
80
- $cmd = $line | ConvertFrom-Json
81
- switch ($cmd.action) {
82
- "add-item" { Add-MenuItem $cmd.index $cmd.title $cmd.enabled }
83
- "update-item" { }
84
- "set-tooltip" { Set-Tooltip $cmd.text }
85
- "kill" {
86
- $notifyIcon.Visible = $false
87
- $notifyIcon.Dispose()
88
- $icon.Dispose()
89
- [System.Runtime.InteropServices.Marshal]::DestroyIcon($hIcon)
90
- [System.Windows.Forms.Application]::Exit()
91
- }
92
- }
93
- }
94
- } catch { Write-Event @{type="error"; message=$_.Exception.Message} }
118
+ # 右键菜单
119
+ $menu = New-Object System.Windows.Forms.ContextMenuStrip
120
+ $titleItem = New-Object System.Windows.Forms.ToolStripMenuItem("Pan Router - :50816")
121
+ $titleItem.Enabled = $false
122
+ $titleItem.Font = New-Object System.Drawing.Font("Segoe UI", 9, [System.Drawing.FontStyle]::Bold)
123
+ $menu.Items.Add($titleItem)
124
+ $menu.Items.Add("-")
125
+
126
+ $autoItem = New-Object System.Windows.Forms.ToolStripMenuItem("开机自启动")
127
+ $autoItem.Checked = Get-Autostart
128
+ $autoItem.Add_Click({
129
+ if ($autoItem.Checked) { Set-Autostart $false; $autoItem.Checked = $false; $notifyIcon.ShowBalloonTip(2000, "Pan Router", "开机自启动已关闭", [System.Windows.Forms.ToolTipIcon]::Info) }
130
+ else { Set-Autostart $true; $autoItem.Checked = $true; $notifyIcon.ShowBalloonTip(2000, "Pan Router", "开机自启动已开启 ✓", [System.Windows.Forms.ToolTipIcon]::Info) }
95
131
  })
96
- $timer.Start()
132
+ $menu.Items.Add($autoItem)
133
+ $menu.Items.Add("-")
134
+
135
+ $exitItem = New-Object System.Windows.Forms.ToolStripMenuItem("退出")
136
+ $exitItem.Add_Click({ Write-Log "Exit"; $notifyIcon.Visible = $false; [System.Windows.Forms.Application]::Exit() })
137
+ $menu.Items.Add($exitItem)
138
+ $notifyIcon.ContextMenuStrip = $menu
139
+
140
+ # 左键: 状态
141
+ $notifyIcon.Add_MouseClick({
142
+ if ($_.Button -eq [System.Windows.Forms.MouseButtons]::Left) {
143
+ if (Test-Online) { $notifyIcon.ShowBalloonTip(2000, "Pan Router", "运行正常 ✓ (端口 50816)", [System.Windows.Forms.ToolTipIcon]::Info) }
144
+ else { $notifyIcon.ShowBalloonTip(2000, "Pan Router", "服务器未响应 ⚠", [System.Windows.Forms.ToolTipIcon]::Error) }
145
+ }
146
+ })
147
+
148
+ # 健康检查 (30秒)
149
+ $healthTimer = New-Object System.Windows.Forms.Timer
150
+ $healthTimer.Interval = 30000
151
+ $healthTimer.Add_Tick({
152
+ if (-not (Test-Online)) {
153
+ Write-Log "Health FAILED, restarting..."
154
+ Start-Server
155
+ Start-Sleep -Seconds 3
156
+ if (Test-Online) { $notifyIcon.ShowBalloonTip(3000, "Pan Router", "服务器已自动重启", [System.Windows.Forms.ToolTipIcon]::Info) }
157
+ }
158
+ })
159
+ $healthTimer.Start()
160
+
161
+ # 退出清理
162
+ [System.Windows.Forms.Application]::ApplicationExit += {
163
+ Write-Log "Cleanup"
164
+ $healthTimer.Stop()
165
+ try {
166
+ $old = wmic process where "name='node.exe'" get ProcessId,CommandLine /format:csv 2>$null
167
+ foreach ($line in $old) { if ($line -match '(\d+),.*?server\.mjs') { Stop-Process -Id $Matches[1] -Force -ErrorAction SilentlyContinue } }
168
+ } catch {}
169
+ $notifyIcon.Dispose()
170
+ [System.Runtime.InteropServices.Marshal]::DestroyIcon($hIcon)
171
+ $icon.Dispose()
172
+ }
97
173
 
98
- Write-Event @{type="started"}
174
+ Write-Log "Message loop"
99
175
  [System.Windows.Forms.Application]::Run()
176
+ Write-Log "Exited"
@@ -1,18 +0,0 @@
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