mirror of
https://github.com/alibaba/anyproxy.git
synced 2025-08-04 21:39:04 +00:00
optimize webServer, move it to /lib
This commit is contained in:
@@ -106,21 +106,22 @@ function Recorder(option){
|
||||
|
||||
if(!bodyContent){
|
||||
cb(null,result);
|
||||
}else{
|
||||
var record = doc[0],
|
||||
resHeader = record['resHeader'] || {};
|
||||
try{
|
||||
var charsetMatch = JSON.stringify(resHeader).match(/charset="?([a-zA-Z0-9\-]+)"?/);
|
||||
if(charsetMatch && charsetMatch.length > 1){
|
||||
var currentCharset = charsetMatch[1].toLowerCase();
|
||||
if(currentCharset != "utf-8" && iconv.encodingExists(currentCharset)){
|
||||
bodyContent = iconv.decode(bodyContent, currentCharset);
|
||||
}
|
||||
}
|
||||
}catch(e){}
|
||||
|
||||
cb(null,bodyContent.toString());
|
||||
}
|
||||
|
||||
var record = doc[0],
|
||||
resHeader = record['resHeader'] || {};
|
||||
try{
|
||||
var charsetMatch = JSON.stringify(resHeader).match(/charset="?([a-zA-Z0-9\-]+)"?/);
|
||||
if(charsetMatch && charsetMatch.length > 1){
|
||||
var currentCharset = charsetMatch[1].toLowerCase();
|
||||
if(currentCharset != "utf-8" && iconv.encodingExists(currentCharset)){
|
||||
bodyContent = iconv.decode(bodyContent, currentCharset);
|
||||
}
|
||||
}
|
||||
}catch(e){}
|
||||
|
||||
cb(null,bodyContent.toString());
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -61,17 +61,6 @@ module.exports = {
|
||||
//fetch entire traffic data
|
||||
fetchTrafficData: function(id,info){},
|
||||
|
||||
//[internal]
|
||||
customMenu:[
|
||||
{
|
||||
name :"test",
|
||||
handler :function(){}
|
||||
},{
|
||||
name :"second-test",
|
||||
handler :function(){}
|
||||
}
|
||||
],
|
||||
|
||||
setInterceptFlag:function(flag){
|
||||
interceptFlag = flag && isRootCAFileExists;
|
||||
}
|
||||
|
||||
107
lib/webInterface.js
Normal file
107
lib/webInterface.js
Normal file
@@ -0,0 +1,107 @@
|
||||
var express = require("express"),
|
||||
url = require('url'),
|
||||
fs = require("fs"),
|
||||
path = require("path"),
|
||||
events = require("events"),
|
||||
inherits = require("util").inherits,
|
||||
qrCode = require('qrcode-npm'),
|
||||
util = require("./util"),
|
||||
certMgr = require("./certMgr"),
|
||||
logUtil = require("./log");
|
||||
|
||||
|
||||
function webInterface(config){
|
||||
var port = config.port,
|
||||
wsPort = config.wsPort,
|
||||
ruleSummary = config.ruleSummary,
|
||||
ipAddress = config.ip;
|
||||
|
||||
var self = this,
|
||||
myAbsAddress = "http://" + ipAddress + ":" + port +"/",
|
||||
crtFilePath = certMgr.getRootCAFilePath(),
|
||||
staticDir = path.join(__dirname,'../web');
|
||||
|
||||
|
||||
var app = express();
|
||||
app.use(function(req, res, next) {
|
||||
res.setHeader("note", "THIS IS A REQUEST FROM ANYPROXY WEB INTERFACE");
|
||||
return next();
|
||||
});
|
||||
|
||||
// app.get("/summary",function(req,res){
|
||||
// recorder.getSummaryList(function(err,docs){
|
||||
// if(err){
|
||||
// res.end(err.toString());
|
||||
// }else{
|
||||
// res.json(docs.slice(docs.length -500));
|
||||
// }
|
||||
// });
|
||||
// });
|
||||
|
||||
app.get("/fetchCrtFile",function(req,res){
|
||||
if(crtFilePath){
|
||||
res.setHeader("Content-Type","application/x-x509-ca-cert");
|
||||
res.setHeader("Content-Disposition",'attachment; filename="rootCA.crt"');
|
||||
res.end(fs.readFileSync(crtFilePath,{encoding:null}));
|
||||
}else{
|
||||
res.setHeader("Content-Type","text/html");
|
||||
res.end("can not file rootCA ,plase use <strong>anyproxy --root</strong> to generate one");
|
||||
}
|
||||
});
|
||||
|
||||
//make qr code
|
||||
app.get("/qr",function(req,res){
|
||||
var qr = qrCode.qrcode(4, 'M'),
|
||||
targetUrl = myAbsAddress,
|
||||
qrImageTag,
|
||||
resDom;
|
||||
|
||||
qr.addData(targetUrl);
|
||||
qr.make();
|
||||
qrImageTag = qr.createImgTag(4);
|
||||
|
||||
resDom = '<a href="__url"> __img <br> click or scan qr code to start client </a>'.replace(/__url/,targetUrl).replace(/__img/,qrImageTag);
|
||||
res.setHeader("Content-Type", "text/html");
|
||||
res.end(resDom);
|
||||
});
|
||||
|
||||
app.get("/qr_root",function(req,res){
|
||||
var qr = qrCode.qrcode(4, 'M'),
|
||||
targetUrl = myAbsAddress + "fetchCrtFile",
|
||||
qrImageTag,
|
||||
resDom;
|
||||
|
||||
qr.addData(targetUrl);
|
||||
qr.make();
|
||||
qrImageTag = qr.createImgTag(4);
|
||||
|
||||
resDom = '<a href="__url"> __img <br> click or scan qr code to download rootCA.crt </a>'.replace(/__url/,targetUrl).replace(/__img/,qrImageTag);
|
||||
res.setHeader("Content-Type", "text/html");
|
||||
res.end(resDom);
|
||||
});
|
||||
|
||||
var indexTpl = fs.readFileSync(path.join(staticDir,"/index.html"),{encoding:"utf8"}),
|
||||
indexHTML = util.simpleRender(indexTpl, {
|
||||
rule : ruleSummary || "",
|
||||
wsPort : wsPort,
|
||||
ipAddress : ipAddress || "127.0.0.1"
|
||||
});
|
||||
|
||||
app.use(function(req,res,next){
|
||||
if(req.url == "/"){
|
||||
res.setHeader("Content-Type", "text/html");
|
||||
res.end(indexHTML);
|
||||
}else{
|
||||
next();
|
||||
}
|
||||
});
|
||||
|
||||
app.use(express.static(staticDir));
|
||||
app.listen(port);
|
||||
|
||||
self.app = app;
|
||||
}
|
||||
|
||||
inherits(webInterface, events.EventEmitter);
|
||||
|
||||
module.exports = webInterface;
|
||||
88
lib/wsServer.js
Normal file
88
lib/wsServer.js
Normal file
@@ -0,0 +1,88 @@
|
||||
//websocket server manager
|
||||
|
||||
var WebSocketServer = require('ws').Server,
|
||||
logUtil = require("./log");
|
||||
|
||||
function resToMsg(msg,cb){
|
||||
var result = {},
|
||||
jsonData;
|
||||
|
||||
try{
|
||||
jsonData = JSON.parse(msg);
|
||||
}catch(e){
|
||||
result = {
|
||||
type : "error",
|
||||
error: "failed to parse your request : " + e.toString()
|
||||
};
|
||||
cb && cb(result);
|
||||
return;
|
||||
}
|
||||
|
||||
if(jsonData.reqRef){
|
||||
result.reqRef = jsonData.reqRef;
|
||||
}
|
||||
|
||||
if(jsonData.type == "reqBody" && jsonData.id){
|
||||
GLOBAL.recorder.getBodyUTF8(jsonData.id, function(err, data){
|
||||
if(err){
|
||||
result = {
|
||||
type : "body",
|
||||
content : {
|
||||
id : null,
|
||||
body : null,
|
||||
error : err.toString()
|
||||
}
|
||||
};
|
||||
}else{
|
||||
result = {
|
||||
type : "body",
|
||||
content : {
|
||||
id : jsonData.id,
|
||||
body : data
|
||||
}
|
||||
};
|
||||
}
|
||||
cb && cb(result);
|
||||
});
|
||||
}else{ // more req handler here
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
//config.port
|
||||
function wsServer(config){
|
||||
//web socket interface
|
||||
var wss = new WebSocketServer({port: config.port});
|
||||
wss.broadcast = function(data) {
|
||||
if(typeof data == "object"){
|
||||
data = JSON.stringify(data);
|
||||
}
|
||||
|
||||
for(var i in this.clients){
|
||||
try{
|
||||
this.clients[i].send(data);
|
||||
}catch(e){
|
||||
logUtil.printLog("websocket failed to send data, " + e, logUtil.T_ERR);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
wss.on("connection",function(ws){
|
||||
ws.on("message",function(msg){
|
||||
resToMsg(msg,function(res){
|
||||
res && ws.send(JSON.stringify(res));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
GLOBAL.recorder.on("update",function(data){
|
||||
wss && wss.broadcast({
|
||||
type : "update",
|
||||
content: data
|
||||
});
|
||||
});
|
||||
|
||||
return wss;
|
||||
}
|
||||
|
||||
module.exports = wsServer;
|
||||
Reference in New Issue
Block a user