MCPcopy
hub / github.com/docmirror/dev-sidecar / lookup

Method lookup

packages/mitmproxy/src/lib/dns/base.js:93–155  ·  view source on GitHub ↗
(hostname, options = {})

Source from the content-addressed store, hash-verified

91 }
92
93 async lookup (hostname, options = {}) {
94 try {
95 let ipCache = this.cache.get(hostname)
96 if (ipCache) {
97 const ip = ipCache.value
98 if (ip != null) {
99 if (options.ipChecker) {
100 if (options.ipChecker(ip)) {
101 ipCache.doCount(ip, false)
102 log.info(`[DNS-over-${this.dnsType} '${this.dnsName}'] 获取IP地址缓存: ${hostname} -> ${ip}(测试通过)`)
103 return ip
104 } else {
105 log.info(`[DNS-over-${this.dnsType} '${this.dnsName}'] 获取IP地址缓存: ${hostname} -> ${ip}(测试不通过)-> ${hostname}`)
106 return hostname
107 }
108 } else {
109 log.info(`[DNS-over-${this.dnsType} '${this.dnsName}'] 获取IP地址缓存: ${hostname} -> ${ip}`)
110 return ip
111 }
112 } else {
113 log.info(`[DNS-over-${this.dnsType} '${this.dnsName}'] 未获取到IP地址缓存: ${hostname}`)
114 }
115 } else {
116 ipCache = new IpCache(hostname)
117 this.cache.set(hostname, ipCache)
118 log.info(`[DNS-over-${this.dnsType} '${this.dnsName}'] 首次创建IP地址缓存区: ${hostname}`)
119 }
120
121 const t = Date.now()
122 let ipList = await this._lookupWithPreSetIpList(hostname, options)
123 if (ipList == null) {
124 // 没有获取到ip
125 ipList = []
126 }
127 // 本机无 IPv6 网络能力时过滤 IPv6 地址,避免 ENETUNREACH
128 if (ipv6Unavailable) {
129 ipList = ipList.filter(ip => !isIPv6(ip))
130 }
131 ipList.push(hostname) // 把原域名加入到统计里去
132
133 ipCache.setBackupList(ipList)
134
135 const ip = ipCache.value
136 log.info(`[DNS-over-${this.dnsType} '${this.dnsName}'] ${hostname} ➜ ${ip} (${Date.now() - t} ms), ipList: ${JSON.stringify(ipList)}, ipCache:`, JSON.stringify(ipCache))
137
138 if (options.ipChecker) {
139 if (ip != null && ip !== hostname && options.ipChecker(ip)) {
140 return ip
141 }
142
143 for (const ip of ipList) {
144 if (ip !== hostname && options.ipChecker(ip)) {
145 return ip
146 }
147 }
148 }
149
150 return ip != null ? ip : hostname

Callers 6

testFunction · 0.80
ipv6-test.mjsFile · 0.80
dnsTest-abroad.mjsFile · 0.80
mainFunction · 0.80
dnsTest.mjsFile · 0.80
createLookupFuncFunction · 0.80

Calls 5

isIPv6Function · 0.85
getMethod · 0.80
doCountMethod · 0.80
setBackupListMethod · 0.45

Tested by 1

testFunction · 0.64