mirror of
https://github.com/alibaba/anyproxy.git
synced 2025-04-24 04:01:27 +00:00
Merge pull request #1 from alexyan/master
User-defined callback methods, such as can be used to inject scripts, et...
This commit is contained in:
commit
c071f04294
@ -6,6 +6,7 @@ var http = require("http"),
|
||||
pathUtil = require("path"),
|
||||
color = require("colorful"),
|
||||
sleep = require("sleep"),
|
||||
Buffer = require('buffer').Buffer,
|
||||
httpsServerMgr = require("./httpsServerMgr");
|
||||
|
||||
var httpsServerMgrInstance = new httpsServerMgr();
|
||||
@ -30,7 +31,8 @@ function userRequestHandler(req,userRes){
|
||||
var host = req.headers.host,
|
||||
urlPattern = url.parse(req.url),
|
||||
path = urlPattern.path,
|
||||
ifLocalruleMatched = false;
|
||||
ifLocalruleMatched = false,
|
||||
callback = null;
|
||||
|
||||
console.log(color.green("\nreceived request to : " + host + path));
|
||||
/*
|
||||
@ -49,6 +51,7 @@ function userRequestHandler(req,userRes){
|
||||
for(var index in handleRule.map){
|
||||
var rule = handleRule.map[index];
|
||||
|
||||
|
||||
var hostTest = new RegExp(rule.host).test(host),
|
||||
pathTest = new RegExp(rule.path).test(path);
|
||||
|
||||
@ -79,12 +82,18 @@ function userRequestHandler(req,userRes){
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//sleep for seconds if configed in the rule file
|
||||
//see rule_sample.js
|
||||
if(hostTest && pathTest && !!rule.sleep){
|
||||
console.log(color.yellow('[' + req.url + '] will sleep for ' + rule.sleep + ' seconds.'));
|
||||
sleep.sleep(rule.sleep);
|
||||
}
|
||||
|
||||
if(hostTest && pathTest && !!rule.callback){
|
||||
callback = rule.callback;
|
||||
}
|
||||
}
|
||||
|
||||
if(ifLocalruleMatched){
|
||||
@ -104,7 +113,17 @@ function userRequestHandler(req,userRes){
|
||||
|
||||
var proxyReq = (ifHttps ? https : http).request(options, function(res) {
|
||||
userRes.writeHead(res.statusCode,mergeCORSHeader(req.headers,res.headers));
|
||||
res.pipe(userRes);
|
||||
if(callback){
|
||||
res.on('data',function(chunk){
|
||||
userRes.write(chunk);
|
||||
});
|
||||
res.on('end',function(){
|
||||
callback(userRes);
|
||||
userRes.end();
|
||||
});
|
||||
}else{
|
||||
res.pipe(userRes);
|
||||
}
|
||||
});
|
||||
|
||||
proxyReq.on("error",function(e){
|
||||
|
@ -19,8 +19,16 @@ var rules = {
|
||||
"localDir" :"/Users/Stella/tmp/"
|
||||
},{
|
||||
"host" :/./,
|
||||
"path" :/\.(js)/,
|
||||
"path" :/response\.(json)/,
|
||||
"sleep" :5//seconds
|
||||
},{
|
||||
"host" :/./,
|
||||
"path" :/(.*)\.html/,
|
||||
"callback" :function(res){
|
||||
//remoty.js will be inject into response via callback
|
||||
res.write("<script type=\"text\/javascript\" src=\"http:\/\/localhost:3001\/remoty\.js\"><\/script>");
|
||||
res.write("<script type=\"text\/javascript\" src=\"http:\/\/localhost:8080\/target\/target\-script\-min\.js\#anonymous\"><\/script>");
|
||||
}
|
||||
}
|
||||
]
|
||||
,"httpsConfig":{
|
||||
|
Loading…
x
Reference in New Issue
Block a user