dns和tester相关日志优化。

pull/333/head
王良 2024-08-16 14:49:14 +08:00
parent c5d123ed24
commit 23c8b0c5ae
5 changed files with 25 additions and 17 deletions

View File

@ -8,11 +8,17 @@ module.exports = {
const dnsMap = {}
for (const provider in dnsProviders) {
const conf = dnsProviders[provider]
if (conf.type === 'ipaddress') {
dnsMap[provider] = new DNSOverIpAddress(conf.server)
continue
} else if (conf.type === 'https') {
dnsMap[provider] = new DNSOverHTTPS(conf.server, preSetIpList)
} else {
dnsMap[provider] = new DNSOverTLS(conf.server)
}
dnsMap[provider] = conf.type === 'https' ? new DNSOverHTTPS(conf.server, preSetIpList) : new DNSOverTLS(conf.server)
// 设置DNS名称到name属性中
dnsMap[provider].name = provider
}
return dnsMap
},

View File

@ -34,6 +34,7 @@ module.exports = function createConnectHandler (sslConnectInterceptor, middlewar
// eslint-disable-next-line node/no-deprecated-api
const { hostname, port } = url.parse(`https://${req.url}`)
if (isSslConnect(sslConnectInterceptors, req, cltSocket, head)) {
// 需要拦截代替目标服务器让客户端连接DS在本地启动的代理服务
fakeServerCenter.getServerPromise(hostname, port).then((serverObj) => {
log.info('--- fakeServer connect', hostname)
connect(req, cltSocket, head, localIP, serverObj.port)
@ -41,7 +42,7 @@ module.exports = function createConnectHandler (sslConnectInterceptor, middlewar
log.error('getServerPromise', e)
})
} else {
log.info('不拦截请求:', hostname)
log.info(`未匹配到任何 sslConnectInterceptors不拦截请求直接连接目标服务器: ${hostname}:${port}`)
connect(req, cltSocket, head, hostname, port, dnsConfig/*, sniRegexpMap */)
}
}
@ -66,19 +67,20 @@ function connect (req, cltSocket, head, hostname, port, dnsConfig/* , sniRegexpM
options.lookup = (hostname, options, callback) => {
const tester = speedTest.getSpeedTester(hostname)
if (tester) {
const ip = tester.pickFastAliveIp()
if (ip) {
log.info(`-----${hostname} use alive ip:${ip}-----`)
callback(null, ip, 4)
const aliveIpObj = tester.pickFastAliveIpObj()
if (aliveIpObj) {
log.info(`----- connect: ${hostname}:${port}, use alive ip from dns '${aliveIpObj.dns}': ${aliveIpObj.host} -----`)
callback(null, aliveIpObj.host, 4)
return
}
}
dns.lookup(hostname).then(ip => {
isDnsIntercept = { dns, hostname, ip }
if (ip !== hostname) {
log.info(`-----${hostname} use ip:${ip}-----`)
log.info(`---- connect: ${hostname}:${port}, use ip from dns '${dns.name}': ${ip} ----`)
callback(null, ip, 4)
} else {
log.info(`----- connect: ${hostname}:${port}, use hostname: ${hostname} -----`)
defaultDns.lookup(hostname, options, callback)
}
})

View File

@ -118,17 +118,17 @@ module.exports = function createRequestHandler (createIntercepts, middlewares, e
rOptions.lookup = (hostname, options, callback) => {
const tester = speedTest.getSpeedTester(hostname)
if (tester) {
const ip = tester.pickFastAliveIp()
if (ip) {
log.info(`----- hostname: ${hostname}, use alive ip: ${ip} -----`)
callback(null, ip, 4)
const aliveIpObj = tester.pickFastAliveIpObj()
if (aliveIpObj) {
log.info(`----- request url: ${url}, use alive ip from dns '${aliveIpObj.dns}': ${aliveIpObj.host} -----`)
callback(null, aliveIpObj.host, 4)
return
}
}
dns.lookup(hostname).then(ip => {
isDnsIntercept = { dns, hostname, ip }
if (ip !== hostname) {
log.info(`---- request url: ${url}, use ip: ${ip} ----`)
log.info(`---- request url: ${url}, use ip from dns '${dns.name}': ${ip} ----`)
callback(null, ip, 4)
} else {
log.info(`---- request url: ${url}, use hostname: ${hostname} ----`)

View File

@ -63,7 +63,7 @@ module.exports = class FakeServersCenter {
SNICallback: (hostname, done) => {
(async () => {
const certObj = await this.certAndKeyContainer.getCertPromise(hostname, port)
log.info('sni callback:', hostname)
log.info(`sni callback: ${hostname}:${port}`)
done(null, tls.createSecureContext({
key: pki.privateKeyToPem(certObj.key),
cert: pki.certificateToPem(certObj.cert)

View File

@ -21,13 +21,13 @@ class SpeedTester {
this.test()
}
pickFastAliveIp () {
pickFastAliveIpObj () {
this.touch()
if (this.alive.length === 0) {
this.test()
this.test() // 异步
return null
}
return this.alive[0].host
return this.alive[0]
}
touch () {