Browse Source

refactor: mirrors

pull/180/head
xiaojunnuo 4 years ago
parent
commit
651a509069
  1. 1
      package.json
  2. 71
      packages/core/package-lock.json
  3. 1
      packages/core/package.json
  4. 35
      packages/core/src/config/index.js
  5. 2
      packages/core/src/lib/interceptor/impl/abort.js
  6. 3
      packages/core/src/lib/interceptor/impl/proxy.js
  7. 1
      packages/core/src/lib/proxy/mitmproxy/createConnectHandler.js
  8. 21
      packages/core/src/lib/proxy/mitmproxy/createRequestHandler.js
  9. 2
      packages/core/src/lib/proxy/mitmproxy/index.js
  10. 1
      packages/core/src/lib/proxy/tls/FakeServersCenter.js
  11. 18
      packages/core/src/server/index.js
  12. 1
      packages/core/src/server/options.js
  13. 48
      packages/core/src/shell/test.js
  14. 22
      packages/core/src/switch/proxy/impl/npm-proxy.js
  15. 4
      packages/core/src/switch/proxy/impl/yarn-proxy.js
  16. 6
      packages/core/src/utils/os.util.js
  17. 9
      packages/core/src/utils/util.js
  18. 1
      packages/gui/src/view/api.js
  19. 5
      packages/gui/src/view/components/App.vue
  20. 43
      test/test.js

1
package.json

@ -6,6 +6,5 @@
"lerna": "^3.22.1"
},
"dependencies": {
"sqlite3": "^5.0.0"
}
}

71
packages/core/package-lock.json generated

