mirror of
https://github.com/alibaba/anyproxy.git
synced 2025-04-23 15:51:25 +00:00
add localfile recorder
This commit is contained in:
parent
ecabacc2f8
commit
ab3e002885
@ -211,7 +211,12 @@ Using https features
|
|||||||
|
|
||||||
Others
|
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
|
npm install anyproxy --save
|
||||||
```
|
```
|
||||||
@ -228,6 +233,7 @@ var options = {
|
|||||||
port : 8001,
|
port : 8001,
|
||||||
hostname : "localhost",
|
hostname : "localhost",
|
||||||
rule : require("path/to/my/ruleModule.js"),
|
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
|
webPort : 8002, // port for web interface
|
||||||
socketPort : 8003, // internal port for web socket, replace this when it is conflict with your own service
|
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
|
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
|
## Contact
|
||||||
|
* anyproxy用户旺旺群:1203077233
|
||||||
* Please feel free to raise any issue about this project, or give us some advice on this doc. :)
|
* Please feel free to raise any issue about this project, or give us some advice on this doc. :)
|
||||||
|
12
bin.js
12
bin.js
@ -7,9 +7,10 @@ var program = require('commander'),
|
|||||||
|
|
||||||
program
|
program
|
||||||
.option('-u, --host [value]', 'hostname for https proxy, localhost for default')
|
.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('-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('-g, --root [value]', 'generate root CA')
|
||||||
.option('-c, --clear', 'clear all the tmp certificates')
|
.option('-c, --clear', 'clear all the tmp certificates')
|
||||||
.parse(process.argv);
|
.parse(process.argv);
|
||||||
@ -41,10 +42,11 @@ if(program.clear){
|
|||||||
}
|
}
|
||||||
|
|
||||||
new proxy.proxyServer({
|
new proxy.proxyServer({
|
||||||
type : program.type,
|
type : program.type,
|
||||||
port : program.port,
|
port : program.port,
|
||||||
hostname : program.hostname,
|
hostname : program.hostname,
|
||||||
rule : ruleModule
|
dbFile : program.file,
|
||||||
|
rule : ruleModule
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,12 +2,41 @@
|
|||||||
var zlib = require('zlib'),
|
var zlib = require('zlib'),
|
||||||
Datastore = require('nedb'),
|
Datastore = require('nedb'),
|
||||||
util = require("util"),
|
util = require("util"),
|
||||||
events = require('events'),
|
fs = require("fs"),
|
||||||
db = new Datastore(); //in-memory store
|
events = require('events');
|
||||||
|
|
||||||
function Recorder(){
|
//option.filename
|
||||||
|
function Recorder(option){
|
||||||
var self = this,
|
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
|
self.recordBodyMap = []; // id - body
|
||||||
|
|
||||||
@ -64,6 +93,9 @@ function Recorder(){
|
|||||||
self.getSummaryList = function(cb){
|
self.getSummaryList = function(cb){
|
||||||
db.find({},cb);
|
db.find({},cb);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
self.db = db;
|
||||||
}
|
}
|
||||||
|
|
||||||
util.inherits(Recorder, events.EventEmitter);
|
util.inherits(Recorder, events.EventEmitter);
|
||||||
|
11
proxy.js
11
proxy.js
@ -30,8 +30,6 @@ var http = require('http'),
|
|||||||
ip = require("ip"),
|
ip = require("ip"),
|
||||||
fork = require("child_process").fork;
|
fork = require("child_process").fork;
|
||||||
|
|
||||||
GLOBAL.recorder = new Recorder();
|
|
||||||
|
|
||||||
var T_TYPE_HTTP = 0,
|
var T_TYPE_HTTP = 0,
|
||||||
T_TYPE_HTTPS = 1,
|
T_TYPE_HTTPS = 1,
|
||||||
DEFAULT_PORT = 8001,
|
DEFAULT_PORT = 8001,
|
||||||
@ -61,6 +59,7 @@ if(fs.existsSync(process.cwd() + '/rule.js')){
|
|||||||
//option.webPort : 8002(default)
|
//option.webPort : 8002(default)
|
||||||
//option.socketPort : 8003(default)
|
//option.socketPort : 8003(default)
|
||||||
//option.webConfigPort : 8088(default)
|
//option.webConfigPort : 8088(default)
|
||||||
|
//option.dbFile : null(default)
|
||||||
function proxyServer(option){
|
function proxyServer(option){
|
||||||
option = option || {};
|
option = option || {};
|
||||||
|
|
||||||
@ -73,6 +72,12 @@ function proxyServer(option){
|
|||||||
socketPort = option.socketPort || DEFAULT_WEBSOCKET_PORT, //port for websocket
|
socketPort = option.socketPort || DEFAULT_WEBSOCKET_PORT, //port for websocket
|
||||||
proxyConfigPort = option.webConfigPort || DEFAULT_CONFIG_PORT; //port to ui config server
|
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
|
requestHandler.setRules(proxyRules); //TODO : optimize calling for set rule
|
||||||
self.httpProxyServer = null;
|
self.httpProxyServer = null;
|
||||||
|
|
||||||
@ -125,7 +130,7 @@ function proxyServer(option){
|
|||||||
|
|
||||||
//kill web server when father process exits
|
//kill web server when father process exits
|
||||||
process.on("exit",function(){
|
process.on("exit",function(){
|
||||||
child_webServer.kill();
|
child_webServer.kill();
|
||||||
});
|
});
|
||||||
|
|
||||||
GLOBAL.recorder.on("update",function(data){
|
GLOBAL.recorder.on("update",function(data){
|
||||||
|
Loading…
x
Reference in New Issue
Block a user