Skip to content

Commit

Permalink
optimize: dns相关日志优化。
Browse files Browse the repository at this point in the history
  • Loading branch information
wangliang181230 committed Dec 5, 2024
1 parent ff17de8 commit cbc3f00
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
7 changes: 4 additions & 3 deletions packages/mitmproxy/src/lib/dns/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class IpCache extends DynamicChoice {
}

module.exports = class BaseDNS {
constructor () {
constructor (dnsName) {
this.dnsName = dnsName
this.cache = new LRUCache({
maxSize: cacheSize,
sizeCalculation: () => {
Expand Down Expand Up @@ -60,11 +61,11 @@ module.exports = class BaseDNS {
ipList.push(hostname) // 把原域名加入到统计里去

ipCache.setBackupList(ipList)
log.info(`[DNS]: ${hostname}${ipCache.value} (${new Date() - t} ms), ipList: ${JSON.stringify(ipList)}, ipCache:`, JSON.stringify(ipCache))
log.info(`[DNS '${this.dnsName}']: ${hostname}${ipCache.value} (${new Date() - t} ms), ipList: ${JSON.stringify(ipList)}, ipCache:`, JSON.stringify(ipCache))

return ipCache.value
} catch (error) {
log.error(`[DNS] cannot resolve hostname ${hostname}, error:`, error)
log.error(`[DNS '${this.dnsName}'] cannot resolve hostname ${hostname}, error:`, error)
return hostname
}
}
Expand Down
13 changes: 7 additions & 6 deletions packages/mitmproxy/src/lib/dns/https.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ function mapToList (ipMap) {
}

module.exports = class DNSOverHTTPS extends BaseDNS {
constructor (dnsServer, preSetIpList) {
super()
constructor (dnsName, dnsServer, preSetIpList) {
super(dnsName)
this.dnsServer = dnsServer
this.preSetIpList = preSetIpList
}
Expand All @@ -44,20 +44,21 @@ module.exports = class DNSOverHTTPS extends BaseDNS {
const start = new Date()
try {
const result = await dohQueryAsync({ url: this.dnsServer }, [{ type: 'A', name: hostname }])
const cost = new Date() - start
if (result.answers.length === 0) {
// 说明没有获取到ip
log.info('该域名没有ip地址解析:', hostname, ', cost:', (new Date() - start), 'ms')
log.info(`DNS '${this.dnsName}' 没有该域名的IP地址: ${hostname}, cost: ${cost} ms`)
return []
}
const ret = result.answers.filter(item => item.type === 'A').map(item => item.data)
if (ret.length === 0) {
log.info('该域名没有IPv4地址解析:', hostname, ', cost:', (new Date() - start), 'ms')
log.info(`DNS '${this.dnsName}' 没有该域名的IPv4地址: ${hostname}, cost: ${cost} ms`)
} else {
log.info('获取到域名地址:', hostname, JSON.stringify(ret), ', cost:', (new Date() - start), 'ms')
log.info(`DNS '${this.dnsName}' 获取到该域名的IPv4地址: ${hostname} ${JSON.stringify(ret)}, cost: ${cost} ms`)
}
return ret
} catch (e) {
log.warn('DNS query error:', hostname, ', dns:', this.dnsServer, ', cost:', (new Date() - start), 'ms, error:', e)
log.warn(`DNS query error: ${hostname}, dns: ${this.dnsName}, dnsServer: ${this.dnsServer}, cost: ${new Date() - start} ms, error:`, e)
return []
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/mitmproxy/src/lib/dns/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ module.exports = {
const conf = dnsProviders[provider]

if (conf.type === 'ipaddress') {
dnsMap[provider] = new DNSOverIpAddress(conf.server)
dnsMap[provider] = new DNSOverIpAddress(provider)
} else if (conf.type === 'https') {
dnsMap[provider] = new DNSOverHTTPS(conf.server, preSetIpList)
dnsMap[provider] = new DNSOverHTTPS(provider, conf.server, preSetIpList)
} else {
dnsMap[provider] = new DNSOverTLS(conf.server)
dnsMap[provider] = new DNSOverTLS(provider)
}

// 设置DNS名称到name属性中
Expand Down
2 changes: 1 addition & 1 deletion packages/mitmproxy/src/lib/speed/SpeedTester.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class SpeedTester {
for (const dnsKey in dnsMap) {
const dns = dnsMap[dnsKey]
const one = this.getFromOneDns(dns).then((ipList) => {
if (ipList) {
if (ipList && ipList.length > 0) {
for (const ip of ipList) {
ips[ip] = { dns: ipList.isPreSet === true ? '预设IP' : dnsKey }
}
Expand Down

0 comments on commit cbc3f00

Please sign in to comment.