update to 4.0

This commit is contained in:
Otto Mao
2017-12-01 21:30:49 +08:00
parent e392fefc64
commit 4be5aa8954
267 changed files with 27008 additions and 84482 deletions

View File

@@ -1,32 +0,0 @@
The following are sample rules.
* rule__blank.js
* blank rule file with some comments. You may read this before writing your own rule file.
* 空白的规则文件模板,和一些注释
* rule_adjust_response_time.js
* delay all the response for 1500ms
* 把所有的响应延迟1500毫秒
* rule_allow_CORS.js
* add CORS headers to allow cross-domain ajax request
* 为ajax请求增加跨域头
* rule_intercept_some_https_requests.js
* intercept https requests toward github.com and append some data
* 截获github.com的https请求再在最后加点文字
* rule_remove_cache_header.js
* remove all cache-related headers from server
* 去除响应头里缓存相关的头
* rule_replace_request_option.js
* replace request parameters before sending to the server
* 在请求发送到服务端前对参数做一些调整
* rule_replace_response_data.js
* modify response data
* 修改响应数据
* rule_replace_response_status_code.js
* replace server's status code
* 改变服务端响应的http状态码
* rule_reverse_proxy.js
* assign a specific ip address for request
* 为请求绑定目标ip
* rule_use_local_data.js
* map some requests to local file
* 把图片响应映射到本地

View File

@@ -1,118 +0,0 @@
/*
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;
}
};

View File

@@ -1,10 +0,0 @@
//rule scheme :
module.exports = {
pauseBeforeSendingResponse : function(req,res){
//delay all the response for 1500ms
return 1500;
}
};

View File

@@ -1,40 +0,0 @@
//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;
}

View File

@@ -1,25 +0,0 @@
//rule scheme :
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;
}
}
};

View File

@@ -1,20 +0,0 @@
//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;
}
};

View File

@@ -1,23 +0,0 @@
//rule scheme :
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/";
}
}
};

View File

@@ -1,19 +0,0 @@
//rule scheme :
module.exports = {
replaceServerResDataAsync: function(req,res,serverResData,callback){
//append "hello world" to all web pages
//for those non-unicode response , serverResData.toString() should not be your first choice.
//this issue may help you understanding how to modify an non-unicode response: https://github.com/alibaba/anyproxy/issues/20
if(/html/i.test(res.headers['content-type'])){
var newDataStr = serverResData.toString();
newDataStr += "hello world!";
callback(newDataStr);
}else{
callback(serverResData);
}
}
};

View File

@@ -1,24 +0,0 @@
//rule scheme :
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;
}
};

View File

@@ -1,23 +0,0 @@
/*
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 = {
summary:function(){
return "reverse proxy - assign an IP adress for some request";
},
replaceRequestOption : function(req,option){
var newOption = option;
//options : http://nodejs.org/api/http.html#http_http_request_options_callback
if(newOption.headers.host == "www.taobao.com"){
newOption.hostname = "127.0.0.1";
newOption.port = "80";
}
return newOption;
}
};

View File

@@ -1,28 +0,0 @@
//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) );
}
}
};

View File

@@ -0,0 +1,18 @@
/*
sample:
modify the post data towards http://httpbin.org/post
test:
curl -H "Content-Type: text/plain" -X POST -d 'original post data' http://httpbin.org/post --proxy http://127.0.0.1:8001
expected response:
{ "data": "i-am-anyproxy-modified-post-data" }
*/
module.exports = {
summary: 'Rule to modify request data',
*beforeSendRequest(requestDetail) {
if (requestDetail.url.indexOf('http://httpbin.org/post') === 0) {
return {
requestData: 'i-am-anyproxy-modified-post-data'
};
}
},
};

View File

@@ -0,0 +1,17 @@
/*
sample:
modify the user-agent in requests toward httpbin.org
test:
curl http://httpbin.org/user-agent --proxy http://127.0.0.1:8001
*/
module.exports = {
*beforeSendRequest(requestDetail) {
if (requestDetail.url.indexOf('http://httpbin.org') === 0) {
const newRequestOptions = requestDetail.requestOptions;
newRequestOptions.headers['User-Agent'] = 'AnyProxy/0.0.0';
return {
requestOptions: newRequestOptions
};
}
},
};

