Skip to content

Commit e82315e

Browse files
bugfix: 1)UDP类型的DNS,并发调用时IP赋值混乱的问题修复;2)UDP和TCP类型的DNS连接未关闭的问题修复。
1 parent d496c39 commit e82315e

File tree

3 files changed

+57
-35
lines changed

3 files changed

+57
-35
lines changed

packages/mitmproxy/src/lib/dns/tcp.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ module.exports = class DNSOverTCP extends BaseDNS {
4141
const length = data.readUInt16BE(0)
4242
const response = dnsPacket.decode(data.subarray(2, 2 + length))
4343
resolve(response)
44+
tcpClient.end()
4445
})
4546

4647
tcpClient.once('error', (err) => {
4748
reject(err)
49+
tcpClient.end()
4850
})
4951
})
5052
}

packages/mitmproxy/src/lib/dns/udp.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ const dnsPacket = require('dns-packet')
33
const randi = require('random-int')
44
const BaseDNS = require('./base')
55

6-
const udpClient = dgram.createSocket('udp4')
7-
86
const defaultPort = 53 // UDP类型的DNS服务默认端口号
97

108
module.exports = class DNSOverUDP extends BaseDNS {
119
constructor (dnsName, cacheSize, preSetIpList, dnsServer, dnsServerPort) {
1210
super(dnsName, 'UDP', cacheSize, preSetIpList)
1311
this.dnsServer = dnsServer
1412
this.dnsServerPort = Number.parseInt(dnsServerPort) || defaultPort
13+
14+
this.isIPv6 = dnsServer.includes(':') && dnsServer.includes('[') && dnsServer.includes(']')
15+
this.socketType = this.isIPv6 ? 'udp6' : 'udp4'
1516
}
1617

1718
_doDnsQuery (hostname) {
@@ -27,18 +28,20 @@ module.exports = class DNSOverUDP extends BaseDNS {
2728
}],
2829
})
2930

31+
// 创建客户端
32+
const udpClient = dgram.createSocket(this.socketType, (msg, _rinfo) => {
33+
const response = dnsPacket.decode(msg)
34+
resolve(response)
35+
udpClient.close()
36+
})
37+
3038
// 发送 UDP 查询
31-
udpClient.send(packet, 0, packet.length, this.dnsServerPort, this.dnsServer, (err) => {
39+
udpClient.send(packet, 0, packet.length, this.dnsServerPort, this.dnsServer, (err, _bytes) => {
3240
if (err) {
3341
reject(err)
42+
udpClient.close()
3443
}
3544
})
36-
37-
// 接收 UDP 响应
38-
udpClient.once('message', (msg) => {
39-
const response = dnsPacket.decode(msg)
40-
resolve(response)
41-
})
4245
})
4346
}
4447
}

packages/mitmproxy/test/dnsTest.mjs

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -118,31 +118,31 @@ console.log('\n\n')
118118

119119
assert.strictEqual(dnsProviders.cloudflare.dnsType, 'HTTPS')
120120
// ip = await dnsProviders.cloudflare.lookup(hostname1)
121-
// console.log('===> test cloudflare:', ip, '\n\n')
121+
// console.log(`===> test cloudflare: ${hostname1} ->`, ip, '\n\n')
122122

123123
assert.strictEqual(dnsProviders.quad9.dnsType, 'HTTPS')
124124
// ip = await dnsProviders.quad9.lookup(hostname1)
125-
// console.log('===> test quad9:', ip, '\n\n')
125+
// console.log(`===> test quad9: ${hostname1} ->`, ip, '\n\n')
126126

127127
assert.strictEqual(dnsProviders.aliyun.dnsType, 'HTTPS')
128-
// ip = await dnsProviders.aliyun.lookup(hostname1)
129-
// console.log('===> test aliyun:', ip, '\n\n')
128+
ip = await dnsProviders.aliyun.lookup(hostname1)
129+
console.log(`===> test aliyun: ${hostname1} ->`, ip, '\n\n')
130130

131131
assert.strictEqual(dnsProviders.aliyun2.dnsType, 'HTTPS')
132-
// ip = await dnsProviders.aliyun2.lookup(hostname1)
133-
// console.log('===> test aliyun2:', ip, '\n\n')
132+
ip = await dnsProviders.aliyun2.lookup(hostname1)
133+
console.log(`===> test aliyun2: ${hostname1} ->`, ip, '\n\n')
134134

135135
assert.strictEqual(dnsProviders.safe360.dnsType, 'HTTPS')
136-
// ip = await dnsProviders.safe360.lookup(hostname1)
137-
// console.log('===> test safe360:', ip, '\n\n')
136+
ip = await dnsProviders.safe360.lookup(hostname1)
137+
console.log(`===> test safe360: ${hostname1} ->`, ip, '\n\n')
138138

139139
assert.strictEqual(dnsProviders.rubyfish.dnsType, 'HTTPS')
140140
// ip = await dnsProviders.rubyfish.lookup(hostname1)
141-
// console.log('===> test rubyfish:', ip, '\n\n')
141+
// console.log(`===> test rubyfish: ${hostname1} ->`, ip, '\n\n')
142142

143143
assert.strictEqual(dnsProviders.py233.dnsType, 'HTTPS')
144144
// ip = await dnsProviders.py233.lookup(hostname1)
145-
// console.log('===> test py233:', ip, '\n\n')
145+
// console.log(`===> test py233: ${hostname1} ->`, ip, '\n\n')
146146

