/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/anyproxy@4.1.3/proxy.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
"use strict";const http=require("http"),https=require("https"),async=require("async"),color=require("colorful"),certMgr=require("./lib/certMgr"),Recorder=require("./lib/recorder"),logUtil=require("./lib/log"),util=require("./lib/util"),events=require("events"),co=require("co"),WebInterface=require("./lib/webInterface"),wsServerMgr=require("./lib/wsServerMgr"),ThrottleGroup=require("stream-throttle").ThrottleGroup,T_TYPE_HTTP="http",T_TYPE_HTTPS="https",DEFAULT_TYPE="http",PROXY_STATUS_INIT="INIT",PROXY_STATUS_READY="READY",PROXY_STATUS_CLOSED="CLOSED";class ProxyCore extends events.EventEmitter{constructor(e){if(super(),e=e||{},this.status="INIT",this.proxyPort=e.port,this.proxyType=/https/i.test(e.type||"http")?"https":"http",this.proxyHostName=e.hostname||"localhost",this.recorder=e.recorder,parseInt(process.versions.node.split(".")[0],10)<4)throw new Error("node.js >= v4.x is required for anyproxy");if(e.forceProxyHttps&&!certMgr.ifRootCAFileExists())throw logUtil.printLog("You can run `anyproxy-ca` to generate one root CA and then re-run this command"),new Error("root CA not found. Please run `anyproxy-ca` to generate one first.");if("https"===this.proxyType&&!e.hostname)throw new Error("hostname is required in https proxy");if(!this.proxyPort)throw new Error("proxy port is required");if(!this.recorder)throw new Error("recorder is required");if(e.forceProxyHttps&&e.rule&&e.rule.beforeDealHttpsRequest&&(logUtil.printLog('both "-i(--intercept)" and rule.beforeDealHttpsRequest are specified, the "-i" option will be ignored.',logUtil.T_WARN),e.forceProxyHttps=!1),this.httpProxyServer=null,this.requestHandler=null,this.proxyRule=e.rule||{},e.silent&&logUtil.setPrintStatus(!1),e.throttle){logUtil.printLog("throttle :"+e.throttle+"kb/s");const r=parseInt(e.throttle,10);if(r<1)throw new Error("Invalid throttle rate value, should be positive integer");global._throttle=new ThrottleGroup({rate:1024*r})}this.recorder=e.recorder;const r=util.freshRequire("./requestHandler");this.requestHandler=new r({wsIntercept:e.wsIntercept,httpServerPort:e.port,forceProxyHttps:!!e.forceProxyHttps,dangerouslyIgnoreUnauthorized:!!e.dangerouslyIgnoreUnauthorized},this.proxyRule,this.recorder)}handleExistConnections(e){const r=this;r.socketIndex++;const t=`socketIndex_${r.socketIndex}`;r.socketPool[t]=e,e.on("close",(()=>{delete r.socketPool[t]}))}start(){const e=this;if(e.socketIndex=0,e.socketPool={},"INIT"!==e.status)throw new Error("server status is not PROXY_STATUS_INIT, can not run start()");return async.series([function(r){"https"===e.proxyType?certMgr.getCertificate(e.proxyHostName,((t,o,s)=>{t?r(t):(e.httpProxyServer=https.createServer({key:o,cert:s},e.requestHandler.userRequestHandler),r(null))})):(e.httpProxyServer=http.createServer(e.requestHandler.userRequestHandler),r(null))},function(r){e.httpProxyServer.on("connect",e.requestHandler.connectReqHandler),r(null)},function(r){wsServerMgr.getWsServer({server:e.httpProxyServer,connHandler:e.requestHandler.wsHandler}),e.httpProxyServer.on("connection",(r=>{e.handleExistConnections.call(e,r)})),r(null)},function(r){e.httpProxyServer.listen(e.proxyPort),r(null)}],((r,t)=>{if(r){const t="err when start proxy server :(";logUtil.printLog(color.red(t),logUtil.T_ERR),logUtil.printLog(r,logUtil.T_ERR),e.emit("error",{error:r})}else{const r=("http"===e.proxyType?"Http":"Https")+" proxy started on port "+e.proxyPort;if(logUtil.printLog(color.green(r)),e.webServerInstance){const r="web interface started on port "+e.webServerInstance.webPort;logUtil.printLog(color.green(r))}let t="";const o=this.proxyRule.summary;o&&co((function*(){t="string"==typeof o?o:yield o(),logUtil.printLog(color.green(`Active rule is: ${t}`))})),e.status="READY",e.emit("ready")}})),e}close(){return new Promise((e=>{if(this.httpProxyServer){for(const e of this.requestHandler.conns){const r=e[0],t=e[1];logUtil.printLog(`destorying https connection : ${r}`),t.end()}for(const e of this.requestHandler.cltSockets){const r=e[0],t=e[1];logUtil.printLog(`closing https cltSocket : ${r}`),t.end()}if(this.requestHandler.httpsServerMgr&&this.requestHandler.httpsServerMgr.close(),this.socketPool)for(const e in this.socketPool)this.socketPool[e].destroy();this.httpProxyServer.close((r=>{r?(console.error(r),logUtil.printLog(`proxy server close FAILED : ${r.message}`,logUtil.T_ERR)):(this.httpProxyServer=null,this.status="CLOSED",logUtil.printLog(`proxy server closed at ${this.proxyHostName}:${this.proxyPort}`)),e(r)}))}else e()}))}}class ProxyServer extends ProxyCore{constructor(e){const r=new Recorder;super(Object.assign({recorder:r},e)),this.proxyWebinterfaceConfig=e.webInterface,this.recorder=r,this.webServerInstance=null}start(){this.recorder&&this.recorder.setDbAutoCompact(),this.proxyWebinterfaceConfig&&this.proxyWebinterfaceConfig.enable?(this.webServerInstance=new WebInterface(this.proxyWebinterfaceConfig,this.recorder),this.webServerInstance.start().then((()=>{super.start()})).catch((e=>{this.emit("error",e)}))):super.start()}close(){const e=this;return e.recorder&&(e.recorder.stopDbAutoCompact(),e.recorder.clear()),e.recorder=null,super.close().then((()=>{if(e.webServerInstance){const r=e.webServerInstance;return e.webServerInstance=null,logUtil.printLog("closing webInterface..."),r.close()}}))}}module.exports.ProxyCore=ProxyCore,module.exports.ProxyServer=ProxyServer,module.exports.ProxyRecorder=Recorder,module.exports.ProxyWebServer=WebInterface,module.exports.utils={systemProxyMgr:require("./lib/systemProxyMgr"),certMgr:certMgr};
//# sourceMappingURL=/sm/ca2515142b74b840af3fe1fc6e863a4bd1f30767c45db64bc79f3ae9155a5769.map