2020-11-06 10:59:54 +00:00
|
|
|
|
const mitmproxy = require('./lib/proxy')
|
|
|
|
|
const ProxyOptions = require('./options')
|
2020-11-24 17:25:47 +00:00
|
|
|
|
const proxyConfig = require('./lib/proxy/common/config')
|
2020-11-13 06:36:00 +00:00
|
|
|
|
const log = require('./utils/util.log')
|
2020-11-24 17:25:47 +00:00
|
|
|
|
const { fireError, fireStatus } = require('./utils/util.process')
|
2021-03-24 17:00:45 +00:00
|
|
|
|
const speedTest = require('./lib/speed/index.js')
|
2020-11-06 10:59:54 +00:00
|
|
|
|
let server
|
|
|
|
|
|
|
|
|
|
function registerProcessListener () {
|
|
|
|
|
process.on('message', function (msg) {
|
2020-11-13 06:36:00 +00:00
|
|
|
|
log.info('child get msg: ' + JSON.stringify(msg))
|
2020-11-06 10:59:54 +00:00
|
|
|
|
if (msg.type === 'action') {
|
|
|
|
|
api[msg.event.key](msg.event.params)
|
2021-03-24 17:00:45 +00:00
|
|
|
|
} else if (msg.type === 'speed') {
|
|
|
|
|
speedTest.action(msg.event)
|
2020-11-06 10:59:54 +00:00
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
process.on('SIGINT', () => {
|
2020-11-13 06:36:00 +00:00
|
|
|
|
log.info('on sigint : closed ')
|
2020-11-06 10:59:54 +00:00
|
|
|
|
process.exit(0)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 避免异常崩溃
|
|
|
|
|
process.on('uncaughtException', function (err) {
|
|
|
|
|
if (err.code === 'ECONNABORTED') {
|
2020-11-13 06:36:00 +00:00
|
|
|
|
// log.error(err.errno)
|
2020-11-06 10:59:54 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
2020-11-13 06:36:00 +00:00
|
|
|
|
log.error('uncaughtException:', err)
|
2020-11-06 10:59:54 +00:00
|
|
|
|
})
|
|
|
|
|
|
2020-11-13 17:22:47 +00:00
|
|
|
|
process.on('unhandledRejection', (err, p) => {
|
|
|
|
|
log.info('Unhandled Rejection at: Promise', p, 'err:', err)
|
2020-11-06 10:59:54 +00:00
|
|
|
|
// application specific logging, throwing an error, or other logic here
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const api = {
|
|
|
|
|
async start (config) {
|
|
|
|
|
const proxyOptions = ProxyOptions(config)
|
2020-11-24 17:25:47 +00:00
|
|
|
|
const setting = config.setting
|
|
|
|
|
if (setting) {
|
|
|
|
|
if (setting.userBasePath) {
|
|
|
|
|
proxyConfig.setDefaultCABasePath(setting.userBasePath)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-06 10:59:54 +00:00
|
|
|
|
if (proxyOptions.setting && proxyOptions.setting.NODE_TLS_REJECT_UNAUTHORIZED === false) {
|
|
|
|
|
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
|
|
|
|
|
} else {
|
|
|
|
|
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '1'
|
|
|
|
|
}
|
|
|
|
|
const newServer = mitmproxy.createProxy(proxyOptions, () => {
|
|
|
|
|
fireStatus(true)
|
2020-11-13 06:36:00 +00:00
|
|
|
|
log.info('代理服务已启动:127.0.0.1:' + proxyOptions.port)
|
2020-11-06 10:59:54 +00:00
|
|
|
|
})
|
|
|
|
|
newServer.on('close', () => {
|
2020-11-13 06:36:00 +00:00
|
|
|
|
log.info('server will closed ')
|
2020-11-06 10:59:54 +00:00
|
|
|
|
if (server === newServer) {
|
|
|
|
|
server = null
|
|
|
|
|
fireStatus(false)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
newServer.on('error', (e) => {
|
2020-11-13 06:36:00 +00:00
|
|
|
|
log.info('server error', e)
|
2020-11-06 10:59:54 +00:00
|
|
|
|
// newServer = null
|
|
|
|
|
fireError(e)
|
|
|
|
|
})
|
|
|
|
|
server = newServer
|
|
|
|
|
|
|
|
|
|
registerProcessListener()
|
|
|
|
|
},
|
|
|
|
|
async close () {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
if (server) {
|
|
|
|
|
server.close((err) => {
|
|
|
|
|
if (err) {
|
2020-11-13 06:36:00 +00:00
|
|
|
|
log.info('close error', err, ',', err.code, ',', err.message, ',', err.errno)
|
2020-11-06 10:59:54 +00:00
|
|
|
|
if (err.code === 'ERR_SERVER_NOT_RUNNING') {
|
2020-11-13 06:36:00 +00:00
|
|
|
|
log.info('代理服务关闭成功')
|
2020-11-06 10:59:54 +00:00
|
|
|
|
resolve()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
reject(err)
|
|
|
|
|
} else {
|
2020-11-13 06:36:00 +00:00
|
|
|
|
log.info('代理服务关闭成功')
|
2020-11-06 10:59:54 +00:00
|
|
|
|
resolve()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
} else {
|
2020-11-13 06:36:00 +00:00
|
|
|
|
log.info('server is null')
|
2020-11-06 10:59:54 +00:00
|
|
|
|
fireStatus(false)
|
|
|
|
|
resolve()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
...api,
|
2020-11-24 17:25:47 +00:00
|
|
|
|
config: proxyConfig,
|
2021-03-24 17:00:45 +00:00
|
|
|
|
log,
|
|
|
|
|
speedTest
|
2020-11-06 10:59:54 +00:00
|
|
|
|
}
|