From 2fa755325624f97e84f18e590dff9d5a32e4e4a7 Mon Sep 17 00:00:00 2001 From: "dong.gao" Date: Thu, 6 Aug 2020 15:55:32 +0800 Subject: [PATCH] add cache option --- lib/recorder.js | 1 + lib/requestHandler.js | 22 ++++++++++++++-------- proxy.js | 18 ++++++++++++------ 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/lib/recorder.js b/lib/recorder.js index cd9707c35..ed61ddc8c 100644 --- a/lib/recorder.js +++ b/lib/recorder.js @@ -84,6 +84,7 @@ class Recorder extends events.EventEmitter { constructor(config) { super(config); this.globalId = 1; + this.fake = false; this.cachePath = getCacheDir(); this.db = new Datastore(); diff --git a/lib/requestHandler.js b/lib/requestHandler.js index 6a6c6c034..afd1db97d 100644 --- a/lib/requestHandler.js +++ b/lib/requestHandler.js @@ -415,12 +415,14 @@ function getUserReqHandler(userRule, recorder) { req, startTime: new Date().getTime() }; - resourceInfoId = recorder.appendRecord(resourceInfo); + if (!recorder.fake) { + resourceInfoId = recorder.appendRecord(resourceInfo); + } } try { resourceInfo.reqBody = reqData.toString(); //TODO: deal reqBody in webInterface.js - recorder && recorder.updateRecord(resourceInfoId, resourceInfo); + recorder && !recorder.fake && recorder.updateRecord(resourceInfoId, resourceInfo); } catch (e) { } }) @@ -505,7 +507,7 @@ function getUserReqHandler(userRule, recorder) { // console.info('===> resbody in record', resourceInfo); - recorder && recorder.updateRecord(resourceInfoId, resourceInfo); + recorder && !recorder.fake && recorder.updateRecord(resourceInfoId, resourceInfo); }) .catch((e) => { logUtil.printLog(color.green('Send final response failed:' + e.message), logUtil.T_ERR); @@ -619,7 +621,9 @@ function getConnectReqHandler(userRule, recorder, httpsServerMgr) { req, startTime: new Date().getTime() }; - resourceInfoId = recorder.appendRecord(resourceInfo); + if (!recorder.fake) { + resourceInfoId = recorder.appendRecord(resourceInfo); + } } }) .then(() => { @@ -677,7 +681,7 @@ function getConnectReqHandler(userRule, recorder, httpsServerMgr) { resourceInfo.resBody = ''; resourceInfo.length = 0; - recorder && recorder.updateRecord(resourceInfoId, resourceInfo); + recorder && !recorder.fake && recorder.updateRecord(resourceInfoId, resourceInfo); } }) .catch(co.wrap(function *(error) { @@ -726,7 +730,9 @@ function getWsHandler(userRule, recorder, wsClient, wsReq) { req: wsReq, startTime: new Date().getTime() }); - resourceInfoId = recorder.appendRecord(resourceInfo); + if (!recorder.fake) { + resourceInfoId = recorder.appendRecord(resourceInfo); + } } /** @@ -794,7 +800,7 @@ function getWsHandler(userRule, recorder, wsClient, wsReq) { }; // resourceInfo.wsMessages.push(message); - recorder && recorder.updateRecordWsMessage(resourceInfoId, message); + recorder && !recorder.fake && recorder.updateRecordWsMessage(resourceInfoId, message); }; proxyWs.onopen = () => { @@ -815,7 +821,7 @@ function getWsHandler(userRule, recorder, wsClient, wsReq) { resourceInfo.resBody = ''; resourceInfo.length = resourceInfo.resBody.length; - recorder && recorder.updateRecord(resourceInfoId, resourceInfo); + recorder && !recorder.fake && recorder.updateRecord(resourceInfoId, resourceInfo); }); proxyWs.onerror = (e) => { diff --git a/proxy.js b/proxy.js index 76ce576b3..2861952fc 100644 --- a/proxy.js +++ b/proxy.js @@ -92,7 +92,6 @@ class ProxyCore extends events.EventEmitter { // init recorder this.recorder = config.recorder; - // init request handler const RequestHandler = util.freshRequire('./requestHandler'); this.requestHandler = new RequestHandler({ @@ -293,13 +292,20 @@ class ProxyServer extends ProxyCore { * @param {object} [config.webInterface] - config of the web interface * @param {boolean} [config.webInterface.enable=false] - if web interface is enabled * @param {number} [config.webInterface.webPort=8002] - http port of the web interface + * @param {boolean} [config.cache=false] - never store cache on disk if cache is false */ constructor(config) { // prepare a recorder - const recorder = new Recorder(); - const configForCore = Object.assign({ + if (!('cache' in config)) { + config.cache = false; + } + const recorder = config.cache ? new Recorder() : { fake: true }; + if (!config.cache && config.webInterface) { + config.webInterface.enable = false; + } + const configForCore = Object.assign(config, { recorder, - }, config); + }); super(configForCore); @@ -309,7 +315,7 @@ class ProxyServer extends ProxyCore { } start() { - if (this.recorder) { + if (this.recorder && !this.recorder.fake) { this.recorder.setDbAutoCompact(); } @@ -333,7 +339,7 @@ class ProxyServer extends ProxyCore { close() { const self = this; // release recorder - if (self.recorder) { + if (self.recorder && !self.recorder.fake) { self.recorder.stopDbAutoCompact(); self.recorder.clear(); }