bugfix: 1)`UDP`类型的DNS,并发调用时IP赋值混乱的问题修复;2)`UDP`和`TCP`类型的DNS连接对象未关闭的问题修复。
parent
fa06a0772e
commit
8ed5788ce2
|
@ -37,14 +37,16 @@ module.exports = class DNSOverTCP extends BaseDNS {
|
|||
tcpClient.write(Buffer.concat([lengthBuffer, packet]))
|
||||
})
|
||||
|
||||
tcpClient.on('data', (data) => {
|
||||
tcpClient.once('data', (data) => {
|
||||
const length = data.readUInt16BE(0)
|
||||
const response = dnsPacket.decode(data.subarray(2, 2 + length))
|
||||
resolve(response)
|
||||
tcpClient.end()
|
||||
})
|
||||
|
||||
tcpClient.on('error', (err) => {
|
||||
tcpClient.once('error', (err) => {
|
||||
reject(err)
|
||||
tcpClient.end()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
|
@ -3,8 +3,6 @@ const dnsPacket = require('dns-packet')
|
|||
const randi = require('random-int')
|
||||
const BaseDNS = require('./base')
|
||||
|
||||
const udpClient = dgram.createSocket('udp4')
|
||||
|
||||
const defaultPort = 53 // UDP类型的DNS服务默认端口号
|
||||
|
||||
module.exports = class DNSOverUDP extends BaseDNS {
|
||||
|
@ -12,6 +10,9 @@ module.exports = class DNSOverUDP extends BaseDNS {
|
|||
super(dnsName, 'UDP', cacheSize, preSetIpList)
|
||||
this.dnsServer = dnsServer
|
||||
this.dnsServerPort = Number.parseInt(dnsServerPort) || defaultPort
|
||||
|
||||
this.isIPv6 = dnsServer.includes(':') && dnsServer.includes('[') && dnsServer.includes(']')
|
||||
this.socketType = this.isIPv6 ? 'udp6' : 'udp4'
|
||||
}
|
||||
|
||||
_doDnsQuery (hostname) {
|
||||
|
@ -27,17 +28,19 @@ module.exports = class DNSOverUDP extends BaseDNS {
|
|||
}],
|
||||
})
|
||||
|
||||
// 发送 UDP 查询
|
||||
udpClient.send(packet, 0, packet.length, this.dnsServerPort, this.dnsServer, (err) => {
|
||||
if (err) {
|
||||
reject(err)
|
||||
}
|
||||
})
|
||||
|
||||
// 接收 UDP 响应
|
||||
udpClient.on('message', (msg) => {
|
||||
// 创建客户端
|
||||
const udpClient = dgram.createSocket(this.socketType, (msg, _rinfo) => {
|
||||
const response = dnsPacket.decode(msg)
|
||||
resolve(response)
|
||||
udpClient.close()
|
||||
})
|
||||
|
||||
// 发送 UDP 查询
|
||||
udpClient.send(packet, 0, packet.length, this.dnsServerPort, this.dnsServer, (err, _bytes) => {
|
||||
if (err) {
|
||||
reject(err)
|
||||
udpClient.close()
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
|
@ -106,9 +106,9 @@ let ip
|
|||
|
||||
console.log('\n--------------- test PreSet ---------------\n')
|
||||
ip = await dnsProviders.PreSet.lookup(presetHostname)
|
||||
assert.strictEqual(ip, presetIp) // test preset
|
||||
console.log('===> test PreSet:', ip, '\n\n')
|
||||
console.log('\n\n')
|
||||
assert.strictEqual(ip, presetIp) // test preset
|
||||
|
||||
|
||||
console.log('\n--------------- test https ---------------\n')
|
||||
|
@ -118,31 +118,31 @@ console.log('\n\n')
|
|||
|
||||
assert.strictEqual(dnsProviders.cloudflare.dnsType, 'HTTPS')
|
||||
// ip = await dnsProviders.cloudflare.lookup(hostname1)
|
||||
// console.log('===> test cloudflare:', ip, '\n\n')
|
||||
// console.log(`===> test cloudflare: ${hostname1} ->`, ip, '\n\n')
|
||||
|
||||
assert.strictEqual(dnsProviders.quad9.dnsType, 'HTTPS')
|
||||
// ip = await dnsProviders.quad9.lookup(hostname1)
|
||||
// console.log('===> test quad9:', ip, '\n\n')
|
||||
// console.log(`===> test quad9: ${hostname1} ->`, ip, '\n\n')
|
||||
|
||||
assert.strictEqual(dnsProviders.aliyun.dnsType, 'HTTPS')
|
||||
// ip = await dnsProviders.aliyun.lookup(hostname1)
|
||||
// console.log('===> test aliyun:', ip, '\n\n')
|
||||
ip = await dnsProviders.aliyun.lookup(hostname1)
|
||||
console.log(`===> test aliyun: ${hostname1} ->`, ip, '\n\n')
|
||||
|
||||
assert.strictEqual(dnsProviders.aliyun2.dnsType, 'HTTPS')
|
||||
// ip = await dnsProviders.aliyun2.lookup(hostname1)
|
||||
// console.log('===> test aliyun2:', ip, '\n\n')
|
||||
ip = await dnsProviders.aliyun2.lookup(hostname1)
|
||||
console.log(`===> test aliyun2: ${hostname1} ->`, ip, '\n\n')
|
||||
|
||||
assert.strictEqual(dnsProviders.safe360.dnsType, 'HTTPS')
|
||||
// ip = await dnsProviders.safe360.lookup(hostname1)
|
||||
// console.log('===> test safe360:', ip, '\n\n')
|
||||
ip = await dnsProviders.safe360.lookup(hostname1)
|
||||
console.log(`===> test safe360: ${hostname1} ->`, ip, '\n\n')
|
||||
|
||||
assert.strictEqual(dnsProviders.rubyfish.dnsType, 'HTTPS')
|
||||
// ip = await dnsProviders.rubyfish.lookup(hostname1)
|
||||
// console.log('===> test rubyfish:', ip, '\n\n')
|
||||
// console.log(`===> test rubyfish: ${hostname1} ->`, ip, '\n\n')
|
||||
|
||||
assert.strictEqual(dnsProviders.py233.dnsType, 'HTTPS')
|
||||
// ip = await dnsProviders.py233.lookup(hostname1)
|
||||
// console.log('===> test py233:', ip, '\n\n')
|
||||
// console.log(`===> test py233: ${hostname1} ->`, ip, '\n\n')
|
||||
|
||||
|
||||
console.log('\n--------------- test TLS ---------------\n')
|
||||
|
@ -152,23 +152,23 @@ console.log('\n\n')
|
|||
|
||||
assert.strictEqual(dnsProviders.cloudflareTLS.dnsType, 'TLS')
|
||||
// ip = await dnsProviders.cloudflareTLS.lookup(hostname1)
|
||||
// console.log('===> test cloudflareTLS:', ip, '\n\n')
|
||||
// console.log(`===> test cloudflareTLS: ${hostname1} ->`, ip, '\n\n')
|
||||
|
||||
assert.strictEqual(dnsProviders.quad9TLS.dnsType, 'TLS')
|
||||
// ip = await dnsProviders.quad9TLS.lookup(hostname1)
|
||||
// console.log('===> test quad9TLS:', ip, '\n\n')
|
||||
// console.log(`===> test quad9TLS: ${hostname1} ->`, ip, '\n\n')
|
||||
|
||||
assert.strictEqual(dnsProviders.aliyunTLS.dnsType, 'TLS')
|
||||
// ip = await dnsProviders.aliyunTLS.lookup(hostname1)
|
||||
// console.log('===> test aliyunTLS:', ip, '\n\n')
|
||||
ip = await dnsProviders.aliyunTLS.lookup(hostname1)
|
||||
console.log(`===> test aliyunTLS: ${hostname1} ->`, ip, '\n\n')
|
||||
|
||||
assert.strictEqual(dnsProviders.aliyunTLS2.dnsType, 'TLS')
|
||||
// ip = await dnsProviders.aliyunTLS2.lookup(hostname1)
|
||||
// console.log('===> test aliyunTLS2:', ip, '\n\n')
|
||||
ip = await dnsProviders.aliyunTLS2.lookup(hostname1)
|
||||
console.log(`===> test aliyunTLS2: ${hostname1} ->`, ip, '\n\n')
|
||||
|
||||
assert.strictEqual(dnsProviders.safe360TLS.dnsType, 'TLS')
|
||||
// ip = await dnsProviders.safe360TLS.lookup(hostname1)
|
||||
// console.log('===> test safe360TLS:', ip, '\n\n')
|
||||
ip = await dnsProviders.safe360TLS.lookup(hostname1)
|
||||
console.log(`===> test safe360TLS: ${hostname1} ->`, ip, '\n\n')
|
||||
|
||||
|
||||
console.log('\n--------------- test TCP ---------------\n')
|
||||
|
@ -177,12 +177,12 @@ assert.strictEqual(ip, presetIp) // test preset
|
|||
console.log('\n\n')
|
||||
|
||||
assert.strictEqual(dnsProviders.googleTCP.dnsType, 'TCP')
|
||||
// ip = await dnsProviders.googleTCP.lookup(hostname1)
|
||||
// console.log('===> test googleTCP:', ip, '\n\n')
|
||||
ip = await dnsProviders.googleTCP.lookup(hostname1)
|
||||
console.log(`===> test googleTCP: ${hostname1} ->`, ip, '\n\n')
|
||||
|
||||
assert.strictEqual(dnsProviders.aliyunTCP.dnsType, 'TCP')
|
||||
// ip = await dnsProviders.aliyunTCP.lookup(hostname1)
|
||||
// console.log('===> test aliyunTCP:', ip, '\n\n')
|
||||
ip = await dnsProviders.aliyunTCP.lookup(hostname1)
|
||||
console.log(`===> test aliyunTCP: ${hostname1} ->`, ip, '\n\n')
|
||||
|
||||
|
||||
console.log('\n--------------- test UDP ---------------\n')
|
||||
|
@ -191,9 +191,26 @@ assert.strictEqual(ip, presetIp) // test preset
|
|||
console.log('\n\n')
|
||||
|
||||
assert.strictEqual(dnsProviders.googleUDP.dnsType, 'UDP')
|
||||
// ip = await dnsProviders.googleUDP.lookup(hostname1)
|
||||
// console.log('===> test googleUDP:', ip, '\n\n')
|
||||
ip = await dnsProviders.googleUDP.lookup(hostname1)
|
||||
console.log(`===> test googleUDP: ${hostname1} ->`, ip, '\n\n')
|
||||
|
||||
assert.strictEqual(dnsProviders.aliyunUDP.dnsType, 'UDP')
|
||||
// ip = await dnsProviders.aliyunUDP.lookup(hostname1)
|
||||
// console.log('===> test aliyunUDP:', ip, '\n\n')
|
||||
ip = await dnsProviders.aliyunUDP.lookup(hostname1)
|
||||
console.log(`===> test aliyunUDP: ${hostname1} ->`, ip, '\n\n')
|
||||
|
||||
dnsProviders.aliyunUDP.lookup(hostname1).then(ip0 => {
|
||||
console.log(`===> test aliyunUDP: ${hostname1} ->`, ip0, '\n\n')
|
||||
assert.strictEqual(ip0, ip)
|
||||
})
|
||||
dnsProviders.aliyunUDP.lookup(hostname2).then(ip0 => {
|
||||
console.log(`===> test aliyunUDP: ${hostname2} ->`, ip0, '\n\n')
|
||||
assert.notStrictEqual(ip0, ip)
|
||||
})
|
||||
dnsProviders.aliyunUDP.lookup('baidu.com').then(ip0 => {
|
||||
console.log('===> test aliyunUDP: baidu.com ->', ip0, '\n\n')
|
||||
assert.notStrictEqual(ip0, ip)
|
||||
})
|
||||
dnsProviders.aliyunUDP.lookup('gitee.com').then(ip0 => {
|
||||
console.log('===> test aliyunUDP: gitee.com ->', ip0, '\n\n')
|
||||
assert.notStrictEqual(ip0, ip)
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue