optimize: dns相关日志优化。

pull/415/head
王良 2024-12-05 10:57:39 +08:00
parent ff17de8f5e
commit cbc3f00964
4 changed files with 15 additions and 13 deletions

View File

@ -22,7 +22,8 @@ class IpCache extends DynamicChoice {
} }
module.exports = class BaseDNS { module.exports = class BaseDNS {
constructor () { constructor (dnsName) {
this.dnsName = dnsName
this.cache = new LRUCache({ this.cache = new LRUCache({
maxSize: cacheSize, maxSize: cacheSize,
sizeCalculation: () => { sizeCalculation: () => {
@ -60,11 +61,11 @@ module.exports = class BaseDNS {
ipList.push(hostname) // 把原域名加入到统计里去 ipList.push(hostname) // 把原域名加入到统计里去
ipCache.setBackupList(ipList) 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 return ipCache.value
} catch (error) { } catch (error) {
log.error(`[DNS] cannot resolve hostname ${hostname}, error:`, error) log.error(`[DNS '${this.dnsName}'] cannot resolve hostname ${hostname}, error:`, error)
return hostname return hostname
} }
} }

View File

@ -18,8 +18,8 @@ function mapToList (ipMap) {
} }
module.exports = class DNSOverHTTPS extends BaseDNS { module.exports = class DNSOverHTTPS extends BaseDNS {
constructor (dnsServer, preSetIpList) { constructor (dnsName, dnsServer, preSetIpList) {
super() super(dnsName)
this.dnsServer = dnsServer this.dnsServer = dnsServer
this.preSetIpList = preSetIpList this.preSetIpList = preSetIpList
} }
@ -44,20 +44,21 @@ module.exports = class DNSOverHTTPS extends BaseDNS {
const start = new Date() const start = new Date()
try { try {
const result = await dohQueryAsync({ url: this.dnsServer }, [{ type: 'A', name: hostname }]) const result = await dohQueryAsync({ url: this.dnsServer }, [{ type: 'A', name: hostname }])
const cost = new Date() - start
if (result.answers.length === 0) { if (result.answers.length === 0) {
// 说明没有获取到ip // 说明没有获取到ip
log.info('该域名没有ip地址解析:', hostname, ', cost:', (new Date() - start), 'ms') log.info(`DNS '${this.dnsName}' 没有该域名的IP地址: ${hostname}, cost: ${cost} ms`)
return [] return []
} }
const ret = result.answers.filter(item => item.type === 'A').map(item => item.data) const ret = result.answers.filter(item => item.type === 'A').map(item => item.data)
if (ret.length === 0) { if (ret.length === 0) {
log.info('该域名没有IPv4地址解析:', hostname, ', cost:', (new Date() - start), 'ms') log.info(`DNS '${this.dnsName}' 没有该域名的IPv4地址: ${hostname}, cost: ${cost} ms`)
} else { } 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 return ret
} catch (e) { } 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 [] return []
} }
} }

View File

@ -13,11 +13,11 @@ module.exports = {
const conf = dnsProviders[provider] const conf = dnsProviders[provider]
if (conf.type === 'ipaddress') { if (conf.type === 'ipaddress') {
dnsMap[provider] = new DNSOverIpAddress(conf.server) dnsMap[provider] = new DNSOverIpAddress(provider)
} else if (conf.type === 'https') { } else if (conf.type === 'https') {
dnsMap[provider] = new DNSOverHTTPS(conf.server, preSetIpList) dnsMap[provider] = new DNSOverHTTPS(provider, conf.server, preSetIpList)
} else { } else {
dnsMap[provider] = new DNSOverTLS(conf.server) dnsMap[provider] = new DNSOverTLS(provider)
} }
// 设置DNS名称到name属性中 // 设置DNS名称到name属性中

View File

@ -62,7 +62,7 @@ class SpeedTester {
for (const dnsKey in dnsMap) { for (const dnsKey in dnsMap) {
const dns = dnsMap[dnsKey] const dns = dnsMap[dnsKey]
const one = this.getFromOneDns(dns).then((ipList) => { const one = this.getFromOneDns(dns).then((ipList) => {
if (ipList) { if (ipList && ipList.length > 0) {
for (const ip of ipList) { for (const ip of ipList) {
ips[ip] = { dns: ipList.isPreSet === true ? '预设IP' : dnsKey } ips[ip] = { dns: ipList.isPreSet === true ? '预设IP' : dnsKey }
} }