mirror of
https://github.com/alibaba/anyproxy.git
synced 2025-08-04 21:39:04 +00:00
Add test cases for the proxy, and do some tiny fixes.
the fixes are: 1. add "content-type" in headers for when dealing with localresponse 2. make a more accurate tip for throttle rate when lower than 1 3. make the crtMgr funcionality a more independent one 4. uppercase the request header before sending it out update the tip
This commit is contained in:
@@ -14,8 +14,7 @@ var http = require("http"),
|
||||
logUtil = require("./log"),
|
||||
httpsServerMgr = require("./httpsServerMgr");
|
||||
|
||||
var defaultRule = require("./rule_default.js"),
|
||||
userRule = defaultRule; //init
|
||||
var userRule = util.freshRequire('./rule_default');
|
||||
|
||||
function userRequestHandler(req,userRes){
|
||||
/*
|
||||
@@ -103,7 +102,7 @@ function userRequestHandler(req,userRes){
|
||||
resourceInfo.resBody = resBody;
|
||||
resourceInfo.length = resBody ? resBody.length : 0;
|
||||
resourceInfo.statusCode = statusCode;
|
||||
|
||||
|
||||
GLOBAL.recorder && GLOBAL.recorder.updateRecord(resourceInfoId,resourceInfo);
|
||||
|
||||
userRes.writeHead(statusCode,resHeader);
|
||||
@@ -140,6 +139,8 @@ function userRequestHandler(req,userRes){
|
||||
options.headers = util.lower_keys(options.headers);
|
||||
options.headers["content-length"] = reqData.length; //rewrite content length info
|
||||
|
||||
options.headers = util.upper_keys(options.headers);
|
||||
|
||||
//send request
|
||||
var proxyReq = ( /https/.test(protocol) ? https : http).request(options, function(res) {
|
||||
|
||||
@@ -202,7 +203,7 @@ function userRequestHandler(req,userRes){
|
||||
|
||||
//delay
|
||||
},function(callback){
|
||||
var pauseTimeInMS = userRule.pauseBeforeSendingResponse(req,res);
|
||||
var pauseTimeInMS = userRule.pauseBeforeSendingResponse(req,res);
|
||||
if(pauseTimeInMS){
|
||||
setTimeout(callback,pauseTimeInMS);
|
||||
}else{
|
||||
@@ -232,7 +233,7 @@ function userRequestHandler(req,userRes){
|
||||
resourceInfo.resHeader = resHeader;
|
||||
resourceInfo.resBody = serverResData;
|
||||
resourceInfo.length = serverResData ? serverResData.length : 0;
|
||||
|
||||
|
||||
GLOBAL.recorder && GLOBAL.recorder.updateRecord(resourceInfoId,resourceInfo);
|
||||
|
||||
callback();
|
||||
@@ -260,7 +261,7 @@ function userRequestHandler(req,userRes){
|
||||
userRes.end();
|
||||
});
|
||||
|
||||
proxyReq.end(reqData);
|
||||
proxyReq.end(reqData);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -272,14 +273,14 @@ function connectReqHandler(req, socket, head){
|
||||
|
||||
var shouldIntercept = userRule.shouldInterceptHttpsReq(req);
|
||||
|
||||
//bypass webSocket on webinterface
|
||||
//bypass webSocket on webinterface
|
||||
if(targetPort == 8003){
|
||||
shouldIntercept = false; // TODO : a more general solution?
|
||||
}
|
||||
|
||||
logUtil.printLog(color.green("\nreceived https CONNECT request " + host));
|
||||
if(shouldIntercept){
|
||||
logUtil.printLog("==>will forward to local https server");
|
||||
logUtil.printLog("==>will forward to local https server");
|
||||
}else{
|
||||
logUtil.printLog("==>will bypass the man-in-the-middle proxy");
|
||||
}
|
||||
@@ -325,7 +326,7 @@ function connectReqHandler(req, socket, head){
|
||||
|
||||
//determine the target server
|
||||
function(callback){
|
||||
|
||||
|
||||
if(shouldIntercept){
|
||||
proxyPort = internalHttpsPort;
|
||||
proxyHost = "127.0.0.1";
|
||||
@@ -356,11 +357,11 @@ function connectReqHandler(req, socket, head){
|
||||
|
||||
callback();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
conn.on("error",function(e){
|
||||
logUtil.printLog("err when connect to + " + host + " , " + e, logUtil.T_ERR);
|
||||
});
|
||||
});
|
||||
}catch(e){
|
||||
logUtil.printLog("err when connect to remote https server (__host)".replace(/__host/,host), logUtil.T_ERR);
|
||||
}
|
||||
@@ -372,7 +373,7 @@ function connectReqHandler(req, socket, head){
|
||||
resourceInfo.resHeader = {};
|
||||
resourceInfo.resBody = "";
|
||||
resourceInfo.length = 0;
|
||||
|
||||
|
||||
GLOBAL.recorder && GLOBAL.recorder.updateRecord(resourceInfoId,resourceInfo);
|
||||
|
||||
callback();
|
||||
@@ -385,9 +386,13 @@ function connectReqHandler(req, socket, head){
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @return return the merged rule for reference
|
||||
*/
|
||||
function setRules(newRule){
|
||||
|
||||
if(!newRule){
|
||||
return;
|
||||
return userRule;
|
||||
}else{
|
||||
|
||||
if(!newRule.summary){
|
||||
@@ -396,8 +401,8 @@ function setRules(newRule){
|
||||
};
|
||||
}
|
||||
|
||||
userRule = util.merge(defaultRule,newRule);
|
||||
|
||||
userRule = util.merge(userRule,newRule);
|
||||
|
||||
var functions = [];
|
||||
if('function' == typeof(userRule.init)){
|
||||
functions.push(function(cb){
|
||||
@@ -416,6 +421,7 @@ function setRules(newRule){
|
||||
}
|
||||
});
|
||||
|
||||
return userRule;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -427,3 +433,4 @@ module.exports.userRequestHandler = userRequestHandler;
|
||||
module.exports.connectReqHandler = connectReqHandler;
|
||||
module.exports.setRules = setRules;
|
||||
module.exports.getRuleSummary = getRuleSummary;
|
||||
module.exports.token = Date.now();
|
||||
|
||||
Reference in New Issue
Block a user