2014-09-01 16:38:43 +08:00
|
|
|
|
module.exports = {
|
|
|
|
|
/*
|
2014-09-05 13:53:12 +08:00
|
|
|
|
These functions will overwrite the default ones, write your own when necessary.
|
|
|
|
|
Comments in Chinese are nothing but a translation of key points. Be relax if you dont understand.
|
|
|
|
|
致中文用户:中文注释都是只摘要,必要时请参阅英文注释。欢迎提出修改建议。
|
2014-09-01 16:38:43 +08:00
|
|
|
|
*/
|
2014-09-03 15:39:31 +08:00
|
|
|
|
summary:function(){
|
|
|
|
|
console.log("this is a blank rule for anyproxy");
|
|
|
|
|
},
|
2014-09-01 16:38:43 +08:00
|
|
|
|
|
2014-09-05 13:53:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//=======================
|
|
|
|
|
//when getting a request from user
|
|
|
|
|
//收到用户请求之后
|
|
|
|
|
//=======================
|
|
|
|
|
|
|
|
|
|
//是否在本地直接发送响应(不再向服务器发出请求)
|
|
|
|
|
//whether to intercept this request by local logic
|
2014-09-01 16:38:43 +08:00
|
|
|
|
//if the return value is true, anyproxy will call dealLocalResponse to get response data and will not send request to remote server anymore
|
2014-09-02 14:54:45 +08:00
|
|
|
|
shouldUseLocalResponse : function(req,reqBody){
|
2014-09-03 15:20:57 +08:00
|
|
|
|
return false;
|
2014-09-01 16:38:43 +08:00
|
|
|
|
},
|
|
|
|
|
|
2014-09-05 13:53:12 +08:00
|
|
|
|
//如果shouldUseLocalResponse返回true,会调用这个函数来获取本地响应内容
|
2014-09-01 17:23:01 +08:00
|
|
|
|
//you may deal the response locally instead of sending it to server
|
2014-09-05 13:53:12 +08:00
|
|
|
|
//this function be called when shouldUseLocalResponse returns true
|
|
|
|
|
//callback(statusCode,resHeader,responseData)
|
|
|
|
|
//e.g. callback(200,{"content-type":"text/html"},"hello world")
|
2014-09-02 14:54:45 +08:00
|
|
|
|
dealLocalResponse : function(req,reqBody,callback){
|
2014-09-02 15:32:59 +08:00
|
|
|
|
callback(statusCode,resHeader,responseData)
|
2014-09-01 16:38:43 +08:00
|
|
|
|
},
|
|
|
|
|
|
2014-09-05 13:53:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//=======================
|
|
|
|
|
//when ready to send a request to server
|
|
|
|
|
//向服务端发出请求之前
|
|
|
|
|
//=======================
|
|
|
|
|
|
|
|
|
|
//替换向服务器发出的请求协议(http和https的替换)
|
2014-09-01 16:38:43 +08:00
|
|
|
|
//replace the request protocol when sending to the real server
|
|
|
|
|
//protocol : "http" or "https"
|
|
|
|
|
replaceRequestProtocol:function(req,protocol){
|
|
|
|
|
var newProtocol = protocol;
|
|
|
|
|
return newProtocol;
|
|
|
|
|
},
|
|
|
|
|
|
2014-09-05 13:53:12 +08:00
|
|
|
|
//替换向服务器发出的请求参数(option)
|
2014-09-04 11:22:35 +08:00
|
|
|
|
//req is user's request which will be sent to the proxy server, docs : http://nodejs.org/api/http.html#http_http_request_options_callback
|
2014-09-02 14:54:45 +08:00
|
|
|
|
//you may return a customized option to replace the original option
|
|
|
|
|
//you should not write content-length header in options, since anyproxy will handle it for you
|
|
|
|
|
replaceRequestOption : function(req,option){
|
|
|
|
|
var newOption = option;
|
|
|
|
|
return newOption;
|
|
|
|
|
},
|
|
|
|
|
|
2014-09-05 13:53:12 +08:00
|
|
|
|
//替换请求的body
|
2014-09-02 14:54:45 +08:00
|
|
|
|
//replace the request body
|
|
|
|
|
replaceRequestData: function(req,data){
|
2014-09-02 15:32:59 +08:00
|
|
|
|
return data;
|
2014-09-02 14:54:45 +08:00
|
|
|
|
},
|
|
|
|
|
|
2014-09-05 13:53:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//=======================
|
|
|
|
|
//when ready to send the response to user after receiving response from server
|
|
|
|
|
//向用户返回服务端的响应之前
|
|
|
|
|
//=======================
|
|
|
|
|
|
|
|
|
|
//替换服务器响应的http状态码
|
2014-09-01 16:38:43 +08:00
|
|
|
|
//replace the statusCode before it's sent to the user
|
|
|
|
|
replaceResponseStatusCode: function(req,res,statusCode){
|
|
|
|
|
var newStatusCode = statusCode;
|
|
|
|
|
return newStatusCode;
|
|
|
|
|
},
|
|
|
|
|
|
2014-09-05 13:53:12 +08:00
|
|
|
|
//替换服务器响应的http头
|
2014-09-01 16:38:43 +08:00
|
|
|
|
//replace the httpHeader before it's sent to the user
|
|
|
|
|
//Here header == res.headers
|
|
|
|
|
replaceResponseHeader: function(req,res,header){
|
|
|
|
|
var newHeader = header;
|
|
|
|
|
return newHeader;
|
|
|
|
|
},
|
|
|
|
|
|
2014-09-05 13:53:12 +08:00
|
|
|
|
//替换服务器响应的数据
|
2014-09-01 16:38:43 +08:00
|
|
|
|
//replace the response from the server before it's sent to the user
|
|
|
|
|
//you may return either a Buffer or a string
|
|
|
|
|
//serverResData is a Buffer, you may get its content by calling serverResData.toString()
|
|
|
|
|
replaceServerResData: function(req,res,serverResData){
|
|
|
|
|
return serverResData;
|
|
|
|
|
},
|
|
|
|
|
|
2014-09-05 13:53:12 +08:00
|
|
|
|
//在请求返回给用户前的延迟时间
|
2014-09-01 16:38:43 +08:00
|
|
|
|
//add a pause before sending response to user
|
|
|
|
|
pauseBeforeSendingResponse : function(req,res){
|
|
|
|
|
var timeInMS = 1; //delay all requests for 1ms
|
|
|
|
|
return timeInMS;
|
|
|
|
|
},
|
|
|
|
|
|
2014-09-05 13:53:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//=======================
|
|
|
|
|
//https config
|
|
|
|
|
//=======================
|
|
|
|
|
|
|
|
|
|
//是否截获https请求
|
2014-09-01 16:38:43 +08:00
|
|
|
|
//should intercept https request, or it will be forwarded to real server
|
|
|
|
|
shouldInterceptHttpsReq :function(req){
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|