feature: IP预设置可配置化。

pull/321/head
王良 2024-07-08 16:42:34 +08:00
parent 6af96e2630
commit a02c2ff306
10 changed files with 143 additions and 99 deletions

View File

@ -218,9 +218,9 @@ const intercepts = {
```js ```js
dns: { dns: {
mapping: { mapping: {
'api.github.com': 'usa', // "解决push的时候需要输入密码的问题", 'api.github.com': 'cloudflare', // "解决push的时候需要输入密码的问题",
'gist.github.com': 'usa' // 解决gist无法访问的问题 'gist.github.com': 'cloudflare' // 解决gist无法访问的问题
'*.githubusercontent.com': 'usa' // 解决github头像经常下载不到的问题 '*.githubusercontent.com': 'cloudflare' // 解决github头像经常下载不到的问题
} }
} }
``` ```

View File

@ -260,6 +260,70 @@ module.exports = {
} }
} }
}, },
// 预设置IP列表
preSetIpList: {
'github.com': [
'20.200.245.247',
'20.27.177.113',
'20.205.243.166',
'20.248.137.48',
'140.82.116.4', '140.82.114.4', '140.82.113.3',
'20.26.156.215',
'20.87.245.0',
'20.201.28.151',
'140.82.121.3', '140.82.121.4'
],
'api.github.com': [
'20.87.245.6',
'140.82.112.5',
'140.82.116.6',
'20.26.156.210',
'20.200.245.245',
'20.27.177.116',
'20.248.137.49',
'20.201.28.148',
'140.82.113.6',
'20.205.243.168',
'140.82.121.6'
],
'codeload.github.com': [
'20.26.156.216',
'20.27.177.114',
'20.87.245.7',
'20.200.245.246',
'20.201.28.149',
'20.205.243.165',
'20.248.137.55',
'140.82.113.9',
'140.82.114.10',
'140.82.116.10',
'140.82.121.9'
],
'*.githubusercontent.com': [
'185.199.108.133',
'185.199.109.133',
'185.199.110.133',
'185.199.111.133'
],
'github.githubassets.com': [
'185.199.108.154',
'185.199.109.154',
'185.199.110.154',
'185.199.111.154'
],
'collector.github.com': ['0.0.0.0'],
'github.io': [
'185.199.108.153',
'185.199.109.153',
'185.199.110.153',
'185.199.111.153'
],
'hub.docker.com': [
'44.221.37.199',
'52.44.227.212',
'54.156.140.159'
]
},
whiteList: { whiteList: {
'*.cn': true, '*.cn': true,
'cn.*': true, 'cn.*': true,
@ -305,6 +369,7 @@ module.exports = {
mapping: { mapping: {
'*.github.com': 'quad9', '*.github.com': 'quad9',
'*github*.com': 'quad9', '*github*.com': 'quad9',
'*.githubusercontent.com': 'quad9',
'*.github.io': 'quad9', '*.github.io': 'quad9',
'*.docker.com': 'quad9', '*.docker.com': 'quad9',
'*.stackoverflow.com': 'quad9', '*.stackoverflow.com': 'quad9',
@ -325,7 +390,7 @@ module.exports = {
enabled: true, enabled: true,
interval: 300000, interval: 300000,
hostnameList: ['github.com'], hostnameList: ['github.com'],
dnsProviders: ['safe360', 'cloudflare', 'rubyfish'] dnsProviders: ['cloudflare', 'safe360', 'rubyfish']
} }
} }
}, },

View File

@ -1,3 +1,4 @@
<!--
<template> <template>
<a-drawer <a-drawer
:title="title" :title="title"
@ -33,7 +34,7 @@
</a-col> </a-col>
<a-col :span="5"> <a-col :span="5">
<a-select :disabled="item.value === false" v-model="item.value"> <a-select :disabled="item.value === false" v-model="item.value">
<a-select-option value="usa">USA DNS</a-select-option> <a-select-option value="cloudflare">CloudFlare DNS</a-select-option>
<a-select-option value="aliyun">Aliyun DNS</a-select-option> <a-select-option value="aliyun">Aliyun DNS</a-select-option>
</a-select> </a-select>
</a-col> </a-col>
@ -246,3 +247,4 @@ export default {
margin-bottom: 10px; margin-bottom: 10px;
} }
</style> </style>
-->

View File

