optimize: 日志优化、DNS相关单测调整、部分代码小调整。
parent
867909cbf5
commit
495f65c92b
|
@ -14,7 +14,7 @@ function parseVersion (version) {
|
|||
* @param log 日志对象
|
||||
* @returns {number} 比较线上版本号是否为更新的版本,大于0=是|0=相等|小于0=否|-999=出现异常,比较结果未知
|
||||
*/
|
||||
export function isNewVersion (onlineVersion, currentVersion, log = console) {
|
||||
export function isNewVersion (onlineVersion, currentVersion, log = null) {
|
||||
if (onlineVersion === currentVersion) {
|
||||
return 0
|
||||
}
|
||||
|
|
|
@ -18,9 +18,6 @@ function handleServerStartError (message, err, app, api) {
|
|||
// 避免重复弹窗
|
||||
const now = Date.now()
|
||||
if (latestConfirmTime != null && now - latestConfirmTime < 1000) {
|
||||
if (now - latestConfirmTime > 5000) {
|
||||
latestConfirmTime = null
|
||||
}
|
||||
return
|
||||
}
|
||||
latestConfirmTime = now
|
||||
|
|
|
@ -313,7 +313,7 @@ export default {
|
|||
<div v-if="activeTabKey === '4'">
|
||||
<a-row style="margin-top:10px">
|
||||
<a-col span="21">
|
||||
<div>这里配置的域名不会通过代理</div>
|
||||
<div>配置为<code>不代理</code>的域名不会通过代理</div>
|
||||
</a-col>
|
||||
<a-col span="3">
|
||||
<a-button style="margin-left:8px" type="primary" icon="plus" @click="addWhiteList()" />
|
||||
|
|
|
@ -126,10 +126,18 @@ module.exports = class BaseDNS {
|
|||
|
||||
async _lookup (hostname) {
|
||||
const start = Date.now()
|
||||
|
||||
let response
|
||||
try {
|
||||
// 执行DNS查询
|
||||
log.debug(`[DNS-over-${this.dnsType} '${this.dnsName}'] query start: ${hostname}`)
|
||||
const response = await this._doDnsQuery(hostname)
|
||||
response = await this._doDnsQuery(hostname, 'A', start)
|
||||
} catch {
|
||||
// 异常日志在 _doDnsQuery已经打印过,这里就不再打印了
|
||||
return []
|
||||
}
|
||||
|
||||
try {
|
||||
const cost = Date.now() - start
|
||||
log.debug(`[DNS-over-${this.dnsType} '${this.dnsName}'] query end: ${hostname}, cost: ${cost} ms, response:`, response)
|
||||
|
||||
|
@ -147,21 +155,23 @@ module.exports = class BaseDNS {
|
|||
|
||||
return ret
|
||||
} catch (e) {
|
||||
log.error(`[DNS-over-${this.dnsType} '${this.dnsName}'] DNS query error, hostname: ${hostname}${this.dnsServer ? `, dnsServer: ${this.dnsServer}` : ''}, cost: ${Date.now() - start} ms, error:`, e)
|
||||
log.error(`[DNS-over-${this.dnsType} '${this.dnsName}'] 解读响应失败,response:`, response, ', error:', e)
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
_doDnsQuery (hostname, type) {
|
||||
_doDnsQuery (hostname, type = 'A', start) {
|
||||
if (start == null) {
|
||||
start = Date.now()
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
// 设置超时任务
|
||||
let isOver = false
|
||||
const timeout = 8000
|
||||
const timeoutId = setTimeout(() => {
|
||||
if (!isOver) {
|
||||
log.error(`[DNS-over-${this.dnsType} '${this.dnsName}'] DNS查询超时, hostname: ${hostname}, sni: ${this.dnsServerName || '无'}, type: ${type}${this.dnsServer ? `, dnsServer: ${this.dnsServer}` : ''}${this.dnsServerPort ? `:${this.dnsServerPort}` : ''}, cost: ${Date.now() - start} ms`)
|
||||
reject(new Error('DNS查询超时'))
|
||||
}
|
||||
}, timeout)
|
||||
|
@ -176,11 +186,17 @@ module.exports = class BaseDNS {
|
|||
.catch((e) => {
|
||||
isOver = true
|
||||
clearTimeout(timeoutId)
|
||||
if (e.message === 'DNS查询超时') {
|
||||
log.error(`[DNS-over-${this.dnsType} '${this.dnsName}'] DNS查询超时. hostname: ${hostname}, sni: ${this.dnsServerName || '无'}, type: ${type}${this.dnsServer ? `, dnsServer: ${this.dnsServer}` : ''}${this.dnsServerPort ? `:${this.dnsServerPort}` : ''}, cost: ${Date.now() - start} ms`)
|
||||
} else {
|
||||
log.error(`[DNS-over-${this.dnsType} '${this.dnsName}'] DNS查询错误, hostname: ${hostname}, sni: ${this.dnsServerName || '无'}, type: ${type}${this.dnsServer ? `, dnsServer: ${this.dnsServer}` : ''}${this.dnsServerPort ? `:${this.dnsServerPort}` : ''}, cost: ${Date.now() - start} ms, error:`, e)
|
||||
}
|
||||
reject(e)
|
||||
})
|
||||
} catch (e) {
|
||||
isOver = true
|
||||
clearTimeout(timeoutId)
|
||||
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, error:`, e)
|
||||
reject(e)
|
||||
}
|
||||
})
|
||||
|
|
|
@ -28,7 +28,7 @@ module.exports = {
|
|||
if (type == null) {
|
||||
if (server.startsWith('https://') || server.startsWith('http://')) {
|
||||
type = 'https'
|
||||
} else if (server.startsWith('tls://')) {
|
||||
} else if (server.startsWith('tls://') || server.startsWith('dot://')) {
|
||||
type = 'tls'
|
||||
} else if (server.startsWith('tcp://')) {
|
||||
type = 'tcp'
|
||||
|
@ -65,7 +65,7 @@ module.exports = {
|
|||
if (type === 'tls' || type === 'dot' || type === 'dns-over-tls') {
|
||||
// 基于 tls
|
||||
dnsMap[provider] = new DNSOverTLS(provider, conf.cacheSize, preSetIpList, server, port, conf.sni || conf.servername)
|
||||
} else if (type === 'tcp' || type === 'dns-over-tcp') {
|
||||
} else if (type === 'tcp') {
|
||||
// 基于 tcp
|
||||
dnsMap[provider] = new DNSOverTCP(provider, conf.cacheSize, preSetIpList, server, port)
|
||||
} else {
|
||||
|
|
|
@ -4,7 +4,7 @@ const dnsPacket = require('dns-packet')
|
|||
const randi = require('random-int')
|
||||
const BaseDNS = require('./base')
|
||||
|
||||
const defaultPort = 53 // UDP类型的DNS服务默认端口号
|
||||
const defaultPort = 53 // TCP类型的DNS服务默认端口号
|
||||
|
||||
module.exports = class DNSOverTCP extends BaseDNS {
|
||||
constructor (dnsName, cacheSize, preSetIpList, dnsServer, dnsServerPort) {
|
||||
|
|
|
@ -55,7 +55,7 @@ module.exports = class DNSOverUDP extends BaseDNS {
|
|||
// 设置超时任务
|
||||
timeoutId = setTimeout(() => {
|
||||
if (!isOver) {
|
||||
reject(new Error('查询超时'))
|
||||
reject(new Error('DNS查询超时'))
|
||||
udpClient.close()
|
||||
}
|
||||
}, timeout)
|
||||
|
|
|
@ -92,9 +92,9 @@ module.exports = {
|
|||
// 如果未手动配置需要缓存,则不允许使用缓存
|
||||
const maxAge = cacheReq.getMaxAge(interceptOpt)
|
||||
if (maxAge == null || maxAge <= 0) {
|
||||
replaceHeaders['cache-control'] = '[remove]'
|
||||
replaceHeaders['last-modified'] = '[remove]'
|
||||
replaceHeaders.expires = '[remove]'
|
||||
replaceHeaders['cache-control'] = REMOVE
|
||||
replaceHeaders['last-modified'] = REMOVE
|
||||
replaceHeaders.expires = REMOVE
|
||||
}
|
||||
|
||||
actions += `${actions ? ',' : ''}download:${filename}`
|
||||
|
|
|
@ -133,7 +133,7 @@ class SpeedTester {
|
|||
async doTest (item, aliveList) {
|
||||
try {
|
||||
const ret = await this.testOne(item)
|
||||
item.title = `${ret.by}测速成功:${item.host}`
|
||||
item.title = `${ret.by}测速成功:${ret.target}`
|
||||
log.info(`[speed] test success: ${this.hostname} ➜ ${item.host}:${this.port} from DNS '${item.dns}'`)
|
||||
_.merge(item, ret)
|
||||
aliveList.push({ ...ret, ...item })
|
||||
|
@ -175,7 +175,7 @@ class SpeedTester {
|
|||
clearTimeout(timeoutId)
|
||||
|
||||
const connectionTime = Date.now()
|
||||
resolve({ status: 'success', by: 'TCP', time: connectionTime - startTime })
|
||||
resolve({ status: 'success', by: 'TCP', target: `${host}:${this.port}`, time: connectionTime - startTime })
|
||||
client.end()
|
||||
})
|
||||
client.on('error', (e) => {
|
||||
|
@ -249,7 +249,7 @@ class SpeedTester {
|
|||
// } else {
|
||||
// // 计算平均延迟
|
||||
// const avg = times.reduce((a, b) => a + b, 0) / times.length
|
||||
// resolve({ status: 'success', by: 'PING', time: Math.round(avg) })
|
||||
// resolve({ status: 'success', by: 'PING', target: host, time: Math.round(avg) })
|
||||
// }
|
||||
// })
|
||||
// })
|
||||
|
@ -272,7 +272,7 @@ class SpeedTester {
|
|||
// reject(new Error(`TCP测速失败:${e.message};PING测速失败:${e2.message};`))
|
||||
// })
|
||||
|
||||
reject(new Error(`TCP测速失败:${e.message}`))
|
||||
reject(new Error(`TCP测速失败:${item.host}:${this.port} ${e.message}`))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
const fs = require('node:fs')
|
||||
const path = require('node:path')
|
||||
const lodash = require('lodash')
|
||||
const jsonApi = require('./json')
|
||||
const dnsUtil = require('./lib/dns')
|
||||
const interceptorImpls = require('./lib/interceptor')
|
||||
const scriptInterceptor = require('./lib/interceptor/impl/res/script')
|
||||
|
@ -110,7 +109,7 @@ module.exports = (serverConfig) => {
|
|||
// 配置了白名单的域名,将跳过代理
|
||||
const inWhiteList = !!matchUtil.matchHostname(whiteList, hostname, 'in whiteList')
|
||||
if (inWhiteList) {
|
||||
log.info(`为白名单域名,不拦截: ${hostname}, headers:`, jsonApi.stringify2(req.headers))
|
||||
log.info(`为白名单域名,不拦截: ${hostname}`)
|
||||
return false // 不拦截
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
const lodash = require('lodash')
|
||||
const log = require('./util.log.server')
|
||||
const mergeApi = require('@docmirror/dev-sidecar/src/merge')
|
||||
|
||||
function isMatched (url, regexp) {
|
||||
if (regexp === '.*' || regexp === '*' || regexp === 'true' || regexp === true) {
|
||||
|
@ -113,16 +114,6 @@ function merge (oldObj, newObj) {
|
|||
}
|
||||
})
|
||||
}
|
||||
function deleteNullItems (target) {
|
||||
lodash.forEach(target, (item, key) => {
|
||||
if (item == null || item === '[delete]') {
|
||||
delete target[key]
|
||||
}
|
||||
if (lodash.isObject(item)) {
|
||||
deleteNullItems(item)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function matchHostnameAll (hostMap, hostname, action) {
|
||||
// log.debug('matchHostname-all:', action, hostMap)
|
||||
|
@ -197,7 +188,7 @@ function matchHostnameAll (hostMap, hostname, action) {
|
|||
}
|
||||
|
||||
if (!lodash.isEmpty(values)) {
|
||||
deleteNullItems(values)
|
||||
mergeApi.deleteNullItems(values)
|
||||
log.info(`matchHostname-all: ${action}: '${hostname}':`, JSON.stringify(values))
|
||||
return values
|
||||
} else {
|
||||
|
|
|
@ -6,8 +6,6 @@ const servers = [
|
|||
'https://max.rethinkdns.com/dns-query',
|
||||
'https://sky.rethinkdns.com/dns-query',
|
||||
'https://doh.opendns.com/dns-query',
|
||||
'https://1.1.1.1/dns-query',
|
||||
'https://dns.cloudflare.com/dns-query',
|
||||
'https://cloudflare-dns.com/dns-query',
|
||||
'https://dns.google/dns-query',
|
||||
'https://dns.bebasid.com/unfiltered',
|
||||
|
@ -30,12 +28,15 @@ const servers = [
|
|||
'https://jp.tiarap.org/dns-query',
|
||||
'https://dns.adguard.com/dns-query',
|
||||
'https://rubyfish.cn/dns-query',
|
||||
'https://i.233py.com/dns-query'
|
||||
|
||||
'https://i.233py.com/dns-query',
|
||||
]
|
||||
|
||||
const hostname1 = 'github.com'
|
||||
const hostnames = [
|
||||
'github.com',
|
||||
'mvnrepository.com',
|
||||
]
|
||||
const sni = 'baidu.com'
|
||||
// const sni = ''
|
||||
|
||||
console.log(`\n--------------- 测试DoH的SNI功能:共 ${servers.length} 个服务,${hostnames.length} 个域名,SNI: ${sni || '无'} ---------------\n`)
|
||||
|
||||
|
@ -44,15 +45,22 @@ let success = 0
|
|||
let error = 0
|
||||
const arr = []
|
||||
|
||||
function count (isSuccess, i, doh, result) {
|
||||
n++
|
||||
function count (isSuccess, hostname, idx, dns, result, cost) {
|
||||
if (isSuccess) {
|
||||
success++
|
||||
arr[i] = `${doh.dnsServer} : ${hostname1} -> ${result.answers[0].data}`;
|
||||
} else error++
|
||||
const ipList = []
|
||||
for (const answer of result.answers) {
|
||||
ipList[ipList.length] = answer.data;
|
||||
}
|
||||
arr[idx] = `${dns.dnsServer} : ${hostname} -> [ ${ipList.join(', ')} ] , cost: ${cost} ms`;
|
||||
} else {
|
||||
error++
|
||||
}
|
||||
|
||||
if (n === servers.length) {
|
||||
console.info(`\n\n=============================================================================\n全部测完:总计:${servers.length}, 成功:${success},失败:${error}`);
|
||||
n++
|
||||
|
||||
if (n === servers.length * hostnames.length) {
|
||||
console.info(`\n\n=============================================================================\n全部测完:总计:${servers.length * hostnames.length}, 成功:${success},失败:${error}`);
|
||||
for (const item of arr) {
|
||||
if (item) {
|
||||
console.info(item);
|
||||
|
@ -62,16 +70,21 @@ function count (isSuccess, i, doh, result) {
|
|||
}
|
||||
}
|
||||
|
||||
let x = 0;
|
||||
for (let i = 0; i < servers.length; i++) {
|
||||
const n = i;
|
||||
const doh = new DNSOverHTTPS(`dns${i}`, null, null, servers[i], sni)
|
||||
doh._doDnsQuery(hostname1)
|
||||
.then((result) => {
|
||||
// console.info(`===> test testDoH '${doh.dnsServer}': ${hostname1} ->`, result.answers, '\n\n')
|
||||
count(true, n, doh, result)
|
||||
})
|
||||
.catch((e) => {
|
||||
// console.error(`===> test testDoH '${doh.dnsServer}': ${hostname1} 失败:`, e, '\n\n')
|
||||
count(false)
|
||||
})
|
||||
for (const hostname of hostnames) {
|
||||
const dns = new DNSOverHTTPS(`dns-${i}-${hostname}`, null, null, servers[i], sni)
|
||||
const start = Date.now()
|
||||
const idx = x;
|
||||
dns._doDnsQuery(hostname)
|
||||
.then((result) => {
|
||||
console.info(`===> ${dns.dnsServer}: ${hostname} ->`, result.answers, '\n\n')
|
||||
count(true, hostname, idx, dns, result, Date.now() - start)
|
||||
})
|
||||
.catch((e) => {
|
||||
console.error(`===> ${dns.dnsServer}: ${hostname} 失败:`, e, '\n\n')
|
||||
count(false, hostname)
|
||||
})
|
||||
x++;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,149 @@
|
|||
import assert from 'node:assert'
|
||||
import dns from '../src/lib/dns/index.js'
|
||||
import matchUtil from '../src/utils/util.match.js'
|
||||
|
||||
const presetIp = '100.100.100.100'
|
||||
const preSetIpList = matchUtil.domainMapRegexply({
|
||||
'xxx.com': [
|
||||
presetIp
|
||||
]
|
||||
})
|
||||
|
||||
// 境外DNS测试
|
||||
const dnsProviders = dns.initDNS({
|
||||
// udp
|
||||
cloudflareUdp: {
|
||||
server: 'udp://1.1.1.1',
|
||||
},
|
||||
quad9Udp: {
|
||||
server: 'udp://9.9.9.9',
|
||||
},
|
||||
|
||||
// tcp
|
||||
cloudflareTcp: {
|
||||
server: 'tcp://1.1.1.1',
|
||||
},
|
||||
quad9Tcp: {
|
||||
server: 'tcp://9.9.9.9',
|
||||
},
|
||||
|
||||
// https
|
||||
cloudflare: {
|
||||
server: 'https://1.1.1.1/dns-query',
|
||||
},
|
||||
quad9: {
|
||||
server: 'https://9.9.9.9/dns-query',
|
||||
forSNI: true,
|
||||
},
|
||||
rubyfish: {
|
||||
server: 'https://rubyfish.cn/dns-query',
|
||||
},
|
||||
py233: {
|
||||
server: ' https://i.233py.com/dns-query',
|
||||
},
|
||||
|
||||
// tls
|
||||
cloudflareTLS: {
|
||||
type: 'tls',
|
||||
server: '1.1.1.1',
|
||||
servername: 'cloudflare-dns.com',
|
||||
},
|
||||
quad9TLS: {
|
||||
server: 'tls://9.9.9.9',
|
||||
servername: 'dns.quad9.net',
|
||||
},
|
||||
}, preSetIpList)
|
||||
|
||||
|
||||
const hasPresetHostname = 'xxx.com'
|
||||
const noPresetHostname = 'yyy.com'
|
||||
|
||||
const hostname1 = 'github.com'
|
||||
const hostname2 = 'api.github.com'
|
||||
const hostname3 = 'hk.docmirror.cn'
|
||||
const hostname4 = 'github.docmirror.cn'
|
||||
const hostname5 = 'gh.docmirror.top'
|
||||
const hostname6 = 'gh2.docmirror.top'
|
||||
|
||||
let ip
|
||||
|
||||
|
||||
console.log('\n--------------- test ForSNI ---------------\n')
|
||||
console.log(`===> test ForSNI: ${dnsProviders.ForSNI.dnsName}`, '\n\n')
|
||||
assert.strictEqual(dnsProviders.ForSNI, dnsProviders.quad9)
|
||||
|
||||
|
||||
console.log('\n--------------- test PreSet ---------------\n')
|
||||
ip = await dnsProviders.PreSet.lookup(hasPresetHostname)
|
||||
console.log(`===> test PreSet: ${hasPresetHostname} ->`, ip, '\n\n')
|
||||
console.log('\n\n')
|
||||
assert.strictEqual(ip, presetIp) // 预设过IP,等于预设的IP
|
||||
|
||||
ip = await dnsProviders.PreSet.lookup(noPresetHostname)
|
||||
console.log(`===> test PreSet: ${noPresetHostname} ->`, ip, '\n\n')
|
||||
console.log('\n\n')
|
||||
assert.strictEqual(ip, noPresetHostname) // 未预设IP,等于域名自己
|
||||
|
||||
|
||||
console.log('\n--------------- test udp ---------------\n')
|
||||
ip = await dnsProviders.cloudflareUdp.lookup(hasPresetHostname)
|
||||
assert.strictEqual(ip, presetIp) // test preset
|
||||
console.log('\n\n')
|
||||
|
||||
assert.strictEqual(dnsProviders.cloudflareUdp.dnsType, 'UDP')
|
||||
ip = await dnsProviders.cloudflareUdp.lookup(hostname1)
|
||||
console.log(`===> test cloudflare: ${hostname1} ->`, ip, '\n\n')
|
||||
|
||||
assert.strictEqual(dnsProviders.quad9Udp.dnsType, 'UDP')
|
||||
ip = await dnsProviders.quad9Udp.lookup(hostname1)
|
||||
console.log(`===> test quad9: ${hostname1} ->`, ip, '\n\n')
|
||||
|
||||
|
||||
console.log('\n--------------- test tcp ---------------\n')
|
||||
ip = await dnsProviders.cloudflareTcp.lookup(hasPresetHostname)
|
||||
assert.strictEqual(ip, presetIp) // test preset
|
||||
console.log('\n\n')
|
||||
|
||||
assert.strictEqual(dnsProviders.cloudflareTcp.dnsType, 'TCP')
|
||||
ip = await dnsProviders.cloudflareTcp.lookup(hostname1)
|
||||
console.log(`===> test cloudflare: ${hostname1} ->`, ip, '\n\n')
|
||||
|
||||
assert.strictEqual(dnsProviders.quad9Tcp.dnsType, 'TCP')
|
||||
ip = await dnsProviders.quad9Tcp.lookup(hostname1)
|
||||
console.log(`===> test quad9: ${hostname1} ->`, ip, '\n\n')
|
||||
|
||||
|
||||
console.log('\n--------------- test https ---------------\n')
|
||||
ip = await dnsProviders.cloudflare.lookup(hasPresetHostname)
|
||||
assert.strictEqual(ip, presetIp) // test preset
|
||||
console.log('\n\n')
|
||||
|
||||
assert.strictEqual(dnsProviders.cloudflare.dnsType, 'HTTPS')
|
||||
ip = await dnsProviders.cloudflare.lookup(hostname1)
|
||||
console.log(`===> test cloudflare: ${hostname1} ->`, ip, '\n\n')
|
||||
|
||||
assert.strictEqual(dnsProviders.quad9.dnsType, 'HTTPS')
|
||||
ip = await dnsProviders.quad9.lookup(hostname1)
|
||||
console.log(`===> test quad9: ${hostname1} ->`, ip, '\n\n')
|
||||
|
||||
assert.strictEqual(dnsProviders.rubyfish.dnsType, 'HTTPS')
|
||||
ip = await dnsProviders.rubyfish.lookup(hostname1)
|
||||
console.log(`===> test rubyfish: ${hostname1} ->`, ip, '\n\n')
|
||||
|
||||
assert.strictEqual(dnsProviders.py233.dnsType, 'HTTPS')
|
||||
ip = await dnsProviders.py233.lookup(hostname1)
|
||||
console.log(`===> test py233: ${hostname1} ->`, ip, '\n\n')
|
||||
|
||||
|
||||
console.log('\n--------------- test TLS ---------------\n')
|
||||
ip = await dnsProviders.cloudflareTLS.lookup(hasPresetHostname)
|
||||
assert.strictEqual(ip, presetIp) // test preset
|
||||
console.log('\n\n')
|
||||
|
||||
assert.strictEqual(dnsProviders.cloudflareTLS.dnsType, 'TLS')
|
||||
ip = await dnsProviders.cloudflareTLS.lookup(hostname1)
|
||||
console.log(`===> test cloudflareTLS: ${hostname1} ->`, ip, '\n\n')
|
||||
|
||||
assert.strictEqual(dnsProviders.quad9TLS.dnsType, 'TLS')
|
||||
ip = await dnsProviders.quad9TLS.lookup(hostname1)
|
||||
console.log(`===> test quad9TLS: ${hostname1} ->`, ip, '\n\n')
|
|
@ -9,17 +9,9 @@ const preSetIpList = matchUtil.domainMapRegexply({
|
|||
]
|
||||
})
|
||||
|
||||
// 常用DNS测试
|
||||
const dnsProviders = dns.initDNS({
|
||||
// https
|
||||
cloudflare: {
|
||||
type: 'https',
|
||||
server: 'https://1.1.1.1/dns-query',
|
||||
cacheSize: 1000,
|
||||
},
|
||||
quad9: {
|
||||
server: 'https://9.9.9.9/dns-query',
|
||||
cacheSize: 1000,
|
||||
},
|
||||
aliyun: {
|
||||
type: 'https',
|
||||
server: 'https://dns.alidns.com/dns-query',
|
||||
|
@ -33,28 +25,10 @@ const dnsProviders = dns.initDNS({
|
|||
safe360: {
|
||||
server: 'https://doh.360.cn/dns-query',
|
||||
cacheSize: 1000,
|
||||
},
|
||||
rubyfish: {
|
||||
server: 'https://rubyfish.cn/dns-query',
|
||||
cacheSize: 1000,
|
||||
},
|
||||
py233: {
|
||||
server: ' https://i.233py.com/dns-query',
|
||||
cacheSize: 1000,
|
||||
forSNI: true,
|
||||
},
|
||||
|
||||
// tls
|
||||
cloudflareTLS: {
|
||||
type: 'tls',
|
||||
server: '1.1.1.1',
|
||||
servername: 'cloudflare-dns.com',
|
||||
cacheSize: 1000,
|
||||
},
|
||||
quad9TLS: {
|
||||
server: 'tls://9.9.9.9',
|
||||
servername: 'dns.quad9.net',
|
||||
cacheSize: 1000,
|
||||
},
|
||||
aliyunTLS: {
|
||||
server: 'tls://223.5.5.5:853',
|
||||
cacheSize: 1000,
|
||||
|
@ -93,7 +67,9 @@ const dnsProviders = dns.initDNS({
|
|||
}, preSetIpList)
|
||||
|
||||
|
||||
const presetHostname = 'xxx.com'
|
||||
const hasPresetHostname = 'xxx.com'
|
||||
const noPresetHostname = 'yyy.com'
|
||||
|
||||
const hostname1 = 'github.com'
|
||||
const hostname2 = 'api.github.com'
|
||||
const hostname3 = 'hk.docmirror.cn'
|
||||
|
@ -104,26 +80,36 @@ const hostname6 = 'gh2.docmirror.top'
|
|||
let ip
|
||||
|
||||
|
||||
console.log('\n--------------- test ForSNI ---------------\n')
|
||||
console.log(`===> test ForSNI: ${dnsProviders.ForSNI.dnsName}`, '\n\n')
|
||||
assert.strictEqual(dnsProviders.ForSNI, dnsProviders.safe360)
|
||||
|
||||
const dnsProviders2 = dns.initDNS({
|
||||
aliyun: {
|
||||
server: 'udp://223.5.5.5',
|
||||
},
|
||||
}, {})
|
||||
console.log(`===> test ForSNI2: ${dnsProviders2.ForSNI.dnsName}`, '\n\n')
|
||||
assert.strictEqual(dnsProviders2.ForSNI, dnsProviders2.PreSet) // 未配置forSNI的DNS时,默认使用PreSet作为ForSNI
|
||||
|
||||
|
||||
console.log('\n--------------- test PreSet ---------------\n')
|
||||
ip = await dnsProviders.PreSet.lookup(presetHostname)
|
||||
console.log('===> test PreSet:', ip, '\n\n')
|
||||
ip = await dnsProviders.PreSet.lookup(hasPresetHostname)
|
||||
console.log(`===> test PreSet: ${hasPresetHostname} ->`, ip, '\n\n')
|
||||
console.log('\n\n')
|
||||
assert.strictEqual(ip, presetIp) // test preset
|
||||
assert.strictEqual(ip, presetIp) // 预设过IP,等于预设的IP
|
||||
|
||||
ip = await dnsProviders.PreSet.lookup(noPresetHostname)
|
||||
console.log(`===> test PreSet: ${noPresetHostname} ->`, ip, '\n\n')
|
||||
console.log('\n\n')
|
||||
assert.strictEqual(ip, noPresetHostname) // 未预设IP,等于域名自己
|
||||
|
||||
|
||||
console.log('\n--------------- test https ---------------\n')
|
||||
ip = await dnsProviders.cloudflare.lookup(presetHostname)
|
||||
ip = await dnsProviders.aliyun.lookup(hasPresetHostname)
|
||||
assert.strictEqual(ip, presetIp) // test preset
|
||||
console.log('\n\n')
|
||||
|
||||
assert.strictEqual(dnsProviders.cloudflare.dnsType, 'HTTPS')
|
||||
// ip = await dnsProviders.cloudflare.lookup(hostname1)
|
||||
// console.log(`===> test cloudflare: ${hostname1} ->`, ip, '\n\n')
|
||||
|
||||
assert.strictEqual(dnsProviders.quad9.dnsType, 'HTTPS')
|
||||
// ip = await dnsProviders.quad9.lookup(hostname1)
|
||||
// console.log(`===> test quad9: ${hostname1} ->`, ip, '\n\n')
|
||||
|
||||
assert.strictEqual(dnsProviders.aliyun.dnsType, 'HTTPS')
|
||||
ip = await dnsProviders.aliyun.lookup(hostname1)
|
||||
console.log(`===> test aliyun: ${hostname1} ->`, ip, '\n\n')
|
||||
|
@ -136,28 +122,12 @@ assert.strictEqual(dnsProviders.safe360.dnsType, 'HTTPS')
|
|||
ip = await dnsProviders.safe360.lookup(hostname1)
|
||||
console.log(`===> test safe360: ${hostname1} ->`, ip, '\n\n')
|
||||
|
||||
assert.strictEqual(dnsProviders.rubyfish.dnsType, 'HTTPS')
|
||||
// ip = await dnsProviders.rubyfish.lookup(hostname1)
|
||||
// console.log(`===> test rubyfish: ${hostname1} ->`, ip, '\n\n')
|
||||
|
||||
assert.strictEqual(dnsProviders.py233.dnsType, 'HTTPS')
|
||||
// ip = await dnsProviders.py233.lookup(hostname1)
|
||||
// console.log(`===> test py233: ${hostname1} ->`, ip, '\n\n')
|
||||
|
||||
|
||||
console.log('\n--------------- test TLS ---------------\n')
|
||||
ip = await dnsProviders.cloudflareTLS.lookup(presetHostname)
|
||||
ip = await dnsProviders.aliyunTLS.lookup(hasPresetHostname)
|
||||
assert.strictEqual(ip, presetIp) // test preset
|
||||
console.log('\n\n')
|
||||
|
||||
assert.strictEqual(dnsProviders.cloudflareTLS.dnsType, 'TLS')
|
||||
// ip = await dnsProviders.cloudflareTLS.lookup(hostname1)
|
||||
// console.log(`===> test cloudflareTLS: ${hostname1} ->`, ip, '\n\n')
|
||||
|
||||
assert.strictEqual(dnsProviders.quad9TLS.dnsType, 'TLS')
|
||||
// ip = await dnsProviders.quad9TLS.lookup(hostname1)
|
||||
// console.log(`===> test quad9TLS: ${hostname1} ->`, ip, '\n\n')
|
||||
|
||||
assert.strictEqual(dnsProviders.aliyunTLS.dnsType, 'TLS')
|
||||
ip = await dnsProviders.aliyunTLS.lookup(hostname1)
|
||||
console.log(`===> test aliyunTLS: ${hostname1} ->`, ip, '\n\n')
|
||||
|
@ -172,7 +142,7 @@ console.log(`===> test safe360TLS: ${hostname1} ->`, ip, '\n\n')
|
|||
|
||||
|
||||
console.log('\n--------------- test TCP ---------------\n')
|
||||
ip = await dnsProviders.googleTCP.lookup(presetHostname)
|
||||
ip = await dnsProviders.googleTCP.lookup(hasPresetHostname)
|
||||
assert.strictEqual(ip, presetIp) // test preset
|
||||
console.log('\n\n')
|
||||
|
||||
|
@ -186,7 +156,7 @@ console.log(`===> test aliyunTCP: ${hostname1} ->`, ip, '\n\n')
|
|||
|
||||
|
||||
console.log('\n--------------- test UDP ---------------\n')
|
||||
ip = await dnsProviders.googleUDP.lookup(presetHostname)
|
||||
ip = await dnsProviders.googleUDP.lookup(hasPresetHostname)
|
||||
assert.strictEqual(ip, presetIp) // test preset
|
||||
console.log('\n\n')
|
||||
|
||||
|
@ -200,17 +170,13 @@ console.log(`===> test aliyunUDP: ${hostname1} ->`, ip, '\n\n')
|
|||
|
||||
dnsProviders.aliyunUDP.lookup(hostname1).then(ip0 => {
|
||||
console.log(`===> test aliyunUDP: ${hostname1} ->`, ip0, '\n\n')
|
||||
assert.strictEqual(ip0, ip)
|
||||
})
|
||||
dnsProviders.aliyunUDP.lookup(hostname2).then(ip0 => {
|
||||
console.log(`===> test aliyunUDP: ${hostname2} ->`, ip0, '\n\n')
|
||||
assert.notStrictEqual(ip0, ip)
|
||||
})
|
||||
dnsProviders.aliyunUDP.lookup('baidu.com').then(ip0 => {
|
||||
console.log('===> test aliyunUDP: baidu.com ->', ip0, '\n\n')
|
||||
assert.notStrictEqual(ip0, ip)
|
||||
})
|
||||
dnsProviders.aliyunUDP.lookup('gitee.com').then(ip0 => {
|
||||
console.log('===> test aliyunUDP: gitee.com ->', ip0, '\n\n')
|
||||
assert.notStrictEqual(ip0, ip)
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue