panrouter 1.5.1 → 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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/tray-daemon.ps1 +136 -59
package/package.json CHANGED
@@ -1,6 +1,6 @@
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": {
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"