cdp-tunnel 1.0.12 → 1.0.13
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/index.js +66 -11
- package/package.json +1 -1
package/cli/index.js
CHANGED
|
@@ -143,10 +143,72 @@ function openChromeExtensions() {
|
|
|
143
143
|
}
|
|
144
144
|
}
|
|
145
145
|
|
|
146
|
+
function startServer(port, watchdog) {
|
|
147
|
+
const serverPath = path.join(__dirname, '..', 'server', 'proxy-server.js');
|
|
148
|
+
const logFd = fs.openSync(LOG_FILE, 'a');
|
|
149
|
+
|
|
150
|
+
const child = spawn('node', [serverPath], {
|
|
151
|
+
detached: !watchdog,
|
|
152
|
+
stdio: ['ignore', logFd, logFd],
|
|
153
|
+
env: { ...process.env, PORT: port.toString() }
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
fs.writeFileSync(PID_FILE, child.pid.toString());
|
|
157
|
+
|
|
158
|
+
if (watchdog) {
|
|
159
|
+
let restartCount = 0;
|
|
160
|
+
const MAX_RESTARTS = 10;
|
|
161
|
+
const RESTART_WINDOW = 60000;
|
|
162
|
+
let restartTimestamps = [];
|
|
163
|
+
|
|
164
|
+
child.on('exit', (code, signal) => {
|
|
165
|
+
const now = Date.now();
|
|
166
|
+
restartTimestamps = restartTimestamps.filter(t => now - t < RESTART_WINDOW);
|
|
167
|
+
restartTimestamps.push(now);
|
|
168
|
+
|
|
169
|
+
if (restartTimestamps.length > MAX_RESTARTS) {
|
|
170
|
+
console.log('');
|
|
171
|
+
log('red', '✗ 服务器在 60 秒内崩溃超过 ' + MAX_RESTARTS + ' 次,停止重启');
|
|
172
|
+
log('gray', ' 请检查日志: ' + LOG_FILE);
|
|
173
|
+
console.log('');
|
|
174
|
+
try { fs.unlinkSync(PID_FILE); } catch {}
|
|
175
|
+
process.exit(1);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const reason = signal ? `信号 ${signal}` : `退出码 ${code}`;
|
|
179
|
+
console.log('');
|
|
180
|
+
log('yellow', '⚠ 服务器异常退出 (' + reason + '),3 秒后自动重启...');
|
|
181
|
+
console.log(' 重启次数: ' + restartTimestamps.length + '/' + MAX_RESTARTS + ' (60秒内)');
|
|
182
|
+
console.log('');
|
|
183
|
+
|
|
184
|
+
setTimeout(() => startServer(port, true), 3000);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
process.on('SIGINT', () => {
|
|
188
|
+
console.log('');
|
|
189
|
+
log('cyan', '正在停止服务器(含 watchdog)...');
|
|
190
|
+
try { child.kill('SIGTERM'); } catch {}
|
|
191
|
+
try { fs.unlinkSync(PID_FILE); } catch {}
|
|
192
|
+
process.exit(0);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
process.on('SIGTERM', () => {
|
|
196
|
+
try { child.kill('SIGTERM'); } catch {}
|
|
197
|
+
try { fs.unlinkSync(PID_FILE); } catch {}
|
|
198
|
+
process.exit(0);
|
|
199
|
+
});
|
|
200
|
+
} else {
|
|
201
|
+
child.unref();
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return child;
|
|
205
|
+
}
|
|
206
|
+
|
|
146
207
|
program
|
|
147
208
|
.command('start')
|
|
148
209
|
.description('启动 CDP Tunnel 服务器')
|
|
149
210
|
.option('-p, --port <port>', '指定端口', parseInt)
|
|
211
|
+
.option('-w, --watchdog', '启用看门狗,服务器崩溃时自动重启')
|
|
150
212
|
.action((options) => {
|
|
151
213
|
const config = getConfig();
|
|
152
214
|
const port = options.port || config.port;
|
|
@@ -168,17 +230,7 @@ program
|
|
|
168
230
|
ensureConfigDir();
|
|
169
231
|
cleanupLogFile();
|
|
170
232
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
const child = spawn('node', [serverPath], {
|
|
174
|
-
detached: true,
|
|
175
|
-
stdio: ['ignore', fs.openSync(LOG_FILE, 'a'), fs.openSync(LOG_FILE, 'a')],
|
|
176
|
-
env: { ...process.env, PORT: port.toString() }
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
child.unref();
|
|
180
|
-
|
|
181
|
-
fs.writeFileSync(PID_FILE, child.pid.toString());
|
|
233
|
+
startServer(port, options.watchdog);
|
|
182
234
|
|
|
183
235
|
if (port !== config.port) {
|
|
184
236
|
config.port = port;
|
|
@@ -191,6 +243,9 @@ program
|
|
|
191
243
|
console.log(' 端口: ' + port);
|
|
192
244
|
console.log(' 插件: ws://localhost:' + port + '/plugin');
|
|
193
245
|
console.log(' CDP: http://localhost:' + port);
|
|
246
|
+
if (options.watchdog) {
|
|
247
|
+
console.log(' 看门狗: 已启用(崩溃自动重启)');
|
|
248
|
+
}
|
|
194
249
|
console.log('');
|
|
195
250
|
log('gray', ' 日志: ' + LOG_FILE);
|
|
196
251
|
console.log('');
|