11const LRUCache = require ( 'lru-cache' )
22const log = require ( '../../utils/util.log.server' )
3+ const net = require ( 'node:net' )
34const matchUtil = require ( '../../utils/util.match' )
45const { DynamicChoice } = require ( '../choice/index' )
56
@@ -52,7 +53,7 @@ module.exports = class BaseDNS {
5253 }
5354 }
5455
55- async lookup ( hostname , ipChecker ) {
56+ async lookup ( hostname , ipChecker , options = { } ) {
5657 try {
5758 let ipCache = this . cache . get ( hostname )
5859 if ( ipCache ) {
@@ -71,9 +72,9 @@ module.exports = class BaseDNS {
7172 }
7273
7374 const t = Date . now ( )
74- let ipList = await this . _lookupWithPreSetIpList ( hostname )
75+ let ipList = await this . _lookupWithPreSetIpList ( hostname , options )
7576 if ( ipList == null ) {
76- // 没有获取到ipv4地址
77+ // 没有获取到ip地址
7778 ipList = [ ]
7879 }
7980 ipList . push ( hostname ) // 把原域名加入到统计里去
@@ -102,7 +103,7 @@ module.exports = class BaseDNS {
102103 }
103104 }
104105
105- async _lookupWithPreSetIpList ( hostname ) {
106+ async _lookupWithPreSetIpList ( hostname , options = { } ) {
106107 if ( this . preSetIpList ) {
107108 // 获取当前域名的预设IP列表
108109 let hostnamePreSetIpList = matchUtil . matchHostname ( this . preSetIpList , hostname , `matched preSetIpList(${ this . dnsName } )` )
@@ -114,28 +115,50 @@ module.exports = class BaseDNS {
114115 }
115116
116117 if ( hostnamePreSetIpList . length > 0 ) {
117- hostnamePreSetIpList . isPreSet = true
118- log . info ( `[DNS-over-PreSet '${ this . dnsName } '] 获取到该域名的预设IP列表: ${ hostname } - ${ JSON . stringify ( hostnamePreSetIpList ) } ` )
119- return hostnamePreSetIpList
118+ const result = [ ]
119+ for ( const item of hostnamePreSetIpList ) {
120+ if ( net . isIP ( item ) ) {
121+ // 如果是IP地址,直接使用
122+ result . push ( item )
123+ } else {
124+ // 如果是域名,进行DNS解析
125+ try {
126+ const resolved = await this . _lookup ( item , options )
127+ if ( resolved && resolved . length > 0 ) {
128+ result . push ( ...resolved )
129+ }
130+ } catch ( e ) {
131+ log . error ( `[DNS-over-${ this . dnsType } '${ this . dnsName } '] 解析预设域名失败: ${ item } ` , e )
132+ }
133+ }
134+ }
135+
136+ if ( result . length > 0 ) {
137+ result . isPreSet = true
138+ log . info ( `[DNS-over-${ this . dnsType } '${ this . dnsName } '] 获取到该域名的预设IP列表: ${ hostname } - ${ JSON . stringify ( result ) } ` )
139+ return result
140+ }
120141 }
121142 }
122143 }
123144
124- return await this . _lookup ( hostname )
145+ return await this . _lookup ( hostname , options )
125146 }
126147
127- async _lookup ( hostname ) {
148+ async _lookup ( hostname , options = { } ) {
128149 const start = Date . now ( )
129150
130151 let response
131152 try {
132153 // 执行DNS查询
133154 log . debug ( `[DNS-over-${ this . dnsType } '${ this . dnsName } '] query start: ${ hostname } ` )
134- response = await this . _doDnsQuery ( hostname , 'A' , start )
155+ response = await this . _doDnsQuery ( hostname , options , start )
135156 } catch {
136157 // 异常日志在 _doDnsQuery已经打印过,这里就不再打印了
137158 return [ ]
138159 }
160+ // 根据查询类型过滤结果
161+ const type = options . family === 6 ? 'AAAA' : 'A'
139162
140163 try {
141164 const cost = Date . now ( ) - start
@@ -146,11 +169,11 @@ module.exports = class BaseDNS {
146169 return [ ]
147170 }
148171
149- const ret = response . answers . filter ( item => item . type === 'A' ) . map ( item => item . data )
172+ const ret = response . answers . filter ( item => item . type === type ) . map ( item => item . data )
150173 if ( ret . length === 0 ) {
151- log . info ( `[DNS-over-${ this . dnsType } '${ this . dnsName } '] 没有该域名的IP地址 : ${ hostname } , cost: ${ cost } ms` )
174+ log . info ( `[DNS-over-${ this . dnsType } '${ this . dnsName } '] 没有该域名的IPv ${ options . family === 6 ? '6' : '4' } 地址 : ${ hostname } , cost: ${ cost } ms` )
152175 } else {
153- log . info ( `[DNS-over-${ this . dnsType } '${ this . dnsName } '] 获取到该域名的IP地址 : ${ hostname } - ${ JSON . stringify ( ret ) } , cost: ${ cost } ms` )
176+ log . info ( `[DNS-over-${ this . dnsType } '${ this . dnsName } '] 获取到该域名的IPv ${ options . family === 6 ? '6' : '4' } 地址 : ${ hostname } - ${ JSON . stringify ( ret ) } , cost: ${ cost } ms` )
154177 }
155178
156179 return ret
@@ -160,7 +183,7 @@ module.exports = class BaseDNS {
160183 }
161184 }
162185
163- _doDnsQuery ( hostname , type = 'A' , start ) {
186+ _doDnsQuery ( hostname , options = { } , start = null ) {
164187 if ( start == null ) {
165188 start = Date . now ( )
166189 }
@@ -171,14 +194,14 @@ module.exports = class BaseDNS {
171194 const timeout = 6000
172195 const timeoutId = setTimeout ( ( ) => {
173196 if ( ! isOver ) {
174- log . error ( `[DNS-over-${ this . dnsType } '${ this . dnsName } '] DNS查询超时, hostname: ${ hostname } , type: ${ type } ${ this . dnsServer ? `, dnsServer: ${ this . dnsServer } ` : '' } ${ this . dnsServerPort ? `: ${ this . dnsServerPort } ` : '' } , cost: ${ Date . now ( ) - start } ms` )
197+ log . error ( `[DNS-over-${ this . dnsType } '${ this . dnsName } '] DNS查询超时, hostname: ${ hostname } , cost: ${ Date . now ( ) - start } ms` )
175198 reject ( new Error ( 'DNS查询超时' ) )
176199 }
177200 } , timeout )
178201
179202 try {
180- this . _dnsQueryPromise ( hostname , type )
181- . then ( ( response ) => {
203+ this . _dnsQueryPromise ( hostname , options )
204+ . then ( ( response ) => {
182205 isOver = true
183206 clearTimeout ( timeoutId )
184207 resolve ( response )
0 commit comments