anyproxy是一款可以高度定制的代理服务器,基于nodejs。
anyproxy把http通信过程中的各个阶段进行抽离,分解成三个阶段:
对于上述每个阶段,anyproxy都提供了API接口,引入开发者编写自己的规则代码,实时干预通信过程,以此满足各类自定义需求。
具体地,我们提供的接口包括:
npm install -g anyproxy
,有可能需要sudo
anyproxy
anyproxy --port 8001
anyproxy --rule ./rule_sample/rule_allow_CORS.js
anyproxy --intercept
(需要安装证书,详情见下文)anyproxy -h
查看明文解析HTTPS
anyproxy anyproxy --intercept
进阶 - 用rule来手动处理https请求(如:代理文件到本地)
shouldInterceptHttpsReq
这个函数来显式指定解析哪个请求。具体可以参照这份sample : rule_intercept_some_https_requests.js其他
anyproxy --clear
可以清除所有已生成的证书。清除后,各终端需要重新安装证书。anyproxy --type https
来调试。AnyProxy使用https over http
的方法来进行代理,而这条命令启动的是一个https代理服务器,两者使用场景完全不同。以“防止CDN返回304”这个需求为例,最直接的方案是拦截请求,在发送到CDN前删除header中的if-modified-since
字段。在AnyProxy中,配置replaceRequestOption接口,3行代码就能实现这个自定义功能:
//rule file
module.exports = {
//在向服务器发出请求前,AnyProxy会调用这个接口,可以在此时修改发送请求的参数
replaceRequestOption : function(req,option){
var newOption = option;
delete newOption.headers['if-modified-since'];
return newOption;
}
};
再举个例子,如果你想修改响应数据,在所有html文件最后加个”Hello World”,就需要调用replaceServerResDataAsync
接口,并结合content-type
字段来进行修改,大约需要8行代码。
//rule file
module.exports = {
replaceServerResDataAsync: function(req,res,serverResData,callback){
//append "hello world" to all web pages
if(/html/i.test(res.headers['content-type'])){
var newDataStr = serverResData.toString();
newDataStr += "hello world!";
callback(newDataStr);
}else{
callback(serverResData);
}
}
};
module.exports = {
pauseBeforeSendingResponse : function(req,res){
//delay all the response for 1500ms
return 1500;
}
};
//rule scheme :
// Ref: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
module.exports = {
shouldUseLocalResponse : function(req,reqBody){
//intercept all options request
if(req.method == "OPTIONS"){
return true;
}else{
return false;
}
},
dealLocalResponse : function(req,reqBody,callback){
if(req.method == "OPTIONS"){
callback(200,mergeCORSHeader(req.headers),"");
}
},
replaceResponseHeader: function(req,res,header){
return mergeCORSHeader(req.headers, header);
}
};
function mergeCORSHeader(reqHeader,originHeader){
var targetObj = originHeader || {};
delete targetObj["Access-Control-Allow-Credentials"];
delete targetObj["Access-Control-Allow-Origin"];
delete targetObj["Access-Control-Allow-Methods"];
delete targetObj["Access-Control-Allow-Headers"];
targetObj["access-control-allow-credentials"] = "true";
targetObj["access-control-allow-origin"] = reqHeader['origin'] || "-___-||";
targetObj["access-control-allow-methods"] = "GET, POST, PUT";
targetObj["access-control-allow-headers"] = reqHeader['access-control-request-headers'] || "-___-||";
return targetObj;
}
module.exports = {
replaceServerResDataAsync: function(req,res,serverResData,callback){
//add "hello github" to all github pages
if(req.headers.host == "github.com"){
serverResData += "hello github";
}
callback(serverResData);
},
shouldInterceptHttpsReq :function(req){
//intercept https://github.com/
//otherwise, all the https traffic will not go through this proxy
// return true;
if(req.headers.host == "github.com"){
return true;
}else{
return false;
}
}
};
//rule scheme :
module.exports = {
replaceRequestOption : function(req,option){
var newOption = option;
delete newOption.headers['if-none-match'];
delete newOption.headers['if-modified-since'];
return newOption;
},
replaceResponseHeader: function(req,res,header){
header = header || {};
header["Cache-Control"] = "no-cache, no-store, must-revalidate";
header["Pragma"] = "no-cache";
header["Expires"] = 0;
return header;
}
};
module.exports = {
replaceRequestOption : function(req,option){
//replace request towards http://www.taobao.com
// to http://www.taobao.com/about/
/*
option scheme:
{
hostname : "www.taobao.com"
port : 80
path : "/"
method : "GET"
headers : {cookie:""}
}
*/
if(option.hostname == "www.taobao.com" && option.path == "/"){
option.path = "/about/";
}
}
};
module.exports = {
replaceResponseStatusCode: function(req,res,statusCode){
//redirect requests toward http://www.taobao.com/*
// to http://www.etao.com
//using 302
if(req.headers.host == "www.taobao.com"){
statusCode = 302;
}
return statusCode;
},
replaceResponseHeader: function(req,res,header){
if(req.headers.host == "www.taobao.com"){
header.location = "http://www.etao.com";
}
return header;
}
};
//replace all the images with local one
var fs = require("fs");
var LOCAL_IMAGE = "/Users/path/to/image.png";
module.exports = {
summary:function(){
return "replace all the images with local one";
},
//mark if use local response
shouldUseLocalResponse : function(req,reqBody){
if(/\.(png|gif|jpg|jpeg)$/.test(req.url)){
req.replaceLocalFile = true;
return true;
}else{
return false;
}
},
dealLocalResponse : function(req,reqBody,callback){
if(req.replaceLocalFile){
callback(200, {"content-type":"image/png"}, fs.readFileSync(LOCAL_IMAGE) );
}
}
};
/*
read the following wiki before using rule file
https://github.com/alibaba/anyproxy/wiki/What-is-rule-file-and-how-to-write-one
*/
module.exports = {
/*
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.
致中文用户:中文注释都是只摘要,必要时请参阅英文文档。欢迎提出修改建议。
*/
summary:function(){
return "this is a blank rule for AnyProxy";
},
//=======================
//when getting a request from user
//收到用户请求之后
//=======================
//是否截获https请求
//should intercept https request, or it will be forwarded to real server
shouldInterceptHttpsReq :function(req){
return false;
},
//是否在本地直接发送响应(不再向服务器发出请求)
//whether to intercept this request by local logic
//if the return value is true, anyproxy will call dealLocalResponse to get response data and will not send request to remote server anymore
//req is the user's request sent to the proxy server
shouldUseLocalResponse : function(req,reqBody){
return false;
},
//如果shouldUseLocalResponse返回true,会调用这个函数来获取本地响应内容
//you may deal the response locally instead of sending it to server
//this function be called when shouldUseLocalResponse returns true
//callback(statusCode,resHeader,responseData)
//e.g. callback(200,{"content-type":"text/html"},"hello world")
dealLocalResponse : function(req,reqBody,callback){
callback(statusCode,resHeader,responseData)
},
//=======================
//when ready to send a request to server
//向服务端发出请求之前
//=======================
//替换向服务器发出的请求协议(http和https的替换)
//replace the request protocol when sending to the real server
//protocol : "http" or "https"
replaceRequestProtocol:function(req,protocol){
var newProtocol = protocol;
return newProtocol;
},
//替换向服务器发出的请求参数(option)
//option is the configuration of the http request sent to remote server. You may refers to http://nodejs.org/api/http.html#http_http_request_options_callback
//you may return a customized option to replace the original one
//you should not overwrite content-length header in options, since anyproxy will handle it for you
replaceRequestOption : function(req,option){
var newOption = option;
return newOption;
},
//替换请求的body
//replace the request body
replaceRequestData: function(req,data){
return data;
},
//=======================
//when ready to send the response to user after receiving response from server
//向用户返回服务端的响应之前
//=======================
//替换服务器响应的http状态码
//replace the statusCode before it's sent to the user
replaceResponseStatusCode: function(req,res,statusCode){
var newStatusCode = statusCode;
return newStatusCode;
},
//替换服务器响应的http头
//replace the httpHeader before it's sent to the user
//Here header == res.headers
replaceResponseHeader: function(req,res,header){
var newHeader = header;
return newHeader;
},
//替换服务器响应的数据
//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. for those non-unicode reponse , serverResData.toString() should not be your first choice.
replaceServerResDataAsync: function(req,res,serverResData,callback){
callback(serverResData);
},
//Deprecated
// replaceServerResData: function(req,res,serverResData){
// return serverResData;
// },
//在请求返回给用户前的延迟时间
//add a pause before sending response to user
pauseBeforeSendingResponse : function(req,res){
var timeInMS = 1; //delay all requests for 1ms
return timeInMS;
}
};
HTTPS相关配置的中文文档
What is rule file and how to write one
代理服务器的新轮子:anyproxy