From ddbf1e11855ad7813a10b23ad5ed541b50907705 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=89=AF?= <841369634@qq.com> Date: Tue, 22 Oct 2024 12:42:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=BF=97=E4=BC=98=E5=8C=96=EF=BC=9A?= =?UTF-8?q?=E5=BC=80=E5=8F=91=E9=98=B6=E6=AE=B5=EF=BC=8C=E6=89=93=E5=8D=B0?= =?UTF-8?q?=E6=9B=B4=E5=A4=9A=E5=AE=A2=E6=88=B7=E7=AB=AF=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E5=92=8C=E4=BB=A3=E7=90=86=E8=BF=9E=E6=8E=A5=E7=9A=84=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=E6=97=A5=E5=BF=97=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../proxy/mitmproxy/createConnectHandler.js | 86 +++++++++++++++++-- 1 file changed, 79 insertions(+), 7 deletions(-) diff --git a/packages/mitmproxy/src/lib/proxy/mitmproxy/createConnectHandler.js b/packages/mitmproxy/src/lib/proxy/mitmproxy/createConnectHandler.js index e5a68f0..83fe6d6 100644 --- a/packages/mitmproxy/src/lib/proxy/mitmproxy/createConnectHandler.js +++ b/packages/mitmproxy/src/lib/proxy/mitmproxy/createConnectHandler.js @@ -58,6 +58,49 @@ function connect (req, cltSocket, head, hostname, port, dnsConfig = null, isDire const isDnsIntercept = {} const hostport = `${hostname}:${port}` try { + // 客户端的连接事件监听 + cltSocket.on('timeout', (e) => { + log.error(`cltSocket timeout: ${hostport}, errorMsg: ${e.message}`) + }) + cltSocket.on('error', (e) => { + log.error(`cltSocket error: ${hostport}, errorMsg: ${e.message}`) + }) + // 开发过程中,如有需要可以将此参数临时改为true,打印所有事件的日志 + const printDebugLog = false && process.env.NODE_ENV === 'development' + if (printDebugLog) { + cltSocket.on('close', (hadError) => { + log.debug('【cltSocket close】', hadError) + }) + cltSocket.on('connect', () => { + log.debug('【cltSocket connect】') + }) + cltSocket.on('connectionAttempt', (ip, port, family) => { + log.debug(`【cltSocket connectionAttempt】${ip}:${port}, family:`, family) + }) + cltSocket.on('connectionAttemptFailed', (ip, port, family) => { + log.debug(`【cltSocket connectionAttemptFailed】${ip}:${port}, family:`, family) + }) + cltSocket.on('connectionAttemptTimeout', (ip, port, family) => { + log.debug(`【cltSocket connectionAttemptTimeout】${ip}:${port}, family:`, family) + }) + cltSocket.on('data', (data) => { + log.debug('【cltSocket data】') + }) + cltSocket.on('drain', () => { + log.debug('【cltSocket drain】') + }) + cltSocket.on('end', () => { + log.debug('【cltSocket end】') + }) + // cltSocket.on('lookup', (err, address, family, host) => { + // }) + cltSocket.on('ready', () => { + log.debug('【cltSocket ready】') + }) + } + + // --------------------------------------------------------------------------------------------------- + const options = { port, host: hostname, @@ -69,8 +112,10 @@ function connect (req, cltSocket, head, hostname, port, dnsConfig = null, isDire options.lookup = dnsLookup.createLookupFunc(null, dns, 'connect', hostport, isDnsIntercept) } } + // 代理连接事件监听 const proxySocket = net.connect(options, () => { if (!isDirect) log.info('Proxy connect start:', hostport) + else log.debug('Direct connect start:', hostport) cltSocket.write('HTTP/1.1 200 Connection Established\r\n' + 'Proxy-agent: dev-sidecar\r\n' + @@ -80,12 +125,6 @@ function connect (req, cltSocket, head, hostname, port, dnsConfig = null, isDire cltSocket.pipe(proxySocket) }) - cltSocket.on('timeout', (e) => { - log.error(`cltSocket timeout: ${hostport}, errorMsg: ${e.message}`) - }) - cltSocket.on('error', (e) => { - log.error(`cltSocket error: ${hostport}, errorMsg: ${e.message}`) - }) proxySocket.on('timeout', () => { const cost = new Date() - start const errorMsg = `${isDirect ? '直连' : '代理连接'}超时: ${hostport}, cost: ${cost} ms` @@ -103,7 +142,7 @@ function connect (req, cltSocket, head, hostname, port, dnsConfig = null, isDire // 连接失败,可能被GFW拦截,或者服务端拥挤 const cost = new Date() - start const errorMsg = `${isDirect ? '直连' : '代理连接'}失败: ${hostport}, cost: ${cost} ms, errorMsg: ${e.message}` - log.error(errorMsg) + log.error(`${errorMsg}\r\n`, e) cltSocket.destroy() @@ -113,6 +152,39 @@ function connect (req, cltSocket, head, hostname, port, dnsConfig = null, isDire log.error(`记录ip失败次数,用于优选ip! hostname: ${hostname}, ip: ${ip}, reason: ${errorMsg}, dns: ${dns.name}`) } }) + + if (printDebugLog) { + proxySocket.on('close', (hadError) => { + log.debug('【proxySocket close】', hadError) + }) + proxySocket.on('connect', () => { + log.debug('【proxySocket connect】') + }) + proxySocket.on('connectionAttempt', (ip, port, family) => { + log.debug(`【proxySocket connectionAttempt】${ip}:${port}, family:`, family) + }) + proxySocket.on('connectionAttemptFailed', (ip, port, family) => { + log.debug(`【proxySocket connectionAttemptFailed】${ip}:${port}, family:`, family) + }) + proxySocket.on('connectionAttemptTimeout', (ip, port, family) => { + log.debug(`【proxySocket connectionAttemptTimeout】${ip}:${port}, family:`, family) + }) + proxySocket.on('data', (data) => { + log.debug('【proxySocket data】') + }) + proxySocket.on('drain', () => { + log.debug('【proxySocket drain】') + }) + proxySocket.on('end', () => { + log.debug('【proxySocket end】') + }) + // proxySocket.on('lookup', (err, address, family, host) => { + // }) + proxySocket.on('ready', () => { + log.debug('【proxySocket ready】') + }) + } + return proxySocket } catch (e) { log.error(`${isDirect ? '直连' : '代理连接'}错误: ${hostport}, error:`, e)