@ -1116,6 +1116,11 @@
}
}
},
"@coolaj86/urequest": {
"version": "1.3.7",
"resolved": "https://registry.npmjs.org/@coolaj86/urequest/-/urequest-1.3.7.tgz",
"integrity": "sha512-PPrVYra9aWvZjSCKl/x1pJ9ZpXda1652oJrPBYy5rQumJJMkmTBN3ux+sK2xAUwVvv2wnewDlaQaHLxLwSHnIA=="
},
"@hapi/address": {
"version": "2.1.4",
"resolved": "https://registry.npmjs.org/@hapi/address/-/address-2.1.4.tgz",
@ -1795,6 +1800,7 @@
"thread-loader": "^2.1.3",
"url-loader": "^2.2.0",
"vue-loader": "^15.9.2",
"vue-loader-v16": "npm:vue-loader@^16.0.0-beta.7",
"vue-style-loader": "^4.1.2",
"webpack": "^4.0.0",
"webpack-bundle-analyzer": "^3.8.0",
@ -11417,6 +11423,14 @@
"tweetnacl": "~0.14.0"
}
},
"ssl-root-cas": {
"version": "1.3.1",
"resolved": "https://registry.npmjs.org/ssl-root-cas/-/ssl-root-cas-1.3.1.tgz",
"integrity": "sha512-KR8J210Wfvjh+iNE9jcQEgbG0VG2713PHreItx6aNCPnkFO8XChz1cJ4iuCGeBj0+8wukLmgHgJqX+O5kRjPkQ==",
"requires": {
"@coolaj86/urequest": "^1.3.6"
}
},
"ssri": {
"version": "6.0.1",
"resolved": "https://registry.npmjs.org/ssri/-/ssri-6.0.1.tgz",
@ -12414,6 +12428,63 @@
}
}
},
"vue-loader-v16": {
"version": "npm:vue-loader@16.0.0-beta.9",
"resolved": "https://registry.npmjs.org/vue-loader/-/vue-loader-16.0.0-beta.9.tgz",
"integrity": "sha512-mu9pg6554GbXDSO8LlxkQM6qUJzUkb/A0FJc9LgRqnU9MCnhzEXwCt1Zx5NObvFpzs2mH2dH/uUCDwL8Qaz9sA==",
"dev": true,
"optional": true,
"requires": {
"chalk": "^4.1.0",
"hash-sum": "^2.0.0",
"loader-utils": "^2.0.0"
},
"dependencies": {
"ansi-styles": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz",
"integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==",
"dev": true,
"optional": true,
"requires": {
"color-convert": "^2.0.1"
}
},
"chalk": {
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.0.tgz",
"integrity": "sha512-qwx12AxXe2Q5xQ43Ac//I6v5aXTipYrSESdOgzrN+9XjgEpyjpKuvSGaN4qE93f7TQTlerQQ8S+EQ0EyDoVL1A==",
"dev": true,
"optional": true,
"requires": {
"ansi-styles": "^4.1.0",
"supports-color": "^7.1.0"
}
},
"color-convert": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
"integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==",
"dev": true,
"optional": true,
"requires": {
"color-name": "~1.1.4"
}
},
"loader-utils": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/loader-utils/-/loader-utils-2.0.0.tgz",
"integrity": "sha512-rP4F0h2RaWSvPEkD7BLDFQnvSf+nK+wr3ESUjNTyAGobqrijmW92zc+SO6d4p4B1wh7+B/Jg1mkQe5NYUEHtHQ==",
"dev": true,
"optional": true,
"requires": {
"big.js": "^5.2.2",
"emojis-list": "^3.0.0",
"json5": "^2.1.2"
}
}
}
},
"vue-style-loader": {
"version": "4.1.2",
"resolved": "https://registry.npmjs.org/vue-style-loader/-/vue-style-loader-4.1.2.tgz",

1
packages/core/package.json

@ -41,6 +41,7 @@
"node-mitmproxy": "^3.1.1",
"node-powershell": "^4.0.0",
"require-context": "^1.1.0",
"ssl-root-cas": "^1.3.1",
"through2": "^2.0.1",
"tunnel-agent": "^0.4.3",
"util": "^0.12.3",

35
packages/core/src/config/index.js

@ -42,14 +42,15 @@ module.exports = {
}
],
// google cdn
// https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js
'ajax.googleapis.com': [
{
proxy: 'https://ajax.loli.net'
proxy: 'https://ajax.proxy.ustclug.org'
}
],
'fonts.googleapis.com': [
{
proxy: 'https://fonts.loli.net'
proxy: 'https://fonts.proxy.ustclug.org'
}
],
'themes.googleapis.com': [
@ -59,7 +60,7 @@ module.exports = {
],
'fonts.gstatic.com': [
{
proxy: 'https://gstatic.loli.net'
proxy: 'https://fonts-gstatic.proxy.ustclug.org'
}
],
'www.google.com': [
@ -68,28 +69,24 @@ module.exports = {
proxy: 'https://www.recaptcha.net'
}
],
'secure.gravatar.com': [
{
redirect: 'https://gravatar.loli.net'
}
],
'clients*.google.com': [
{
abort: true
}
],
'lh*.googleusercontent.com': [
{
abort: true
}
],
'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
'*.s3.amazonaws.com': [
{
regexp: '/sqlite3/.*',
redirect: 'http://npm.taobao.org/mirrors'
}
]
],
'registry-1.docker.io': [{ proxy: 'docker.mirrors.ustc.edu.cn' }],
'packages.elastic.co': [{ proxy: 'elastic.proxy.ustclug.org' }],
'ppa.launchpad.net': [{ proxy: 'launchpad.proxy.ustclug.org' }],
'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: {

2
packages/core/src/lib/interceptor/impl/abort.js

@ -1,7 +1,7 @@
module.exports = {
requestInterceptor (interceptOpt, rOptions, req, res, ssl) {
req.abort()
console.log('abort:', rOptions.hostname, req.url)
req.destroy()
},
is (interceptOpt) {
return !!interceptOpt.abort

3
packages/core/src/lib/interceptor/impl/proxy.js

@ -1,8 +1,9 @@
const url = require('url')
module.exports = {
requestInterceptor (interceptOpt, rOptions, req, res, ssl) {
const proxy = interceptOpt.proxy.indexOf('http') === 0 ? interceptOpt.proxy : rOptions.protocol + '//' + interceptOpt.proxy
// eslint-disable-next-line node/no-deprecated-api
const URL = url.parse(interceptOpt.proxy)
const URL = url.parse(proxy)
rOptions.protocol = URL.protocol
rOptions.hostname = URL.host
rOptions.host = URL.host

1
packages/core/src/lib/proxy/mitmproxy/createConnectHandler.js

@ -9,7 +9,6 @@ module.exports = function createConnectHandler (sslConnectInterceptor, fakeServe
return function connectHandler (req, cltSocket, head) {
// eslint-disable-next-line node/no-deprecated-api
const srvUrl = url.parse(`https://${req.url}`)
const hostname = srvUrl.hostname
if (typeof sslConnectInterceptor === 'function' && sslConnectInterceptor(req, cltSocket, head)) {
fakeServerCenter.getServerPromise(hostname, srvUrl.port).then((serverObj) => {

21
packages/core/src/lib/proxy/mitmproxy/createRequestHandler.js

@ -11,7 +11,6 @@ module.exports = function createRequestHandler (requestInterceptor, responseInte
let proxyReq
const rOptions = commonUtil.getOptionsFormRequest(req, ssl, externalProxy)
if (rOptions.headers.connection === 'close') {
req.socket.setKeepAlive(false)
} else if (rOptions.customSocketId != null) { // for NTLM
@ -63,9 +62,7 @@ module.exports = function createRequestHandler (requestInterceptor, responseInte
function onFree () {
const url = `${rOptions.protocol}//${rOptions.hostname}:${rOptions.port}${rOptions.path}`
const start = new Date().getTime()
if (rOptions.protocol === 'https:') {
console.log('代理请求:', url)
}
proxyReq = (rOptions.protocol === 'https:' ? https : http).request(rOptions, (proxyRes) => {
const end = new Date().getTime()
@ -76,12 +73,12 @@ module.exports = function createRequestHandler (requestInterceptor, responseInte
})
proxyReq.on('timeout', () => {
console.error('代理请求超时', rOptions.host, rOptions.path)
console.error('代理请求超时', rOptions.hostname, rOptions.path)
reject(new Error(`${rOptions.host}:${rOptions.port}, 代理请求超时`))
})
proxyReq.on('error', (e, req, res) => {
console.error('代理请求错误', e.errno, rOptions.host, rOptions.path)
console.error('代理请求错误', e.errno, rOptions.hostname, rOptions.path)
reject(e)
if (res) {
res.end()
@ -89,26 +86,26 @@ module.exports = function createRequestHandler (requestInterceptor, responseInte
})
proxyReq.on('aborted', () => {
console.error('代理请求被取消', rOptions.host, rOptions.path)
console.error('代理请求被取消', rOptions.hostname, rOptions.path)
reject(new Error('代理请求被取消'))
req.abort()
req.destroy()
})
req.on('aborted', function () {
console.error('请求被取消', rOptions.host, rOptions.path)
proxyReq.abort()
console.error('请求被取消', rOptions.hostname, rOptions.path)
proxyReq.destroy()
reject(new Error('请求被取消'))
})
req.on('error', function (e, req, res) {
console.error('请求错误:', e.errno, rOptions.host, rOptions.path)
console.error('请求错误:', e.errno, rOptions.hostname, rOptions.path)
reject(e)
if (res) {
res.end()
}
})
req.on('timeout', () => {
console.error('请求超时', rOptions.host, rOptions.path)
reject(new Error(`${rOptions.host}:${rOptions.port}, 请求超时`))
console.error('请求超时', rOptions.hostname, rOptions.path)
reject(new Error(`${rOptions.hostname}:${rOptions.port}, 请求超时`))
})
req.pipe(proxyReq)
}

2
packages/core/src/lib/proxy/mitmproxy/index.js

@ -65,10 +65,12 @@ module.exports = {
})
server.on('request', (req, res) => {
const ssl = false
// console.log('request,', req.url, req.port, req.host)
requestHandler(req, res, ssl)
})
// tunneling for https
server.on('connect', (req, cltSocket, head) => {
// console.log('connect,', req.url)
connectHandler(req, cltSocket, head)
})
// TODO: handler WebSocket

1
packages/core/src/lib/proxy/tls/FakeServersCenter.js

@ -64,6 +64,7 @@ module.exports = class FakeServersCenter {
SNICallback: (hostname, done) => {
(async () => {
const certObj = await this.certAndKeyContainer.getCertPromise(hostname, port)
console.log('sni callback:', hostname)
done(null, tls.createSecureContext({
key: pki.privateKeyToPem(certObj.key),
cert: pki.certificateToPem(certObj.cert)

18
packages/core/src/server/index.js

@ -1,12 +1,13 @@
const ProxyOptions = require('./options')
const mitmproxy = require('../lib/proxy')
const getLogger = require('../lib/utils/logger')
const logger = getLogger('proxy')
const config = require('../config')
const event = require('../event')
let server
const serverApi = {
async start (newConfig) {
if (server != null) {
server.close()
}
config.set(newConfig)
const proxyOptions = ProxyOptions(config.get())
server = mitmproxy.createProxy(proxyOptions, () => {
@ -22,21 +23,22 @@ const serverApi = {
return server.config
},
async close () {
try {
if (server) {
return new Promise((resolve, reject) => {
if (server) {
server.close((err) => {
if (err) {
console.log('close error', err)
reject(err)
} else {
resolve()
}
})
})
}
} catch (err) {
logger.error(err)
server = null
} else {
console.log('server is null')
resolve()
}
})
},
async restart () {
try {

1
packages/core/src/server/options.js

@ -26,7 +26,6 @@ function isMatched (url, regexp) {
}
module.exports = (config) => {
console.log('config', config)
return {
port: config.server.port,
dnsConfig: {

48
packages/core/src/shell/test.js

@ -1,14 +1,14 @@
const cmd1 = require('node-cmd')
cmd1.get('set',
function (err, data, stderr) {
console.log('cmd complete:', err, data, stderr)
if (err) {
console.error('cmd 命令执行错误:', err, stderr)
} else {
console.log('cmd 命令执行结果:', data)
}
}
)
// const cmd1 = require('node-cmd')
// cmd1.get('set',
// function (err, data, stderr) {
// console.log('cmd complete:', err, data, stderr)
// if (err) {
// console.error('cmd 命令执行错误:', err, stderr)
// } else {
// console.log('cmd 命令执行结果:', data)
// }
// }
// )
// var process = require('child_process')
//
@ -18,3 +18,29 @@ cmd1.get('set',
// console.log('stdout:' + stdout)
// console.log('stderr:' + stderr)
// })
// var HttpsProxyAgent = require('https-proxy-agent')
// var proxy = 'http://user:pass@xxx.com:port'
// var agent = new HttpsProxyAgent(proxy)
// const https = require('https')
// https.get('https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js', (res) => {
// console.log('状态码:', res.statusCode)
// console.log('请求头:', res.headers)
//
// res.on('data', (d) => {
// process.stdout.write(d)
// })
// }).on('error', (e) => {
// console.error(e)
// })
const request = require('request')
const fs = require('fs')
request({
url: 'https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js',
proxy: 'http://127.0.0.1:1181',
ca: fs.readFileSync('C:/Users/Administrator/.dev-sidecar/dev-sidecar.ca.crt')
// eslint-disable-next-line handle-callback-err
}, (err, res, body) => {
console.log(body)
})

22
packages/core/src/switch/proxy/impl/npm-proxy.js

@ -28,11 +28,14 @@ class WindowsSystemProxy extends SystemProxy {
ret = await winExec(`npm config set https-proxy=http://${ip}:${port}`)
console.log('npm https proxy set success', ret)
ret = await winExec(`npm config set ca ${config.getDefaultCACertPath()}`)
console.log('npm cafile set success', ret)
// ret = await winExec(`npm config set cafile ${config.getDefaultCACertPath()}`)
// console.log('npm cafile set success', ret)
// ret = await winExec('npm config set strict-ssl false')
// console.log('npm strict-ssl false success', ret)
ret = await winExec(`npm config set NODE_EXTRA_CA_CERTS ${config.getDefaultCACertPath()}`)
console.log('npm NODE_EXTRA_CA_CERTS set success', ret)
ret = await winExec('npm config set strict-ssl false')
console.log('npm strict-ssl false success', ret)
}
static async unsetProxy () {
@ -41,10 +44,13 @@ class WindowsSystemProxy extends SystemProxy {
await winExec('npm config delete https-proxy')
console.log('npm https proxy unset success')
await winExec('npm config delete ca')
console.log('npm ca unset success')
// await winExec(' npm config delete strict-ssl')
// console.log('npm strict-ssl true success')
// await winExec('npm config delete cafile')
// console.log('npm ca unset success')
await winExec('npm config delete NODE_EXTRA_CA_CERTS')
console.log('npm NODE_EXTRA_CA_CERTS unset success')
await winExec(' npm config delete strict-ssl')
console.log('npm strict-ssl true success')
}
static _asyncRegSet (regKey, name, type, value) {

4
packages/core/src/switch/proxy/impl/yarn-proxy.js

@ -29,7 +29,7 @@ class WindowsSystemProxy extends SystemProxy {
ret = await winExec(`yarn config set https-proxy=http://${ip}:${port}`)
console.log('yarn https proxy set success', ret)
ret = await winExec(`yarn config set ca ${config.getDefaultCACertPath()}`)
ret = await winExec(`yarn config set cafile ${config.getDefaultCACertPath()}`)
console.log('yarn cafile set success', ret)
// ret = await winExec('yarn config set strict-ssl false')
@ -42,7 +42,7 @@ class WindowsSystemProxy extends SystemProxy {
await winExec('yarn config delete https-proxy')
console.log('yarn https proxy unset success')
await winExec('yarn config delete ca')
await winExec('yarn config delete cafile')
console.log('yarn ca unset success')
// await winExec(' yarn config delete strict-ssl')

6
packages/core/src/utils/os.util.js

@ -0,0 +1,6 @@
const os = require('os')
module.exports = {
isWindows7 () {
const version = os.release()
}
}

9
packages/core/src/utils/util.js

@ -0,0 +1,9 @@
const os = require('os')
const util = {
getNodeVersion () {
const version = process.version
console.log(version)
}
}
util.getNodeVersion()
module.exports = util

1
packages/gui/src/view/api.js

@ -22,7 +22,6 @@ let inited = false
export function apiInit () {
if (!inited) {
return doInvoke('getApiList').then(list => {
console.log('apiList', list)
inited = true
for (const item of list) {
bindApi(item)

5
packages/gui/src/view/components/App.vue

@ -108,13 +108,11 @@ export default {
this.proxy = this.createProxyBtns()
this.reloadConfig().then(() => {
this.start(true)
console.log('proxy', this.proxy)
})
},
methods: {
reloadConfig () {
return api.config.reload().then(ret => {
console.log('config', ret)
this.config = ret
return ret
})
@ -128,7 +126,6 @@ export default {
},
createProxyBtns () {
const btns = {}
console.log('api.proxy', api.proxy, api)
for (const type in api.proxy) {
btns[type] = {
loading: false,
@ -169,7 +166,7 @@ export default {
this.settings.visible = true
},
onConfigChanged (newConfig) {
console.log('config chagned', newConfig)
console.log('config changed', newConfig)
this.reloadConfig().then(() => {
if (this.status.server) {
return api.server.restart()

43
test/test.js

@ -0,0 +1,43 @@
// const cmd1 = require('node-cmd')
// cmd1.get('set',
// function (err, data, stderr) {
// console.log('cmd complete:', err, data, stderr)
// if (err) {
// console.error('cmd 命令执行错误:', err, stderr)
// } else {
// console.log('cmd 命令执行结果:', data)
// }
// }
// )
// var process = require('child_process')
//
// var cmd = 'set'
// process.exec(cmd, function (error, stdout, stderr) {
// console.log('error:' + error)
// console.log('stdout:' + stdout)
// 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')));
Loading…
Cancel
Save