panrouter 1.1.2 → 1.3.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 +20 -13
- package/package.json +3 -3
- package/tray-daemon.ps1 +251 -0
- package/tray-launcher.vbs +14 -0
- package/tray-app.cs +0 -324
- package/tray-manager.ps1 +0 -198
package/cli.mjs
CHANGED
|
@@ -138,30 +138,37 @@ async function startServer() {
|
|
|
138
138
|
console.log("\n 现在可以运行: \x1b[33mclaude \"你好\"\x1b[0m\n");
|
|
139
139
|
}
|
|
140
140
|
|
|
141
|
-
// ─── 4.
|
|
141
|
+
// ─── 4. 以托盘模式启动(VBS 启动器 + 自包含 PS 托盘) ─────────────────────
|
|
142
142
|
|
|
143
|
+
/**
|
|
144
|
+
* 启动策略 (参考 9Router):
|
|
145
|
+
* 1. tray-launcher.vbs 启动 tray-daemon.ps1 (WshShell.Run, 完全隐藏)
|
|
146
|
+
* 2. tray-daemon.ps1 自包含:
|
|
147
|
+
* - 隐藏启动 server.mjs
|
|
148
|
+
* - NotifyIcon (右下角)
|
|
149
|
+
* - 右键菜单 (开机自启动 / 退出)
|
|
150
|
+
* - 30秒健康检查自动重启
|
|
151
|
+
* 3. cli.mjs 进程立刻退出
|
|
152
|
+
*/
|
|
143
153
|
function startTray() {
|
|
144
|
-
const
|
|
145
|
-
const trayScript = path.join(__dirname, "tray-manager.ps1");
|
|
154
|
+
const launcherPath = path.join(__dirname, "tray-launcher.vbs");
|
|
146
155
|
|
|
147
|
-
if (!fs.existsSync(
|
|
148
|
-
log("!!", "未找到 tray-
|
|
156
|
+
if (!fs.existsSync(launcherPath)) {
|
|
157
|
+
log("!!", "未找到 tray-launcher.vbs", "red");
|
|
149
158
|
process.exit(1);
|
|
150
159
|
}
|
|
151
160
|
|
|
152
161
|
log("..", "正在以托盘模式启动 Pan Router...", "yellow");
|
|
153
162
|
|
|
154
|
-
//
|
|
155
|
-
const child = spawn("
|
|
156
|
-
"
|
|
157
|
-
|
|
158
|
-
"-STA",
|
|
159
|
-
"-File", trayScript,
|
|
160
|
-
"-ServerPath", serverPath,
|
|
163
|
+
// wscript.exe //B = 无窗口, 自动退出
|
|
164
|
+
const child = spawn("wscript.exe", [
|
|
165
|
+
"//B", "//NoLogo",
|
|
166
|
+
launcherPath,
|
|
161
167
|
], {
|
|
162
|
-
cwd: __dirname,
|
|
163
168
|
stdio: "ignore",
|
|
164
169
|
detached: true,
|
|
170
|
+
windowsHide: true,
|
|
171
|
+
shell: false,
|
|
165
172
|
});
|
|
166
173
|
child.unref();
|
|
167
174
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "panrouter",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "让 Claude Code 免费使用 DeepSeek 等模型,无需 API Key",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
"files": [
|
|
10
10
|
"cli.mjs",
|
|
11
11
|
"server.mjs",
|
|
12
|
-
"tray-
|
|
13
|
-
"tray-
|
|
12
|
+
"tray-daemon.ps1",
|
|
13
|
+
"tray-launcher.vbs"
|
|
14
14
|
],
|
|
15
15
|
"license": "MIT"
|
|
16
16
|
}
|
package/tray-daemon.ps1
ADDED
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
<#
|
|
2
|
+
.SYNOPSIS
|
|
3
|
+
Pan Router Tray Daemon — 完全自包含的托盘进程
|
|
4
|
+
|
|
5
|
+
功能:
|
|
6
|
+
- 隐藏启动 server.mjs (node)
|
|
7
|
+
- 右下角 NotifyIcon
|
|
8
|
+
- 右键菜单: 开机自启动 / 退出
|
|
9
|
+
- 30秒健康检查, 自动重启
|
|
10
|
+
- 日志到 %TEMP%\panrouter-tray.log
|
|
11
|
+
|
|
12
|
+
用法 (由 tray-launcher.vbs 调用):
|
|
13
|
+
powershell -ExecutionPolicy Bypass -WindowStyle Hidden -STA -File tray-daemon.ps1
|
|
14
|
+
|
|
15
|
+
也可直接测试:
|
|
16
|
+
powershell -ExecutionPolicy Bypass -STA -File tray-daemon.ps1
|
|
17
|
+
#>
|
|
18
|
+
|
|
19
|
+
$logFile = "$env:TEMP\panrouter-tray.log"
|
|
20
|
+
function Write-Log($m) { "$(Get-Date -Format 'HH:mm:ss') $m" | Out-File -Append -Encoding utf8 $logFile }
|
|
21
|
+
|
|
22
|
+
Write-Log "=== PanRouter Tray Daemon ==="
|
|
23
|
+
|
|
24
|
+
# ─── 路径 (与脚本同目录) ────────────────────────
|
|
25
|
+
$scriptDir = Split-Path $MyInvocation.MyCommand.Path -Parent
|
|
26
|
+
$serverPath = Join-Path $scriptDir "server.mjs"
|
|
27
|
+
|
|
28
|
+
# ─── 加载 WinForms ─────────────────────────────
|
|
29
|
+
Add-Type -AssemblyName System.Windows.Forms
|
|
30
|
+
Add-Type -AssemblyName System.Drawing
|
|
31
|
+
Write-Log "Assemblies loaded"
|
|
32
|
+
|
|
33
|
+
# ═══════════════════════════════════════════════════════════
|
|
34
|
+
# 1. 生成托盘图标 (蓝色底 P)
|
|
35
|
+
# ═══════════════════════════════════════════════════════════
|
|
36
|
+
$bmp = New-Object System.Drawing.Bitmap(16, 16)
|
|
37
|
+
$g = [System.Drawing.Graphics]::FromImage($bmp)
|
|
38
|
+
$g.SmoothingMode = 'HighQuality'
|
|
39
|
+
$g.Clear([System.Drawing.Color]::Transparent)
|
|
40
|
+
$brush = New-Object System.Drawing.SolidBrush([System.Drawing.Color]::FromArgb(0, 120, 215))
|
|
41
|
+
$g.FillEllipse($brush, 0, 0, 15, 15)
|
|
42
|
+
$font = New-Object System.Drawing.Font("Segoe UI", 8.5, [System.Drawing.FontStyle]::Bold)
|
|
43
|
+
$fg = New-Object System.Drawing.SolidBrush([System.Drawing.Color]::White)
|
|
44
|
+
$g.DrawString("P", $font, $fg, 3, 1.5)
|
|
45
|
+
$font.Dispose(); $fg.Dispose(); $brush.Dispose(); $g.Dispose()
|
|
46
|
+
$hIcon = $bmp.GetHicon()
|
|
47
|
+
$icon = [System.Drawing.Icon]::FromHandle($hIcon)
|
|
48
|
+
$bmp.Dispose()
|
|
49
|
+
Write-Log "Icon created"
|
|
50
|
+
|
|
51
|
+
# ═══════════════════════════════════════════════════════════
|
|
52
|
+
# 2. NotifyIcon
|
|
53
|
+
# ═══════════════════════════════════════════════════════════
|
|
54
|
+
$notifyIcon = New-Object System.Windows.Forms.NotifyIcon
|
|
55
|
+
$notifyIcon.Icon = $icon
|
|
56
|
+
$notifyIcon.Text = "Pan Router | 端口 50816"
|
|
57
|
+
$notifyIcon.Visible = $true
|
|
58
|
+
Write-Log "NotifyIcon visible"
|
|
59
|
+
|
|
60
|
+
# ═══════════════════════════════════════════════════════════
|
|
61
|
+
# 3. 隐藏启动 server.mjs
|
|
62
|
+
# ═══════════════════════════════════════════════════════════
|
|
63
|
+
function Start-Server {
|
|
64
|
+
# 杀掉旧 server
|
|
65
|
+
try {
|
|
66
|
+
$old = wmic process where "name='node.exe'" get ProcessId,CommandLine /format:csv 2>$null
|
|
67
|
+
foreach ($line in $old) {
|
|
68
|
+
if ($line -match '(\d+),.*?server\.mjs') {
|
|
69
|
+
$pid = $Matches[1]
|
|
70
|
+
Write-Log "Kill old server PID=$pid"
|
|
71
|
+
Stop-Process -Id $pid -Force -ErrorAction SilentlyContinue
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
} catch {}
|
|
75
|
+
|
|
76
|
+
# UseShellExecute=$true 避免输出缓冲死锁
|
|
77
|
+
$psi = New-Object System.Diagnostics.ProcessStartInfo
|
|
78
|
+
$psi.FileName = "node"
|
|
79
|
+
$psi.Arguments = "`"$serverPath`""
|
|
80
|
+
$psi.WorkingDirectory = $scriptDir
|
|
81
|
+
$psi.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Hidden
|
|
82
|
+
$psi.CreateNoWindow = $true
|
|
83
|
+
$psi.UseShellExecute = $true
|
|
84
|
+
try {
|
|
85
|
+
$p = [System.Diagnostics.Process]::Start($psi)
|
|
86
|
+
Write-Log "Server started PID=$($p.Id)"
|
|
87
|
+
return $p
|
|
88
|
+
} catch {
|
|
89
|
+
Write-Log "Server start FAILED: $_"
|
|
90
|
+
return $null
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
# ═══════════════════════════════════════════════════════════
|
|
95
|
+
# 4. 健康检查
|
|
96
|
+
# ═══════════════════════════════════════════════════════════
|
|
97
|
+
function Test-Online {
|
|
98
|
+
try {
|
|
99
|
+
$req = [System.Net.WebRequest]::Create("http://127.0.0.1:50816/health")
|
|
100
|
+
$req.Timeout = 1500
|
|
101
|
+
$resp = $req.GetResponse()
|
|
102
|
+
$resp.Close()
|
|
103
|
+
return $true
|
|
104
|
+
} catch { return $false }
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
# ═══════════════════════════════════════════════════════════
|
|
108
|
+
# 5. 开机自启动 (Registry)
|
|
109
|
+
# ═══════════════════════════════════════════════════════════
|
|
110
|
+
$autostartName = "PanRouter"
|
|
111
|
+
$runKey = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run"
|
|
112
|
+
|
|
113
|
+
function Get-Autostart {
|
|
114
|
+
try {
|
|
115
|
+
$val = Get-ItemProperty -Path $runKey -Name $autostartName -ErrorAction Stop
|
|
116
|
+
return $val.$autostartName -ne $null
|
|
117
|
+
} catch { return $false }
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function Set-Autostart($enable) {
|
|
121
|
+
if ($enable) {
|
|
122
|
+
$vbsPath = Join-Path $scriptDir "tray-launcher.vbs"
|
|
123
|
+
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Run" /v $autostartName /t REG_SZ /d "wscript.exe //B `"$vbsPath`"" /f 2>&1 | Out-Null
|
|
124
|
+
Write-Log "Autostart ON: $vbsPath"
|
|
125
|
+
} else {
|
|
126
|
+
reg delete "HKCU\Software\Microsoft\Windows\CurrentVersion\Run" /v $autostartName /f 2>&1 | Out-Null
|
|
127
|
+
Write-Log "Autostart OFF"
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
# ═══════════════════════════════════════════════════════════
|
|
132
|
+
# ══ 启动流程 ══
|
|
133
|
+
# ═══════════════════════════════════════════════════════════
|
|
134
|
+
|
|
135
|
+
# 启动服务器
|
|
136
|
+
$serverProcess = Start-Server
|
|
137
|
+
|
|
138
|
+
# 等待就绪 (10秒)
|
|
139
|
+
$ready = $false
|
|
140
|
+
for ($i = 0; $i -lt 20; $i++) {
|
|
141
|
+
Start-Sleep -Milliseconds 500
|
|
142
|
+
if (Test-Online) { $ready = $true; break }
|
|
143
|
+
}
|
|
144
|
+
Write-Log "Server ready=$ready"
|
|
145
|
+
if ($ready) {
|
|
146
|
+
$notifyIcon.ShowBalloonTip(3000, "Pan Router", "服务器已就绪 (端口 50816)", [System.Windows.Forms.ToolTipIcon]::Info)
|
|
147
|
+
} else {
|
|
148
|
+
$notifyIcon.ShowBalloonTip(3000, "Pan Router", "服务器启动失败, 查看日志", [System.Windows.Forms.ToolTipIcon]::Error)
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
# ═══════════════════════════════════════════════════════════
|
|
152
|
+
# ══ 右键菜单 ══
|
|
153
|
+
# ═══════════════════════════════════════════════════════════
|
|
154
|
+
$menu = New-Object System.Windows.Forms.ContextMenuStrip
|
|
155
|
+
|
|
156
|
+
# 标题
|
|
157
|
+
$titleItem = New-Object System.Windows.Forms.ToolStripMenuItem("Pan Router - :50816")
|
|
158
|
+
$titleItem.Enabled = $false
|
|
159
|
+
$titleItem.Font = New-Object System.Drawing.Font("Segoe UI", 9, [System.Drawing.FontStyle]::Bold)
|
|
160
|
+
$menu.Items.Add($titleItem)
|
|
161
|
+
$menu.Items.Add("-")
|
|
162
|
+
|
|
163
|
+
# 开机自启动
|
|
164
|
+
$autoItem = New-Object System.Windows.Forms.ToolStripMenuItem("开机自启动")
|
|
165
|
+
$autoItem.Checked = Get-Autostart
|
|
166
|
+
$autoItem.Add_Click({
|
|
167
|
+
if ($autoItem.Checked) {
|
|
168
|
+
Set-Autostart $false
|
|
169
|
+
$autoItem.Checked = $false
|
|
170
|
+
$notifyIcon.ShowBalloonTip(2000, "Pan Router", "开机自启动已关闭", [System.Windows.Forms.ToolTipIcon]::Info)
|
|
171
|
+
} else {
|
|
172
|
+
Set-Autostart $true
|
|
173
|
+
$autoItem.Checked = $true
|
|
174
|
+
$notifyIcon.ShowBalloonTip(2000, "Pan Router", "开机自启动已开启 ✓", [System.Windows.Forms.ToolTipIcon]::Info)
|
|
175
|
+
}
|
|
176
|
+
})
|
|
177
|
+
$menu.Items.Add($autoItem)
|
|
178
|
+
$menu.Items.Add("-")
|
|
179
|
+
|
|
180
|
+
# 退出
|
|
181
|
+
$exitItem = New-Object System.Windows.Forms.ToolStripMenuItem("退出")
|
|
182
|
+
$exitItem.Add_Click({
|
|
183
|
+
Write-Log "Exit clicked"
|
|
184
|
+
$notifyIcon.Visible = $false
|
|
185
|
+
[System.Windows.Forms.Application]::Exit()
|
|
186
|
+
})
|
|
187
|
+
$menu.Items.Add($exitItem)
|
|
188
|
+
|
|
189
|
+
$notifyIcon.ContextMenuStrip = $menu
|
|
190
|
+
|
|
191
|
+
# ═══════════════════════════════════════════════════════════
|
|
192
|
+
# ══ 左键: 状态气泡 ══
|
|
193
|
+
# ═══════════════════════════════════════════════════════════
|
|
194
|
+
$notifyIcon.Add_MouseClick({
|
|
195
|
+
if ($_.Button -eq [System.Windows.Forms.MouseButtons]::Left) {
|
|
196
|
+
if (Test-Online) {
|
|
197
|
+
$notifyIcon.ShowBalloonTip(2000, "Pan Router", "运行正常 ✓ (端口 50816)", [System.Windows.Forms.ToolTipIcon]::Info)
|
|
198
|
+
} else {
|
|
199
|
+
$notifyIcon.ShowBalloonTip(2000, "Pan Router", "服务器未响应 ⚠", [System.Windows.Forms.ToolTipIcon]::Error)
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
# ═══════════════════════════════════════════════════════════
|
|
205
|
+
# ══ 定时健康检查 (30秒) ══
|
|
206
|
+
# ═══════════════════════════════════════════════════════════
|
|
207
|
+
$healthTimer = New-Object System.Windows.Forms.Timer
|
|
208
|
+
$healthTimer.Interval = 30000
|
|
209
|
+
$healthTimer.Add_Tick({
|
|
210
|
+
if (-not (Test-Online)) {
|
|
211
|
+
Write-Log "Health check FAILED, restarting server..."
|
|
212
|
+
Start-Server
|
|
213
|
+
Start-Sleep -Seconds 3
|
|
214
|
+
if (Test-Online) {
|
|
215
|
+
$notifyIcon.ShowBalloonTip(3000, "Pan Router", "服务器已自动重启", [System.Windows.Forms.ToolTipIcon]::Info)
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
})
|
|
219
|
+
$healthTimer.Start()
|
|
220
|
+
|
|
221
|
+
# ═══════════════════════════════════════════════════════════
|
|
222
|
+
# ══ 退出清理 ══
|
|
223
|
+
# ═══════════════════════════════════════════════════════════
|
|
224
|
+
[System.Windows.Forms.Application]::ApplicationExit += {
|
|
225
|
+
Write-Log "AppExit cleanup"
|
|
226
|
+
$healthTimer.Stop()
|
|
227
|
+
try {
|
|
228
|
+
if ($serverProcess -and !$serverProcess.HasExited) {
|
|
229
|
+
$serverProcess.Kill()
|
|
230
|
+
$serverProcess.WaitForExit(3000)
|
|
231
|
+
}
|
|
232
|
+
} catch {}
|
|
233
|
+
# 补刀: 杀残留
|
|
234
|
+
try {
|
|
235
|
+
$old = wmic process where "name='node.exe'" get ProcessId,CommandLine /format:csv 2>$null
|
|
236
|
+
foreach ($line in $old) {
|
|
237
|
+
if ($line -match '(\d+),.*?server\.mjs') { Stop-Process -Id $Matches[1] -Force -ErrorAction SilentlyContinue }
|
|
238
|
+
}
|
|
239
|
+
} catch {}
|
|
240
|
+
$notifyIcon.Dispose()
|
|
241
|
+
[System.Runtime.InteropServices.Marshal]::DestroyIcon($hIcon)
|
|
242
|
+
$icon.Dispose()
|
|
243
|
+
Write-Log "Cleanup done"
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
# ═══════════════════════════════════════════════════════════
|
|
247
|
+
# ══ 消息循环 ══
|
|
248
|
+
# ═══════════════════════════════════════════════════════════
|
|
249
|
+
Write-Log "Entering message loop"
|
|
250
|
+
[System.Windows.Forms.Application]::Run()
|
|
251
|
+
Write-Log "Message loop exited"
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
' Pan Router Tray Launcher
|
|
2
|
+
' Windows 原生无窗口启动器(VBScript 内置, 无需任何运行时)
|
|
3
|
+
' 用法: wscript.exe tray-launcher.vbs
|
|
4
|
+
' 或 cscript.exe //nologo tray-launcher.vbs
|
|
5
|
+
|
|
6
|
+
Dim WshShell, FSO, ScriptDir
|
|
7
|
+
Set WshShell = CreateObject("WScript.Shell")
|
|
8
|
+
Set FSO = CreateObject("Scripting.FileSystemObject")
|
|
9
|
+
|
|
10
|
+
ScriptDir = FSO.GetParentFolderName(WScript.ScriptFullName)
|
|
11
|
+
PS1Path = ScriptDir & "\tray-daemon.ps1"
|
|
12
|
+
|
|
13
|
+
' 用 PowerShell 启动托盘守护进程 (0 = 隐藏窗口, False = 不等待返回)
|
|
14
|
+
WshShell.Run "powershell -ExecutionPolicy Bypass -WindowStyle Hidden -STA -File """ & PS1Path & """", 0, False
|
package/tray-app.cs
DELETED
|
@@ -1,324 +0,0 @@
|
|
|
1
|
-
/* Pan Router Tray App
|
|
2
|
-
* 编译: csc /nologo /target:winexe /reference:System.Windows.Forms.dll /reference:System.Drawing.dll tray-app.cs
|
|
3
|
-
*
|
|
4
|
-
* 功能: 以系统托盘方式运行 panrouter 服务器, 无窗口
|
|
5
|
-
* 右键菜单: 状态, 开机自启动开关, 退出
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
using System;
|
|
9
|
-
using System.Diagnostics;
|
|
10
|
-
using System.Drawing;
|
|
11
|
-
using System.IO;
|
|
12
|
-
using System.Net;
|
|
13
|
-
using System.Reflection;
|
|
14
|
-
using System.Runtime.InteropServices;
|
|
15
|
-
using System.Text;
|
|
16
|
-
using System.Threading;
|
|
17
|
-
using System.Windows.Forms;
|
|
18
|
-
|
|
19
|
-
public class PanRouterTrayApp
|
|
20
|
-
{
|
|
21
|
-
private static NotifyIcon _notifyIcon;
|
|
22
|
-
private static Process _serverProcess;
|
|
23
|
-
private static string _serverPath;
|
|
24
|
-
private static string _appDir;
|
|
25
|
-
private static System.Threading.Timer _healthTimer;
|
|
26
|
-
private static StreamWriter _log;
|
|
27
|
-
private static readonly string _logPath = Path.Combine(Path.GetTempPath(), "panrouter-tray.log");
|
|
28
|
-
|
|
29
|
-
// ─── Win32 API: 隐藏窗口 ──────────────────────
|
|
30
|
-
[DllImport("user32.dll")]
|
|
31
|
-
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
|
|
32
|
-
private const int SW_HIDE = 0;
|
|
33
|
-
|
|
34
|
-
[STAThread]
|
|
35
|
-
public static void Main(string[] args)
|
|
36
|
-
{
|
|
37
|
-
// Parse args
|
|
38
|
-
foreach (var a in args)
|
|
39
|
-
{
|
|
40
|
-
if (a.StartsWith("--serverPath:", StringComparison.OrdinalIgnoreCase))
|
|
41
|
-
_serverPath = a.Substring("--serverPath:".Length).Trim('"');
|
|
42
|
-
}
|
|
43
|
-
if (string.IsNullOrEmpty(_serverPath) || !File.Exists(_serverPath))
|
|
44
|
-
{
|
|
45
|
-
MessageBox.Show("Pan Router: 找不到 server.mjs", "Pan Router", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
_appDir = Path.GetDirectoryName(_serverPath);
|
|
49
|
-
|
|
50
|
-
// Open log
|
|
51
|
-
try
|
|
52
|
-
{
|
|
53
|
-
_log = new StreamWriter(_logPath, false, Encoding.UTF8);
|
|
54
|
-
_log.AutoFlush = true;
|
|
55
|
-
Log("=== PanRouter Tray App v1 ===");
|
|
56
|
-
Log("ServerPath: " + _serverPath);
|
|
57
|
-
}
|
|
58
|
-
catch { }
|
|
59
|
-
|
|
60
|
-
// Kill old servers
|
|
61
|
-
KillOldServers();
|
|
62
|
-
|
|
63
|
-
// Start hidden server
|
|
64
|
-
StartServer();
|
|
65
|
-
|
|
66
|
-
// Wait for ready
|
|
67
|
-
bool ready = false;
|
|
68
|
-
for (int i = 0; i < 20; i++)
|
|
69
|
-
{
|
|
70
|
-
Thread.Sleep(500);
|
|
71
|
-
if (IsOnline()) { ready = true; break; }
|
|
72
|
-
}
|
|
73
|
-
Log("Server ready=" + ready);
|
|
74
|
-
|
|
75
|
-
// Create tray icon
|
|
76
|
-
CreateTrayIcon();
|
|
77
|
-
|
|
78
|
-
// Show balloon
|
|
79
|
-
if (ready)
|
|
80
|
-
_notifyIcon.ShowBalloonTip(3000, "Pan Router", "服务器已就绪 (端口 50816)", ToolTipIcon.Info);
|
|
81
|
-
else
|
|
82
|
-
_notifyIcon.ShowBalloonTip(3000, "Pan Router", "服务器启动异常,查看日志", ToolTipIcon.Warning);
|
|
83
|
-
|
|
84
|
-
// Health check every 30s
|
|
85
|
-
_healthTimer = new System.Threading.Timer(HealthCheck, null, 30000, 30000);
|
|
86
|
-
|
|
87
|
-
// Message loop
|
|
88
|
-
Application.Run();
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
private static void Log(string msg)
|
|
92
|
-
{
|
|
93
|
-
try { _log?.WriteLine(DateTime.Now.ToString("HH:mm:ss") + " " + msg); } catch { }
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// ─── 杀掉旧服务器 ────────────────────────────
|
|
97
|
-
private static void KillOldServers()
|
|
98
|
-
{
|
|
99
|
-
try
|
|
100
|
-
{
|
|
101
|
-
// Use WMIC via command line to find node.exe processes running server.mjs
|
|
102
|
-
var psi = new ProcessStartInfo("wmic", "process where \"name='node.exe'\" get ProcessId,CommandLine /format:csv")
|
|
103
|
-
{
|
|
104
|
-
CreateNoWindow = true,
|
|
105
|
-
WindowStyle = ProcessWindowStyle.Hidden,
|
|
106
|
-
UseShellExecute = false,
|
|
107
|
-
RedirectStandardOutput = true,
|
|
108
|
-
RedirectStandardError = true
|
|
109
|
-
};
|
|
110
|
-
using (var p = Process.Start(psi))
|
|
111
|
-
{
|
|
112
|
-
string output = p.StandardOutput.ReadToEnd();
|
|
113
|
-
p.WaitForExit(2000);
|
|
114
|
-
|
|
115
|
-
foreach (string line in output.Split('\n'))
|
|
116
|
-
{
|
|
117
|
-
if (line.Contains("server.mjs"))
|
|
118
|
-
{
|
|
119
|
-
string[] parts = line.Split(',');
|
|
120
|
-
if (parts.Length >= 3 && int.TryParse(parts[2].Trim(), out int pid))
|
|
121
|
-
{
|
|
122
|
-
Log("Killing old server PID=" + pid);
|
|
123
|
-
try { Process.GetProcessById(pid)?.Kill(); } catch { }
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
catch (Exception ex) { Log("KillOld error: " + ex.Message); }
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
// ─── 启动隐藏服务器 ───────────────────────────
|
|
133
|
-
private static void StartServer()
|
|
134
|
-
{
|
|
135
|
-
try
|
|
136
|
-
{
|
|
137
|
-
_serverProcess = new Process();
|
|
138
|
-
_serverProcess.StartInfo.FileName = "node";
|
|
139
|
-
_serverProcess.StartInfo.Arguments = "\"" + _serverPath + "\"";
|
|
140
|
-
_serverProcess.StartInfo.WorkingDirectory = _appDir;
|
|
141
|
-
_serverProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
|
|
142
|
-
_serverProcess.StartInfo.CreateNoWindow = true;
|
|
143
|
-
_serverProcess.StartInfo.UseShellExecute = false;
|
|
144
|
-
// Read output so buffer never blocks
|
|
145
|
-
_serverProcess.StartInfo.RedirectStandardOutput = true;
|
|
146
|
-
_serverProcess.StartInfo.RedirectStandardError = true;
|
|
147
|
-
_serverProcess.OutputDataReceived += (s, e) => { if (!string.IsNullOrEmpty(e.Data)) Log("[srv] " + e.Data); };
|
|
148
|
-
_serverProcess.ErrorDataReceived += (s, e) => { if (!string.IsNullOrEmpty(e.Data)) Log("[srv-err] " + e.Data); };
|
|
149
|
-
_serverProcess.Start();
|
|
150
|
-
_serverProcess.BeginOutputReadLine();
|
|
151
|
-
_serverProcess.BeginErrorReadLine();
|
|
152
|
-
Log("Server started PID=" + _serverProcess.Id);
|
|
153
|
-
}
|
|
154
|
-
catch (Exception ex)
|
|
155
|
-
{
|
|
156
|
-
Log("StartServer FAILED: " + ex.Message);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
// ─── 健康检查 ────────────────────────────────
|
|
161
|
-
private static bool IsOnline()
|
|
162
|
-
{
|
|
163
|
-
try
|
|
164
|
-
{
|
|
165
|
-
var req = WebRequest.CreateHttp("http://127.0.0.1:50816/health");
|
|
166
|
-
req.Timeout = 1500;
|
|
167
|
-
using (var resp = req.GetResponse()) { return true; }
|
|
168
|
-
}
|
|
169
|
-
catch { return false; }
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
private static void HealthCheck(object state)
|
|
173
|
-
{
|
|
174
|
-
try
|
|
175
|
-
{
|
|
176
|
-
if (!IsOnline())
|
|
177
|
-
{
|
|
178
|
-
Log("Health check FAILED, restarting...");
|
|
179
|
-
KillOldServers();
|
|
180
|
-
StartServer();
|
|
181
|
-
Thread.Sleep(3000);
|
|
182
|
-
if (IsOnline())
|
|
183
|
-
_notifyIcon.ShowBalloonTip(3000, "Pan Router", "服务器已自动重启 ✓", ToolTipIcon.Info);
|
|
184
|
-
else
|
|
185
|
-
_notifyIcon.ShowBalloonTip(3000, "Pan Router", "服务器重启失败 ⚠", ToolTipIcon.Error);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
catch (Exception ex) { Log("HealthCheck error: " + ex.Message); }
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
// ─── 创建托盘图标 ────────────────────────────
|
|
192
|
-
private static void CreateTrayIcon()
|
|
193
|
-
{
|
|
194
|
-
var bmp = new Bitmap(16, 16);
|
|
195
|
-
using (var g = Graphics.FromImage(bmp))
|
|
196
|
-
{
|
|
197
|
-
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
|
|
198
|
-
g.Clear(Color.Transparent);
|
|
199
|
-
using (var brush = new SolidBrush(Color.FromArgb(0, 120, 215)))
|
|
200
|
-
g.FillEllipse(brush, 0, 0, 15, 15);
|
|
201
|
-
using (var font = new Font("Segoe UI", 8.5f, FontStyle.Bold))
|
|
202
|
-
using (var fg = new SolidBrush(Color.White))
|
|
203
|
-
g.DrawString("P", font, fg, 3, 1.5f);
|
|
204
|
-
}
|
|
205
|
-
var icon = Icon.FromHandle(bmp.GetHicon());
|
|
206
|
-
|
|
207
|
-
_notifyIcon = new NotifyIcon();
|
|
208
|
-
_notifyIcon.Icon = icon;
|
|
209
|
-
_notifyIcon.Text = "Pan Router\n端口 50816 | 运行中";
|
|
210
|
-
_notifyIcon.Visible = true;
|
|
211
|
-
Log("Tray icon created");
|
|
212
|
-
|
|
213
|
-
// Left click: balloon status
|
|
214
|
-
_notifyIcon.MouseClick += (s, e) =>
|
|
215
|
-
{
|
|
216
|
-
if (e.Button == MouseButtons.Left)
|
|
217
|
-
{
|
|
218
|
-
bool online = IsOnline();
|
|
219
|
-
_notifyIcon.ShowBalloonTip(2000, "Pan Router",
|
|
220
|
-
online ? "运行正常 ✓ (端口 50816)" : "服务器未响应 ⚠",
|
|
221
|
-
online ? ToolTipIcon.Info : ToolTipIcon.Error);
|
|
222
|
-
}
|
|
223
|
-
};
|
|
224
|
-
|
|
225
|
-
// Right-click menu
|
|
226
|
-
var menu = new ContextMenuStrip();
|
|
227
|
-
|
|
228
|
-
var titleItem = new ToolStripMenuItem("Pan Router - :50816") { Enabled = false };
|
|
229
|
-
titleItem.Font = new Font("Segoe UI", 9, FontStyle.Bold);
|
|
230
|
-
menu.Items.Add(titleItem);
|
|
231
|
-
menu.Items.Add(new ToolStripSeparator());
|
|
232
|
-
|
|
233
|
-
// Autostart
|
|
234
|
-
var autoItem = new ToolStripMenuItem("开机自启动");
|
|
235
|
-
autoItem.Checked = IsAutostart();
|
|
236
|
-
autoItem.Click += (s, e) =>
|
|
237
|
-
{
|
|
238
|
-
if (autoItem.Checked)
|
|
239
|
-
{
|
|
240
|
-
SetAutostart(false);
|
|
241
|
-
autoItem.Checked = false;
|
|
242
|
-
_notifyIcon.ShowBalloonTip(2000, "Pan Router", "开机自启动已关闭", ToolTipIcon.Info);
|
|
243
|
-
Log("Autostart OFF");
|
|
244
|
-
}
|
|
245
|
-
else
|
|
246
|
-
{
|
|
247
|
-
SetAutostart(true);
|
|
248
|
-
autoItem.Checked = true;
|
|
249
|
-
_notifyIcon.ShowBalloonTip(2000, "Pan Router", "开机自启动已开启 ✓", ToolTipIcon.Info);
|
|
250
|
-
Log("Autostart ON");
|
|
251
|
-
}
|
|
252
|
-
};
|
|
253
|
-
menu.Items.Add(autoItem);
|
|
254
|
-
menu.Items.Add(new ToolStripSeparator());
|
|
255
|
-
|
|
256
|
-
// Exit
|
|
257
|
-
var exitItem = new ToolStripMenuItem("退出");
|
|
258
|
-
exitItem.Click += (s, e) =>
|
|
259
|
-
{
|
|
260
|
-
Log("Exit clicked");
|
|
261
|
-
Cleanup();
|
|
262
|
-
Application.Exit();
|
|
263
|
-
};
|
|
264
|
-
menu.Items.Add(exitItem);
|
|
265
|
-
|
|
266
|
-
_notifyIcon.ContextMenuStrip = menu;
|
|
267
|
-
|
|
268
|
-
// Cleanup on exit
|
|
269
|
-
Application.ApplicationExit += (s, e) => { Cleanup(); bmp.Dispose(); };
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
// ─── 开机自启动 ──────────────────────────────
|
|
273
|
-
private static bool IsAutostart()
|
|
274
|
-
{
|
|
275
|
-
try
|
|
276
|
-
{
|
|
277
|
-
using (var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run"))
|
|
278
|
-
return key?.GetValue("PanRouter") != null;
|
|
279
|
-
}
|
|
280
|
-
catch { return false; }
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
private static void SetAutostart(bool enable)
|
|
284
|
-
{
|
|
285
|
-
try
|
|
286
|
-
{
|
|
287
|
-
using (var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true))
|
|
288
|
-
{
|
|
289
|
-
if (enable)
|
|
290
|
-
{
|
|
291
|
-
var exePath = Assembly.GetExecutingAssembly().Location;
|
|
292
|
-
key.SetValue("PanRouter",
|
|
293
|
-
$"\"{exePath}\" --serverPath:\"{_serverPath}\"");
|
|
294
|
-
}
|
|
295
|
-
else
|
|
296
|
-
{
|
|
297
|
-
key.DeleteValue("PanRouter", false);
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
catch (Exception ex) { Log("Autostart error: " + ex.Message); }
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
// ─── 退出清理 ────────────────────────────────
|
|
305
|
-
private static void Cleanup()
|
|
306
|
-
{
|
|
307
|
-
try
|
|
308
|
-
{
|
|
309
|
-
Log("Cleanup starting...");
|
|
310
|
-
_healthTimer?.Dispose();
|
|
311
|
-
if (_serverProcess != null && !_serverProcess.HasExited)
|
|
312
|
-
{
|
|
313
|
-
Log("Killing server PID=" + _serverProcess.Id);
|
|
314
|
-
_serverProcess.Kill();
|
|
315
|
-
_serverProcess.WaitForExit(3000);
|
|
316
|
-
_serverProcess.Dispose();
|
|
317
|
-
Log("Server killed");
|
|
318
|
-
}
|
|
319
|
-
KillOldServers();
|
|
320
|
-
try { _log?.Close(); } catch { }
|
|
321
|
-
}
|
|
322
|
-
catch (Exception ex) { try { _log?.WriteLine("Cleanup error: " + ex.Message); } catch { } }
|
|
323
|
-
}
|
|
324
|
-
}
|
package/tray-manager.ps1
DELETED
|
@@ -1,198 +0,0 @@
|
|
|
1
|
-
<#
|
|
2
|
-
.SYNOPSIS
|
|
3
|
-
Pan Router 系统托盘管理器
|
|
4
|
-
.DESCRIPTION
|
|
5
|
-
在通知区域(右下角)显示图标,隐藏命令窗口。
|
|
6
|
-
右键菜单: 状态, 开机自启动开关, 退出
|
|
7
|
-
|
|
8
|
-
用法(必须 STA):
|
|
9
|
-
powershell -ExecutionPolicy Bypass -STA -WindowStyle Hidden -File tray-manager.ps1 -ServerPath "server.mjs"
|
|
10
|
-
|
|
11
|
-
注意: 不能在脚本中弹任何对话框(隐藏窗口下没人点)
|
|
12
|
-
#>
|
|
13
|
-
|
|
14
|
-
param([string]$ServerPath)
|
|
15
|
-
|
|
16
|
-
# ─── 启动即写日志 ──────────────────────────────
|
|
17
|
-
$logPath = "$env:TEMP\panrouter-tray.log"
|
|
18
|
-
"===" + (Get-Date -Format "HH:mm:ss") + " PanRouter Tray===" | Out-File $logPath
|
|
19
|
-
function Log { param($m) (Get-Date -Format "HH:mm:ss") + " " + $m | Out-File -Append $logPath }
|
|
20
|
-
|
|
21
|
-
Log "ServerPath=$ServerPath"
|
|
22
|
-
Log "Args=$([environment]::CommandLine)"
|
|
23
|
-
|
|
24
|
-
# ─── 加载 WinForms ─────────────────────────────
|
|
25
|
-
Add-Type -AssemblyName System.Windows.Forms
|
|
26
|
-
Add-Type -AssemblyName System.Drawing
|
|
27
|
-
Log "Assemblies loaded"
|
|
28
|
-
|
|
29
|
-
# ─── 常量 ──────────────────────────────────────
|
|
30
|
-
$autostartName = "PanRouter"
|
|
31
|
-
$runKeyPath = "Software\Microsoft\Windows\CurrentVersion\Run"
|
|
32
|
-
$appDir = Split-Path $ServerPath -Parent
|
|
33
|
-
$tooltipText = "Pan Router`n端口 50816 | 运行中"
|
|
34
|
-
|
|
35
|
-
# ─── 生成图标 ──────────────────────────────────
|
|
36
|
-
$bmp = New-Object System.Drawing.Bitmap(16, 16)
|
|
37
|
-
$g = [System.Drawing.Graphics]::FromImage($bmp)
|
|
38
|
-
$g.SmoothingMode = 'HighQuality'
|
|
39
|
-
$g.Clear([System.Drawing.Color]::Transparent)
|
|
40
|
-
$brush = New-Object System.Drawing.SolidBrush([System.Drawing.Color]::FromArgb(0, 120, 215))
|
|
41
|
-
$g.FillEllipse($brush, 2, 2, 12, 12)
|
|
42
|
-
$font = New-Object System.Drawing.Font("Segoe UI", 8, [System.Drawing.FontStyle]::Bold)
|
|
43
|
-
$fg = New-Object System.Drawing.SolidBrush([System.Drawing.Color]::White)
|
|
44
|
-
$g.DrawString("P", $font, $fg, 4, 2.5)
|
|
45
|
-
$font.Dispose(); $fg.Dispose(); $brush.Dispose(); $g.Dispose()
|
|
46
|
-
$hIcon = $bmp.GetHicon()
|
|
47
|
-
$icon = [System.Drawing.Icon]::FromHandle($hIcon)
|
|
48
|
-
$bmp.Dispose()
|
|
49
|
-
Log "Icon generated"
|
|
50
|
-
|
|
51
|
-
# ─── NotifyIcon ────────────────────────────────
|
|
52
|
-
$notifyIcon = New-Object System.Windows.Forms.NotifyIcon
|
|
53
|
-
$notifyIcon.Icon = $icon
|
|
54
|
-
$notifyIcon.Text = $tooltipText
|
|
55
|
-
$notifyIcon.Visible = $true
|
|
56
|
-
Log "NotifyIcon visible"
|
|
57
|
-
|
|
58
|
-
# ─── 启动隐藏服务器 ────────────────────────────
|
|
59
|
-
function Start-Server {
|
|
60
|
-
$psi = New-Object System.Diagnostics.ProcessStartInfo
|
|
61
|
-
$psi.FileName = "node"
|
|
62
|
-
$psi.Arguments = "`"$ServerPath`""
|
|
63
|
-
$psi.WorkingDirectory = $appDir
|
|
64
|
-
$psi.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Hidden
|
|
65
|
-
$psi.CreateNoWindow = $true
|
|
66
|
-
$psi.UseShellExecute = $true # 关键: 不重定向输出, 无缓冲死锁
|
|
67
|
-
try {
|
|
68
|
-
$p = [System.Diagnostics.Process]::Start($psi)
|
|
69
|
-
Log "Server started PID=$($p.Id)"
|
|
70
|
-
return $p
|
|
71
|
-
} catch {
|
|
72
|
-
Log "Server start FAILED: $_"
|
|
73
|
-
return $null
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
# ─── 杀掉旧服务器 (wmic, 不依赖 CIM/WMI 版本) ─
|
|
78
|
-
function Stop-OldServer {
|
|
79
|
-
try {
|
|
80
|
-
$wmicOut = & wmic process where "name='node.exe'" get ProcessId,CommandLine /format:csv 2>$null
|
|
81
|
-
foreach ($line in $wmicOut) {
|
|
82
|
-
if ($line -match '(\d+),.*?server\.mjs') {
|
|
83
|
-
$pid = $Matches[1]
|
|
84
|
-
Log "Kill old PID=$pid"
|
|
85
|
-
Stop-Process -Id $pid -Force -ErrorAction SilentlyContinue
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
} catch { Log "KillOld: $_" }
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
# ─── 健康检查 ──────────────────────────────────
|
|
92
|
-
function Test-Online {
|
|
93
|
-
try {
|
|
94
|
-
$req = [System.Net.WebRequest]::Create("http://127.0.0.1:50816/health")
|
|
95
|
-
$req.Timeout = 1500
|
|
96
|
-
$resp = $req.GetResponse()
|
|
97
|
-
$resp.Close()
|
|
98
|
-
return $true
|
|
99
|
-
} catch { return $false }
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
# ═══════════════ 主流程 ═══════════════
|
|
103
|
-
|
|
104
|
-
# 1. 杀旧 + 启动
|
|
105
|
-
Stop-OldServer
|
|
106
|
-
$serverProcess = Start-Server
|
|
107
|
-
|
|
108
|
-
# 2. 前台等待就绪 (10秒超时)
|
|
109
|
-
$ready = $false
|
|
110
|
-
for ($i = 0; $i -lt 20; $i++) {
|
|
111
|
-
Start-Sleep -Milliseconds 500
|
|
112
|
-
if (Test-Online) { $ready = $true; break }
|
|
113
|
-
}
|
|
114
|
-
Log "Server ready=$ready"
|
|
115
|
-
if ($ready) {
|
|
116
|
-
$notifyIcon.ShowBalloonTip(3000, "Pan Router", "服务器已就绪 (端口 50816)", [System.Windows.Forms.ToolTipIcon]::Info)
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
# 3. 右键菜单
|
|
120
|
-
$menu = New-Object System.Windows.Forms.ContextMenuStrip
|
|
121
|
-
|
|
122
|
-
$titleItem = New-Object System.Windows.Forms.ToolStripMenuItem
|
|
123
|
-
$titleItem.Text = "Pan Router - :50816"
|
|
124
|
-
$titleItem.Enabled = $false
|
|
125
|
-
$titleItem.Font = New-Object System.Drawing.Font("Segoe UI", 9, [System.Drawing.FontStyle]::Bold)
|
|
126
|
-
$menu.Items.Add($titleItem)
|
|
127
|
-
$menu.Items.Add("-")
|
|
128
|
-
|
|
129
|
-
$autoStartItem = New-Object System.Windows.Forms.ToolStripMenuItem
|
|
130
|
-
$autoStartItem.Text = "开机自启动"
|
|
131
|
-
$rk = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey($runKeyPath, $true)
|
|
132
|
-
$autoStartItem.Checked = ($rk.GetValue($autostartName) -ne $null)
|
|
133
|
-
$rk.Close()
|
|
134
|
-
$autoStartItem.Add_Click({
|
|
135
|
-
$rk2 = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey($runKeyPath, $true)
|
|
136
|
-
if ($autoStartItem.Checked) {
|
|
137
|
-
$rk2.DeleteValue($autostartName, $false)
|
|
138
|
-
$autoStartItem.Checked = $false
|
|
139
|
-
$notifyIcon.ShowBalloonTip(2000, "Pan Router", "开机自启动已关闭", [System.Windows.Forms.ToolTipIcon]::Info)
|
|
140
|
-
Log "Autostart OFF"
|
|
141
|
-
} else {
|
|
142
|
-
$cmd = "powershell -ExecutionPolicy Bypass -WindowStyle Hidden -STA -File `"$($MyInvocation.MyCommand.Path)`" -ServerPath `"$ServerPath`""
|
|
143
|
-
$rk2.SetValue($autostartName, $cmd)
|
|
144
|
-
$autoStartItem.Checked = $true
|
|
145
|
-
$notifyIcon.ShowBalloonTip(2000, "Pan Router", "开机自启动已开启 ✓", [System.Windows.Forms.ToolTipIcon]::Info)
|
|
146
|
-
Log "Autostart ON"
|
|
147
|
-
}
|
|
148
|
-
$rk2.Close()
|
|
149
|
-
})
|
|
150
|
-
$menu.Items.Add($autoStartItem)
|
|
151
|
-
$menu.Items.Add("-")
|
|
152
|
-
|
|
153
|
-
$exitItem = New-Object System.Windows.Forms.ToolStripMenuItem
|
|
154
|
-
$exitItem.Text = "退出"
|
|
155
|
-
$exitItem.Add_Click({
|
|
156
|
-
Log "Exit clicked"
|
|
157
|
-
$notifyIcon.Visible = $false
|
|
158
|
-
[System.Windows.Forms.Application]::Exit()
|
|
159
|
-
})
|
|
160
|
-
$menu.Items.Add($exitItem)
|
|
161
|
-
|
|
162
|
-
$notifyIcon.ContextMenuStrip = $menu
|
|
163
|
-
|
|
164
|
-
# 4. 左键: 状态
|
|
165
|
-
$notifyIcon.Add_MouseClick({
|
|
166
|
-
if ($_.Button -eq [System.Windows.Forms.MouseButtons]::Left) {
|
|
167
|
-
if (Test-Online) {
|
|
168
|
-
$notifyIcon.ShowBalloonTip(2000, "Pan Router", "运行正常 ✓ (端口 50816)", [System.Windows.Forms.ToolTipIcon]::Info)
|
|
169
|
-
} else {
|
|
170
|
-
$notifyIcon.ShowBalloonTip(2000, "Pan Router", "服务器未响应 ⚠", [System.Windows.Forms.ToolTipIcon]::Error)
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
})
|
|
174
|
-
|
|
175
|
-
# 5. 退出清理
|
|
176
|
-
[System.Windows.Forms.Application]::ApplicationExit += {
|
|
177
|
-
Log "AppExit cleanup"
|
|
178
|
-
if ($serverProcess -and !$serverProcess.HasExited) {
|
|
179
|
-
$serverProcess.Kill()
|
|
180
|
-
$serverProcess.WaitForExit(3000)
|
|
181
|
-
Log "Server killed"
|
|
182
|
-
}
|
|
183
|
-
Stop-OldServer
|
|
184
|
-
$notifyIcon.Dispose()
|
|
185
|
-
[System.Runtime.InteropServices.Marshal]::DestroyIcon($hIcon)
|
|
186
|
-
$icon.Dispose()
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
# 6. 运行消息循环
|
|
190
|
-
Log "Entering message loop"
|
|
191
|
-
[System.Windows.Forms.Application]::Run()
|
|
192
|
-
Log "Message loop exited"
|
|
193
|
-
|
|
194
|
-
# 7. 循环结束后再清理一次
|
|
195
|
-
try {
|
|
196
|
-
if ($serverProcess -and !$serverProcess.HasExited) { $serverProcess.Kill() }
|
|
197
|
-
Stop-OldServer
|
|
198
|
-
} catch {}
|