import path from 'path' ; import { logger } from '@modern-js/utils' ; import execSync from './execSync' ; import { isRootCAExists , generateRootCA , defaultRootCA , trustRootCA , } from './macCAManager' ; const { disableGlobalProxy , enableGlobalProxy } = require ( './macProxyManager' ) ; interface ProxyConfig { rule : string ; port : number ; } export default class WhistleProxy { private readonly rule : string ; private readonly port : number ; private readonly bin : string ; private readonly certDir : string ; constructor ( config ) { this . rule = config . rule ; this . port = config . port ; this . bin = path . resolve ( path . dirname ( require . resolve ( 'whistle' ) ) , 'bin/whistle.js' ) ; this . certDir = path . dirname ( defaultRootCA ) ; } async installRootCA ( ) { try { if ( ! isRootCAExists ( ) ) { await generateRootCA ( ) ; trustRootCA ( ) ; } } catch ( err ) { this . close ( ) ; throw err ; } } async start ( ) { logger . info ( `Starting the proxy server.....` ) ; execSync ( `${ this . bin } start --certDir=${ this . certDir } --port=${ this . port }` ) ; execSync ( `${ this . bin } use ${ this . rule } --force` ) ; await this . installRootCA ( ) ; enableGlobalProxy ( 'localhost' , this . port ) ; logger . info ( `Proxy Server start on localhost:${ this . port }\n` ) ; } close ( ) { execSync ( `${ this . bin } stop` ) ; disableGlobalProxy ( ) ; logger . info ( `Proxy Server has closed` ) ; } }