1- const mitmproxy = require ( './lib/proxy' )
2- const proxyConfig = require ( './lib/proxy/common/config' )
3- const speedTest = require ( './lib/speed/index.js' )
4- const ProxyOptions = require ( './options' )
5- const log = require ( './utils/util.log.server' )
6- const { fireError, fireStatus } = require ( './utils/util.process' )
1+ import proxyLib from './lib/proxy/index.js'
2+ import proxyConfig , { setDefaultCABasePath } from './lib/proxy/common/config.js'
3+ import speedTest from './lib/speed/index.js'
4+ import ProxyOptions from './options.js'
5+ import log from './utils/util.log.server.js'
6+ import utilProcess from './utils/util.process.js'
7+
8+ const { createProxy } = proxyLib
9+ const { action } = speedTest
10+ const { fireError, fireStatus } = utilProcess
11+ const { info, error, warn } = log
12+ // const mitmproxy = require('./lib/proxy')
13+ // const proxyConfig = require('./lib/proxy/common/config')
14+ // const speedTest = require('./lib/speed/index.js')
15+ // const ProxyOptions = require('./options')
16+ // const log = require('./utils/util.log.server')
17+ // const { fireError, fireStatus } = require('./utils/util.process')
718
819let servers = [ ]
920
@@ -13,7 +24,7 @@ const api = {
1324 const setting = config . setting
1425 if ( setting ) {
1526 if ( setting . userBasePath ) {
16- proxyConfig . setDefaultCABasePath ( setting . userBasePath )
27+ setDefaultCABasePath ( setting . userBasePath )
1728 }
1829 }
1930
@@ -23,7 +34,7 @@ const api = {
2334 process . env . NODE_TLS_REJECT_UNAUTHORIZED = '1'
2435 }
2536 // log.info('启动代理服务时的配置:', JSON.stringify(proxyOptions, null, '\t'))
26- const newServers = mitmproxy . createProxy ( proxyOptions , ( server , port , host , ssl ) => {
37+ const newServers = createProxy ( proxyOptions , ( server , port , host , ssl ) => {
2738 fireStatus ( true )
2839 log . info ( `代理服务已启动:${ host } :${ port } , ssl: ${ ssl } ` )
2940 } )
@@ -54,22 +65,22 @@ const api = {
5465 server . close ( ( err ) => {
5566 if ( err && err . code !== 'ERR_SERVER_NOT_RUNNING' ) {
5667 if ( err . code === 'ERR_SERVER_NOT_RUNNING' ) {
57- log . info ( '代理服务未运行,无需关闭' )
68+ info ( '代理服务未运行,无需关闭' )
5869 resolve ( )
5970 } else {
60- log . error ( '代理服务关闭失败:' , err )
71+ error ( '代理服务关闭失败:' , err )
6172 reject ( err )
6273 }
6374 return
6475 }
6576
66- log . info ( '代理服务关闭成功' )
77+ info ( '代理服务关闭成功' )
6778 resolve ( )
6879 } )
6980 }
7081 servers = [ ]
7182 } else {
72- log . info ( 'server is null, no need to close.' )
83+ info ( 'server is null, no need to close.' )
7384 fireStatus ( false )
7485 resolve ( )
7586 }
@@ -79,16 +90,16 @@ const api = {
7990
8091function registerProcessListener ( ) {
8192 process . on ( 'message' , ( msg ) => {
82- log . info ( 'child get msg:' , JSON . stringify ( msg ) )
93+ info ( 'child get msg:' , JSON . stringify ( msg ) )
8394 if ( msg . type === 'action' ) {
8495 api [ msg . event . key ] ( msg . event . params )
8596 } else if ( msg . type === 'speed' ) {
86- speedTest . action ( msg . event )
97+ action ( msg . event )
8798 }
8899 } )
89100
90101 process . on ( 'SIGINT' , ( ) => {
91- log . info ( 'on sigint : closed ' )
102+ info ( 'on sigint : closed ' )
92103 process . exit ( 0 )
93104 } )
94105
@@ -98,29 +109,29 @@ function registerProcessListener () {
98109 // log.error(err.errno)
99110 return
100111 }
101- log . error ( 'Process uncaughtException:' , err )
112+ error ( 'Process uncaughtException:' , err )
102113 } )
103114
104115 process . on ( 'unhandledRejection' , ( err , p ) => {
105- log . info ( 'Process unhandledRejection at: Promise' , p , 'err:' , err )
116+ info ( 'Process unhandledRejection at: Promise' , p , 'err:' , err )
106117 // application specific logging, throwing an error, or other logic here
107118 } )
108119 process . on ( 'uncaughtExceptionMonitor' , ( err , origin ) => {
109- log . info ( 'Process uncaughtExceptionMonitor:' , err , origin )
120+ info ( 'Process uncaughtExceptionMonitor:' , err , origin )
110121 } )
111122 process . on ( 'exit' , ( code , signal ) => {
112- log . info ( '代理服务进程被关闭:' , code , signal )
123+ info ( '代理服务进程被关闭:' , code , signal )
113124 } )
114125 // Aviod keeping print log before exit
115126 // process.on('beforeExit', (code, signal) => {
116127 // log.info('Process beforeExit event with code: ', code, signal)
117128 // })
118129 process . on ( 'SIGPIPE' , ( code , signal ) => {
119- log . warn ( 'sub Process SIGPIPE' , code , signal )
130+ warn ( 'sub Process SIGPIPE' , code , signal )
120131 } )
121132}
122133
123- module . exports = {
134+ export default {
124135 ...api ,
125136 config : proxyConfig ,
126137 log,
0 commit comments