Skip to content

Commit

Permalink
bugfix: 1)UDP类型的DNS,并发调用时IP赋值混乱的问题修复;2)UDPTCP类型的DNS连接对象未关闭的问题修复。
Browse files Browse the repository at this point in the history
  • Loading branch information
wangliang181230 committed Mar 6, 2025
1 parent fa06a07 commit 8ed5788
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 38 deletions.
6 changes: 4 additions & 2 deletions packages/mitmproxy/src/lib/dns/tcp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
}
Expand Down
21 changes: 12 additions & 9 deletions packages/mitmproxy/src/lib/dns/udp.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ 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 {
constructor (dnsName, cacheSize, preSetIpList, dnsServer, dnsServerPort) {
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) {
Expand All @@ -27,18 +28,20 @@ module.exports = class DNSOverUDP extends BaseDNS {
}],
})

// 创建客户端
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) => {
udpClient.send(packet, 0, packet.length, this.dnsServerPort, this.dnsServer, (err, _bytes) => {
if (err) {
reject(err)
udpClient.close()
}
})

// 接收 UDP 响应
udpClient.on('message', (msg) => {
const response = dnsPacket.decode(msg)
resolve(response)
})
})
}
}
71 changes: 44 additions & 27 deletions packages/mitmproxy/test/dnsTest.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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)
})

0 comments on commit 8ed5788

Please sign in to comment.