From a02c2ff30634cccceea920f0a1f6647ed114681a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=89=AF?= <841369634@qq.com> Date: Mon, 8 Jul 2024 16:42:34 +0800 Subject: [PATCH] =?UTF-8?q?feature:=20IP=E9=A2=84=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E5=8F=AF=E9=85=8D=E7=BD=AE=E5=8C=96=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +- packages/core/src/config/index.js | 67 +++++++++++++++- packages/gui/src/view/components/settings.vue | 4 +- packages/gui/src/view/pages/server.vue | 78 ++++++++++--------- packages/mitmproxy/src/lib/dns/https.js | 51 +++--------- packages/mitmproxy/src/lib/dns/index.js | 12 ++- .../mitmproxy/src/lib/speed/SpeedTester.js | 2 +- packages/mitmproxy/src/options.js | 2 +- .../test/{dnsSpeed.js => dnsSpeedTest.js} | 2 +- packages/mitmproxy/test/dnsTest.mjs | 18 ++--- 10 files changed, 143 insertions(+), 99 deletions(-) rename packages/mitmproxy/test/{dnsSpeed.js => dnsSpeedTest.js} (98%) diff --git a/README.md b/README.md index 97d7a4c..ea3aff2 100644 --- a/README.md +++ b/README.md @@ -218,9 +218,9 @@ const intercepts = { ```js dns: { mapping: { - 'api.github.com': 'usa', // "解决push的时候需要输入密码的问题", - 'gist.github.com': 'usa' // 解决gist无法访问的问题 - '*.githubusercontent.com': 'usa' // 解决github头像经常下载不到的问题 + 'api.github.com': 'cloudflare', // "解决push的时候需要输入密码的问题", + 'gist.github.com': 'cloudflare' // 解决gist无法访问的问题 + '*.githubusercontent.com': 'cloudflare' // 解决github头像经常下载不到的问题 } } ``` diff --git a/packages/core/src/config/index.js b/packages/core/src/config/index.js index 2a30654..6a38c59 100644 --- a/packages/core/src/config/index.js +++ b/packages/core/src/config/index.js @@ -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: { '*.cn': true, 'cn.*': true, @@ -305,6 +369,7 @@ module.exports = { mapping: { '*.github.com': 'quad9', '*github*.com': 'quad9', + '*.githubusercontent.com': 'quad9', '*.github.io': 'quad9', '*.docker.com': 'quad9', '*.stackoverflow.com': 'quad9', @@ -325,7 +390,7 @@ module.exports = { enabled: true, interval: 300000, hostnameList: ['github.com'], - dnsProviders: ['safe360', 'cloudflare', 'rubyfish'] + dnsProviders: ['cloudflare', 'safe360', 'rubyfish'] } } }, diff --git a/packages/gui/src/view/components/settings.vue b/packages/gui/src/view/components/settings.vue index 34e3b1f..c333b09 100644 --- a/packages/gui/src/view/components/settings.vue +++ b/packages/gui/src/view/components/settings.vue @@ -1,3 +1,4 @@ + diff --git a/packages/gui/src/view/pages/server.vue b/packages/gui/src/view/pages/server.vue index a71f060..24fcdd8 100644 --- a/packages/gui/src/view/pages/server.vue +++ b/packages/gui/src/view/pages/server.vue @@ -7,7 +7,6 @@
- - + +
说明:IP预设置功能,需要与 `DNS设置` 或 `IP测速` 功能一起使用才会生效。
+ +
+
@@ -119,7 +123,7 @@
- + @@ -140,7 +144,7 @@ - +
@@ -194,7 +198,7 @@ - {{ element.host }} {{ element.time }}{{ element.time ? 'ms' : '' }} @@ -230,8 +234,8 @@ export default { key: 'server', dnsMappings: [], speedTestList: [], - whiteList: [], - sniList: [] + whiteList: [] + // sniList: [] } }, created () { @@ -270,7 +274,7 @@ export default { ready () { this.initDnsMapping() this.initWhiteList() - this.initSniList() + // this.initSniList() if (this.config.server.dns.speedTest.dnsProviders) { this.speedDns = this.config.server.dns.speedTest.dnsProviders } @@ -278,7 +282,7 @@ export default { async applyBefore () { this.submitDnsMapping() this.submitWhiteList() - this.submitSniList() + // this.submitSniList() }, async applyAfter () { if (this.status.server.enabled) { @@ -343,34 +347,34 @@ export default { this.whiteList.unshift({ key: '', value: true }) }, - // sniList - initSniList () { - this.sniList = [] - for (const key in this.config.server.sniList) { - const value = this.config.server.sniList[key] - this.sniList.push({ - key, value - }) - } - }, - submitSniList () { - const sniList = {} - for (const item of this.sniList) { - if (item.key) { - sniList[item.key] = item.value - } - } - this.config.server.sniList = sniList - }, - deleteSniList (item, index) { - this.sniList.splice(index, 1) - }, - restoreDefSniList (item, index) { - - }, - addSniList () { - this.sniList.unshift({ key: '', value: true }) - }, + // // sniList + // initSniList () { + // this.sniList = [] + // for (const key in this.config.server.sniList) { + // const value = this.config.server.sniList[key] + // this.sniList.push({ + // key, value + // }) + // } + // }, + // submitSniList () { + // const sniList = {} + // for (const item of this.sniList) { + // if (item.key) { + // sniList[item.key] = item.value + // } + // } + // this.config.server.sniList = sniList + // }, + // deleteSniList (item, index) { + // this.sniList.splice(index, 1) + // }, + // restoreDefSniList (item, index) { + // + // }, + // addSniList () { + // this.sniList.unshift({ key: '', value: true }) + // }, async openLog () { const dir = await this.$api.info.getConfigDir() diff --git a/packages/mitmproxy/src/lib/dns/https.js b/packages/mitmproxy/src/lib/dns/https.js index 4168f4c..728e461 100644 --- a/packages/mitmproxy/src/lib/dns/https.js +++ b/packages/mitmproxy/src/lib/dns/https.js @@ -3,57 +3,24 @@ const doh = require('dns-over-http') const BaseDNS = require('./base') const log = require('../../utils/util.log') const dohQueryAsync = promisify(doh.query) +const matchUtil = require('../../utils/util.match') module.exports = class DNSOverHTTPS extends BaseDNS { - constructor (dnsServer) { + constructor (dnsServer, preSetIpList) { super() this.dnsServer = dnsServer + this.preSetIpList = preSetIpList } async _lookup (hostname) { - // 直接判断域名是否为example.com - if (hostname === 'github.com') { - log.info('域名github.com使用内置IP集') - // 返回预设的IP地址集 - 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'] - } - // 直接判断域名是否为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列表 + const hostnamePreSetIpList = matchUtil.matchHostname(this.preSetIpList, hostname, 'matched preSetIpList') + if (hostnamePreSetIpList && hostnamePreSetIpList.length > 0) { + hostnamePreSetIpList.isPreSet = true + return hostnamePreSetIpList } + // 未预设当前域名的IP列表,则从dns服务器获取 try { const result = await dohQueryAsync({ url: this.dnsServer }, [{ type: 'A', name: hostname }]) if (result.answers.length === 0) { diff --git a/packages/mitmproxy/src/lib/dns/index.js b/packages/mitmproxy/src/lib/dns/index.js index af11da9..1fdcb81 100644 --- a/packages/mitmproxy/src/lib/dns/index.js +++ b/packages/mitmproxy/src/lib/dns/index.js @@ -4,7 +4,7 @@ const DNSOverIpAddress = require('./ipaddress.js') const matchUtil = require('../../utils/util.match') module.exports = { - initDNS (dnsProviders) { + initDNS (dnsProviders, preSetIpList) { const dnsMap = {} for (const provider in dnsProviders) { const conf = dnsProviders[provider] @@ -12,12 +12,18 @@ module.exports = { dnsMap[provider] = new DNSOverIpAddress(conf.server) 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 }, 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) { return dnsConfig.providers[providerName] } diff --git a/packages/mitmproxy/src/lib/speed/SpeedTester.js b/packages/mitmproxy/src/lib/speed/SpeedTester.js index 633b60c..140b624 100644 --- a/packages/mitmproxy/src/lib/speed/SpeedTester.js +++ b/packages/mitmproxy/src/lib/speed/SpeedTester.js @@ -63,7 +63,7 @@ class SpeedTester { const one = this.getFromOneDns(dns).then(ipList => { if (ipList) { for (const ip of ipList) { - ips[ip] = { dns: dnsKey } + ips[ip] = { dns: ipList.isPreSet === true ? '预设IP' : dnsKey } } } }) diff --git a/packages/mitmproxy/src/options.js b/packages/mitmproxy/src/options.js index 7799891..e962027 100644 --- a/packages/mitmproxy/src/options.js +++ b/packages/mitmproxy/src/options.js @@ -48,7 +48,7 @@ module.exports = (config) => { host: serverConfig.host, port: serverConfig.port, dnsConfig: { - providers: dnsUtil.initDNS(serverConfig.dns.providers), + providers: dnsUtil.initDNS(serverConfig.dns.providers, matchUtil.domainMapRegexply(config.preSetIpList)), mapping: matchUtil.domainMapRegexply(dnsMapping), speedTest: config.dns.speedTest }, diff --git a/packages/mitmproxy/test/dnsSpeed.js b/packages/mitmproxy/test/dnsSpeedTest.js similarity index 98% rename from packages/mitmproxy/test/dnsSpeed.js rename to packages/mitmproxy/test/dnsSpeedTest.js index 5f7281c..a8c6e5d 100644 --- a/packages/mitmproxy/test/dnsSpeed.js +++ b/packages/mitmproxy/test/dnsSpeedTest.js @@ -8,7 +8,7 @@ const dnsMap = dns.initDNS({ // server: 'ipaddress', // cacheSize: 1000 // }, - usa: { + cloudflare: { type: 'https', server: 'https://1.1.1.1/dns-query', cacheSize: 1000 diff --git a/packages/mitmproxy/test/dnsTest.mjs b/packages/mitmproxy/test/dnsTest.mjs index f344b03..66df322 100644 --- a/packages/mitmproxy/test/dnsTest.mjs +++ b/packages/mitmproxy/test/dnsTest.mjs @@ -6,7 +6,7 @@ const dnsProviders = dns.initDNS({ server: 'https://dns.alidns.com/dns-query', cacheSize: 1000 }, - usa: { + cloudflare: { type: 'https', server: 'https://1.1.1.1/dns-query', cacheSize: 1000 @@ -54,24 +54,24 @@ const dnsProviders = dns.initDNS({ const hostname0 = 'github.com' // console.log('first') -// dnsProviders.usa.lookup(hostname0) +// dnsProviders.cloudflare.lookup(hostname0) console.log('test') dnsProviders.py233.lookup(hostname0) -// dnsProviders.usa.lookup(hostname0) +// dnsProviders.cloudflare.lookup(hostname0) // dnsProviders.ipaddress.lookup(hostname0) // dnsProviders.ipaddress.lookup(hostname0) // const hostname = 'api.github.com' -// dnsProviders.usa.lookup(hostname) +// dnsProviders.cloudflare.lookup(hostname) // const hostname1 = 'api.github.com' -// dnsProviders.usa.lookup(hostname1) +// dnsProviders.cloudflare.lookup(hostname1) // // const hostname2 = 'hk.docmirror.cn' -// dnsProviders.usa.lookup(hostname2) +// dnsProviders.cloudflare.lookup(hostname2) // const hostname3 = 'github.docmirror.cn' -// dnsProviders.usa.lookup(hostname3) +// dnsProviders.cloudflare.lookup(hostname3) // const hostname4 = 'gh.docmirror.top' -// dnsProviders.usa.lookup(hostname4) +// dnsProviders.cloudflare.lookup(hostname4) // const hostname5 = 'gh2.docmirror.top' -// dnsProviders.usa.lookup(hostname5) +// dnsProviders.cloudflare.lookup(hostname5)