panrouter 1.5.0 → 1.5.2

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,28 +141,29 @@ async function startServer() {
141
141
  // ─── 4. 以托盘模式启动 ──────────────────────────────────────────────────
142
142
 
143
143
  /**
144
- * 启动策略 (Windows BAT 原生方式):
144
+ * VBS 隐藏启动 server + PS tray (Windows 原生, 最可靠)
145
145
  *
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
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
- * cmd /c start /B 是 Windows 最原生的后台/隐藏启动方式,
152
- * 不依赖 detached vs non-detachedWindow Station 差异。
152
+ * WScript.Shell.Run 0 是 Windows 最可靠的隐藏启动方式,
153
+ * 不依赖 spawn detached/Window Station 行为。
153
154
  */
154
155
  function startTray() {
155
- const batPath = path.join(__dirname, "panrouter-tray.bat");
156
+ const vbsPath = path.join(__dirname, "panrouter-tray.vbs");
156
157
 
157
- if (!fs.existsSync(batPath)) {
158
- log("!!", "未找到 panrouter-tray.bat", "red");
158
+ if (!fs.existsSync(vbsPath)) {
159
+ log("!!", "未找到 panrouter-tray.vbs", "red");
159
160
  process.exit(1);
160
161
  }
161
162
 
162
163
  log("..", "正在以托盘模式启动 Pan Router...", "yellow");
163
164
 
164
- const child = spawn("cmd.exe", ["/c", batPath], {
165
- cwd: __dirname,
165
+ // wscript //B = 批处理模式 (无窗口, 无交互)
166
+ const child = spawn("wscript.exe", ["//B", "//NoLogo", vbsPath], {
166
167
  stdio: "ignore",
167
168
  windowsHide: true,
168
169
  detached: true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "panrouter",
3
- "version": "1.5.0",
3
+ "version": "1.5.2",
4
4
  "description": "让 Claude Code 免费使用 DeepSeek 等模型,无需 API Key",
5
5
  "type": "module",
6
6
  "bin": {
@@ -10,7 +10,7 @@
10
10
  "cli.mjs",
11
11
  "server.mjs",
12
12
  "tray-daemon.ps1",
13
- "panrouter-tray.bat"
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/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,26 +0,0 @@
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 立即结束 (不影响后台进程)