From d69c25de2659ba5e357e01ab9672190334062ca7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=89=AF?= <841369634@qq.com> Date: Fri, 29 Mar 2024 11:46:10 +0800 Subject: [PATCH] =?UTF-8?q?bugfix:=20=E8=A7=A3=E6=9E=90IPv6=E6=97=B6?= =?UTF-8?q?=EF=BC=8Chostname=E5=92=8Cport=E6=9C=89=E8=AF=AF=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E4=BF=AE=E5=A4=8D=E3=80=82=20(#284)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mitmproxy/src/lib/proxy/common/util.js | 33 ++++++++++++- packages/mitmproxy/test/utilTest.js | 49 +++++++++++++++++++ 2 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 packages/mitmproxy/test/utilTest.js diff --git a/packages/mitmproxy/src/lib/proxy/common/util.js b/packages/mitmproxy/src/lib/proxy/common/util.js index 21d59416..ae383999 100644 --- a/packages/mitmproxy/src/lib/proxy/common/util.js +++ b/packages/mitmproxy/src/lib/proxy/common/util.js @@ -19,6 +19,29 @@ let socketId = 0 let httpsOverHttpAgent, httpOverHttpsAgent, httpsOverHttpsAgent +util.parseHostnameAndPort = (host, defaultPort) => { + let arr = host.match(/^(\[[^\]]+\])(?::(\d+))?$/) // 尝试解析IPv6 + if (arr) { + arr = arr.slice(1) + if (arr[1]) { + arr[1] = parseInt(arr[1], 10) + } + } else { + arr = host.split(':') + if (arr.length > 1) { + arr[1] = parseInt(arr[1], 10) + } + } + + if (defaultPort > 0 && (arr.length === 1 || arr[1] === undefined)) { + arr[1] = defaultPort + } else if (arr.length === 2 && arr[1] === undefined) { + arr.pop() + } + + return arr +} + util.getOptionsFromRequest = (req, ssl, externalProxy = null) => { // eslint-disable-next-line node/no-deprecated-api const urlObject = url.parse(req.url) @@ -57,16 +80,22 @@ util.getOptionsFromRequest = (req, ssl, externalProxy = null) => { agent = util.getTunnelAgent(protocol === 'https:', externalProxyUrl) } + // 解析host和port + const arr = util.parseHostnameAndPort(urlObject.host) + + // 初始化options const options = { protocol: protocol, - hostname: req.headers.host.split(':')[0], + hostname: arr[0], method: req.method, - port: req.headers.host.split(':')[1] || defaultPort, + port: arr[1] || defaultPort, path: urlObject.path, headers: req.headers, agent: agent } + log.info('options:', options) + // eslint-disable-next-line node/no-deprecated-api if (protocol === 'http:' && externalProxyUrl && (url.parse(externalProxyUrl)).protocol === 'http:') { // eslint-disable-next-line node/no-deprecated-api diff --git a/packages/mitmproxy/test/utilTest.js b/packages/mitmproxy/test/utilTest.js new file mode 100644 index 00000000..9366b874 --- /dev/null +++ b/packages/mitmproxy/test/utilTest.js @@ -0,0 +1,49 @@ +const util = require('../src/lib/proxy/common/util') + +let arr + +arr = util.parseHostnameAndPort('www.baidu.com') +console.log(arr) +console.log(arr.length === 1) // true +console.log(arr[0] === 'www.baidu.com') // true + +arr = util.parseHostnameAndPort('www.baidu.com', 80) +console.log(arr) +console.log(arr.length === 2) // true +console.log(arr[0] === 'www.baidu.com') // true +console.log(arr[1] === 80) // true + +arr = util.parseHostnameAndPort('www.baidu.com:8080') +console.log(arr) +console.log(arr.length === 2) // true +console.log(arr[0] === 'www.baidu.com') // true +console.log(arr[1] === 8080) // true + +arr = util.parseHostnameAndPort('www.baidu.com:8080', 8080) +console.log(arr) +console.log(arr.length === 2) // true +console.log(arr[0] === 'www.baidu.com') // true +console.log(arr[1] === 8080) // true + +arr = util.parseHostnameAndPort('[2001:abcd::1]') +console.log(arr) +console.log(arr.length === 1) // true +console.log(arr[0] === '[2001:abcd::1]') // ture + +arr = util.parseHostnameAndPort('[2001:abcd::1]', 80) +console.log(arr) +console.log(arr.length === 2) // true +console.log(arr[0] === '[2001:abcd::1]') // ture +console.log(arr[1] === 80) // ture + +arr = util.parseHostnameAndPort('[2001:abcd::1]:8080') +console.log(arr) +console.log(arr.length === 2) // true +console.log(arr[0] === '[2001:abcd::1]') // true +console.log(arr[1] === 8080) // ture + +arr = util.parseHostnameAndPort('[2001:abcd::1]:8080', 8080) +console.log(arr) +console.log(arr.length === 2) // true +console.log(arr[0] === '[2001:abcd::1]') // true +console.log(arr[1] === 8080) // ture