147147

148148
console.log('\n--------------- test TLS ---------------\n')
@@ -152,23 +152,23 @@ console.log('\n\n')
152152

153153
assert.strictEqual(dnsProviders.cloudflareTLS.dnsType, 'TLS')
154154
// ip = await dnsProviders.cloudflareTLS.lookup(hostname1)
155-
// console.log('===> test cloudflareTLS:', ip, '\n\n')
155+
// console.log(`===> test cloudflareTLS: ${hostname1} ->`, ip, '\n\n')
156156

157157
assert.strictEqual(dnsProviders.quad9TLS.dnsType, 'TLS')
158158
// ip = await dnsProviders.quad9TLS.lookup(hostname1)
159-
// console.log('===> test quad9TLS:', ip, '\n\n')
159+
// console.log(`===> test quad9TLS: ${hostname1} ->`, ip, '\n\n')
160160

161161
assert.strictEqual(dnsProviders.aliyunTLS.dnsType, 'TLS')
162-
// ip = await dnsProviders.aliyunTLS.lookup(hostname1)
163-
// console.log('===> test aliyunTLS:', ip, '\n\n')
162+
ip = await dnsProviders.aliyunTLS.lookup(hostname1)
163+
console.log(`===> test aliyunTLS: ${hostname1} ->`, ip, '\n\n')
164164

165165
assert.strictEqual(dnsProviders.aliyunTLS2.dnsType, 'TLS')
166-
// ip = await dnsProviders.aliyunTLS2.lookup(hostname1)
167-
// console.log('===> test aliyunTLS2:', ip, '\n\n')
166+
ip = await dnsProviders.aliyunTLS2.lookup(hostname1)
167+
console.log(`===> test aliyunTLS2: ${hostname1} ->`, ip, '\n\n')
168168

169169
assert.strictEqual(dnsProviders.safe360TLS.dnsType, 'TLS')
170-
// ip = await dnsProviders.safe360TLS.lookup(hostname1)
171-
// console.log('===> test safe360TLS:', ip, '\n\n')
170+
ip = await dnsProviders.safe360TLS.lookup(hostname1)
171+
console.log(`===> test safe360TLS: ${hostname1} ->`, ip, '\n\n')
172172

173173

174174
console.log('\n--------------- test TCP ---------------\n')
@@ -177,12 +177,12 @@ assert.strictEqual(ip, presetIp) // test preset
177177
console.log('\n\n')
178178

179179
assert.strictEqual(dnsProviders.googleTCP.dnsType, 'TCP')
180-
// ip = await dnsProviders.googleTCP.lookup(hostname1)
181-
// console.log('===> test googleTCP:', ip, '\n\n')
180+
ip = await dnsProviders.googleTCP.lookup(hostname1)
181+
console.log(`===> test googleTCP: ${hostname1} ->`, ip, '\n\n')
182182

183183
assert.strictEqual(dnsProviders.aliyunTCP.dnsType, 'TCP')
184-
// ip = await dnsProviders.aliyunTCP.lookup(hostname1)
185-
// console.log('===> test aliyunTCP:', ip, '\n\n')
184+
ip = await dnsProviders.aliyunTCP.lookup(hostname1)
185+
console.log(`===> test aliyunTCP: ${hostname1} ->`, ip, '\n\n')
186186

187187

188188
console.log('\n--------------- test UDP ---------------\n')
@@ -191,9 +191,26 @@ assert.strictEqual(ip, presetIp) // test preset
191191
console.log('\n\n')
192192

193193
assert.strictEqual(dnsProviders.googleUDP.dnsType, 'UDP')
194-
// ip = await dnsProviders.googleUDP.lookup(hostname1)
195-
// console.log('===> test googleUDP:', ip, '\n\n')
194+
ip = await dnsProviders.googleUDP.lookup(hostname1)
195+
console.log(`===> test googleUDP: ${hostname1} ->`, ip, '\n\n')
196196

197197
assert.strictEqual(dnsProviders.aliyunUDP.dnsType, 'UDP')
198-
// ip = await dnsProviders.aliyunUDP.lookup(hostname1)
199-
// console.log('===> test aliyunUDP:', ip, '\n\n')
198+
ip = await dnsProviders.aliyunUDP.lookup(hostname1)
199+
console.log(`===> test aliyunUDP: ${hostname1} ->`, ip, '\n\n')
200+
201+
dnsProviders.aliyunUDP.lookup(hostname1).then(ip0 => {
202+
console.log(`===> test aliyunUDP: ${hostname1} ->`, ip0, '\n\n')
203+
assert.strictEqual(ip0, ip)
204+
})
205+
dnsProviders.aliyunUDP.lookup(hostname2).then(ip0 => {
206+
console.log(`===> test aliyunUDP: ${hostname2} ->`, ip0, '\n\n')
207+
assert.notStrictEqual(ip0, ip)
208+
})
209+
dnsProviders.aliyunUDP.lookup('baidu.com').then(ip0 => {
210+
console.log('===> test aliyunUDP: baidu.com ->', ip0, '\n\n')
211+
assert.notStrictEqual(ip0, ip)
212+
})
213+
dnsProviders.aliyunUDP.lookup('gitee.com').then(ip0 => {
214+
console.log('===> test aliyunUDP: gitee.com ->', ip0, '\n\n')
215+
assert.notStrictEqual(ip0, ip)
216+
})

0 commit comments

Comments
 (0)