diff --git a/packages/core/src/config.js b/packages/core/src/config.js index 1ca2911a..fd922103 100644 --- a/packages/core/src/config.js +++ b/packages/core/src/config.js @@ -1,7 +1,7 @@ const Shell = require('./shell') const lodash = require('lodash') const defConfig = require('./config/index.js') -const proxyServer = require('@docmirror/mitmproxy') + let configTarget = lodash.cloneDeep(defConfig) function _deleteDisabledItem (target) { lodash.forEach(target, (item, key) => { @@ -13,6 +13,7 @@ function _deleteDisabledItem (target) { } }) } + const configApi = { get () { return configTarget @@ -68,7 +69,7 @@ const configApi = { }) if (list.length > 0) { const context = { - ca_cert_path: proxyServer.config.getDefaultCACertPath() + root_ca_cert_path: configApi.get().server.setting.rootCaFile.certPath } 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 aeccb9ab..ad5aab56 100644 --- a/packages/core/src/config/index.js +++ b/packages/core/src/config/index.js @@ -1,3 +1,14 @@ +const path = require('path') +function getUserBasePath () { + const userHome = process.env.USERPROFILE + return path.resolve(userHome, './.dev-sidecar') +} +function getRootCaCertPath () { + return getUserBasePath() + '/dev-sidecar.ca.crt' +} +function getRootCaKeyPath () { + return getUserBasePath() + '/dev-sidecar.ca.key.pem' +} module.exports = { server: { enabled: true, @@ -7,6 +18,11 @@ module.exports = { script: { enabled: true, defaultDir: '../../../scripts/' + }, + userBasePath: getUserBasePath(), + rootCaFile: { + certPath: getRootCaCertPath(), + keyPath: getRootCaKeyPath() } }, intercepts: { diff --git a/packages/core/src/expose.js b/packages/core/src/expose.js index bcf633c6..a69396cc 100644 --- a/packages/core/src/expose.js +++ b/packages/core/src/expose.js @@ -4,15 +4,12 @@ const event = require('./event') const shell = require('./shell') const modules = require('./modules') const lodash = require('lodash') -const proxyServer = require('@docmirror/mitmproxy') -const proxyConfig = proxyServer.config const log = require('./utils/util.log') const context = { config, shell, status, event, - rootCaFile: proxyConfig.getDefaultCACertPath(), log } @@ -127,6 +124,7 @@ module.exports = { shell, server, proxy, - plugin + plugin, + log } } diff --git a/packages/core/src/modules/plugin/node/index.js b/packages/core/src/modules/plugin/node/index.js index 72b1c2fa..8bd62d82 100644 --- a/packages/core/src/modules/plugin/node/index.js +++ b/packages/core/src/modules/plugin/node/index.js @@ -1,6 +1,6 @@ const nodeConfig = require('./config') const NodePlugin = function (context) { - const { config, shell, event, rootCaFile, log } = context + const { config, shell, event, log } = context const nodeApi = { async start () { try { @@ -101,16 +101,17 @@ const NodePlugin = function (context) { NODE_TLS_REJECT_UNAUTHORIZED: false */ const nodeConfig = config.get().plugin.node + const rootCaCertFile = config.get().server.setting.rootCaFile.certPath if (nodeConfig.setting['strict-ssl']) { cmds.push('npm config set strict-ssl false') } if (nodeConfig.setting.cafile) { - cmds.push(`npm config set cafile "${rootCaFile}"`) + cmds.push(`npm config set cafile "${rootCaCertFile}"`) } if (nodeConfig.setting.NODE_EXTRA_CA_CERTS) { - cmds.push(`npm config set NODE_EXTRA_CA_CERTS "${rootCaFile}"`) - env.push({ key: 'NODE_EXTRA_CA_CERTS', value: rootCaFile }) + cmds.push(`npm config set NODE_EXTRA_CA_CERTS "${rootCaCertFile}"`) + env.push({ key: 'NODE_EXTRA_CA_CERTS', value: rootCaCertFile }) } if (nodeConfig.setting.NODE_TLS_REJECT_UNAUTHORIZED) { diff --git a/packages/core/src/modules/server/index.js b/packages/core/src/modules/server/index.js index df6bd51b..ae042822 100644 --- a/packages/core/src/modules/server/index.js +++ b/packages/core/src/modules/server/index.js @@ -4,6 +4,7 @@ const status = require('../../status') const lodash = require('lodash') const fork = require('child_process').fork const log = require('../../utils/util.log') +const fs = require('fs') let server function fireStatus (status) { event.fire('status', { key: 'server.enabled', value: status }) @@ -45,7 +46,10 @@ const serverApi = { }) } // fireStatus('ing') // 启动中 - const serverProcess = fork(mitmproxyPath, [JSON.stringify(serverConfig)]) + const basePath = serverConfig.setting.userBasePath + const runningConfig = basePath + '/running.json' + fs.writeFileSync(runningConfig, JSON.stringify(serverConfig)) + const serverProcess = fork(mitmproxyPath, [runningConfig]) server = { id: serverProcess.pid, process: serverProcess, @@ -64,7 +68,7 @@ const serverApi = { event.fire('error', { key: 'server', value: 'EADDRINUSE', error: msg.event }) } }) - return { port: config.port } + return { port: runningConfig.port } }, async kill () { if (server) { diff --git a/packages/core/src/shell/scripts/setup-ca.js b/packages/core/src/shell/scripts/setup-ca.js index b9080856..d9ac5204 100644 --- a/packages/core/src/shell/scripts/setup-ca.js +++ b/packages/core/src/shell/scripts/setup-ca.js @@ -1,17 +1,16 @@ const Shell = require('../shell') const execute = Shell.execute -const proxyServer = require('@docmirror/mitmproxy') const executor = { - async windows (exec) { - const cmds = ['start ' + proxyServer.config.getDefaultCACertPath()] + async windows (exec, { certPath }) { + const cmds = ['start "" "' + certPath + '"'] // eslint-disable-next-line no-unused-vars const ret = await exec(cmds, { type: 'cmd' }) return true }, - async linux (exec, { port }) { + async linux (exec, { certPath }) { throw Error('暂未实现此功能') }, - async mac (exec, { port }) { + async mac (exec, { certPath }) { throw Error('暂未实现此功能') } } diff --git a/packages/core/src/utils/util.log.js b/packages/core/src/utils/util.log.js index 484bd783..04c8a5ae 100644 --- a/packages/core/src/utils/util.log.js +++ b/packages/core/src/utils/util.log.js @@ -1,8 +1,7 @@ const log4js = require('log4js') -const path = require('path') -const getDefaultConfigBasePath = function () { - const userHome = process.env.USERPROFILE - return path.resolve(userHome, './.dev-sidecar') +const config = require('../config/index') +function getDefaultConfigBasePath () { + return config.server.setting.userBasePath } log4js.configure({ appenders: { std: { type: 'stdout' }, file: { type: 'file', pattern: 'yyyy-MM-dd', daysToKeep: 3, filename: getDefaultConfigBasePath() + '/logs/core.log' } }, diff --git a/packages/core/src/utils/util.proxy.js b/packages/core/src/utils/util.proxy.js new file mode 100644 index 00000000..8dd2cbd5 --- /dev/null +++ b/packages/core/src/utils/util.proxy.js @@ -0,0 +1,3 @@ +const proxyConfig = require('@docmirror/mitmproxy/config.js') +module.exports = { +} diff --git a/packages/core/start/mitmproxy.js b/packages/core/start/mitmproxy.js index b133da98..f0dc2c6a 100644 --- a/packages/core/start/mitmproxy.js +++ b/packages/core/start/mitmproxy.js @@ -1,7 +1,10 @@ // eslint-disable-next-line no-unused-vars const server = require('@docmirror/mitmproxy') -const config = JSON.parse(process.argv[2]) +const configPath = process.argv[2] +const fs = require('fs') const path = require('path') +const configJson = fs.readFileSync(configPath) +const config = JSON.parse(configJson) const scriptDir = '../../gui/extra/scripts/' config.setting.script.defaultDir = path.join(__dirname, scriptDir) server.start(config) diff --git a/packages/core/start/user_config.json5 b/packages/core/start/user_config.json5 index cbf61b09..4cdefd17 100644 --- a/packages/core/start/user_config.json5 +++ b/packages/core/start/user_config.json5 @@ -6,7 +6,7 @@ abort: true }, } - } + }, }, plugin: { node: { diff --git a/packages/gui/src/bridge/index.js b/packages/gui/src/bridge/index.js index 9f19a817..caa6fd4d 100644 --- a/packages/gui/src/bridge/index.js +++ b/packages/gui/src/bridge/index.js @@ -8,8 +8,7 @@ const pk = require('../../package.json') const mitmproxyPath = path.join(__dirname, 'mitmproxy.js') const log = require('../utils/util.log') const getDefaultConfigBasePath = function () { - const userHome = process.env.USERPROFILE - return path.resolve(userHome, './.dev-sidecar') + return DevSidecar.api.config.get().server.setting.userBasePath } const localApi = { /** diff --git a/packages/gui/src/bridge/mitmproxy.js b/packages/gui/src/bridge/mitmproxy.js index 2d555fcc..acd487ac 100644 --- a/packages/gui/src/bridge/mitmproxy.js +++ b/packages/gui/src/bridge/mitmproxy.js @@ -1,12 +1,15 @@ // eslint-disable-next-line no-unused-vars const server = require('@docmirror/mitmproxy') -const config = JSON.parse(process.argv[2]) +const configPath = process.argv[2] + +const fs = require('fs') const path = require('path') -const log = require('../utils/util.log') +const configJson = fs.readFileSync(configPath) +const config = JSON.parse(configJson) + let scriptDir = '../extra/scripts/' if (process.env.NODE_ENV === 'development') { scriptDir = '../extra/scripts/' } config.setting.script.defaultDir = path.join(__dirname, scriptDir) -log.debug('scriptDir', config.setting.script.defaultDir) server.start(config) diff --git a/packages/gui/src/utils/util.log.js b/packages/gui/src/utils/util.log.js index 0a99b1cf..8507a401 100644 --- a/packages/gui/src/utils/util.log.js +++ b/packages/gui/src/utils/util.log.js @@ -1,8 +1,7 @@ const log4js = require('log4js') -const path = require('path') +const DevSidecar = require('@docmirror/dev-sidecar') const getDefaultConfigBasePath = function () { - const userHome = process.env.USERPROFILE - return path.resolve(userHome, './.dev-sidecar') + return DevSidecar.api.config.get().server.setting.userBasePath } log4js.configure({ appenders: { std: { type: 'stdout' }, file: { type: 'file', pattern: 'yyyy-MM-dd', daysToKeep: 3, filename: getDefaultConfigBasePath() + '/logs/gui.log' } }, diff --git a/packages/gui/src/view/components/setup-ca.vue b/packages/gui/src/view/components/setup-ca.vue index 8fccd76d..66867b22 100644 --- a/packages/gui/src/view/components/setup-ca.vue +++ b/packages/gui/src/view/components/setup-ca.vue @@ -24,7 +24,6 @@