panrouter 3.0.0 → 3.1.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 +36 -10
- package/package.json +1 -1
- package/tray-daemon.ps1 +12 -2
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("..", "
|
|
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
|
-
|
|
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
|
-
|
|
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
package/tray-daemon.ps1
CHANGED
|
@@ -26,7 +26,18 @@ function Start-Backend {
|
|
|
26
26
|
Start-Process -FilePath $nodePath -ArgumentList "`"$serverPath`"" -WorkingDirectory $scriptDir -WindowStyle Hidden
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
# 【核心修复】:不要盲目重启!检查端口如果已经通了,直接跳过耗时的杀进程步骤
|
|
30
|
+
$portOpen = $false
|
|
31
|
+
try {
|
|
32
|
+
$tcp = New-Object System.Net.Sockets.TcpClient
|
|
33
|
+
$ar = $tcp.BeginConnect("127.0.0.1", 50816, $null, $null)
|
|
34
|
+
$portOpen = $ar.AsyncWaitHandle.WaitOne(500, $false)
|
|
35
|
+
$tcp.Close()
|
|
36
|
+
} catch {}
|
|
37
|
+
|
|
38
|
+
if (-not $portOpen) {
|
|
39
|
+
Start-Backend
|
|
40
|
+
}
|
|
30
41
|
|
|
31
42
|
# ─── 初始化托盘 ────────────────────────────────────────────
|
|
32
43
|
$notifyIcon = New-Object System.Windows.Forms.NotifyIcon
|
|
@@ -65,7 +76,6 @@ $autoItem.Add_Click({
|
|
|
65
76
|
reg delete "HKCU\Software\Microsoft\Windows\CurrentVersion\Run" /v PanRouter /f 2>&1 | Out-Null
|
|
66
77
|
$autoItem.Checked = $false
|
|
67
78
|
} else {
|
|
68
|
-
# 开机自启也摒弃 VBS,直接用隐藏模式的 PowerShell
|
|
69
79
|
$cmd = "powershell.exe -WindowStyle Hidden -ExecutionPolicy Bypass -File `"$($MyInvocation.MyCommand.Path)`""
|
|
70
80
|
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Run" /v PanRouter /t REG_SZ /d $cmd /f 2>&1 | Out-Null
|
|
71
81
|
$autoItem.Checked = $true
|