bugfix: 解析IPv6时,hostname和port有误的问题修复。 (#284)

pull/286/head
王良 2024-03-29 11:46:10 +08:00 committed by GitHub
parent 4c7be0dc71
commit d69c25de26
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 80 additions and 2 deletions

View File

@ -19,6 +19,29 @@ let socketId = 0
let httpsOverHttpAgent, httpOverHttpsAgent, httpsOverHttpsAgent 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) => { util.getOptionsFromRequest = (req, ssl, externalProxy = null) => {
// eslint-disable-next-line node/no-deprecated-api // eslint-disable-next-line node/no-deprecated-api
const urlObject = url.parse(req.url) const urlObject = url.parse(req.url)
@ -57,16 +80,22 @@ util.getOptionsFromRequest = (req, ssl, externalProxy = null) => {
agent = util.getTunnelAgent(protocol === 'https:', externalProxyUrl) agent = util.getTunnelAgent(protocol === 'https:', externalProxyUrl)
} }
// 解析host和port
const arr = util.parseHostnameAndPort(urlObject.host)
// 初始化options
const options = { const options = {
protocol: protocol, protocol: protocol,
hostname: req.headers.host.split(':')[0], hostname: arr[0],
method: req.method, method: req.method,
port: req.headers.host.split(':')[1] || defaultPort, port: arr[1] || defaultPort,
path: urlObject.path, path: urlObject.path,
headers: req.headers, headers: req.headers,
agent: agent agent: agent
} }
log.info('options:', options)
// eslint-disable-next-line node/no-deprecated-api // eslint-disable-next-line node/no-deprecated-api
if (protocol === 'http:' && externalProxyUrl && (url.parse(externalProxyUrl)).protocol === 'http:') { if (protocol === 'http:' && externalProxyUrl && (url.parse(externalProxyUrl)).protocol === 'http:') {
// eslint-disable-next-line node/no-deprecated-api // eslint-disable-next-line node/no-deprecated-api

View File

@ -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