panrouter 3.0.0 → 3.2.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.
Files changed (3) hide show
  1. package/cli.mjs +36 -10
  2. package/package.json +1 -1
  3. package/tray-daemon.ps1 +120 -85
package/cli.mjs CHANGED
@@ -87,10 +87,43 @@ async function startServer() {
87
87
  }
88
88
 
89
89
  async function startTray() {
90
+ const serverPath = path.join(__dirname, "server.mjs");
90
91
  const psPath = path.join(__dirname, "tray-daemon.ps1");
91
- log("..", "正在后台启动代理与托盘...", "yellow");
92
+ log("..", "正在后台启动代理...", "yellow");
93
+
94
+ // 1. 由主程序直接启动 Node 代理,彻底杜绝 PowerShell 启动慢导致的超时
95
+ try {
96
+ if (process.platform === "win32") {
97
+ execSync('taskkill /f /fi "WINDOWTITLE eq Pan Router*" >nul 2>&1', { stdio: "pipe" });
98
+ }
99
+ } catch {}
100
+
101
+ const srv = spawn(process.execPath, [serverPath], {
102
+ cwd: __dirname,
103
+ stdio: "ignore",
104
+ windowsHide: true,
105
+ detached: true
106
+ });
107
+ srv.unref();
108
+
109
+ // 2. 验证端口(由于是直接用 Node 启动,这步通常 1 秒内就会通过)
110
+ let ok = false;
111
+ for (let i = 0; i < 15; i++) {
112
+ if (await isPortOpen()) {
113
+ ok = true;
114
+ break;
115
+ }
116
+ await new Promise(rs => setTimeout(rs, 1000));
117
+ }
92
118
 
93
- // 【核心改变】:彻底抛弃 VBS,利用 Node 原生的 windowsHide 属性无黑框直接启动 PowerShell
119
+ if (ok) {
120
+ log("OK", "代理服务已就绪!(端口 50816)", "green");
121
+ } else {
122
+ log("!!", "服务启动超时,但仍将尝试加载托盘", "red");
123
+ }
124
+
125
+ // 3. 独立拉起系统托盘
126
+ log("..", "正在加载系统托盘...", "yellow");
94
127
  const tray = spawn("powershell.exe", [
95
128
  "-NoProfile",
96
129
  "-ExecutionPolicy", "Bypass",
@@ -105,14 +138,7 @@ async function startTray() {
105
138
  });
106
139
  tray.unref();
107
140
 
108
- for (let i = 0; i < 15; i++) {
109
- if (await isPortOpen()) {
110
- log("OK", "后台服务及托盘已启动!(端口 50816)", "green");
111
- return;
112
- }
113
- await new Promise(rs => setTimeout(rs, 1000));
114
- }
115
- log("!!", "服务启动超时", "red");
141
+ console.log(" API 代理已立即可用。托盘图标稍后将在右下角显示。");
116
142
  }
117
143
 
118
144
  async function main() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "panrouter",
3
- "version": "3.0.0",
3
+ "version": "3.2.0",
4
4
  "description": "让 Claude Code 免费使用 DeepSeek 等模型,无需 API Key",
5
5
  "type": "module",
6
6
  "bin": {
package/tray-daemon.ps1 CHANGED
@@ -1,97 +1,132 @@
1
1
  <#
2
2
  .SYNOPSIS
3
- Pan Router 托盘守护脚本 (极简直连版)
3
+ Pan Router 托盘守护脚本 (终极原生挂载版 + 日志诊断)
4
4
  #>
5
- $ErrorActionPreference = "SilentlyContinue"
5
+ $ErrorActionPreference = "Stop" # 遇到错误直接抛出,由 try-catch 捕获写日志
6
6
 
7
- Add-Type -AssemblyName System.Windows.Forms
8
- Add-Type -AssemblyName System.Drawing
9
- [System.Windows.Forms.Application]::EnableVisualStyles()
7
+ $logFile = "$env:TEMP\panrouter_tray.log"
8
+ "--- $(Get-Date -Format 'HH:mm:ss') 托盘脚本启动 ---" | Out-File $logFile
10
9
 
11
- $scriptDir = Split-Path $MyInvocation.MyCommand.Path -Parent
12
- $serverPath = Join-Path $scriptDir "server.mjs"
13
-
14
- # 拿到 Node 路径
15
- $nodePath = $env:PANROUTER_NODE
16
- if ([string]::IsNullOrWhiteSpace($nodePath)) { $nodePath = "node" }
17
-
18
- # ─── 管理后台代理 ──────────────────────────────────────────
19
- function Start-Backend {
20
- # 杀掉旧代理
21
- Get-WmiObject Win32_Process -Filter "Name = 'node.exe'" |
22
- Where-Object CommandLine -match "server\.mjs" |
23
- ForEach-Object { Stop-Process -Id $_.ProcessId -Force }
24
-
25
- # 启动新代理
26
- Start-Process -FilePath $nodePath -ArgumentList "`"$serverPath`"" -WorkingDirectory $scriptDir -WindowStyle Hidden
27
- }
28
-
29
- Start-Backend
30
-
31
- # ─── 初始化托盘 ────────────────────────────────────────────
32
- $notifyIcon = New-Object System.Windows.Forms.NotifyIcon
33
- $notifyIcon.Text = "Pan Router (:50816)"
34
-
35
- # 绘图报错是导致图标出不来的元凶之一,这里做强兜底
36
10
  try {
37
- $bmp = New-Object System.Drawing.Bitmap(16, 16)
38
- $g = [System.Drawing.Graphics]::FromImage($bmp)
39
- $g.Clear([System.Drawing.Color]::FromArgb(0, 120, 215))
40
- $font = New-Object System.Drawing.Font("Arial", 8, [System.Drawing.FontStyle]::Bold)
41
- $g.DrawString("P", $font, [System.Drawing.Brushes]::White, 2, 1)
42
- $notifyIcon.Icon = [System.Drawing.Icon]::FromHandle($bmp.GetHicon())
43
- } catch {
44
- # 终极兜底:如果上面的画图代码因为 GDI 环境被拦,直接用系统自带的安全盾牌图标
45
- $notifyIcon.Icon = [System.Drawing.SystemIcons]::Shield
46
- }
47
-
48
- # ─── 菜单配置 ──────────────────────────────────────────────
49
- $menu = New-Object System.Windows.Forms.ContextMenuStrip
50
-
51
- $titleItem = $menu.Items.Add("Pan Router | :50816")
52
- $titleItem.Enabled = $false
53
- $menu.Items.Add("-") | Out-Null
54
-
55
- $restartItem = $menu.Items.Add("重启服务")
56
- $restartItem.Add_Click({
57
- Start-Backend
58
- $notifyIcon.ShowBalloonTip(2000, "Pan Router", "服务已重启", [System.Windows.Forms.ToolTipIcon]::Info)
59
- })
60
-
61
- $autoItem = $menu.Items.Add("开机自启动")
62
- try { $autoItem.Checked = (Get-ItemProperty "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run" -Name PanRouter) -ne $null } catch {}
63
- $autoItem.Add_Click({
64
- if ($autoItem.Checked) {
65
- reg delete "HKCU\Software\Microsoft\Windows\CurrentVersion\Run" /v PanRouter /f 2>&1 | Out-Null
66
- $autoItem.Checked = $false
67
- } else {
68
- # 开机自启也摒弃 VBS,直接用隐藏模式的 PowerShell
69
- $cmd = "powershell.exe -WindowStyle Hidden -ExecutionPolicy Bypass -File `"$($MyInvocation.MyCommand.Path)`""
70
- reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Run" /v PanRouter /t REG_SZ /d $cmd /f 2>&1 | Out-Null
71
- $autoItem.Checked = $true
11
+ Add-Type -AssemblyName System.Windows.Forms
12
+ Add-Type -AssemblyName System.Drawing
13
+ [System.Windows.Forms.Application]::EnableVisualStyles()
14
+ "1. .NET 库加载成功" | Out-File $logFile -Append
15
+
16
+ $scriptDir = Split-Path $MyInvocation.MyCommand.Path -Parent
17
+ $serverPath = Join-Path $scriptDir "server.mjs"
18
+
19
+ # 拿到 Node 路径
20
+ $nodePath = $env:PANROUTER_NODE
21
+ if ([string]::IsNullOrWhiteSpace($nodePath)) { $nodePath = "node" }
22
+ "2. Node 路径: $nodePath" | Out-File $logFile -Append
23
+
24
+ # ─── 管理后台代理 ──────────────────────────────────────────
25
+ function Start-Backend {
26
+ "-> 执行重启后台服务逻辑" | Out-File $logFile -Append
27
+ Get-WmiObject Win32_Process -Filter "Name = 'node.exe'" |
28
+ Where-Object CommandLine -match "server\.mjs" |
29
+ ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue }
30
+
31
+ Start-Process -FilePath $nodePath -ArgumentList "`"$serverPath`"" -WorkingDirectory $scriptDir -WindowStyle Hidden
72
32
  }
73
- })
74
-
75
- $menu.Items.Add("-") | Out-Null
76
-
77
- $exitItem = $menu.Items.Add("退出")
78
- $exitItem.Add_Click({
79
- $notifyIcon.Visible = $false
80
- Get-WmiObject Win32_Process -Filter "Name = 'node.exe'" | Where-Object CommandLine -match "server\.mjs" | ForEach-Object { Stop-Process -Id $_.ProcessId -Force }
81
- [System.Windows.Forms.Application]::Exit()
82
- })
83
33
 
84
- $notifyIcon.ContextMenuStrip = $menu
34
+ # 检查端口
35
+ $portOpen = $false
36
+ try {
37
+ $tcp = New-Object System.Net.Sockets.TcpClient
38
+ $ar = $tcp.BeginConnect("127.0.0.1", 50816, $null, $null)
39
+ $portOpen = $ar.AsyncWaitHandle.WaitOne(500, $false)
40
+ $tcp.Close()
41
+ "3. 端口探测完成,已开放状态: $portOpen" | Out-File $logFile -Append
42
+ } catch {
43
+ "3. 端口探测报错 (忽略并继续): $($_.Exception.Message)" | Out-File $logFile -Append
44
+ }
85
45
 
86
- # 左键点击直接弹出原生菜单(去除了那些花里胡哨的反射代码)
87
- $notifyIcon.Add_MouseClick({
88
- if ($_.Button -eq [System.Windows.Forms.MouseButtons]::Left) {
89
- $notifyIcon.ContextMenuStrip.Show([System.Windows.Forms.Cursor]::Position)
46
+ if (-not $portOpen) {
47
+ Start-Backend
90
48
  }
91
- })
92
49
 
93
- $notifyIcon.Visible = $true
50
+ # ─── 初始化托盘 ────────────────────────────────────────────
51
+ $notifyIcon = New-Object System.Windows.Forms.NotifyIcon
52
+ $notifyIcon.Text = "Pan Router (:50816)"
53
+
54
+ # 图标兜底处理
55
+ try {
56
+ $bmp = New-Object System.Drawing.Bitmap(16, 16)
57
+ $g = [System.Drawing.Graphics]::FromImage($bmp)
58
+ $g.Clear([System.Drawing.Color]::FromArgb(0, 120, 215))
59
+ $font = New-Object System.Drawing.Font("Arial", 8, [System.Drawing.FontStyle]::Bold)
60
+ $g.DrawString("P", $font, [System.Drawing.Brushes]::White, 2, 1)
61
+ $notifyIcon.Icon = [System.Drawing.Icon]::FromHandle($bmp.GetHicon())
62
+ "4. 自定义图标绘制成功" | Out-File $logFile -Append
63
+ } catch {
64
+ "4. 画图异常,改用原生盾牌图标: $($_.Exception.Message)" | Out-File $logFile -Append
65
+ $notifyIcon.Icon = [System.Drawing.SystemIcons]::Shield
66
+ }
94
67
 
95
- # 保持进程存活的最纯粹方式
96
- $ctx = New-Object System.Windows.Forms.ApplicationContext
97
- [System.Windows.Forms.Application]::Run($ctx)
68
+ # ─── 原生菜单配置 (ContextMenu) ────────────────────────────
69
+ # 【核心修复】:使用原生的 ContextMenu 代替极易失去焦点的 ContextMenuStrip
70
+ $menu = New-Object System.Windows.Forms.ContextMenu
71
+
72
+ $titleItem = New-Object System.Windows.Forms.MenuItem("Pan Router | :50816")
73
+ $titleItem.Enabled = $false
74
+ $menu.MenuItems.Add($titleItem) | Out-Null
75
+
76
+ $menu.MenuItems.Add("-") | Out-Null
77
+
78
+ $restartItem = New-Object System.Windows.Forms.MenuItem("重启服务")
79
+ $restartItem.Add_Click({
80
+ Start-Backend
81
+ $notifyIcon.ShowBalloonTip(2000, "Pan Router", "服务已重启", [System.Windows.Forms.ToolTipIcon]::Info)
82
+ })
83
+ $menu.MenuItems.Add($restartItem) | Out-Null
84
+
85
+ $autoItem = New-Object System.Windows.Forms.MenuItem("开机自启动")
86
+ try { $autoItem.Checked = (Get-ItemProperty "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run" -Name PanRouter -ErrorAction SilentlyContinue) -ne $null } catch {}
87
+ $autoItem.Add_Click({
88
+ if ($autoItem.Checked) {
89
+ reg delete "HKCU\Software\Microsoft\Windows\CurrentVersion\Run" /v PanRouter /f 2>&1 | Out-Null
90
+ $autoItem.Checked = $false
91
+ } else {
92
+ $cmd = "powershell.exe -WindowStyle Hidden -ExecutionPolicy Bypass -File `"$($MyInvocation.MyCommand.Path)`""
93
+ reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Run" /v PanRouter /t REG_SZ /d $cmd /f 2>&1 | Out-Null
94
+ $autoItem.Checked = $true
95
+ }
96
+ })
97
+ $menu.MenuItems.Add($autoItem) | Out-Null
98
+
99
+ $menu.MenuItems.Add("-") | Out-Null
100
+
101
+ $exitItem = New-Object System.Windows.Forms.MenuItem("退出")
102
+ $exitItem.Add_Click({
103
+ $notifyIcon.Visible = $false
104
+ "-> 执行退出清理" | Out-File $logFile -Append
105
+ Get-WmiObject Win32_Process -Filter "Name = 'node.exe'" | Where-Object CommandLine -match "server\.mjs" | ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue }
106
+ [System.Windows.Forms.Application]::Exit()
107
+ })
108
+ $menu.MenuItems.Add($exitItem) | Out-Null
109
+
110
+ $notifyIcon.ContextMenu = $menu
111
+ $notifyIcon.Visible = $true
112
+ "5. 托盘图标已绑定并设置为可见" | Out-File $logFile -Append
113
+
114
+ # ─── 隐形实体主窗口 ─────────────────────────────────────────
115
+ # 【核心修复】:挂载真实的 Form 窗口,防止 Windows 将该 PowerShell UI 线程当做垃圾回收掉
116
+ $form = New-Object System.Windows.Forms.Form
117
+ $form.ShowInTaskbar = $false
118
+ $form.WindowState = [System.Windows.Forms.FormWindowState]::Minimized
119
+ $form.Opacity = 0
120
+ $form.Add_Load({
121
+ $form.Hide()
122
+ "6. 实体窗口初始化完毕,正式进入消息挂起循环..." | Out-File $logFile -Append
123
+ })
124
+
125
+ [System.Windows.Forms.Application]::Run($form)
126
+
127
+ "7. 程序正常退出" | Out-File $logFile -Append
128
+ } catch {
129
+ "【致命报错】无法完成执行:" | Out-File $logFile -Append
130
+ $_.Exception.Message | Out-File $logFile -Append
131
+ $_.Exception.StackTrace | Out-File $logFile -Append
132
+ }