add localfile recorder

This commit is contained in:
加里 2014-10-23 11:07:02 +08:00
parent ecabacc2f8
commit ab3e002885
4 changed files with 59 additions and 13 deletions

View File

@ -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. :)

12
bin.js
View File

@ -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
});
}

View File

@ -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);

View File

@ -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){