@ -7,7 +7,6 @@
</template> </template>
<div style="height: 100%" class="json-wrapper"> <div style="height: 100%" class="json-wrapper">
<a-tabs <a-tabs
default-active-key="1" default-active-key="1"
tab-position="left" tab-position="left"
@ -90,7 +89,12 @@
</a-col> </a-col>
</a-row> </a-row>
</a-tab-pane> </a-tab-pane>
<a-tab-pane tab="DNS设置" key="4"> <a-tab-pane tab="IP预设置" key="4">
<div>说明IP预设置功能需要与 `DNS设置` `IP测速` 功能一起使用才会生效</div>
<vue-json-editor style="height:100%;margin-top:10px;" ref="editor" v-model="config.server.preSetIpList" mode="code"
:show-btns="false" :expandedOnStart="true"></vue-json-editor>
</a-tab-pane>
<a-tab-pane tab="DNS设置" key="5">
<div> <div>
<a-row style="margin-top:10px"> <a-row style="margin-top:10px">
<a-col span="19"> <a-col span="19">
@ -119,7 +123,7 @@
</a-row> </a-row>
</div> </div>
</a-tab-pane> </a-tab-pane>
<!-- <a-tab-pane tab="SNI" key="5">--> <!-- <a-tab-pane tab="SNI" key="6">-->
<!-- <a-row style="margin-top:10px">--> <!-- <a-row style="margin-top:10px">-->
<!-- <a-col span="19">--> <!-- <a-col span="19">-->
<!-- <div>这里配置哪些域名要修改sni</div>--> <!-- <div>这里配置哪些域名要修改sni</div>-->
@ -140,7 +144,7 @@
<!-- </a-col>--> <!-- </a-col>-->
<!-- </a-row>--> <!-- </a-row>-->
<!-- </a-tab-pane>--> <!-- </a-tab-pane>-->
<a-tab-pane tab="IP测速" key="6"> <a-tab-pane tab="IP测速" key="7">
<div style="padding-right: 10px"> <div style="padding-right: 10px">
<a-alert type="info" message="对从dns获取到的ip进行测速使用速度最快的ip进行访问。对使用增强功能的域名没啥用"></a-alert> <a-alert type="info" message="对从dns获取到的ip进行测速使用速度最快的ip进行访问。对使用增强功能的域名没啥用"></a-alert>
<a-form-item label="开启dns测速" :label-col="labelCol" :wrapper-col="wrapperCol"> <a-form-item label="开启dns测速" :label-col="labelCol" :wrapper-col="wrapperCol">
@ -194,7 +198,7 @@
<a-icon v-if="item.alive.length>0" type="check"/> <a-icon v-if="item.alive.length>0" type="check"/>
<a-icon v-else type="info-circle"/> <a-icon v-else type="info-circle"/>
</a> </a>
<a-tag style="margin:2px;" v-for="(element,index) of item.backupList" <a-tag style="margin:2px;" v-for="(element,index) of item.backupList" :title="element.dns"
:color="element.time?'green':'red'" :key='index'>{{ element.host }} :color="element.time?'green':'red'" :key='index'>{{ element.host }}
{{ element.time }}{{ element.time ? 'ms' : '' }} {{ element.time }}{{ element.time ? 'ms' : '' }}
</a-tag> </a-tag>
@ -230,8 +234,8 @@ export default {
key: 'server', key: 'server',
dnsMappings: [], dnsMappings: [],
speedTestList: [], speedTestList: [],
whiteList: [], whiteList: []
sniList: [] // sniList: []
} }
}, },
created () { created () {
@ -270,7 +274,7 @@ export default {
ready () { ready () {
this.initDnsMapping() this.initDnsMapping()
this.initWhiteList() this.initWhiteList()
this.initSniList() // this.initSniList()
if (this.config.server.dns.speedTest.dnsProviders) { if (this.config.server.dns.speedTest.dnsProviders) {
this.speedDns = this.config.server.dns.speedTest.dnsProviders this.speedDns = this.config.server.dns.speedTest.dnsProviders
} }
@ -278,7 +282,7 @@ export default {
async applyBefore () { async applyBefore () {
this.submitDnsMapping() this.submitDnsMapping()
this.submitWhiteList() this.submitWhiteList()
this.submitSniList() // this.submitSniList()
}, },
async applyAfter () { async applyAfter () {
if (this.status.server.enabled) { if (this.status.server.enabled) {
@ -343,34 +347,34 @@ export default {
this.whiteList.unshift({ key: '', value: true }) this.whiteList.unshift({ key: '', value: true })
}, },
// sniList // // sniList
initSniList () { // initSniList () {
this.sniList = [] // this.sniList = []
for (const key in this.config.server.sniList) { // for (const key in this.config.server.sniList) {
const value = this.config.server.sniList[key] // const value = this.config.server.sniList[key]
this.sniList.push({ // this.sniList.push({
key, value // key, value
}) // })
} // }
}, // },
submitSniList () { // submitSniList () {
const sniList = {} // const sniList = {}
for (const item of this.sniList) { // for (const item of this.sniList) {
if (item.key) { // if (item.key) {
sniList[item.key] = item.value // sniList[item.key] = item.value
} // }
} // }
this.config.server.sniList = sniList // this.config.server.sniList = sniList
}, // },
deleteSniList (item, index) { // deleteSniList (item, index) {
this.sniList.splice(index, 1) // this.sniList.splice(index, 1)
}, // },
restoreDefSniList (item, index) { // restoreDefSniList (item, index) {
//
}, // },
addSniList () { // addSniList () {
this.sniList.unshift({ key: '', value: true }) // this.sniList.unshift({ key: '', value: true })
}, // },
async openLog () { async openLog () {
const dir = await this.$api.info.getConfigDir() const dir = await this.$api.info.getConfigDir()

View File

@ -3,57 +3,24 @@ const doh = require('dns-over-http')
const BaseDNS = require('./base') 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)
const matchUtil = require('../../utils/util.match')
module.exports = class DNSOverHTTPS extends BaseDNS { module.exports = class DNSOverHTTPS extends BaseDNS {
constructor (dnsServer) { constructor (dnsServer, preSetIpList) {
super() super()
this.dnsServer = dnsServer this.dnsServer = dnsServer
this.preSetIpList = preSetIpList
} }
async _lookup (hostname) { async _lookup (hostname) {
// 直接判断域名是否为example.com // 获取当前域名的预设IP列表
if (hostname === 'github.com') { const hostnamePreSetIpList = matchUtil.matchHostname(this.preSetIpList, hostname, 'matched preSetIpList')
log.info('域名github.com使用内置IP集') if (hostnamePreSetIpList && hostnamePreSetIpList.length > 0) {
// 返回预设的IP地址集 hostnamePreSetIpList.isPreSet = true
return ['140.82.114.4', '20.87.245.0', '20.27.177.113', '20.205.243.166', '20.248.137.48', '140.82.121.3', '140.82.116.4', '20.200.245.247', '20.26.156.215', '140.82.113.3', '140.82.121.4', '20.201.28.151'] return hostnamePreSetIpList
}
// 直接判断域名是否为example.com
if (hostname === 'api.github.com') {
log.info('域名api.github.com使用内置IP集')
// 返回预设的IP地址集
return ['20.87.245.6', '140.82.112.5', '140.82.116.6', '20.26.156.210', '20.200.245.245', '20.27.177.116', '20.248.137.49', '20.201.28.148', '140.82.113.6', '20.205.243.168', '140.82.121.6']
}
if (hostname === 'codeload.github.com') {
log.info('域名codeload.github.com使用内置IP集')
// 返回预设的IP地址集
return ['20.27.177.114', '140.82.116.10', '140.82.114.10', '20.26.156.216', '20.87.245.7', '20.200.245.246', '20.248.137.55', '20.205.243.165', '20.201.28.149', '140.82.121.9', '140.82.113.9']
}
if (hostname.match(/.*\.githubusercontent\.com$/)) {
log.info('域名githubusercontent及其子域使用内置IP集')
// 返回预设的IP地址集
return ['185.199.111.133', '185.199.108.133', '185.199.109.133', '185.199.110.133']
}
if (hostname === 'github.githubassets.com') {
log.info('域名github.githubassets.com使用内置IP集')
// 返回预设的IP地址集
return ['185.199.110.154', '185.199.111.154', '185.199.109.154', '185.199.108.154']
}
if (hostname === 'collector.github.com') {
log.info('域名collector.github.com已根据AdGuard DNS filter规则拦截')
// 返回预设的IP地址集
return ['0.0.0.0']
}
if (hostname === 'github.io') {
log.info('域名github.io使用内置IP集')
// 返回预设的IP地址集
return ['185.199.108.153', '185.199.109.153', '185.199.111.153', '185.199.110.153']
}
if (hostname === 'hub.docker.com') {
log.info('域名hub.docker.com使用内置IP集')
// 返回预设的IP地址集
return ['54.156.140.159', '52.44.227.212', '44.221.37.199']
} }
// 未预设当前域名的IP列表则从dns服务器获取
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) { if (result.answers.length === 0) {

View File

@ -4,7 +4,7 @@ const DNSOverIpAddress = require('./ipaddress.js')
const matchUtil = require('../../utils/util.match') const matchUtil = require('../../utils/util.match')
module.exports = { module.exports = {
initDNS (dnsProviders) { initDNS (dnsProviders, preSetIpList) {
const dnsMap = {} const dnsMap = {}
for (const provider in dnsProviders) { for (const provider in dnsProviders) {
const conf = dnsProviders[provider] const conf = dnsProviders[provider]
@ -12,12 +12,18 @@ module.exports = {
dnsMap[provider] = new DNSOverIpAddress(conf.server) dnsMap[provider] = new DNSOverIpAddress(conf.server)
continue continue
} }
dnsMap[provider] = conf.type === 'https' ? new DNSOverHTTPS(conf.server) : new DNSOverTLS(conf.server) dnsMap[provider] = conf.type === 'https' ? new DNSOverHTTPS(conf.server, preSetIpList) : new DNSOverTLS(conf.server)
} }
return dnsMap return dnsMap
}, },
hasDnsLookup (dnsConfig, hostname) { hasDnsLookup (dnsConfig, hostname) {
const providerName = matchUtil.matchHostname(dnsConfig.mapping, hostname, 'get dns providerName') let providerName = matchUtil.matchHostname(dnsConfig.mapping, hostname, 'get dns providerName')
// usa已重命名为cloudflare以下为向下兼容处理
if (providerName === 'usa') {
providerName = 'cloudflare'
}
if (providerName) { if (providerName) {
return dnsConfig.providers[providerName] return dnsConfig.providers[providerName]
} }

View File

@ -63,7 +63,7 @@ class SpeedTester {
const one = this.getFromOneDns(dns).then(ipList => { const one = this.getFromOneDns(dns).then(ipList => {
if (ipList) { if (ipList) {
for (const ip of ipList) { for (const ip of ipList) {
ips[ip] = { dns: dnsKey } ips[ip] = { dns: ipList.isPreSet === true ? '预设IP' : dnsKey }
} }
} }
}) })

View File

@ -48,7 +48,7 @@ module.exports = (config) => {
host: serverConfig.host, host: serverConfig.host,
port: serverConfig.port, port: serverConfig.port,
dnsConfig: { dnsConfig: {
providers: dnsUtil.initDNS(serverConfig.dns.providers), providers: dnsUtil.initDNS(serverConfig.dns.providers, matchUtil.domainMapRegexply(config.preSetIpList)),
mapping: matchUtil.domainMapRegexply(dnsMapping), mapping: matchUtil.domainMapRegexply(dnsMapping),
speedTest: config.dns.speedTest speedTest: config.dns.speedTest
}, },

View File

@ -8,7 +8,7 @@ const dnsMap = dns.initDNS({
// server: 'ipaddress', // server: 'ipaddress',
// cacheSize: 1000 // cacheSize: 1000
// }, // },
usa: { cloudflare: {
type: 'https', type: 'https',
server: 'https://1.1.1.1/dns-query', server: 'https://1.1.1.1/dns-query',
cacheSize: 1000 cacheSize: 1000

View File

@ -6,7 +6,7 @@ const dnsProviders = dns.initDNS({
server: 'https://dns.alidns.com/dns-query', server: 'https://dns.alidns.com/dns-query',
cacheSize: 1000 cacheSize: 1000
}, },
usa: { cloudflare: {
type: 'https', type: 'https',
server: 'https://1.1.1.1/dns-query', server: 'https://1.1.1.1/dns-query',
cacheSize: 1000 cacheSize: 1000
@ -54,24 +54,24 @@ const dnsProviders = dns.initDNS({
const hostname0 = 'github.com' const hostname0 = 'github.com'
// console.log('first') // console.log('first')
// dnsProviders.usa.lookup(hostname0) // dnsProviders.cloudflare.lookup(hostname0)
console.log('test') console.log('test')
dnsProviders.py233.lookup(hostname0) dnsProviders.py233.lookup(hostname0)
// dnsProviders.usa.lookup(hostname0) // dnsProviders.cloudflare.lookup(hostname0)
// dnsProviders.ipaddress.lookup(hostname0) // dnsProviders.ipaddress.lookup(hostname0)
// dnsProviders.ipaddress.lookup(hostname0) // dnsProviders.ipaddress.lookup(hostname0)
// const hostname = 'api.github.com' // const hostname = 'api.github.com'
// dnsProviders.usa.lookup(hostname) // dnsProviders.cloudflare.lookup(hostname)
// const hostname1 = 'api.github.com' // const hostname1 = 'api.github.com'
// dnsProviders.usa.lookup(hostname1) // dnsProviders.cloudflare.lookup(hostname1)
// //
// const hostname2 = 'hk.docmirror.cn' // const hostname2 = 'hk.docmirror.cn'
// dnsProviders.usa.lookup(hostname2) // dnsProviders.cloudflare.lookup(hostname2)
// const hostname3 = 'github.docmirror.cn' // const hostname3 = 'github.docmirror.cn'
// dnsProviders.usa.lookup(hostname3) // dnsProviders.cloudflare.lookup(hostname3)
// const hostname4 = 'gh.docmirror.top' // const hostname4 = 'gh.docmirror.top'
// dnsProviders.usa.lookup(hostname4) // dnsProviders.cloudflare.lookup(hostname4)
// const hostname5 = 'gh2.docmirror.top' // const hostname5 = 'gh2.docmirror.top'
// dnsProviders.usa.lookup(hostname5) // dnsProviders.cloudflare.lookup(hostname5)