cdp-tunnel 2.1.0 → 2.1.1

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 CHANGED
@@ -181,6 +181,18 @@ function startServer(port, watchdog, autoRestart) {
181
181
  console.log(' 重启次数: ' + restartTimestamps.length + '/' + MAX_RESTARTS + ' (60秒内)');
182
182
  console.log('');
183
183
 
184
+ // Kill any leftover process occupying the port before restarting
185
+ try {
186
+ const result = execSync(`lsof -ti:${port} 2>/dev/null || true`).toString().trim();
187
+ if (result) {
188
+ const pids = result.split('\n').filter(p => p && parseInt(p) !== process.pid);
189
+ pids.forEach(p => { try { process.kill(parseInt(p), 'SIGKILL'); } catch {} });
190
+ if (pids.length > 0) {
191
+ log('gray', ' 已清理占用端口 ' + port + ' 的残留进程: ' + pids.join(', '));
192
+ }
193
+ }
194
+ } catch {}
195
+
184
196
  setTimeout(() => startServer(port, true, autoRestart), 3000);
185
197
  });
186
198
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cdp-tunnel",
3
- "version": "2.1.0",
3
+ "version": "2.1.1",
4
4
  "description": "Bridge Chrome's debugger API to WebSocket — control your existing browser with Playwright/Puppeteer via CDP",
5
5
  "main": "server/proxy-server.js",
6
6
  "bin": "./cli/index.js",
@@ -22,6 +22,7 @@ const PORT = CONFIG.PORT;
22
22
  const CONFIG_DIR = path.join(os.homedir(), '.cdp-tunnel');
23
23
  const EXTENSION_STATE_FILE = path.join(CONFIG_DIR, 'extension-state.json');
24
24
  const PLUGIN_EVER_CONNECTED_FILE = path.join(CONFIG_DIR, 'plugin-ever-connected');
25
+ const SERVER_START_TIME = Date.now();
25
26
 
26
27
  let lastChromeRestartAttempt = 0;
27
28
  const CHROME_RESTART_COOLDOWN = CONFIG.CHROME_RESTART_COOLDOWN;
@@ -739,7 +740,7 @@ function handlePluginConnection(ws, clientInfo) {
739
740
  type: 'connected',
740
741
  role: 'plugin',
741
742
  id: id,
742
- fresh: true,
743
+ fresh: (Date.now() - SERVER_START_TIME) < 5000,
743
744
  timestamp: Date.now()
744
745
  }));
745
746
  }
@@ -1513,4 +1514,14 @@ process.on('SIGTERM', () => {
1513
1514
  process.exit(0);
1514
1515
  });
1515
1516
 
1517
+ server.on('error', (err) => {
1518
+ if (err.code === 'EADDRINUSE') {
1519
+ console.error(`[FATAL] Port ${PORT} is already in use. Is another cdp-tunnel running?`);
1520
+ console.error(` Run "cdp-tunnel stop" first, or kill the process on port ${PORT}.`);
1521
+ process.exit(2);
1522
+ }
1523
+ console.error('[FATAL] Server error:', err.message);
1524
+ process.exit(1);
1525
+ });
1526
+
1516
1527
  server.listen(PORT, '0.0.0.0');