cdp-tunnel 3.1.0 → 3.2.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.
@@ -12,8 +12,11 @@ var SpecialHandler = (function() {
12
12
  function _getConnectionTag(ctx) {
13
13
  var wm = ctx._wsManager;
14
14
  var state = ctx._state;
15
- // 优先从 state.connectionTag 读(端口池设置的端口号)
16
- if (state && state.connectionTag) return state.connectionTag;
15
+ var clientId = ctx.clientId;
16
+ if (state && clientId && state.getTagForClient) {
17
+ var tag = state.getTagForClient(clientId);
18
+ if (tag) return tag;
19
+ }
17
20
  return (wm && wm.config && wm.config.tag) || null;
18
21
  }
19
22
 
@@ -2,6 +2,7 @@ function ConnectionState(connectionId, mode) {
2
2
  this.connectionId = connectionId;
3
3
  this.mode = mode || 'create';
4
4
  this.connectionTag = null;
5
+ this.clientIdToTag = new Map();
5
6
  this.ws = null;
6
7
  this.reconnectTimer = null;
7
8
  this._hasConnectedClient = false;
@@ -299,6 +300,14 @@ ConnectionState.prototype.clearPreExistingTabsForClient = function(clientId) {
299
300
  });
300
301
  };
301
302
 
303
+ ConnectionState.prototype.setTagForClient = function(clientId, tag) {
304
+ if (clientId && tag) this.clientIdToTag.set(clientId, tag);
305
+ };
306
+
307
+ ConnectionState.prototype.getTagForClient = function(clientId) {
308
+ return this.clientIdToTag.get(clientId) || null;
309
+ };
310
+
302
311
  ConnectionState.prototype.addCDPClient = function(clientId, info) {
303
312
  var exists = false;
304
313
  for (var i = 0; i < this.cdpClients.length; i++) {
@@ -242,7 +242,7 @@ var WebSocketConnection = (function() {
242
242
  self.state.setHasConnectedClient(true);
243
243
  self.state.addCDPClient(message.clientId, message.clientId);
244
244
  if (message.__connectionTag) {
245
- self.state.connectionTag = message.__connectionTag;
245
+ self.state.setTagForClient(message.clientId, message.__connectionTag);
246
246
  }
247
247
  self._createGroupForClient(message.clientId, message.__mode);
248
248
  self._broadcastStateUpdate();
@@ -580,7 +580,7 @@ var WebSocketConnection = (function() {
580
580
  var readyPromise = new Promise(function(resolve) { resolveGroupReady = resolve; });
581
581
  self.state.setGroupCreationPromise(clientId, readyPromise);
582
582
 
583
- var tag = self.state.connectionTag || (self.config ? self.config.tag : null);
583
+ var tag = (self.state.getTagForClient ? self.state.getTagForClient(clientId) : null) || (self.config ? self.config.tag : null);
584
584
  var baseName = CDPUtils.getGroupBaseName(clientId, tag, mode);
585
585
  chrome.tabs.query({ currentWindow: true }, function(tabs) {
586
586
  if (!tabs || tabs.length === 0) {
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "manifest_version": 3,
3
3
  "name": "CDP Bridge",
4
- "version": "3.1.0",
4
+ "version": "3.2.0",
5
5
  "description": "Chrome DevTools Protocol Bridge for Playwright/Puppeteer automation",
6
6
  "permissions": [
7
7
  "debugger",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cdp-tunnel",
3
- "version": "3.1.0",
3
+ "version": "3.2.0",
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",
@@ -1,5 +1,9 @@
1
1
  const CONFIG = {
2
2
  PORT: process.env.PORT ? parseInt(process.env.PORT) : 9221,
3
+ TAKEOVER_PORT: process.env.TAKEOVER_PORT ? parseInt(process.env.TAKEOVER_PORT) : 9220,
4
+ POOL_START: process.env.POOL_START ? parseInt(process.env.POOL_START) : 9231,
5
+ POOL_SIZE: process.env.POOL_SIZE ? parseInt(process.env.POOL_SIZE) : 9,
6
+ POOL_TAKEOVER_PORT: process.env.POOL_TAKEOVER_PORT ? parseInt(process.env.POOL_TAKEOVER_PORT) : 9220,
3
7
  HEARTBEAT_INTERVAL: 30000,
4
8
  STATUS_PRINT_INTERVAL: 60000,
5
9
  TARGETS_CACHE_TTL: 2000,
@@ -10,11 +14,7 @@ const CONFIG = {
10
14
  LOG_LEVEL: process.env.LOG_LEVEL || 'info',
11
15
  AUTO_RESTART: process.env.AUTO_RESTART === 'true',
12
16
  CHROME_RESTART_COOLDOWN: 30000,
13
- PLUGIN_MAX_MISSED_PINGS: 3,
14
- TAKEOVER_PORT: process.env.TAKEOVER_PORT ? parseInt(process.env.TAKEOVER_PORT) : (parseInt(process.env.PORT || '9221') + 1),
15
- POOL_TAKEOVER_PORT: process.env.POOL_TAKEOVER_PORT ? parseInt(process.env.POOL_TAKEOVER_PORT) : 9220,
16
- POOL_START: process.env.POOL_START ? parseInt(process.env.POOL_START) : 9231,
17
- POOL_SIZE: process.env.POOL_SIZE ? parseInt(process.env.POOL_SIZE) : 9
17
+ PLUGIN_MAX_MISSED_PINGS: 3
18
18
  };
19
19
 
20
20
  const LOG_LEVELS = {
@@ -67,16 +67,24 @@ class PortPoolManager {
67
67
  const server = http.createServer();
68
68
  this.createServers[portIndex] = server;
69
69
 
70
- // 端口池端口复用主 proxy wss——通过 localPort 区分
70
+ server.on('request', (req, res) => {
71
+ this._handleHttp(req, res, session);
72
+ });
73
+
71
74
  server.on('upgrade', (req, socket, head) => {
72
75
  req._poolPortIndex = portIndex;
73
76
  req._poolPort = port;
74
- // 转发给主 proxy wss 处理
75
- this.mainProxy.handlePoolUpgrade(req, socket, head, portIndex, port);
76
- });
77
+ const url = new URL(req.url, `http://localhost:${port}`);
78
+ const path = url.pathname;
79
+
80
+ // 只接受 client 连接(plugin 连 9221)
81
+ if (path !== '/client' && !path.startsWith('/client/') &&
82
+ !path.startsWith('/devtools/browser/') && !path.startsWith('/devtools/page/')) {
83
+ socket.destroy();
84
+ return;
85
+ }
77
86
 
78
- server.on('request', (req, res) => {
79
- this._handleHttp(req, res, session);
87
+ this.mainProxy.handlePoolUpgrade(req, socket, head, portIndex, port);
80
88
  });
81
89
 
82
90
  server.on('error', (err) => {
@@ -2096,49 +2096,33 @@ server.on('error', (err) => {
2096
2096
  process.exit(1);
2097
2097
  });
2098
2098
 
2099
+ // v3.2: 9221 = 主 server(plugin + client),9222 废弃
2099
2100
  server.listen(PORT, '0.0.0.0');
2100
2101
 
2101
- const takeoverServer = http.createServer((req, res) => {
2102
- req._takeoverMode = true;
2103
- handleHttpRequest(req, res);
2104
- });
2105
- takeoverServer.on('upgrade', (req, socket, head) => {
2106
- req._takeoverMode = true;
2107
- const url = new URL(req.url, `http://localhost:${TAKEOVER_PORT}`);
2108
- const path = url.pathname;
2109
- const isPlugin = path === '/plugin';
2110
- const isClient = path === '/client' ||
2111
- path.startsWith('/client/') ||
2112
- path.startsWith('/client-') ||
2113
- path.startsWith('/devtools/browser/') ||
2114
- path.startsWith('/devtools/page/');
2115
-
2116
- if (!isPlugin && !isClient) {
2117
- socket.destroy();
2118
- return;
2119
- }
2120
-
2121
- wss.handleUpgrade(req, socket, head, (ws) => {
2122
- wss.emit('connection', ws, req);
2123
- });
2124
- });
2125
- takeoverServer.on('error', (err) => {
2126
- if (err.code === 'EADDRINUSE') {
2127
- console.error(`[WARN] Takeover port ${TAKEOVER_PORT} is already in use. Takeover mode disabled.`);
2128
- } else {
2129
- console.error('[WARN] Takeover server error:', err.message);
2130
- }
2131
- });
2132
- takeoverServer.listen(TAKEOVER_PORT, '0.0.0.0', () => {
2133
- console.log(`[TAKEOVER] Listening on port ${TAKEOVER_PORT}`);
2134
- });
2135
-
2136
- // v3.0 端口池启动
2102
+ // v3.0 端口池启动(唯一模式)
2137
2103
  portPool = new PortPoolManager({
2138
2104
  getPluginConnection: () => {
2139
2105
  for (const ws of pluginConnections) return ws;
2140
2106
  return null;
2141
2107
  },
2108
+ handlePluginUpgrade: (req, socket, head) => {
2109
+ wss.handleUpgrade(req, socket, head, (ws) => {
2110
+ wss.emit('connection', ws, req);
2111
+ });
2112
+ },
2113
+ handleTakeoverUpgrade: (req, socket, head) => {
2114
+ req._takeoverMode = true;
2115
+ const url = new URL(req.url, `http://localhost:${CONFIG.TAKEOVER_PORT}`);
2116
+ const path = url.pathname;
2117
+ if (path !== '/plugin' && path !== '/client' && !path.startsWith('/client/') &&
2118
+ !path.startsWith('/devtools/browser/') && !path.startsWith('/devtools/page/')) {
2119
+ socket.destroy();
2120
+ return;
2121
+ }
2122
+ wss.handleUpgrade(req, socket, head, (ws) => {
2123
+ wss.emit('connection', ws, req);
2124
+ });
2125
+ },
2142
2126
  handleHttpRequest: (req, res) => handleHttpRequest(req, res),
2143
2127
  handlePoolUpgrade: (req, socket, head, portIndex, port) => {
2144
2128
  req._poolPortIndex = portIndex;