add web interface

This commit is contained in:
加里
2014-08-27 17:42:42 +08:00
parent 5c2fac9352
commit 385a4a8fe6
50 changed files with 533 additions and 24688 deletions

120
lib/recorder.js Normal file
View File

@@ -0,0 +1,120 @@
//start recording and share a list when required
var zlib = require('zlib'),
Datastore = require('nedb'),
util = require("util"),
events = require('events'),
db = new Datastore(); //in-memory store
function Recorder(){
var self = this,
id = 1;
self.recordBodyMap = []; // id - body
self.updateRecord = function(id,info){
if(id < 0 ) return;
var finalInfo = normalizeInfo(id,info);
db.update({_id:id},finalInfo);
self.updateRecordBody(id,info);
self.emit("update",finalInfo);
};
self.appendRecord = function(info){
if(info.req.headers.anyproxy_web_req){ //request from web interface
return -1;
}
var thisId = id++,
finalInfo = normalizeInfo(thisId,info);
db.insert(finalInfo);
self.updateRecordBody(id,info);
self.emit("update",finalInfo);
return thisId;
};
//update recordBody if exits
self.updateRecordBody =function(id,info){
if(id == -1) return;
if(!id || !info.resBody) return;
//add to body map
//do not save image data
if(/image/.test(info.res.headers['content-type'])){
self.recordBodyMap[id] = "(image)";
}else if(/gzip/.test(info.res.headers['content-encoding'])){
zlib.unzip(info.resBody,function(err,buffer){
if(err){
self.recordBodyMap[id] = "(err when unzip response buffer)";
}else{
self.recordBodyMap[id] = buffer.toString();
}
});
}else{
self.recordBodyMap[id] = info.resBody.toString();
}
};
self.getBody = function(id){
if(id < 0){
return "";
}
return self.recordBodyMap[id] || "";
};
self.getSummaryList = function(cb){
db.find({},cb);
};
}
util.inherits(Recorder, events.EventEmitter);
function normalizeInfo(id,info){
var singleRecord = {};
//general
singleRecord._id = id;
singleRecord.id = id;
singleRecord.url = info.url;
singleRecord.host = info.host;
singleRecord.path = info.path;
singleRecord.method = info.method;
//req
singleRecord.reqHeader = info.req.headers;
singleRecord.startTime = info.startTime;
//res
if(info.res){
singleRecord.statusCode= info.res.statusCode;
singleRecord.endTime = info.endTime;
singleRecord.resHeader = info.res.headers;
singleRecord.length = info.length;
if(info.res.headers['content-type']){
singleRecord.mime = info.res.headers['content-type'].split(";")[0];
}else{
singleRecord.mime = "";
}
singleRecord.duration = info.endTime - info.startTime;
}else{
singleRecord.statusCode= "";
singleRecord.endTime = "";
singleRecord.resHeader = "";
singleRecord.length = "";
singleRecord.mime = "";
singleRecord.duration = "";
}
return singleRecord;
}
module.exports = Recorder;

View File

@@ -29,10 +29,24 @@ var handleRule = {
function userRequestHandler(req,userRes){
var host = req.headers.host,
urlPattern = url.parse(req.url),
path = urlPattern.path,
urlPattern = url.parse(req.url),
path = urlPattern.path,
ifLocalruleMatched = false,
callback = null;
callback = null,
ifHttps = !!req.connection.encrypted && !/http:/.test(req.url),
resourceInfo = {},
resourceInfoId = -1;
resourceInfo.host = host;
resourceInfo.method = req.method;
resourceInfo.path = path;
resourceInfo.url = (ifHttps ? "https://" :"http://") + host + path;
resourceInfo.req = req;
resourceInfo.startTime = new Date().getTime();
try{
resourceInfoId = GLOBAL.recorder.appendRecord(resourceInfo);
}catch(e){}
console.log(color.green("\nreceived request to : " + host + path));
/*
@@ -41,6 +55,7 @@ function userRequestHandler(req,userRes){
in https server : /work/alibaba
*/
//handle OPTIONS request
if(req.method == "OPTIONS"){
console.log("==>OPTIONS req for CROS, will allow all");
userRes.writeHead(200,mergeCORSHeader(req.headers)); //remove any cache related header, add crossdomain headers
@@ -83,8 +98,6 @@ function userRequestHandler(req,userRes){
}
}
//sleep for seconds if configed in the rule file
//see rule_sample.js
if(hostTest && pathTest && !!rule.sleep){
@@ -102,7 +115,6 @@ function userRequestHandler(req,userRes){
}else{
console.log("==>will forward to real server by proxy");
var ifHttps = !!req.connection.encrypted && !/http:/.test(req.url);
var options = {
hostname : urlPattern.hostname || req.headers.host,
@@ -114,17 +126,31 @@ function userRequestHandler(req,userRes){
var proxyReq = (ifHttps ? https : http).request(options, function(res) {
userRes.writeHead(res.statusCode,mergeCORSHeader(req.headers,res.headers));
if(callback){
res.on('data',function(chunk){
userRes.write(chunk);
});
res.on('end',function(){
callback(userRes);
userRes.end();
});
}else{
res.pipe(userRes);
}
var resData = [],
length = 0;
res.on("data",function(chunk){
resData.push(chunk);
length += chunk.length;
userRes.write(chunk);
});
res.on("end",function(){
callback && callback.call(null,userRes);
userRes.end();
//update record info
resourceInfo.endTime = new Date().getTime();
resourceInfo.res = res;
resourceInfo.resBody = Buffer.concat(resData);
resourceInfo.length = length;
try{
GLOBAL.recorder.updateRecord(resourceInfoId,resourceInfo);
}catch(e){}
});
});
proxyReq.on("error",function(e){