From 2be957968e98e699f2f74d1822242af672f9c49f Mon Sep 17 00:00:00 2001 From: xiaojunnuo Date: Thu, 29 Oct 2020 16:14:12 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20dns=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 + packages/core/src/config.js | 11 +++ packages/core/src/config/index.js | 66 +++++++++------- packages/core/src/lib/dns/index.js | 3 +- .../core/src/lib/interceptor/impl/redirect.js | 2 +- test/test.js | 75 +++++++++++++------ 6 files changed, 106 insertions(+), 53 deletions(-) diff --git a/package.json b/package.json index 7ae55e4..357ffde 100644 --- a/package.json +++ b/package.json @@ -6,5 +6,7 @@ "lerna": "^3.22.1" }, "dependencies": { + "d2-crud-x": "^2.12.0", + "sqlite3": "^5.0.0" } } diff --git a/packages/core/src/config.js b/packages/core/src/config.js index 01ea17a..45adb67 100644 --- a/packages/core/src/config.js +++ b/packages/core/src/config.js @@ -1,6 +1,7 @@ const Shell = require('./shell') const lodash = require('lodash') const defConfig = require('./config/index.js') +const proxyConfig = require('./lib/proxy/common/config') let configTarget = lodash.cloneDeep(defConfig) function _deleteDisabledItem (target, objKey) { const obj = lodash.get(target, objKey) @@ -55,6 +56,16 @@ const configApi = { return !item.exists }) if (list.length > 0) { + const context = { + ca_cert_path: proxyConfig.getDefaultCACertPath() + } + for (const item of noSetList) { + if (item.value.indexOf('${') >= 0) { + for (const key in context) { + item.value = item.value.replcace(new RegExp('${' + key + '}', 'g'), context[key]) + } + } + } const method = type === 'npm' ? Shell.setNpmEnv : Shell.setSystemEnv return method({ list: noSetList }) } diff --git a/packages/core/src/config/index.js b/packages/core/src/config/index.js index 119702c..4e8e561 100644 --- a/packages/core/src/config/index.js +++ b/packages/core/src/config/index.js @@ -10,73 +10,78 @@ module.exports = { '/.*/.*/releases/download/', '/.*/.*/archive/' ], - redirect: 'https://download.fastgit.org' + redirect: 'download.fastgit.org' }, { regexp: [ '/.*/.*/raw/', '/.*/.*/blame/' ], - redirect: 'https://hub.fastgit.org' + redirect: 'hub.fastgit.org' } ], // 'codeload.github.com': [ // { // regexp: '.*', - // redirect:"https://download.fastgit.org" + // redirect:"download.fastgit.org" // } // ], - 'raw.githubusercontent.com': [ - { - proxy: 'https://raw.fastgit.org' - } - ], + 'raw.githubusercontent.com': [{ proxy: 'raw.fastgit.org' }], 'github.githubassets.com': [ { - proxy: 'https://assets.fastgit.org' + proxy: 'assets.fastgit.org' } ], 'customer-stories-feed.github.com': [ { - proxy: 'https://customer-stories-feed.fastgit.org' + proxy: 'customer-stories-feed.fastgit.org' } ], + // google cdn - // https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js 'ajax.googleapis.com': [ { - proxy: 'https://ajax.proxy.ustclug.org' + proxy: 'ajax.loli.net', + backup: ['ajax.proxy.ustclug.org'], + case: 'ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js' } ], 'fonts.googleapis.com': [ { - proxy: 'https://fonts.proxy.ustclug.org' + proxy: 'fonts.loli.net', + backup: ['fonts.proxy.ustclug.org'], + case: 'https://fonts.googleapis.com/css?family=Oswald' } ], 'themes.googleapis.com': [ { - proxy: 'https://themes.loli.net' + proxy: 'themes.loli.net', + backup: ['themes.proxy.ustclug.org'] } ], - 'fonts.gstatic.com': [ - { - proxy: 'https://fonts-gstatic.proxy.ustclug.org' - } + 'themes.googleusercontent.com': [ + { proxy: 'google-themes.proxy.ustclug.org' } ], 'www.google.com': [ { regexp: '/recaptcha/.*', - proxy: 'https://www.recaptcha.net' + proxy: 'www.recaptcha.net' + } + ], + 'fonts.gstatic.com': [ + { + proxy: 'fonts-gstatic.proxy.ustclug.org', + backup: ['gstatic.loli.net'] } ], 'clients*.google.com': [{ abort: true }], 'www.googleapis.com': [{ abort: true }], 'lh*.googleusercontent.com': [{ abort: true }], - // https://mapbox-node-binary.s3.amazonaws.com/sqlite3/v5.0.0/napi-v3-win32-x64.tar.gz + // mapbox-node-binary.s3.amazonaws.com/sqlite3/v5.0.0/napi-v3-win32-x64.tar.gz '*.s3.amazonaws.com': [ { regexp: '/sqlite3/.*', - redirect: 'http://npm.taobao.org/mirrors' + redirect: 'npm.taobao.org/mirrors' } ], 'registry-1.docker.io': [{ proxy: 'docker.mirrors.ustc.edu.cn' }], @@ -85,19 +90,18 @@ module.exports = { 'archive.cloudera.com': [{ regexp: '/cdh5/.*', proxy: 'cloudera.proxy.ustclug.org' }], 'downloads.lede-project.org': [{ proxy: 'lede.proxy.ustclug.org' }], 'downloads.openwrt.org': [{ proxy: 'openwrt.proxy.ustclug.org' }], - 'themes.googleusercontent.com': [{ proxy: 'google-themes.proxy.ustclug.org' }], 'secure.gravatar.com': [{ proxy: 'gravatar.proxy.ustclug.org' }] }, dns: { providers: { aliyun: { type: 'https', - server: 'https://dns.alidns.com/dns-query', + server: 'dns.alidns.com/dns-query', cacheSize: 1000 }, usa: { type: 'https', - server: 'https://cloudflare-dns.com/dns-query', + server: 'cloudflare-dns.com/dns-query', cacheSize: 1000 } }, @@ -114,11 +118,15 @@ module.exports = { PHANTOMJS_CDNURL: 'https://npm.taobao.org/mirrors/phantomjs/', ELECTRON_MIRROR: 'https://npm.taobao.org/mirrors/electron/', CYPRESS_DOWNLOAD_MIRROR: 'https://cdn.cypress.io', - NVM_NODEJS_ORG_MIRROR: 'http://npm.taobao.org/mirrors/node', - CHROMEDRIVER_CDNURL: 'http://npm.taobao.org/mirrors/chromedriver', - OPERADRIVER: 'http://npm.taobao.org/mirrors/operadriver', - ELECTRON_BUILDER_BINARIES_MIRROR: 'http://npm.taobao.org/mirrors/electron-builder-binaries/', - PYTHON_MIRROR: 'http://npm.taobao.org/mirrors/python' + NVM_NODEJS_ORG_MIRROR: 'https://npm.taobao.org/mirrors/node', + CHROMEDRIVER_CDNURL: 'https://npm.taobao.org/mirrors/chromedriver', + OPERADRIVER: 'https://npm.taobao.org/mirrors/operadriver', + ELECTRON_BUILDER_BINARIES_MIRROR: 'https://npm.taobao.org/mirrors/electron-builder-binaries/', + PYTHON_MIRROR: 'https://npm.taobao.org/mirrors/python' + }, + system: { + // eslint-disable-next-line no-template-curly-in-string + NODE_EXTRA_CA_CERTS: '${ca_cert_path}' } }, setting: { diff --git a/packages/core/src/lib/dns/index.js b/packages/core/src/lib/dns/index.js index 91acbf6..275aa23 100644 --- a/packages/core/src/lib/dns/index.js +++ b/packages/core/src/lib/dns/index.js @@ -16,8 +16,7 @@ module.exports = { if (target.indexOf('*') < 0) { continue } - const regexp = target.replace('.', '\\.') - .replace('*', '.*') + const regexp = target.replace(/\./g, '\\.').replace(/\*/g, '.*') if (hostname.match(regexp)) { providerName = dnsConfig.mapping[target] } diff --git a/packages/core/src/lib/interceptor/impl/redirect.js b/packages/core/src/lib/interceptor/impl/redirect.js index 693c8cc..e8e3ef6 100644 --- a/packages/core/src/lib/interceptor/impl/redirect.js +++ b/packages/core/src/lib/interceptor/impl/redirect.js @@ -3,7 +3,7 @@ module.exports = { const url = req.url let redirect if (typeof interceptOpt.redirect === 'string') { - redirect = interceptOpt.redirect + url + redirect = rOptions.protocol + '//' + interceptOpt.redirect + url } else { redirect = interceptOpt.redirect(url) } diff --git a/test/test.js b/test/test.js index bf2f166..e6bf0c4 100644 --- a/test/test.js +++ b/test/test.js @@ -19,25 +19,58 @@ // console.log('stderr:' + stderr) // }) -// var HttpsProxyAgent = require('https-proxy-agent') -// var proxy = 'http://127.0.0.1:1181' -// var agent = new HttpsProxyAgent(proxy) -// console.log('111',process.env.NODE_EXTRA_CA_CERTS) -// const https = require('https') -// https_options = { -// "agent": agent, -// }; -// https.get('https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js', https_options,(res) => { -// console.log('状态码:', res.statusCode) -// console.log('请求头:', res.headers) -// -// res.on('data', (d) => { -// process.stdout.write(d) -// }) -// }).on('error', (e) => { -// console.error(e) -// }) -const fs = require('fs') -const content = fs.readFileSync('C:\\Users\\Administrator\\.dev-sidecar\\dev-sidecar.ca.crt') -console.log('content:',JSON.stringify(content.toString().replace(new RegExp('\r\n','g'),'\n'))); +// const fs = require('fs') +// const content = fs.readFileSync('C:\\Users\\Administrator\\.dev-sidecar\\dev-sidecar.ca.crt') +// console.log('content:',JSON.stringify(content.toString().replace(new RegExp('\r\n','g'),'\n'))); + +function testCa() { + const https = require('https') + const fs = require('fs') + const content = fs.readFileSync('C:\\Users\\Administrator\\.dev-sidecar\\dev-sidecar.ca.crt') + process.env.NODE_EXTRA_CA_CERTS = 'C:\\Users\\Administrator\\.dev-sidecar\\dev-sidecar.ca.crt' + process.env.GLOBAL_AGENT_HTTP_PROXY = "http://127.0.0.1:1181" + process.env.GLOBAL_AGENT_HTTPS_PROXY = "http://127.0.0.1:1181" + console.log('111', process.env.NODE_EXTRA_CA_CERTS) + + const options = { + agent : new https.Agent({ + proxy: "http://127.0.0.1:1181" + }) + } + console.log('options', options) + + https.get('https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js',options, (res) => { + console.log('状态码:', res.statusCode) + console.log('请求头:', res.headers) + + res.on('data', (d) => { + process.stdout.write(d) + }) + }).on('error', (e) => { + console.error(e) + }) +} + +function testRequest(){ + // process.env.NODE_EXTRA_CA_CERTS='C:\\Users\\Administrator\\.dev-sidecar\\dev-sidecar.ca.crt' + console.log(process.env.NODE_EXTRA_CA_CERTS) + const request = require("request").defaults({ + proxy: "http://127.0.0.1:1181" + }) + request("https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js",(error, response, body)=>{ + if(error){ + console.error(error) + }else{ + console.log(body) + } + }) + +} + + +// testCa() + +testRequest() + +