View File

@@ -0,0 +1,24 @@
/*
sample:
redirect all https://httpbin.org/user-agent requests to http://localhost:8008/index.html
test:
curl https://httpbin.org/user-agent --proxy http://127.0.0.1:8001
expected response:
'hello world' from 127.0.0.1:8001/index.html
*/
module.exports = {
*beforeSendRequest(requestDetail) {
if (requestDetail.url.indexOf('https://httpbin.org/user-agent') === 0) {
const newRequestOptions = requestDetail.requestOptions;
requestDetail.protocol = 'http';
newRequestOptions.hostname = '127.0.0.1'
newRequestOptions.port = '8008';
newRequestOptions.path = '/index.html';
newRequestOptions.method = 'GET';
return requestDetail;
}
},
*beforeDealHttpsRequest(requestDetail) {
return true;
}
};

View File

@@ -0,0 +1,20 @@
/*
sample:
redirect all http requests of httpbin.org to https
test:
curl 'http://httpbin.org/get?show_env=1' --proxy http://127.0.0.1:8001
expected response:
{ "X-Forwarded-Protocol": "https" }
*/
module.exports = {
*beforeSendRequest(requestDetail) {
if (requestDetail.url.indexOf('http://httpbin.org') === 0) {
const newOption = requestDetail.requestOptions;
newOption.port = 443;
return {
protocol: 'https',
requestOptions: newOption
};
}
}
};

View File

@@ -0,0 +1,22 @@
/*
sample:
modify response data of http://httpbin.org/user-agent
test:
curl 'http://httpbin.org/user-agent' --proxy http://127.0.0.1:8001
expected response:
{ "user-agent": "curl/7.43.0" } -- AnyProxy Hacked! --
*/
module.exports = {
*beforeSendResponse(requestDetail, responseDetail) {
if (requestDetail.url === 'http://httpbin.org/user-agent') {
const newResponse = responseDetail.response;
newResponse.body += '-- AnyProxy Hacked! --';
return new Promise((resolve, reject) => {
setTimeout(() => { // delay the response for 5s
resolve({ response: newResponse });
}, 5000);
});
}
},
};

View File

@@ -0,0 +1,19 @@
/*
sample:
modify response header of http://httpbin.org/user-agent
test:
curl -I 'http://httpbin.org/user-agent' --proxy http://127.0.0.1:8001
expected response:
X-Proxy-By: AnyProxy
*/
module.exports = {
*beforeSendResponse(requestDetail, responseDetail) {
if (requestDetail.url.indexOf('http://httpbin.org/user-agent') === 0) {
const newResponse = responseDetail.response;
newResponse.header['X-Proxy-By'] = 'AnyProxy';
return {
response: newResponse
};
}
}
};

View File

@@ -0,0 +1,19 @@
/*
sample:
modify all status code of http://httpbin.org/ to 404
test:
curl -I 'http://httpbin.org/user-agent' --proxy http://127.0.0.1:8001
expected response:
HTTP/1.1 404 Not Found
*/
module.exports = {
*beforeSendResponse(requestDetail, responseDetail) {
if (requestDetail.url.indexOf('http://httpbin.org') === 0) {
const newResponse = responseDetail.response;
newResponse.statusCode = 404;
return {
response: newResponse
};
}
}
};

View File

@@ -0,0 +1,20 @@
/*
sample:
intercept all requests toward httpbin.org, use a local response
test:
curl http://httpbin.org/user-agent --proxy http://127.0.0.1:8001
*/
module.exports = {
*beforeSendRequest(requestDetail) {
const localResponse = {
statusCode: 200,
header: { 'Content-Type': 'application/json' },
body: '{"hello": "this is local response"}'
};
if (requestDetail.url.indexOf('http://httpbin.org') === 0) {
return {
response: localResponse
};
}
},
};