From 6d18a3ba09a6c3b9f20dde72876b4ffe61c06d52 Mon Sep 17 00:00:00 2001 From: 8odream Date: Sat, 14 Jun 2025 01:08:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81IP=E9=A2=84=E8=AE=BE=E4=BD=BF?= =?UTF-8?q?=E7=94=A8CNAME=EF=BC=8C=E5=90=8C=E6=97=B6=E4=BF=AE=E6=AD=A3?= =?UTF-8?q?=E4=BA=86=E6=9D=A5=E6=BA=90=E6=98=BE=E7=A4=BA=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/mitmproxy/src/lib/dns/base.js | 27 ++++++++++++++++--- .../mitmproxy/src/lib/speed/SpeedTester.js | 6 +++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/packages/mitmproxy/src/lib/dns/base.js b/packages/mitmproxy/src/lib/dns/base.js index 6a745c6..3ee308c 100644 --- a/packages/mitmproxy/src/lib/dns/base.js +++ b/packages/mitmproxy/src/lib/dns/base.js @@ -1,4 +1,5 @@ const LRUCache = require('lru-cache') +const net = require('node:net') const log = require('../../utils/util.log.server') const matchUtil = require('../../utils/util.match') const { DynamicChoice } = require('../choice/index') @@ -94,9 +95,29 @@ module.exports = class BaseDNS { } if (hostnamePreSetIpList.length > 0) { - hostnamePreSetIpList.isPreSet = true - log.info(`[DNS-over-${this.dnsType} '${this.dnsName}'] 获取到该域名的预设IP列表: ${hostname} - ${JSON.stringify(hostnamePreSetIpList)}`) - return hostnamePreSetIpList + const result = [] + for (const item of hostnamePreSetIpList) { + if (net.isIP(item)) { + // 如果是IP地址,直接使用 + result.push(item) + } else { + // 如果是域名,进行DNS解析 + try { + const resolved = await this._lookup(item, options) + if (resolved && resolved.length > 0) { + result.push(...resolved) + } + } catch (e) { + log.error(`[DNS-over-${this.dnsType} '${this.dnsName}'] 解析预设域名失败: ${item}`, e) + } + } + } + + if (result.length > 0) { + result.isPreSet = true + log.info(`[DNS-over-${this.dnsType} '${this.dnsName}'] 获取到该域名的预设IP列表: ${hostname} - ${JSON.stringify(result)}`) + return result + } } } diff --git a/packages/mitmproxy/src/lib/speed/SpeedTester.js b/packages/mitmproxy/src/lib/speed/SpeedTester.js index c781e99..0ac40ad 100644 --- a/packages/mitmproxy/src/lib/speed/SpeedTester.js +++ b/packages/mitmproxy/src/lib/speed/SpeedTester.js @@ -80,11 +80,13 @@ class SpeedTester { async getFromOneDns (dns) { const results = [] + let isPreSet = false // 优先尝试IPv6查询 try { const ipv6Result = await dns._lookupInternal(this.hostname, { family: 6 }) if (ipv6Result && ipv6Result.length > 0) { + isPreSet = ipv6Result.isPreSet === true // 标准化IPv6地址格式 const standardized = ipv6Result.map(ip => { // 确保IPv6地址格式统一 @@ -103,12 +105,16 @@ class SpeedTester { try { const ipv4Result = await dns._lookupInternal(this.hostname) if (ipv4Result) { + isPreSet = isPreSet || ipv4Result.isPreSet === true results.push(...ipv4Result) } } catch (e) { // IPv4查询失败 } + if (isPreSet) { + results.isPreSet = true + } return results }