cdp-tunnel 1.0.11 → 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 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
- const serverPath = path.join(__dirname, '..', 'server', 'proxy-server.js');
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('');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cdp-tunnel",
3
- "version": "1.0.11",
3
+ "version": "1.0.13",
4
4
  "description": "Chrome Extension CDP Proxy - 通过 Chrome 扩展将 chrome.debugger API 暴露为 WebSocket 端点",
5
5
  "main": "server/proxy-server.js",
6
6
  "bin": "./cli/index.js",
@@ -11,6 +11,7 @@ const statusLogFile = path.join(logDir, 'server-status.log');
11
11
 
12
12
  const MAX_LOG_SIZE = 10 * 1024 * 1024;
13
13
  const MAX_LOG_FILES = 5;
14
+ const MAX_TOTAL_LOG_SIZE = 30 * 1024 * 1024;
14
15
 
15
16
  let logWriteQueue = [];
16
17
  let isWritingLog = false;
@@ -84,6 +85,25 @@ function flushStatusQueue() {
84
85
  function checkLogRotation() {
85
86
  checkAndRotateLog(logFile);
86
87
  checkAndRotateLog(statusLogFile);
88
+ cleanupOldLogs();
89
+ }
90
+
91
+ function cleanupOldLogs() {
92
+ try {
93
+ const files = fs.readdirSync(logDir).filter(f => f.endsWith('.log')).map(f => {
94
+ const fp = path.join(logDir, f);
95
+ try {
96
+ return { path: fp, stat: fs.statSync(fp) };
97
+ } catch { return null; }
98
+ }).filter(Boolean).sort((a, b) => a.stat.mtimeMs - b.stat.mtimeMs);
99
+
100
+ let totalSize = files.reduce((sum, f) => sum + f.stat.size, 0);
101
+ while (totalSize > MAX_TOTAL_LOG_SIZE && files.length > 1) {
102
+ const oldest = files.shift();
103
+ fs.unlinkSync(oldest.path);
104
+ totalSize -= oldest.stat.size;
105
+ }
106
+ } catch {}
87
107
  }
88
108
 
89
109
  setInterval(checkLogRotation, 60000);
@@ -105,7 +125,43 @@ const NOISY_METHODS = [
105
125
  'Network.responseReceivedExtraInfo',
106
126
  'Network.dataReceived',
107
127
  'Network.loadingFinished',
128
+ 'Network.resourceChangedPriority',
129
+ 'Network.requestServedFromCache',
130
+ 'Network.webSocketFrameSent',
131
+ 'Network.webSocketFrameReceived',
132
+ 'Network.eventSourceMessageReceived',
108
133
  'Input.dispatchMouseEvent',
134
+ 'Input.mouseMoved',
135
+ 'Input.keyDown',
136
+ 'Input.keyUp',
137
+ 'Input.char',
138
+ 'Input.dispatchKeyEvent',
139
+ 'Page.lifecycleEvent',
140
+ 'Page.frameStartedLoading',
141
+ 'Page.frameStoppedLoading',
142
+ 'Page.frameNavigated',
143
+ 'Page.frameRequestedNavigation',
144
+ 'Page.frameScheduledNavigation',
145
+ 'Page.frameStartedNavigating',
146
+ 'Page.frameAttached',
147
+ 'Page.frameClearedScheduledNavigation',
148
+ 'Page.navigatedWithinDocument',
149
+ 'Page.domContentEventFired',
150
+ 'Page.loadEventFired',
151
+ 'Page.screencastFrame',
152
+ 'Page.screencastFrameAck',
153
+ 'Runtime.executionContextCreated',
154
+ 'Runtime.executionContextDestroyed',
155
+ 'Runtime.executionContextsCleared',
156
+ 'Runtime.bindingCalled',
157
+ 'CSS.styleChanged',
158
+ 'CSS.fontsUpdated',
159
+ 'DOM.childNodeInserted',
160
+ 'DOM.childNodeRemoved',
161
+ 'DOM.attributeModified',
162
+ 'DOM.attributeRemoved',
163
+ 'DOM.childNodeCountUpdated',
164
+ 'Log.entryAdded',
109
165
  ];
110
166
 
111
167
  function truncateMessage(message) {