terminal-bridge-setup 2.2.0 → 2.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/files/native/host.js +40 -8
- package/package.json +1 -1
package/files/native/host.js
CHANGED
|
@@ -79,6 +79,21 @@ function isPidAlive(pid) {
|
|
|
79
79
|
} catch { return false; }
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
+
// 通过端口找占用进程的 PID(PID 文件缺失时的回退方案)
|
|
83
|
+
// 用 lsof -ti :port 拿监听该端口的进程 PID
|
|
84
|
+
function findPidByPort(port) {
|
|
85
|
+
return new Promise((resolve) => {
|
|
86
|
+
const child = spawn("lsof", ["-ti", `-i:${port}`, "-sTCP:LISTEN"], { stdio: ["ignore", "pipe", "ignore"] });
|
|
87
|
+
let out = "";
|
|
88
|
+
child.stdout.on("data", (d) => { out += d.toString(); });
|
|
89
|
+
child.on("error", () => resolve(null)); // lsof 不存在或没权限
|
|
90
|
+
child.on("close", () => {
|
|
91
|
+
const pid = parseInt(out.trim().split("\n")[0], 10);
|
|
92
|
+
resolve(isNaN(pid) ? null : pid);
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
82
97
|
// ============== 命令处理 ==============
|
|
83
98
|
async function startProxy() {
|
|
84
99
|
// 已在运行?
|
|
@@ -120,24 +135,41 @@ async function startProxy() {
|
|
|
120
135
|
|
|
121
136
|
function stopProxy() {
|
|
122
137
|
return new Promise(async (resolve) => {
|
|
123
|
-
|
|
138
|
+
let pid = readPid();
|
|
139
|
+
let pidSource = pid ? "pid file" : null;
|
|
140
|
+
|
|
141
|
+
// PID 文件缺失 → 回退用 lsof 按端口找
|
|
124
142
|
if (!pid) {
|
|
125
|
-
// 没有 PID 文件,但端口可能在监听(手动启动的),尝试用 lsof 找
|
|
126
143
|
const up = await isProxyListening();
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
144
|
+
if (!up) {
|
|
145
|
+
resolve({ ok: true, status: "stopped", msg: "proxy was not running" });
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
pid = await findPidByPort(PROXY_PORT);
|
|
149
|
+
pidSource = pid ? "port lookup" : null;
|
|
150
|
+
if (!pid) {
|
|
151
|
+
resolve({ ok: false, status: "running", msg: "无法停止:代理在监听但找不到 PID(lsof 失败),请手动 kill" });
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
131
154
|
}
|
|
155
|
+
|
|
132
156
|
try {
|
|
133
157
|
process.kill(pid, "SIGTERM");
|
|
134
158
|
// 等 2s 确认退出
|
|
135
|
-
setTimeout(() => {
|
|
159
|
+
setTimeout(async () => {
|
|
136
160
|
if (isPidAlive(pid)) {
|
|
137
161
|
try { process.kill(pid, "SIGKILL"); } catch {}
|
|
138
162
|
}
|
|
139
163
|
clearPid();
|
|
140
|
-
|
|
164
|
+
// 再确认端口确实释放了
|
|
165
|
+
const stillUp = await isProxyListening();
|
|
166
|
+
resolve({
|
|
167
|
+
ok: !stillUp,
|
|
168
|
+
status: stillUp ? "running" : "stopped",
|
|
169
|
+
msg: stillUp
|
|
170
|
+
? `SIGTERM+SIGKILL 已发(PID ${pid} via ${pidSource})但端口仍被占用,可能有其他进程`
|
|
171
|
+
: `proxy stopped (PID ${pid} via ${pidSource})`,
|
|
172
|
+
});
|
|
141
173
|
}, 2000);
|
|
142
174
|
} catch (err) {
|
|
143
175
|
clearPid();
|
package/package.json
CHANGED