Skip to content

Commit

Permalink
optimize: 优化预设IP相关代码。
Browse files Browse the repository at this point in the history
  • Loading branch information
wangliang181230 committed Nov 1, 2024
1 parent f7e5d58 commit e3e7710
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 24 deletions.
2 changes: 1 addition & 1 deletion packages/gui/src/view/pages/server.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 19 additions & 18 deletions packages/mitmproxy/src/lib/dns/index.js
Original file line number Diff line number Diff line change
@@ -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]

Expand All @@ -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]
}
}
}
39 changes: 39 additions & 0 deletions packages/mitmproxy/src/lib/dns/preset.js
Original file line number Diff line number Diff line change
@@ -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 []
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.`)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/mitmproxy/src/lib/proxy/mitmproxy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down
2 changes: 1 addition & 1 deletion packages/mitmproxy/src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down

0 comments on commit e3e7710

Please sign in to comment.