DNS查询失败的日志优化,避免真正的异常丢失。
parent
52805fe313
commit
a141c9e920
|
@ -124,10 +124,18 @@ module.exports = class BaseDNS {
|
|||
|
||||
async _lookup (hostname) {
|
||||
const start = Date.now()
|
||||
|
||||
let response
|
||||
try {
|
||||
// 执行DNS查询
|
||||
log.debug(`[DNS-over-${this.dnsType} '${this.dnsName}'] query start: ${hostname}`)
|
||||
const response = await this._doDnsQuery(hostname)
|
||||
response = await this._doDnsQuery(hostname, 'A', start)
|
||||
} catch {
|
||||
// 异常日志在 _doDnsQuery已经打印过,这里就不再打印了
|
||||
return []
|
||||
}
|
||||
|
||||
try {
|
||||
const cost = Date.now() - start
|
||||
log.debug(`[DNS-over-${this.dnsType} '${this.dnsName}'] query end: ${hostname}, cost: ${cost} ms, response:`, response)
|
||||
|
||||
|
@ -145,18 +153,19 @@ module.exports = class BaseDNS {
|
|||
|
||||
return ret
|
||||
} catch (e) {
|
||||
log.error(`[DNS-over-${this.dnsType} '${this.dnsName}'] DNS query error, hostname: ${hostname}${this.dnsServer ? `, dnsServer: ${this.dnsServer}` : ''}${this.dnsServerPort ? `:${this.dnsServerPort}` : ''}, cost: ${Date.now() - start} ms, error:`, e)
|
||||
log.error(`[DNS-over-${this.dnsType} '${this.dnsName}'] 解读响应失败,response:`, response, ', error:', e)
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
_doDnsQuery (hostname, type) {
|
||||
_doDnsQuery (hostname, type = 'A', start) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// 设置超时任务
|
||||
let isOver = false
|
||||
const timeout = 6000
|
||||
const timeoutId = setTimeout(() => {
|
||||
if (!isOver) {
|
||||
log.error(`[DNS-over-${this.dnsType} '${this.dnsName}'] DNS查询超时, hostname: ${hostname}, type: ${type}, ${this.dnsServer ? `, dnsServer: ${this.dnsServer}` : ''}${this.dnsServerPort ? `:${this.dnsServerPort}` : ''}, cost: ${Date.now() - start} ms`)
|
||||
reject(new Error('DNS查询超时'))
|
||||
}
|
||||
}, timeout)
|
||||
|
@ -171,11 +180,17 @@ module.exports = class BaseDNS {
|
|||
.catch((e) => {
|
||||
isOver = true
|
||||
clearTimeout(timeoutId)
|
||||
if (e.message === 'DNS查询超时') {
|
||||
log.error(`[DNS-over-${this.dnsType} '${this.dnsName}'] DNS查询超时. hostname: ${hostname}, type: ${type}, ${this.dnsServer ? `, dnsServer: ${this.dnsServer}` : ''}${this.dnsServerPort ? `:${this.dnsServerPort}` : ''}, cost: ${Date.now() - start} ms`)
|
||||
} else {
|
||||
log.error(`[DNS-over-${this.dnsType} '${this.dnsName}'] DNS查询错误, hostname: ${hostname}, type: ${type}${this.dnsServer ? `, dnsServer: ${this.dnsServer}` : ''}${this.dnsServerPort ? `:${this.dnsServerPort}` : ''}, cost: ${Date.now() - start} ms, error:`, e)
|
||||
}
|
||||
reject(e)
|
||||
})
|
||||
} catch (e) {
|
||||
isOver = true
|
||||
clearTimeout(timeoutId)
|
||||
log.error(`[DNS-over-${this.dnsType} '${this.dnsName}'] DNS查询异常, hostname: ${hostname}, type: ${type}${this.dnsServer ? `, dnsServer: ${this.dnsServer}` : ''}${this.dnsServerPort ? `:${this.dnsServerPort}` : ''}, cost: ${Date.now() - start} ms, error:`, e)
|
||||
reject(e)
|
||||
}
|
||||
})
|
||||
|
|
|
@ -11,24 +11,35 @@ const preSetIpList = matchUtil.domainMapRegexply({
|
|||
|
||||
// 境外DNS测试
|
||||
const dnsProviders = dns.initDNS({
|
||||
// udp
|
||||
cloudflareUdp: {
|
||||
server: 'udp://1.1.1.1',
|
||||
},
|
||||
quad9Udp: {
|
||||
server: 'udp://9.9.9.9',
|
||||
},
|
||||
|
||||
// tcp
|
||||
cloudflareTcp: {
|
||||
server: 'tcp://1.1.1.1',
|
||||
},
|
||||
quad9Tcp: {
|
||||
server: 'tcp://9.9.9.9',
|
||||
},
|
||||
|
||||
// https
|
||||
cloudflare: {
|
||||
type: 'https',
|
||||
server: 'https://1.1.1.1/dns-query',
|
||||
cacheSize: 1000,
|
||||
},
|
||||
quad9: {
|
||||
server: 'https://9.9.9.9/dns-query',
|
||||
cacheSize: 1000,
|
||||
forSNI: true,
|
||||
},
|
||||
rubyfish: {
|
||||
server: 'https://rubyfish.cn/dns-query',
|
||||
cacheSize: 1000,
|
||||
},
|
||||
py233: {
|
||||
server: ' https://i.233py.com/dns-query',
|
||||
cacheSize: 1000,
|
||||
},
|
||||
|
||||
// tls
|
||||
|
@ -36,12 +47,10 @@ const dnsProviders = dns.initDNS({
|
|||
type: 'tls',
|
||||
server: '1.1.1.1',
|
||||
servername: 'cloudflare-dns.com',
|
||||
cacheSize: 1000,
|
||||
},
|
||||
quad9TLS: {
|
||||
server: 'tls://9.9.9.9',
|
||||
servername: 'dns.quad9.net',
|
||||
cacheSize: 1000,
|
||||
},
|
||||
}, preSetIpList)
|
||||
|
||||
|
@ -76,6 +85,34 @@ console.log('\n\n')
|
|||
assert.strictEqual(ip, noPresetHostname) // 未预设IP,等于域名自己
|
||||
|
||||
|
||||
console.log('\n--------------- test udp ---------------\n')
|
||||
ip = await dnsProviders.cloudflareUdp.lookup(hasPresetHostname)
|
||||
assert.strictEqual(ip, presetIp) // test preset
|
||||
console.log('\n\n')
|
||||
|
||||
assert.strictEqual(dnsProviders.cloudflareUdp.dnsType, 'UDP')
|
||||
ip = await dnsProviders.cloudflareUdp.lookup(hostname1)
|
||||
console.log(`===> test cloudflare: ${hostname1} ->`, ip, '\n\n')
|
||||
|
||||
assert.strictEqual(dnsProviders.quad9Udp.dnsType, 'UDP')
|
||||
ip = await dnsProviders.quad9Udp.lookup(hostname1)
|
||||
console.log(`===> test quad9: ${hostname1} ->`, ip, '\n\n')
|
||||
|
||||
|
||||
console.log('\n--------------- test tcp ---------------\n')
|
||||
ip = await dnsProviders.cloudflareTcp.lookup(hasPresetHostname)
|
||||
assert.strictEqual(ip, presetIp) // test preset
|
||||
console.log('\n\n')
|
||||
|
||||
assert.strictEqual(dnsProviders.cloudflareTcp.dnsType, 'TCP')
|
||||
ip = await dnsProviders.cloudflareTcp.lookup(hostname1)
|
||||
console.log(`===> test cloudflare: ${hostname1} ->`, ip, '\n\n')
|
||||
|
||||
assert.strictEqual(dnsProviders.quad9Tcp.dnsType, 'TCP')
|
||||
ip = await dnsProviders.quad9Tcp.lookup(hostname1)
|
||||
console.log(`===> test quad9: ${hostname1} ->`, ip, '\n\n')
|
||||
|
||||
|
||||
console.log('\n--------------- test https ---------------\n')
|
||||
ip = await dnsProviders.cloudflare.lookup(hasPresetHostname)
|
||||
assert.strictEqual(ip, presetIp) // test preset
|
||||
|
|
Loading…
Reference in New Issue