optimize: 优化预设IP相关代码。

pull/384/head
王良 2024-11-01 22:05:56 +08:00
parent f7e5d58af1
commit e3e7710ee4
7 changed files with 64 additions and 24 deletions

View File

@ -260,7 +260,7 @@ export default {
if (!this.config || !this.config.server || !this.config.server.dns || !this.config.server.dns.providers) {
return options
}
_.forEach(this.config.server.dns.providers, (dnsConf, key) => {
_.forEach(this.config.server.dns.providers, (dnsConfig, key) => {
options.push({
value: key,
label: key

View File

@ -1,12 +1,14 @@
const DNSOverTLS = require('./tls.js')
const DNSOverHTTPS = require('./https.js')
const DNSOverIpAddress = require('./ipaddress.js')
const DNSOverPreSetIpList = require('./preset.js')
const matchUtil = require('../../utils/util.match')
const log = require('../../utils/util.log')
module.exports = {
initDNS (dnsProviders, preSetIpList) {
const dnsMap = {}
// 创建普通的DNS
for (const provider in dnsProviders) {
const conf = dnsProviders[provider]
@ -22,32 +24,31 @@ module.exports = {
dnsMap[provider].name = provider
dnsMap[provider].type = conf.type
}
// 创建预设IP的DNS
dnsMap.PreSet = new DNSOverPreSetIpList(preSetIpList)
return dnsMap
},
hasDnsLookup (dnsConfig, hostname) {
let providerName = matchUtil.matchHostname(dnsConfig.mapping, hostname, 'get dns providerName')
let providerName = null
// usa已重命名为cloudflare以下为向下兼容处理
if (providerName === 'usa' && dnsConfig.providers[providerName] == null) {
providerName = 'cloudflare'
// 先匹配 预设IP配置
const hostnamePreSetIpList = matchUtil.matchHostname(dnsConfig.preSetIpList, hostname, 'matched preSetIpList')
if (hostnamePreSetIpList) {
return dnsConfig.dnsMap.PreSet
}
// 如果为空尝试从预设IP中匹配如果配置过预设IP则随便
if (providerName == null || dnsConfig.providers[providerName] == null) {
const hostnamePreSetIpList = matchUtil.matchHostname(dnsConfig.preSetIpList, hostname, 'matched preSetIpList')
if (hostnamePreSetIpList) {
for (const name in dnsConfig.providers) {
const provider = dnsConfig.providers[name]
if (provider.type === 'https') {
log.debug(`当前域名未配置过DNS但配置了预设IP现返回DNS '${name}' 作为预设IP的使用工具hostname: ${hostname}, preSetIpList:`, hostnamePreSetIpList)
return dnsConfig.providers[name]
}
}
}
// 再匹配 DNS映射配置
providerName = matchUtil.matchHostname(dnsConfig.mapping, hostname, 'get dns providerName')
// 由于DNS中的usa已重命名为cloudflare所以做以下处理为了向下兼容
if (providerName === 'usa' && dnsConfig.dnsMap.usa == null && dnsConfig.dnsMap.cloudflare != null) {
return dnsConfig.dnsMap.cloudflare
}
if (providerName) {
return dnsConfig.providers[providerName]
return dnsConfig.dnsMap[providerName]
}
}
}

View File

@ -0,0 +1,39 @@
const BaseDNS = require('./base')
const matchUtil = require('../../utils/util.match')
function mapToList (ipMap) {
const ipList = []
for (const key in ipMap) {
if (!ipMap[key]) continue
ipList.push(ipMap[key])
}
return ipList
}
module.exports = class DNSOverPreSetIpList extends BaseDNS {
constructor (preSetIpList) {
super()
this.preSetIpList = preSetIpList
this.name = 'PreSet'
this.type = 'PreSet'
}
async _lookup (hostname) {
// 获取当前域名的预设IP列表
let hostnamePreSetIpList = matchUtil.matchHostname(this.preSetIpList, hostname, 'matched preSetIpList')
if (hostnamePreSetIpList && (hostnamePreSetIpList.length > 0 || hostnamePreSetIpList.length === undefined)) {
if (hostnamePreSetIpList.length > 0) {
hostnamePreSetIpList = hostnamePreSetIpList.slice()
} else {
hostnamePreSetIpList = mapToList(hostnamePreSetIpList)
}
if (hostnamePreSetIpList.length > 0) {
return hostnamePreSetIpList
}
}
// 未预设当前域名的IP列表
return []
}
}

View File

@ -106,7 +106,7 @@ function connect (req, cltSocket, head, hostname, port, dnsConfig = null, isDire
host: hostname,
connectTimeout: 10000
}
if (dnsConfig && dnsConfig.providers) {
if (dnsConfig && dnsConfig.dnsMap) {
const dns = DnsUtil.hasDnsLookup(dnsConfig, hostname)
if (dns) {
options.lookup = dnsLookup.createLookupFunc(null, dns, 'connect', hostport, isDnsIntercept)

View File

@ -110,10 +110,10 @@ module.exports = function createRequestHandler (createIntercepts, middlewares, e
log.info('发起代理请求:', url, (rOptions.servername ? ', sni: ' + rOptions.servername : ''), ', headers:', jsonApi.stringify2(rOptions.headers))
const isDnsIntercept = {}
if (dnsConfig && dnsConfig.providers) {
if (dnsConfig && dnsConfig.dnsMap) {
let dns = DnsUtil.hasDnsLookup(dnsConfig, rOptions.hostname)
if (!dns && rOptions.servername) {
dns = dnsConfig.providers.quad9
dns = dnsConfig.dnsMap.quad9
if (dns) {
log.info(`域名 ${rOptions.hostname} 在dns中未配置但使用了 sni: ${rOptions.servername}, 必须使用dns现默认使用 'quad9' DNS.`)
}

View File

@ -41,7 +41,7 @@ module.exports = {
port = ~~port
const speedTestConfig = dnsConfig.speedTest
const dnsMap = dnsConfig.providers
const dnsMap = dnsConfig.dnsMap
if (speedTestConfig) {
const dnsProviders = speedTestConfig.dnsProviders
const map = {}

View File

@ -95,7 +95,7 @@ module.exports = (serverConfig) => {
port: serverConfig.port,
dnsConfig: {
preSetIpList,
providers: dnsUtil.initDNS(serverConfig.dns.providers, preSetIpList),
dnsMap: dnsUtil.initDNS(serverConfig.dns.providers, preSetIpList),
mapping: matchUtil.domainMapRegexply(dnsMapping),
speedTest: serverConfig.dns.speedTest
},