Skip to content

Commit 1e68ae5

Browse files
optimize: 优化DNS配置解析
1 parent 93af0c9 commit 1e68ae5

File tree

12 files changed

+90
-73
lines changed

12 files changed

+90
-73
lines changed

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ class IpCache extends DynamicChoice {
3333
}
3434

3535
module.exports = class BaseDNS {
36-
constructor (dnsName, cacheSize, preSetIpList) {
36+
constructor (dnsName, dnsType, cacheSize, preSetIpList) {
3737
this.dnsName = dnsName
38+
this.dnsType = dnsType
3839
this.preSetIpList = preSetIpList
3940
this.cache = new LRUCache({
4041
maxSize: (cacheSize > 0 ? cacheSize : defaultCacheSize),
@@ -73,11 +74,11 @@ module.exports = class BaseDNS {
7374
ipList.push(hostname) // 把原域名加入到统计里去
7475

7576
ipCache.setBackupList(ipList)
76-
log.info(`[DNS '${this.dnsName}']: ${hostname}${ipCache.value} (${new Date() - t} ms), ipList: ${JSON.stringify(ipList)}, ipCache:`, JSON.stringify(ipCache))
77+
log.info(`[DNS-over-${this.dnsType} '${this.dnsName}'] ${hostname}${ipCache.value} (${new Date() - t} ms), ipList: ${JSON.stringify(ipList)}, ipCache:`, JSON.stringify(ipCache))
7778

7879
return ipCache.value
7980
} catch (error) {
80-
log.error(`[DNS '${this.dnsName}'] cannot resolve hostname ${hostname}, error:`, error)
81+
log.error(`[DNS-over-${this.dnsType} '${this.dnsName}'] cannot resolve hostname ${hostname}, error:`, error)
8182
return hostname
8283
}
8384
}
@@ -94,6 +95,7 @@ module.exports = class BaseDNS {
9495

9596
if (hostnamePreSetIpList.length > 0) {
9697
hostnamePreSetIpList.isPreSet = true
98+
log.info(`[DNS-over-${this.dnsType} '${this.dnsName}'] 获取到该域名的预设IP列表: ${hostname} - ${JSON.stringify(hostnamePreSetIpList)}`)
9799
return hostnamePreSetIpList
98100
}
99101
}
@@ -108,18 +110,18 @@ module.exports = class BaseDNS {
108110
const cost = Date.now() - start
109111
if (response == null || response.answers == null || response.answers.length == null || response.answers.length === 0) {
110112
// 说明没有获取到ip
111-
log.warn(`DNS '${this.dnsName}' 没有该域名的IP地址: ${hostname}, cost: ${cost} ms, response:`, response)
113+
log.warn(`[DNS-over-${this.dnsType} '${this.dnsName}'] 没有该域名的IP地址: ${hostname}, cost: ${cost} ms, response:`, response)
112114
return []
113115
}
114116
const ret = response.answers.filter(item => item.type === 'A').map(item => item.data)
115117
if (ret.length === 0) {
116-
log.info(`DNS '${this.dnsName}' 没有该域名的IPv4地址: ${hostname}, cost: ${cost} ms`)
118+
log.info(`[DNS-over-${this.dnsType} '${this.dnsName}'] 没有该域名的IPv4地址: ${hostname}, cost: ${cost} ms`)
117119
} else {
118-
log.info(`DNS '${this.dnsName}' 获取到该域名的IPv4地址: ${hostname} - ${JSON.stringify(ret)}, cost: ${cost} ms`)
120+
log.info(`[DNS-over-${this.dnsType} '${this.dnsName}'] 获取到该域名的IPv4地址: ${hostname} - ${JSON.stringify(ret)}, cost: ${cost} ms`)
119121
}
120122
return ret
121123
} catch (e) {
122-
log.error(`DNS query error: ${hostname}, dns: ${this.dnsName}${this.dnsServer ? (`, dnsServer: ${this.dnsServer}`) : ''}, cost: ${Date.now() - start} ms, error:`, e)
124+
log.error(`[DNS-over-${this.dnsType} '${this.dnsName}'] DNS query error, hostname: ${hostname}${this.dnsServer ? `, dnsServer: ${this.dnsServer}` : ''}, cost: ${Date.now() - start} ms, error:`, e)
123125
return []
124126
}
125127
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const dohQueryAsync = promisify(doh.query)
66

77
module.exports = class DNSOverHTTPS extends BaseDNS {
88
constructor (dnsName, cacheSize, preSetIpList, dnsServer) {
9-
super(dnsName, cacheSize, preSetIpList)
9+
super(dnsName, 'HTTPS', cacheSize, preSetIpList)
1010
this.dnsServer = dnsServer
1111
}
1212

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

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module.exports = {
1313
for (const provider in dnsProviders) {
1414
const conf = dnsProviders[provider]
1515

16+
// 获取DNS服务器
1617
let server = conf.server || conf.host
1718
if (server != null) {
1819
server = server.replace(/\s+/, '')
@@ -22,44 +23,55 @@ module.exports = {
2223
}
2324

2425
// 获取DNS类型
25-
if (conf.type == null) {
26+
let type = conf.type
27+
if (type == null) {
2628
if (server.startsWith('https://') || server.startsWith('http://')) {
27-
conf.type = 'https'
29+
type = 'https'
2830
} else if (server.startsWith('tls://')) {
29-
conf.type = 'tls'
31+
type = 'tls'
3032
} else if (server.startsWith('tcp://')) {
31-
conf.type = 'tcp'
33+
type = 'tcp'
3234
} else if (server.includes('://') && !server.startsWith('udp://')) {
3335
throw new Error(`Unknown type DNS: ${server}, provider: ${provider}`)
3436
} else {
35-
conf.type = 'udp'
37+
type = 'udp'
3638
}
3739
} else {
38-
conf.type = conf.type.toLowerCase()
40+
type = type.replace(/\s+/, '').toLowerCase()
3941
}
4042

41-
if (conf.type === 'https') {
43+
// 创建DNS对象
44+
if (type === 'https' || type === 'doh' || type === 'dns-over-https') {
45+
if (!server.includes('/')) {
46+
server = `https://${server}/dns-query`
47+
}
48+
49+
// 基于 https
4250
dnsMap[provider] = new DNSOverHTTPS(provider, conf.cacheSize, preSetIpList, server)
43-
} else if (conf.type === 'tls') {
44-
if (server.startsWith('tls://')) {
45-
server = server.substring(6)
51+
} else {
52+
// 获取DNS端口
53+
let port = conf.port
54+
55+
// 处理带协议的DNS服务地址
56+
if (server.includes('://')) {
57+
server = server.split('://')[1]
4658
}
47-
dnsMap[provider] = new DNSOverTLS(provider, conf.cacheSize, preSetIpList, server, conf.port, conf.servername)
48-
} else if (conf.type === 'tcp') {
49-
if (server.startsWith('tcp://')) {
50-
server = server.substring(6)
59+
// 处理带端口的DNS服务地址
60+
if (port == null && server.includes(':')) {
61+
[server, port] = server.split(':')
5162
}
52-
dnsMap[provider] = new DNSOverTCP(provider, conf.cacheSize, preSetIpList, server, conf.port)
53-
} else { // udp
54-
if (server.startsWith('udp://')) {
55-
server = server.substring(6)
63+
64+
if (type === 'tls' || type === 'dot' || type === 'dns-over-tls') {
65+
// 基于 tls
66+
dnsMap[provider] = new DNSOverTLS(provider, conf.cacheSize, preSetIpList, server, port, conf.servername)
67+
} else if (type === 'tcp' || type === 'dns-over-tcp') {
68+
// 基于 tcp
69+
dnsMap[provider] = new DNSOverTCP(provider, conf.cacheSize, preSetIpList, server, port)
70+
} else {
71+
// 基于 udp
72+
dnsMap[provider] = new DNSOverUDP(provider, conf.cacheSize, preSetIpList, server, port)
5673
}
57-
dnsMap[provider] = new DNSOverUDP(provider, conf.cacheSize, preSetIpList, server, conf.port)
5874
}
59-
60-
// 设置DNS名称到name属性中
61-
dnsMap[provider].name = provider
62-
dnsMap[provider].type = conf.type
6375
}
6476

6577
// 创建预设IP的DNS

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

Lines changed: 0 additions & 4 deletions
This file was deleted.

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ const BaseDNS = require('./base')
22

33
module.exports = class DNSOverPreSetIpList extends BaseDNS {
44
constructor (preSetIpList) {
5-
super('PreSet', null, preSetIpList)
6-
this.name = 'PreSet'
7-
this.type = 'PreSet'
5+
super('PreSet', 'PreSet', null, preSetIpList)
86
}
97

108
async _lookup (_hostname) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const defaultPort = 53 // UDP类型的DNS服务默认端口号
88

99
module.exports = class DNSOverTCP extends BaseDNS {
1010
constructor (dnsName, cacheSize, preSetIpList, dnsServer, dnsServerPort) {
11-
super(dnsName, cacheSize, preSetIpList)
11+
super(dnsName, 'TCP', cacheSize, preSetIpList)
1212
this.dnsServer = dnsServer
1313
this.dnsServerPort = Number.parseInt(dnsServerPort) || defaultPort
1414
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const defaultPort = 853
55

66
module.exports = class DNSOverTLS extends BaseDNS {
77
constructor (dnsName, cacheSize, preSetIpList, dnsServer, dnsServerPort, dnsServerName) {
8-
super(dnsName, cacheSize, preSetIpList)
8+
super(dnsName, 'TLS', cacheSize, preSetIpList)
99
this.dnsServer = dnsServer
1010
this.dnsServerPort = Number.parseInt(dnsServerPort) || defaultPort
1111
this.dnsServerName = dnsServerName

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const defaultPort = 53 // UDP类型的DNS服务默认端口号
99

1010
module.exports = class DNSOverUDP extends BaseDNS {
1111
constructor (dnsName, cacheSize, preSetIpList, dnsServer, dnsServerPort) {
12-
super(dnsName, cacheSize, preSetIpList)
12+
super(dnsName, 'UDP', cacheSize, preSetIpList)
1313
this.dnsServer = dnsServer
1414
this.dnsServerPort = Number.parseInt(dnsServerPort) || defaultPort
1515
}

packages/mitmproxy/src/lib/proxy/mitmproxy/createConnectHandler.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ function connect (req, cltSocket, head, hostname, port, dnsConfig = null, isDire
143143
if (isDnsIntercept && isDnsIntercept.dns && isDnsIntercept.ip !== isDnsIntercept.hostname) {
144144
const { dns, ip, hostname } = isDnsIntercept
145145
dns.count(hostname, ip, true)
146-
log.error(`记录ip失败次数,用于优选ip! hostname: ${hostname}, ip: ${ip}, reason: ${errorMsg}, dns: ${dns.name}`)
146+
log.error(`记录ip失败次数,用于优选ip! hostname: ${hostname}, ip: ${ip}, reason: ${errorMsg}, dns: ${dns.dnsName}`)
147147
}
148148
})
149149
proxySocket.on('error', (e) => {
@@ -157,7 +157,7 @@ function connect (req, cltSocket, head, hostname, port, dnsConfig = null, isDire
157157
if (isDnsIntercept && isDnsIntercept.dns && isDnsIntercept.ip !== isDnsIntercept.hostname) {
158158
const { dns, ip, hostname } = isDnsIntercept
159159
dns.count(hostname, ip, true)
160-
log.error(`记录ip失败次数,用于优选ip! hostname: ${hostname}, ip: ${ip}, reason: ${errorMsg}, dns: ${dns.name}`)
160+
log.error(`记录ip失败次数,用于优选ip! hostname: ${hostname}, ip: ${ip}, reason: ${errorMsg}, dns: ${dns.dnsName}`)
161161
}
162162
})
163163

packages/mitmproxy/src/lib/proxy/mitmproxy/createRequestHandler.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ module.exports = function createRequestHandler (createIntercepts, middlewares, e
8484
if (isDnsIntercept && isDnsIntercept.dns && isDnsIntercept.ip !== isDnsIntercept.hostname) {
8585
const { dns, ip, hostname } = isDnsIntercept
8686
dns.count(hostname, ip, true)
87-
log.error(`记录ip失败次数,用于优选ip! hostname: ${hostname}, ip: ${ip}, reason: ${reason}, dns: ${dns.name}`)
87+
log.error(`记录ip失败次数,用于优选ip! hostname: ${hostname}, ip: ${ip}, reason: ${reason}, dns: ${dns.dnsName}`)
8888
}
8989
const counter = context.requestCount
9090
if (counter != null) {
@@ -123,8 +123,8 @@ module.exports = function createRequestHandler (createIntercepts, middlewares, e
123123
}
124124
if (dns) {
125125
rOptions.lookup = dnsLookup.createLookupFunc(res, dns, 'request url', url, isDnsIntercept)
126-
log.debug(`域名 ${rOptions.hostname} DNS: ${dns.name}`)
127-
res.setHeader('DS-DNS', dns.name)
126+
log.debug(`域名 ${rOptions.hostname} DNS: ${dns.dnsName}`)
127+
res.setHeader('DS-DNS', dns.dnsName)
128128
} else {
129129
log.info(`域名 ${rOptions.hostname} 在DNS中未配置`)
130130
}

0 commit comments

Comments
 (0)