optimize: dns相关日志优化。
parent
ff17de8f5e
commit
cbc3f00964
|
@ -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: () => {
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
@ -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 []
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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属性中
|
||||
|
|
|
@ -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 }
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue