diff --git a/packages/core/mitmproxy.js b/packages/core/mitmproxy.js new file mode 100644 index 0000000..195df53 --- /dev/null +++ b/packages/core/mitmproxy.js @@ -0,0 +1,4 @@ +// eslint-disable-next-line no-unused-vars +const server = require('@docmirror/mitmproxy') +const config = JSON.parse(process.argv[2]) +server.start(config) diff --git a/packages/core/package.json b/packages/core/package.json index 9bbc6fb..4904a5e 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -47,7 +47,8 @@ "util": "^0.12.3", "validator": "^13.1.17", "vue": "^2.6.11", - "winreg": "^1.2.4" + "winreg": "^1.2.4", + "@docmirror/mitmproxy": "1.0.0" }, "devDependencies": { "@vue/cli-plugin-babel": "~4.5.0", diff --git a/packages/core/src/config.js b/packages/core/src/config.js index 26e74ab..1ca2911 100644 --- a/packages/core/src/config.js +++ b/packages/core/src/config.js @@ -1,15 +1,17 @@ const Shell = require('./shell') const lodash = require('lodash') const defConfig = require('./config/index.js') -const proxyConfig = require('./lib/proxy/common/config') +const proxyServer = require('@docmirror/mitmproxy') let configTarget = lodash.cloneDeep(defConfig) -function _deleteDisabledItem (target, objKey) { - const obj = lodash.get(target, objKey) - for (const key in obj) { - if (obj[key] === false) { - delete obj[key] +function _deleteDisabledItem (target) { + lodash.forEach(target, (item, key) => { + if (item == null) { + delete target[key] } - } + if (lodash.isObject(item)) { + _deleteDisabledItem(item) + } + }) } const configApi = { get () { @@ -24,8 +26,7 @@ const configApi = { lodash.merge(merged, clone) lodash.merge(merged, newConfig) - _deleteDisabledItem(merged, 'intercepts') - _deleteDisabledItem(merged, 'dns.mapping') + _deleteDisabledItem(merged) configTarget = merged return configTarget }, @@ -35,8 +36,15 @@ const configApi = { addDefault (key, defValue) { lodash.set(defConfig, key, defValue) }, - resetDefault () { - configTarget = lodash.cloneDeep(defConfig) + resetDefault (key) { + if (key) { + let value = lodash.get(defConfig, key) + value = lodash.cloneDeep(value) + lodash.set(configTarget, key, value) + } else { + configTarget = lodash.cloneDeep(defConfig) + } + return configTarget }, async getVariables (type) { const method = type === 'npm' ? Shell.getNpmEnv : Shell.getSystemEnv @@ -60,7 +68,7 @@ const configApi = { }) if (list.length > 0) { const context = { - ca_cert_path: proxyConfig.getDefaultCACertPath() + ca_cert_path: proxyServer.config.getDefaultCACertPath() } for (const item of noSetList) { if (item.value.indexOf('${') >= 0) { diff --git a/packages/core/src/config/index.js b/packages/core/src/config/index.js index 7ba21c6..675e807 100644 --- a/packages/core/src/config/index.js +++ b/packages/core/src/config/index.js @@ -2,95 +2,83 @@ module.exports = { server: { enabled: true, port: 1181, + setting: { + NODE_TLS_REJECT_UNAUTHORIZED: true + }, intercepts: { - 'github.com': [ - { - // "release archive 下载链接替换", - regexp: [ - '/.*/.*/releases/download/', - '/.*/.*/archive/' - ], + 'github.com': { + '/.*/.*/releases/download/': { + redirect: 'download.fastgit.org' + }, + '/.*/.*/archive/': { redirect: 'download.fastgit.org' }, - { - regexp: [ - '/.*/.*/raw/', - '/.*/.*/blame/' - ], + '/.*/.*/raw/': { + redirect: 'hub.fastgit.org' + }, + '/.*/.*/blame/': { redirect: 'hub.fastgit.org' } - ], - // 'codeload.github.com': [ - // { - // regexp: '.*', - // redirect:"download.fastgit.org" - // } - // ], - 'raw.githubusercontent.com': [{ proxy: 'raw.fastgit.org' }], - 'github.githubassets.com': [ - { - proxy: 'assets.fastgit.org' - } - ], - 'customer-stories-feed.github.com': [ - { - proxy: 'customer-stories-feed.fastgit.org' - } - ], + }, + 'raw.githubusercontent.com': { + '.*': { proxy: 'raw.fastgit.org' } + }, + 'github.githubassets.com': { + '.*': { proxy: 'assets.fastgit.org' } + }, + 'customer-stories-feed.github.com': { + '.*': { proxy: 'customer-stories-feed.fastgit.org' } + }, // google cdn - 'ajax.googleapis.com': [ - { + 'ajax.googleapis.com': { + '.*': { proxy: 'ajax.loli.net', backup: ['ajax.proxy.ustclug.org'], - case: 'ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js' + test: 'ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js' } - ], - 'fonts.googleapis.com': [ - { + }, + 'fonts.googleapis.com': { + '.*': { proxy: 'fonts.loli.net', backup: ['fonts.proxy.ustclug.org'], - case: 'https://fonts.googleapis.com/css?family=Oswald' + test: 'https://fonts.googleapis.com/css?family=Oswald' } - ], - 'themes.googleapis.com': [ - { + }, + 'themes.googleapis.com': { + '.*': { proxy: 'themes.loli.net', backup: ['themes.proxy.ustclug.org'] } - ], - 'themes.googleusercontent.com': [ - { proxy: 'google-themes.proxy.ustclug.org' } - ], - 'www.google.com': [ - { - regexp: '/recaptcha/.*', - proxy: 'www.recaptcha.net' - } - ], - 'fonts.gstatic.com': [ - { + }, + 'themes.googleusercontent.com': { + '.*': { proxy: 'google-themes.proxy.ustclug.org' } + }, + 'www.google.com': { + '/recaptcha/.*': { 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 }], + }, + 'clients*.google.com': { '.*': { abort: true } }, + 'www.googleapis.com': { '.*': { abort: true } }, + 'lh*.googleusercontent.com': { '.*': { abort: true } }, // mapbox-node-binary.s3.amazonaws.com/sqlite3/v5.0.0/napi-v3-win32-x64.tar.gz - '*.s3.amazonaws.com': [ - { - regexp: '/sqlite3/.*', + '*.s3.amazonaws.com': { + '/sqlite3/.*': { redirect: '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' }], - 'secure.gravatar.com': [{ proxy: 'gravatar.proxy.ustclug.org' }] + }, + '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' } }, + 'secure.gravatar.com': { '.*': { proxy: 'gravatar.proxy.ustclug.org' } } }, dns: { providers: { diff --git a/packages/core/src/expose.js b/packages/core/src/expose.js index 6ed00d2..e6d800f 100644 --- a/packages/core/src/expose.js +++ b/packages/core/src/expose.js @@ -3,8 +3,9 @@ const config = require('./config') const event = require('./event') const shell = require('./shell') const modules = require('./modules') -const proxyConfig = require('./lib/proxy/common/config') const lodash = require('lodash') +const proxyServer = require('@docmirror/mitmproxy') +const proxyConfig = proxyServer.config const context = { config, shell, @@ -23,10 +24,6 @@ function setupPlugin (key, plugin, context, config) { return api } -function fireStatus (target) { - event.fire('status', target) -} - const server = modules.server const proxy = setupPlugin('proxy', modules.proxy, context, config) const plugin = {} @@ -40,14 +37,11 @@ config.resetDefault() module.exports = { status, api: { - startup: async (newConfig) => { - if (newConfig) { - config.set(newConfig) - } + startup: async ({ mitmproxyPath }) => { const conf = config.get() if (conf.server.enabled) { try { - await server.start() + await server.start({ mitmproxyPath }) } catch (err) { console.error('代理服务启动失败:', err) } diff --git a/packages/core/src/modules/abstract-plugin.js b/packages/core/src/modules/abstract-plugin.js deleted file mode 100644 index 99f0dc7..0000000 --- a/packages/core/src/modules/abstract-plugin.js +++ /dev/null @@ -1,19 +0,0 @@ -class AbstractPlugin { - constructor (context) { - this._context = context - } - - _getConfig () { - return this._context.config.get() - } - - _getShell () { - return this._context.shell - } - - _fireStatus (event) { - this._context.event.fire('status', event) - } -} - -module.exports = AbstractPlugin diff --git a/packages/core/src/modules/plugin/node/config.js b/packages/core/src/modules/plugin/node/config.js index 91565e8..986779b 100644 --- a/packages/core/src/modules/plugin/node/config.js +++ b/packages/core/src/modules/plugin/node/config.js @@ -1,15 +1,13 @@ module.exports = { name: 'NPM加速', - enabled: true, + enabled: false, startup: { - npm: true, - yarn: true, variables: true }, setting: { - 'strict-ssl': false, - cafile: true, - NODE_EXTRA_CA_CERTS: true, + 'strict-ssl': true, + cafile: false, + NODE_EXTRA_CA_CERTS: false, NODE_TLS_REJECT_UNAUTHORIZED: false, registry: 'https://registry.npmjs.org'// 可以选择切换官方或者淘宝镜像 }, diff --git a/packages/core/src/modules/plugin/node/index.js b/packages/core/src/modules/plugin/node/index.js index b999898..e924c88 100644 --- a/packages/core/src/modules/plugin/node/index.js +++ b/packages/core/src/modules/plugin/node/index.js @@ -83,7 +83,6 @@ const NodePlugin = function (context) { async setRegistry (registry) { await nodeApi.setNpmEnv([{ key: 'registry', value: registry }]) - console.log(1111) return true }, @@ -103,7 +102,7 @@ const NodePlugin = function (context) { */ const nodeConfig = config.get().plugin.node if (nodeConfig.setting['strict-ssl']) { - cmds.push('npm nodeConfig set strict-ssl false') + cmds.push('npm config set strict-ssl false') } if (nodeConfig.setting.cafile) { cmds.push(`npm config set cafile "${rootCaFile}"`) @@ -115,13 +114,13 @@ const NodePlugin = function (context) { } if (nodeConfig.setting.NODE_TLS_REJECT_UNAUTHORIZED) { - cmds.push('npm nodeConfig set NODE_TLS_REJECT_UNAUTHORIZED 0') + cmds.push('npm config set NODE_TLS_REJECT_UNAUTHORIZED 0') env.push({ key: 'NODE_TLS_REJECT_UNAUTHORIZED', value: '0' }) } const ret = await shell.exec(cmds, { type: 'cmd' }) if (env.length > 0) { - // await shell.setSystemEnv({ list: env }) + await shell.setSystemEnv({ list: env }) } event.fire('status', { key: 'plugin.node.enabled', value: true }) console.info('开启【NPM】代理成功') diff --git a/packages/core/src/modules/proxy/index.js b/packages/core/src/modules/proxy/index.js index 9b1d66d..bee6975 100644 --- a/packages/core/src/modules/proxy/index.js +++ b/packages/core/src/modules/proxy/index.js @@ -39,12 +39,7 @@ module.exports = { enabled: true, name: '系统代理', use: 'local', - other: { - host: undefined, - port: undefined, - username: undefined, - password: undefined - } + other: [] }, status: { enabled: false, diff --git a/packages/core/src/modules/server/index.js b/packages/core/src/modules/server/index.js index 7333c32..09ed1a1 100644 --- a/packages/core/src/modules/server/index.js +++ b/packages/core/src/modules/server/index.js @@ -1,10 +1,19 @@ -const ProxyOptions = require('./options') -const mitmproxy = require('../../lib/proxy') const config = require('../../config') const event = require('../../event') const status = require('../../status') -const shell = require('../../shell') +const lodash = require('lodash') +const fork = require('child_process').fork let server +function fireStatus (status) { + event.fire('status', { key: 'server.enabled', value: status }) +} +function sleep (time) { + return new Promise(resolve => { + setTimeout(() => { + resolve() + }, time) + }) +} const serverApi = { async startup () { if (config.get().server.startup) { @@ -16,66 +25,73 @@ const serverApi = { return this.close() } }, - async start (newConfig) { - if (server != null) { - server.close() + async start ({ mitmproxyPath }) { + const allConfig = config.get() + const serverConfig = lodash.cloneDeep(allConfig.server) + + const intercepts = serverConfig.intercepts + const dnsMapping = serverConfig.dns.mapping + + if (allConfig.plugin) { + lodash.each(allConfig.plugin, (value) => { + const plugin = value + if (plugin.intercepts) { + lodash.merge(intercepts, plugin.intercepts) + } + if (plugin.dns) { + lodash.merge(dnsMapping, plugin.dns) + } + }) } - config.set(newConfig) - const proxyOptions = ProxyOptions(config.get()) - const newServer = mitmproxy.createProxy(proxyOptions, () => { - event.fire('status', { key: 'server.enabled', value: true }) - console.log('代理服务已启动:127.0.0.1:' + proxyOptions.port) - }) - newServer.on('close', () => { - if (server === newServer) { - server = null - event.fire('status', { key: 'server.enabled', value: false }) + // fireStatus('ing') // 启动中 + const serverProcess = fork(mitmproxyPath, [JSON.stringify(serverConfig)]) + server = { + id: serverProcess.pid, + process: serverProcess, + close () { + serverProcess.send({ type: 'action', event: { key: 'close' } }) + } + } + console.log('fork return pid: ' + serverProcess.pid) + serverProcess.on('message', function (msg) { + console.log('收到子进程消息', msg) + if (msg.type === 'status') { + fireStatus(msg.event) + } else if (msg.type === 'error') { + event.fire('error', { key: 'server', error: msg.event }) } }) - newServer.on('error', (e) => { - console.log('server error', e) - // newServer = null - event.fire('error', { key: 'server', error: e }) - }) - newServer.config = proxyOptions - server = newServer - return { port: proxyOptions.port } + return { port: config.port } + }, + async kill () { + if (server) { + server.process.kill('SIGINT') + await sleep(1000) + } + fireStatus(false) }, async close () { + return await serverApi.kill() + }, + async close1 () { return new Promise((resolve, reject) => { if (server) { - const currentServer = server - let closed = false + // fireStatus('ing')// 关闭中 server.close((err) => { if (err) { console.log('close error', err, ',', err.code, ',', err.message, ',', err.errno) if (err.code === 'ERR_SERVER_NOT_RUNNING') { console.log('代理服务关闭成功') - closed = true resolve() return } + console.log('代理服务关闭失败', err) reject(err) } else { console.log('代理服务关闭成功') - closed = true resolve() } }) - // 3秒后强制关闭 - setTimeout(async () => { - if (closed) { - return - } - console.log('强制关闭:', config.get().server.port) - await shell.killByPort({ port: config.get().server.port }) - if (currentServer === server) { - server = null - event.fire('status', { key: 'server.enabled', value: false }) - } - closed = true - resolve() - }, 3000) } else { console.log('server is null') resolve() @@ -83,11 +99,7 @@ const serverApi = { }) }, async restart () { - try { - await serverApi.close() - } catch (err) { - console.log('stop error', err) - } + await serverApi.kill() await serverApi.start() }, getServer () { diff --git a/packages/core/src/modules/server/options.js b/packages/core/src/modules/server/options.js deleted file mode 100644 index 355fe7a..0000000 --- a/packages/core/src/modules/server/options.js +++ /dev/null @@ -1,128 +0,0 @@ -const interceptors = require('../../lib/interceptor') -const dnsUtil = require('../../lib/dns') -const lodash = require('lodash') -function matchHostname (intercepts, hostname) { - const interceptOpts = intercepts[hostname] - if (interceptOpts) { - return interceptOpts - } - if (!interceptOpts) { - for (const target in intercepts) { - if (target.indexOf('*') < 0) { - continue - } - // 正则表达式匹配 - if (hostname.match(target)) { - return intercepts[target] - } - } - } -} - -function isMatched (url, regexp) { - return url.match(regexp) -} - -function domainRegexply (target) { - return target.replace(/\./g, '\\.').replace(/\*/g, '.*') -} - -// function test () { -// const ret = domainRegexply('*.aaa.com') -// console.log(ret) -// const success = 'aa.aaa.com'.match(ret) -// console.log(success) -// const fail = 'a.aaaa.com'.match(ret) -// console.log(fail) -// } -// test() - -module.exports = (config) => { - let intercepts = lodash.cloneDeep(config.server.intercepts) - const dnsMapping = lodash.cloneDeep(config.server.dns.mapping) - - if (config.plugin) { - lodash.each(config.plugin, (value) => { - const plugin = value - if (plugin.intercepts) { - lodash.merge(intercepts, plugin.intercepts) - } - if (plugin.dns) { - lodash.merge(dnsMapping, plugin.dns) - } - }) - } - - const regexpIntercepts = {} - lodash.each(intercepts, (value, domain) => { - if (domain.indexOf('*') >= 0) { - const regDomain = domainRegexply(domain) - regexpIntercepts[regDomain] = value - } else { - regexpIntercepts[domain] = value - } - }) - intercepts = regexpIntercepts - - const serverConfig = config.server - - return { - port: serverConfig.port, - dnsConfig: { - providers: dnsUtil.initDNS(serverConfig.dns.providers), - mapping: dnsMapping - }, - sslConnectInterceptor: (req, cltSocket, head) => { - const hostname = req.url.split(':')[0] - return !!matchHostname(intercepts, hostname) // 配置了拦截的域名,将会被代理 - }, - requestInterceptor: (rOptions, req, res, ssl, next) => { - const hostname = rOptions.hostname - const interceptOpts = matchHostname(intercepts, hostname) - if (!interceptOpts) { // 该域名没有配置拦截器,直接过 - next() - return - } - - for (const interceptOpt of interceptOpts) { // 遍历拦截配置 - let regexpList - if (interceptOpt.regexp != null) { - if (interceptOpt.regexp instanceof Array) { - regexpList = interceptOpt.regexp - } else { - regexpList = [interceptOpt.regexp] - } - } else { - regexpList = [true] - } - - for (const regexp of regexpList) { // 遍历regexp配置 - if (regexp !== true) { - if (!isMatched(req.url, regexp)) { - continue - } - } - for (const interceptImpl of interceptors) { - // 根据拦截配置挑选合适的拦截器来处理 - if (!interceptImpl.is(interceptOpt) && interceptImpl.requestInterceptor) { - continue - } - try { - const result = interceptImpl.requestInterceptor(interceptOpt, rOptions, req, res, ssl) - if (result) { // 拦截成功,其他拦截器就不处理了 - return - } - } catch (err) { - // 拦截失败 - console.error(err) - } - } - } - } - next() - }, - responseInterceptor: (req, res, proxyReq, proxyRes, ssl, next) => { - next() - } - } -} diff --git a/packages/core/src/modules/server/process/options.js b/packages/core/src/modules/server/process/options.js new file mode 100644 index 0000000..0ab7297 --- /dev/null +++ b/packages/core/src/modules/server/process/options.js @@ -0,0 +1,92 @@ +const interceptors = require('../../../lib/interceptor') +const dnsUtil = require('../../../lib/dns') +const lodash = require('lodash') +function matchHostname (intercepts, hostname) { + const interceptOpts = intercepts[hostname] + if (interceptOpts) { + return interceptOpts + } + if (!interceptOpts) { + for (const target in intercepts) { + if (target.indexOf('*') < 0) { + continue + } + // 正则表达式匹配 + if (hostname.match(target)) { + return intercepts[target] + } + } + } +} + +function isMatched (url, regexp) { + return url.match(regexp) +} + +function domainRegexply (target) { + return target.replace(/\./g, '\\.').replace(/\*/g, '.*') +} + +module.exports = (config) => { + const regexpIntercepts = {} + lodash.each(config.intercepts, (value, domain) => { + if (domain.indexOf('*') >= 0) { + const regDomain = domainRegexply(domain) + regexpIntercepts[regDomain] = value + } else { + regexpIntercepts[domain] = value + } + }) + + const intercepts = regexpIntercepts + const dnsMapping = config.dns.mapping + const serverConfig = config + + return { + port: serverConfig.port, + dnsConfig: { + providers: dnsUtil.initDNS(serverConfig.dns.providers), + mapping: dnsMapping + }, + sslConnectInterceptor: (req, cltSocket, head) => { + const hostname = req.url.split(':')[0] + return !!matchHostname(intercepts, hostname) // 配置了拦截的域名,将会被代理 + }, + requestInterceptor: (rOptions, req, res, ssl, next) => { + const hostname = rOptions.hostname + const interceptOpts = matchHostname(intercepts, hostname) + if (!interceptOpts) { // 该域名没有配置拦截器,直接过 + next() + return + } + + for (const regexp in interceptOpts) { // 遍历拦截配置 + const interceptOpt = interceptOpts[regexp] + if (regexp !== true) { + if (!isMatched(req.url, regexp)) { + continue + } + } + for (const interceptImpl of interceptors) { + // 根据拦截配置挑选合适的拦截器来处理 + if (!interceptImpl.is(interceptOpt) && interceptImpl.requestInterceptor) { + continue + } + try { + const result = interceptImpl.requestInterceptor(interceptOpt, rOptions, req, res, ssl) + if (result) { // 拦截成功,其他拦截器就不处理了 + return + } + } catch (err) { + // 拦截失败 + console.error(err) + } + } + } + next() + }, + responseInterceptor: (req, res, proxyReq, proxyRes, ssl, next) => { + next() + } + } +} diff --git a/packages/core/src/modules/server/process/proxy-server.js b/packages/core/src/modules/server/process/proxy-server.js new file mode 100644 index 0000000..42c17e1 --- /dev/null +++ b/packages/core/src/modules/server/process/proxy-server.js @@ -0,0 +1,68 @@ +const mitmproxy = require('../../../lib/proxy') +const ProxyOptions = require('./options') + +function fireError (e) { + process.send({ type: 'error', event: e }) +} +function fireStatus (status) { + process.send({ type: 'status', event: status }) +} + +let server +function start () { + const config = JSON.parse(process.argv[2]) + const proxyOptions = ProxyOptions(config) + console.log('proxy options:', proxyOptions) + const newServer = mitmproxy.createProxy(proxyOptions, () => { + fireStatus(true) + console.log('代理服务已启动:127.0.0.1:' + proxyOptions.port) + }) + newServer.on('close', () => { + if (server === newServer) { + server = null + fireStatus(false) + } + }) + newServer.on('error', (e) => { + console.log('server error', e) + // newServer = null + fireError(e) + }) +} + +const api = { + async close () { + return new Promise((resolve, reject) => { + if (server) { + server.close((err) => { + if (err) { + console.log('close error', err, ',', err.code, ',', err.message, ',', err.errno) + if (err.code === 'ERR_SERVER_NOT_RUNNING') { + console.log('代理服务关闭成功') + resolve() + return + } + reject(err) + } else { + console.log('代理服务关闭成功') + resolve() + } + }) + } else { + console.log('server is null') + fireStatus(false) + resolve() + } + }) + } +} + +process.on('message', function (msg) { + console.log('child get msg: ' + JSON.stringify(msg)) + if (msg.type === 'action') { + api[msg.event.key](msg.event.params) + } +}) + +// 启动服务 +start() diff --git a/packages/core/src/shell/index.js b/packages/core/src/shell/index.js index 7a1b9dd..a86dff8 100644 --- a/packages/core/src/shell/index.js +++ b/packages/core/src/shell/index.js @@ -12,7 +12,7 @@ module.exports = { setSystemEnv, getNpmEnv, setNpmEnv, - exec (cmds, args) { - shell.getSystemShell().exec(cmds, args) + async exec (cmds, args) { + return shell.getSystemShell().exec(cmds, args) } } diff --git a/packages/core/src/shell/scripts/setup-ca.js b/packages/core/src/shell/scripts/setup-ca.js index b368456..b908085 100644 --- a/packages/core/src/shell/scripts/setup-ca.js +++ b/packages/core/src/shell/scripts/setup-ca.js @@ -1,9 +1,9 @@ const Shell = require('../shell') const execute = Shell.execute -const proxyConfig = require('../../lib/proxy/common/config') +const proxyServer = require('@docmirror/mitmproxy') const executor = { async windows (exec) { - const cmds = ['start ' + proxyConfig.getDefaultCACertPath()] + const cmds = ['start ' + proxyServer.config.getDefaultCACertPath()] // eslint-disable-next-line no-unused-vars const ret = await exec(cmds, { type: 'cmd' }) return true diff --git a/packages/core/src/shell/shell.js b/packages/core/src/shell/shell.js index 2b0acb0..82524ed 100644 --- a/packages/core/src/shell/shell.js +++ b/packages/core/src/shell/shell.js @@ -34,8 +34,9 @@ class DarwinSystemShell extends SystemShell { } class WindowsSystemShell extends SystemShell { - static async exec (cmds, args = { type: 'ps' }) { - const { type } = args + static async exec (cmds, args = { }) { + let { type } = args + type = type || 'ps' if (cmds instanceof String) { cmds = [cmds] } @@ -50,28 +51,33 @@ class WindowsSystemShell extends SystemShell { } const ret = await ps.invoke() - console.log('ps complete:', cmds, ret) + // console.log('ps complete:', cmds, ret) return ret } else { let compose = 'chcp 65001 ' for (const cmd of cmds) { compose += ' && ' + cmd } - return new Promise((resolve, reject) => { - childProcess.exec(compose, function (error, stdout, stderr) { - if (error) { - console.error('cmd 命令执行错误:', compose, error, stderr) - reject(error) - } else { - const data = stdout - resolve(data) - } - }) - }) + const ret = await childExec(compose) + // console.log('cmd complete:', cmds) + return ret } } } +function childExec (composeCmds) { + return new Promise((resolve, reject) => { + childProcess.exec(composeCmds, function (error, stdout, stderr) { + if (error) { + console.error('cmd 命令执行错误:', composeCmds, error, stderr) + reject(error) + } else { + resolve(stdout) + } + }) + }) +} + function getSystemShell () { switch (getSystemPlatform()) { case 'mac': diff --git a/packages/core/src/switch/proxy/impl/system-proxy.js b/packages/core/src/switch/proxy/impl/system-proxy.js index 1822da5..1ddf817 100644 --- a/packages/core/src/switch/proxy/impl/system-proxy.js +++ b/packages/core/src/switch/proxy/impl/system-proxy.js @@ -134,7 +134,7 @@ class WindowsSystemProxy extends SystemProxy { ps.invoke() .then(output => { - console.log(output) + // console.log(output) resolve() }) .catch(err => { diff --git a/packages/core/start.js b/packages/core/start.js index d382191..aa5fca7 100644 --- a/packages/core/start.js +++ b/packages/core/start.js @@ -2,7 +2,8 @@ const DevSidercar = require('.') // require('json5/lib/register') // const config = require('../../config/index.json5') // 启动服务 -DevSidercar.api.startup() +const mitmproxyPath = './mitmproxy' +DevSidercar.api.startup({ mitmproxyPath }) async function onClose () { console.log('on sigint ') await DevSidercar.api.shutdown() diff --git a/packages/gui/package.json b/packages/gui/package.json index bd89ab6..d84264d 100644 --- a/packages/gui/package.json +++ b/packages/gui/package.json @@ -16,6 +16,7 @@ "main": "background.js", "dependencies": { "@docmirror/dev-sidecar": "1.0.0", + "@docmirror/mitmproxy": "1.0.0", "ant-design-vue": "^1.6.5", "core-js": "^3.6.5", "electron-updater": "^4.3.5", diff --git a/packages/gui/public/index.html b/packages/gui/public/index.html index 4123528..60adb88 100644 --- a/packages/gui/public/index.html +++ b/packages/gui/public/index.html @@ -1,5 +1,5 @@ - +
@@ -7,11 +7,13 @@