cdp-tunnel 3.1.1 → 3.2.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.
@@ -12,9 +12,9 @@ var SpecialHandler = (function() {
12
12
  function _getConnectionTag(ctx) {
13
13
  var wm = ctx._wsManager;
14
14
  var state = ctx._state;
15
- // 端口池:按 clientId connectionTag(端口号)
16
- if (state && ctx.clientId && state.getTagForClient) {
17
- var tag = state.getTagForClient(ctx.clientId);
15
+ var clientId = ctx.clientId;
16
+ if (state && clientId && state.getTagForClient) {
17
+ var tag = state.getTagForClient(clientId);
18
18
  if (tag) return tag;
19
19
  }
20
20
  return (wm && wm.config && wm.config.tag) || null;
@@ -414,7 +414,8 @@ var SpecialHandler = (function() {
414
414
  var groupId = state ? state.getGroupIdForClient(clientId) : null;
415
415
  if (!groupId) return;
416
416
 
417
- var connectionTag = (wsManager && wsManager.config && wsManager.config.tag) || null;
417
+ var connectionTag = (state && state.getTagForClient) ? state.getTagForClient(clientId) : null;
418
+ if (!connectionTag) connectionTag = (wsManager && wsManager.config && wsManager.config.tag) || null;
418
419
 
419
420
  chrome.tabs.query({ groupId: groupId }, function(tabs) {
420
421
  if (chrome.runtime.lastError || !tabs) return;
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "manifest_version": 3,
3
3
  "name": "CDP Bridge",
4
- "version": "3.1.1",
4
+ "version": "3.2.1",
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.1",
3
+ "version": "3.2.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",
@@ -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;