Update https.js

pull/311/head
璀璨梦星 2024-05-11 01:37:19 +08:00
parent d7ca3f9581
commit d7ea4cf632
1 changed files with 35 additions and 30 deletions

View File

@ -4,43 +4,48 @@ const BaseDNS = require('./base')
const log = require('../../utils/util.log') const log = require('../../utils/util.log')
const dohQueryAsync = promisify(doh.query) const dohQueryAsync = promisify(doh.query)
module.exports = class DNSOverHTTPS extends BaseDNS { const PRESETS = {
constructor(dnsServer) { 'github.com': ['20.27.177.113', '20.205.243.166', '20.200.245.247'],
super(); 'api.github.com': ['20.27.177.116', '20.205.243.168', '20.200.245.245']
this.dnsServer = dnsServer; }
class DNSOverHTTPS extends BaseDNS {
constructor (dnsServer) {
super()
this.dnsServer = dnsServer
this.lastLookupError = null // 添加属性来记录最后一次查询错误
} }
async _lookup(hostname) { processQueryResult (result) {
// 直接判断域名是否为example.com if (result.answers.length === 0) {
if (hostname === 'github.com') { log.error('该域名没有ip地址解析:', result.name)
log.info('域名github.com使用内置IP集') return []
// 返回预设的IP地址集
return ['20.27.177.113', '20.205.243.166', '20.200.245.247']
// 20.27.177.113日本(三网平均延时88MS(三网优秀)) 20.205.243.166新加坡(三网平均延时96MS(电信联通106.5平均延时移动平均77MS)) 20.200.245.247韩国(三网平均108ms(移动平均120ms))
}
if (hostname === 'api.github.com') {
log.info('域名api.github.com使用内置IP集')
// 返回预设的IP地址集
return ['20.27.177.116', '20.205.243.168', '20.200.245.245']
} }
return result.answers
.filter(item => item.type === 'A')
.map(item => item.data)
}
async _lookup (hostname) {
const preset = PRESETS[hostname]
if (preset) {
log.debug(`使用预设IP集${hostname}`)
return preset
}
try { try {
const result = await dohQueryAsync({ url this.dnsServer }, [{ type 'A', name hostname }]) const result = await dohQueryAsync({ url: this.dnsServer }, [{ type: 'A', name: hostname }])
if (result.answers.length === 0) { const ips = this.processQueryResult(result)
// 说明没有获取到ip if (ips.length > 0) {
log.info('该域名没有ip地址解析', hostname) log.info('获取到域名地址:', hostname, ips)
return [];
} }
const ret = result.answers.filter(item => item.type === 'A').map(item => item.data) this.lastLookupError = null // 重置错误记录
if (ret.length === 0) { return ips
log.info('该域名没有IPv4地址解析', hostname)
} else {
log.info('获取到域名地址:', hostname, JSON.stringify(ret))
}
return ret;
} catch (e) { } catch (e) {
log.warn('DNS query error', hostname, ', dns', this.dnsServer, ', error', e) log.error('DNS查询错误:', hostname, ', DNS服务器:', this.dnsServer, ', 错误:', e)
return []; this.lastLookupError = e // 记录错误
throw e // 重新抛出错误,让调用者处理
} }
} }
} }
module.exports = DNSOverHTTPS