reconstruct map local

This commit is contained in:
OttoMao
2015-07-07 17:31:56 +08:00
parent b595e3aa9c
commit 8a146f7373
36 changed files with 56684 additions and 530 deletions

View File

@@ -14,8 +14,8 @@ var http = require("http"),
logUtil = require("./log"),
httpsServerMgr = require("./httpsServerMgr");
var defaultRule = require("./rule_default.js"),
userRule = defaultRule; //init
var defaultRule = require("./rule_default.js"),
userRule = defaultRule; //init
function userRequestHandler(req,userRes){

View File

@@ -1,5 +1,12 @@
var utils = require("./util"),
bodyParser = require("body-parser"),
fs = require("fs");
var isRootCAFileExists = require("./certMgr.js").isRootCAFileExists(),
interceptFlag = false;
interceptFlag = false;
//e.g. [ { keyword: 'aaa', local: '/Users/Stella/061739.pdf' } ]
var mapConfig = [];
module.exports = {
summary:function(){
@@ -15,13 +22,30 @@ module.exports = {
if(req.method == "OPTIONS"){
return true;
}else{
return false;
var simpleUrl = (req.headers.host || "") + (req.url || "");
mapConfig.map(function(item){
var key = item.keyword;
if(simpleUrl.indexOf(key) >= 0){
req.anyproxy_map_local = item.local;
return false;
}
});
return !!req.anyproxy_map_local;
}
},
dealLocalResponse : function(req,reqBody,callback){
if(req.method == "OPTIONS"){
callback(200,mergeCORSHeader(req.headers),"");
}else if(req.anyproxy_map_local){
try{
var fileContent = fs.readFile(req.anyproxy_map_local,function(err,buffer){
callback(200, {}, buffer);
});
}catch(e){
callback(200, {}, "failed to load local file :" + req.anyproxy_map_local);
}
}
},
@@ -61,8 +85,33 @@ module.exports = {
//fetch entire traffic data
fetchTrafficData: function(id,info){},
setInterceptFlag:function(flag){
setInterceptFlag: function(flag){
interceptFlag = flag && isRootCAFileExists;
},
_plugIntoWebinterface: function(app,cb){
app.get("/filetree",function(req,res){
try{
var root = req.query.root || process.env.HOME || "/";
utils.filewalker(root,function(err, info){
res.json(info);
});
}catch(e){
res.end(e);
}
});
app.use(bodyParser.json());
app.get("/getMapConfig",function(req,res){
res.json(mapConfig);
});
app.post("/setMapConfig",function(req,res){
mapConfig = req.body;
res.json(mapConfig);
});
cb();
}
};

View File

@@ -1,3 +1,6 @@
var fs = require("fs"),
path = require("path");
// {"Content-Encoding":"gzip"} --> {"content-encoding":"gzip"}
module.exports.lower_keys = function(obj){
for(var key in obj){
@@ -27,5 +30,37 @@ module.exports.simpleRender = function(str, object, regexp){
if (match.charAt(0) == '\\') return match.slice(1);
return (object[name] != null) ? object[name] : '';
});
}
module.exports.filewalker = function(root,cb){
root = root || process.cwd();
var ret = {
directory :[],
file :[]
};
fs.readdir(root,function(err, list){
if(list && list.length){
list.map(function(item){
var fullPath = path.join(root,item),
stat = fs.lstatSync(fullPath);
if(stat.isFile()){
ret.file.push({
name : item,
fullPath : fullPath
});
}else if(stat.isDirectory()){
ret.directory.push({
name : item,
fullPath : fullPath
});
}
});
}
cb && cb.apply(null,[null,ret]);
});
}

View File

@@ -14,8 +14,13 @@ var express = require("express"),
function webInterface(config){
var port = config.port,
wsPort = config.wsPort,
ruleSummary = config.ruleSummary,
ipAddress = config.ip;
ipAddress = config.ip,
userRule = config.userRule,
ruleSummary = "";
try{
ruleSummary = userRule.summary();
}catch(e){}
var self = this,
myAbsAddress = "http://" + ipAddress + ":" + port +"/",
@@ -80,8 +85,6 @@ function webInterface(config){
res.setHeader("Content-Type", "text/html");
res.end(resDom);
});
app.use(function(req,res,next){
var indexTpl = fs.readFileSync(path.join(staticDir,"/index.html"),{encoding:"utf8"}),
@@ -100,7 +103,14 @@ function webInterface(config){
});
app.use(express.static(staticDir));
app.listen(port);
if(typeof userRule._plugIntoWebinterface == "function"){
userRule._plugIntoWebinterface(app,function(){
app.listen(port);
});
}else{
app.listen(port);
}
self.app = app;
}