| 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 |