Skip to content

Commit

Permalink
optimize: 代理请求使用了DNS获取到的IP时,添加响应头 DS-Lookup,返回当前所使用的IP。
Browse files Browse the repository at this point in the history
  • Loading branch information
wangliang181230 committed Sep 14, 2024
1 parent a5f2b95 commit b9a5344
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function connect (req, cltSocket, head, hostname, port, dnsConfig) {
if (dnsConfig && dnsConfig.providers) {
const dns = DnsUtil.hasDnsLookup(dnsConfig, hostname)
if (dns) {
options.lookup = dnsLookup.createLookupFunc(dns, 'connect', hostport, isDnsIntercept)
options.lookup = dnsLookup.createLookupFunc(null, dns, 'connect', hostport, isDnsIntercept)
}
}
const proxySocket = net.connect(options, () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ module.exports = function createRequestHandler (createIntercepts, middlewares, e
}
}
if (dns) {
rOptions.lookup = dnsLookup.createLookupFunc(dns, 'request url', url, isDnsIntercept)
rOptions.lookup = dnsLookup.createLookupFunc(res, dns, 'request url', url, isDnsIntercept)
}
}

Expand Down
4 changes: 3 additions & 1 deletion packages/mitmproxy/src/lib/proxy/mitmproxy/dnsLookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const log = require('../../../utils/util.log')
const defaultDns = require('dns')

module.exports = {
createLookupFunc: function (dns, action, target, isDnsIntercept) {
createLookupFunc: function (res, dns, action, target, isDnsIntercept) {
target = target ? (', target: ' + target) : ''

return (hostname, options, callback) => {
Expand All @@ -12,6 +12,7 @@ module.exports = {
const aliveIpObj = tester.pickFastAliveIpObj()
if (aliveIpObj) {
log.info(`----- ${action}: ${hostname}, use alive ip from dns '${aliveIpObj.dns}': ${aliveIpObj.host}${target} -----`)
if (res) res.setHeader('DS-Lookup', `IpTester: ${aliveIpObj.host}(${aliveIpObj.dns})`)
callback(null, aliveIpObj.host, 4)
return
} else {
Expand Down Expand Up @@ -41,6 +42,7 @@ module.exports = {
}
if (isTestFailedIp === false) {
log.info(`----- ${action}: ${hostname}, use ip from dns '${dns.name}': ${ip}${target} -----`)
if (res) res.setHeader('DS-Lookup', `DNS: ${ip.host}(${dns.name})`)
callback(null, ip, 4)
return
} else {
Expand Down

0 comments on commit b9a5344

Please sign in to comment.