From ab3e0028850938fdabf21cf9a641d4beefc9be46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8A=A0=E9=87=8C?= Date: Thu, 23 Oct 2014 11:07:02 +0800 Subject: [PATCH] add localfile recorder --- README.md | 9 ++++++++- bin.js | 12 +++++++----- lib/recorder.js | 40 ++++++++++++++++++++++++++++++++++++---- proxy.js | 11 ++++++++--- 4 files changed, 59 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index a71f912..b9fabb3 100644 --- a/README.md +++ b/README.md @@ -211,7 +211,12 @@ Using https features Others ----------------- -#### work as a module + +#### to save request data +* to save request data to local file, using the following param ``` anyproxy --file /path/to/file ``` +* anyproxy uses [nedb](https://github.com/louischatriot/nedb) to save request data. Since NeDB's persistence uses an append-only format, you may get some duplicate record in local file. Remember to use the last records with the same id. + +#### work as a module for nodejs ``` npm install anyproxy --save ``` @@ -228,6 +233,7 @@ var options = { port : 8001, hostname : "localhost", rule : require("path/to/my/ruleModule.js"), + dbFile : null, //save request data to a specified file, will use in-memory db if not specified webPort : 8002, // port for web interface socketPort : 8003, // internal port for web socket, replace this when it is conflict with your own service webConfigPort : 8088 // internal port for web config(beta), replace this when it is conflict with your own service @@ -238,4 +244,5 @@ new proxy.proxyServer(options); ## Contact +* anyproxy用户旺旺群:1203077233 * Please feel free to raise any issue about this project, or give us some advice on this doc. :) diff --git a/bin.js b/bin.js index 6a8ef37..948bdad 100644 --- a/bin.js +++ b/bin.js @@ -7,9 +7,10 @@ var program = require('commander'), program .option('-u, --host [value]', 'hostname for https proxy, localhost for default') - .option('-t, --type [value]', 'http|https,http for default') + .option('-t, --type [value]', 'http|https, http for default') .option('-p, --port [value]', 'proxy port, 8001 for default') - .option('-r, --rule [value]', 'rule file to map localfiles') + .option('-f, --file [value]', 'save request data to a specified file, will use in-memory db if not specified') + .option('-r, --rule [value]', 'path for rule file,') .option('-g, --root [value]', 'generate root CA') .option('-c, --clear', 'clear all the tmp certificates') .parse(process.argv); @@ -41,10 +42,11 @@ if(program.clear){ } new proxy.proxyServer({ - type : program.type, - port : program.port, + type : program.type, + port : program.port, hostname : program.hostname, - rule : ruleModule + dbFile : program.file, + rule : ruleModule }); } diff --git a/lib/recorder.js b/lib/recorder.js index 8f204d1..19cad6f 100644 --- a/lib/recorder.js +++ b/lib/recorder.js @@ -2,12 +2,41 @@ var zlib = require('zlib'), Datastore = require('nedb'), util = require("util"), - events = require('events'), - db = new Datastore(); //in-memory store + fs = require("fs"), + events = require('events'); -function Recorder(){ +//option.filename +function Recorder(option){ var self = this, - id = 1; + id = 1, + db; + + option = option || {}; + if(option.filename){ + + try{ + if(fs.existsSync(option.filename)){ + fs.writeFileSync(option.filename,""); //empty original file + } + + db = new Datastore({ + filename : option.filename, + autoload :true + }); + db.persistence.setAutocompactionInterval(5001); + console.log("db file : " + option.filename); + + }catch(e){ + console.log(e); + console.log("Failed to load on-disk db file. Will use in-meomory db instead."); + db = new Datastore(); + } + + }else{ + //in-memory db + db = new Datastore(); + } + self.recordBodyMap = []; // id - body @@ -64,6 +93,9 @@ function Recorder(){ self.getSummaryList = function(cb){ db.find({},cb); }; + + + self.db = db; } util.inherits(Recorder, events.EventEmitter); diff --git a/proxy.js b/proxy.js index 24150d0..a7d2756 100644 --- a/proxy.js +++ b/proxy.js @@ -30,8 +30,6 @@ var http = require('http'), ip = require("ip"), fork = require("child_process").fork; -GLOBAL.recorder = new Recorder(); - var T_TYPE_HTTP = 0, T_TYPE_HTTPS = 1, DEFAULT_PORT = 8001, @@ -61,6 +59,7 @@ if(fs.existsSync(process.cwd() + '/rule.js')){ //option.webPort : 8002(default) //option.socketPort : 8003(default) //option.webConfigPort : 8088(default) +//option.dbFile : null(default) function proxyServer(option){ option = option || {}; @@ -73,6 +72,12 @@ function proxyServer(option){ socketPort = option.socketPort || DEFAULT_WEBSOCKET_PORT, //port for websocket proxyConfigPort = option.webConfigPort || DEFAULT_CONFIG_PORT; //port to ui config server + if(option.dbFile){ + GLOBAL.recorder = new Recorder({filename: option.dbFile}); + }else{ + GLOBAL.recorder = new Recorder(); + } + requestHandler.setRules(proxyRules); //TODO : optimize calling for set rule self.httpProxyServer = null; @@ -125,7 +130,7 @@ function proxyServer(option){ //kill web server when father process exits process.on("exit",function(){ - child_webServer.kill(); + child_webServer.kill(); }); GLOBAL.recorder.on("update",function(data){