Skip to content

Commit

Permalink
optimize: DNS相关代码细节优化。
Browse files Browse the repository at this point in the history
  • Loading branch information
wangliang181230 committed Oct 21, 2024
1 parent c883327 commit 9fc844e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
12 changes: 8 additions & 4 deletions packages/mitmproxy/src/lib/dns/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,28 @@ module.exports = {

// 设置DNS名称到name属性中
dnsMap[provider].name = provider
dnsMap[provider].type = conf.type
}
return dnsMap
},
hasDnsLookup (dnsConfig, hostname) {
let providerName = matchUtil.matchHostname(dnsConfig.mapping, hostname, 'get dns providerName')

// usa已重命名为cloudflare,以下为向下兼容处理
if (providerName === 'usa') {
if (providerName === 'usa' && dnsConfig.providers[providerName] == null) {
providerName = 'cloudflare'
}

// 如果为空,尝试从预设IP中匹配,如果配置过预设IP,则随便
if (providerName == null) {
if (providerName == null || dnsConfig.providers[providerName] == null) {
const hostnamePreSetIpList = matchUtil.matchHostname(dnsConfig.preSetIpList, hostname, 'matched preSetIpList')
if (hostnamePreSetIpList) {
for (const name in dnsConfig.providers) {
log.debug(`当前域名未配置过DNS,但配置了预设IP,现返回DNS '${name}' 作为预设IP的使用工具,hostname: ${hostname}, preSetIpList:`, hostnamePreSetIpList)
return dnsConfig.providers[name]
const provider = dnsConfig.providers[name]
if (provider.type === 'https') {
log.debug(`当前域名未配置过DNS,但配置了预设IP,现返回DNS '${name}' 作为预设IP的使用工具,hostname: ${hostname}, preSetIpList:`, hostnamePreSetIpList)
return dnsConfig.providers[name]
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ module.exports = function createRequestHandler (createIntercepts, middlewares, e
}
if (dns) {
rOptions.lookup = dnsLookup.createLookupFunc(res, dns, 'request url', url, isDnsIntercept)
log.debug(`域名 ${rOptions.hostname} DNS: ${dns.name}`)
} else {
log.debug(`域名 ${rOptions.hostname} 在dns中未配置`)
}
}

Expand Down
20 changes: 7 additions & 13 deletions packages/mitmproxy/src/utils/util.match.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ function domainRegexply (target) {
}

function domainMapRegexply (hostMap) {
const regexpMap = {}
const origin = {} // 用于快速匹配,见matchHostname、matchHostnameAll方法
if (hostMap == null) {
return regexpMap
return { origin: {} }
}
const regexpMap = {}
const origin = {} // 用于快速匹配,见matchHostname、matchHostnameAll方法
lodash.each(hostMap, (value, domain) => {
if (domain.indexOf('*') >= 0 || domain[0] === '^') {
const regDomain = domain[0] !== '^' ? domainRegexply(domain) : domain
Expand Down Expand Up @@ -127,25 +127,19 @@ function matchHostnameAll (hostMap, hostname, action) {
let value

// 通配符匹配 或 正则表达式匹配(优先级:1,最低)
for (const target in hostMap) {
if (target === 'origin') {
for (const regexp in hostMap) {
if (regexp === 'origin') {
continue
}

// if (target.indexOf('*') < 0 && target[0] !== '^') {
// continue // 不是通配符匹配串,也不是正则表达式,跳过
// }

// 如果是通配符匹配串,转换为正则表达式
let regexp = target
// if (target[0] !== '^') {
// regexp = domainRegexply(regexp)
// }

// 正则表达式匹配
if (hostname.match(regexp)) {
value = hostMap[target]
log.debug(`matchHostname-one: ${action}: '${hostname}' -> { "${target}": ${JSON.stringify(value)} }`)
value = hostMap[regexp]
log.debug(`matchHostname-one: ${action}: '${hostname}' -> { "${regexp}": ${JSON.stringify(value)} }`)
values = merge(values, value)
}
}
Expand Down

0 comments on commit 9fc844e

Please sign in to comment.