update doc

This commit is contained in:
加里 2014-09-05 13:53:12 +08:00
parent f334a659c2
commit a4c171e2b9
3 changed files with 88 additions and 16 deletions

View File

@ -1,6 +1,9 @@
anyproxy anyproxy
========== ==========
A fully configurable proxy in NodeJS, which can handle HTTPS requests perfectly. A fully configurable proxy in NodeJS, which can handle HTTPS requests perfectly.
(Some Chinese in this doc is nothing but translation of some key points. Be relax if you dont understand.)
![](https://i.alipayobjects.com/i/ecmng/png/201409/3NKRCRk2Uf.png)
Feature Feature
------------ ------------
@ -36,15 +39,33 @@ How to write your own rule file
* ``anyproxy --rule /path/to/ruleFile.js`` * ``anyproxy --rule /path/to/ruleFile.js``
* you may learn how it works by our samples: [https://github.com/alipay-ct-wd/anyproxy/tree/master/rule_sample](https://github.com/alipay-ct-wd/anyproxy/tree/master/rule_sample) * you may learn how it works by our samples: [https://github.com/alipay-ct-wd/anyproxy/tree/master/rule_sample](https://github.com/alipay-ct-wd/anyproxy/tree/master/rule_sample)
* samples in [rule_sample](https://github.com/alipay-ct-wd/anyproxy/tree/master/rule_sample) * samples in [rule_sample](https://github.com/alipay-ct-wd/anyproxy/tree/master/rule_sample)
* **rule__blank.js**, blank rule file with some comments. You may read this before writing your own rule file. * **rule__blank.js**,
* **rule_adjust_response_time.js**, delay all the response for 1500ms * blank rule file with some comments. You may read this before writing your own rule file.
* **rule_allow_CORS.js**, add CORS headers to allow cross-domain ajax request * 空白的规则文件模板,和一些注释
* **rule_intercept_some_https_requests.js**, intercept https requests toward github.com * **rule_adjust_response_time.js**
* **rule_remove_cache_header.js**, remove all cache-related headers from server * delay all the response for 1500ms
* **rule_replace_request_option.js**, replace request parameters before sending to the server * 把所有的响应延迟1500毫秒
* **rule_replace_response_data.js**, modify response data * **rule_allow_CORS.js**
* **rule_replace_response_status_code.js**, replace server's status code * add CORS headers to allow cross-domain ajax request
* **rule_use_local_data.js**, map some requests to local file * 为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_use_local_data.js**
* map some requests to local file
* 把响应映射到本地
* rule file scheme is as follows, you may also get it from [rule__blank.js](https://github.com/alipay-ct-wd/anyproxy/blob/master/rule_sample/rule__blank.js) * rule file scheme is as follows, you may also get it from [rule__blank.js](https://github.com/alipay-ct-wd/anyproxy/blob/master/rule_sample/rule__blank.js)

View File

@ -43,9 +43,9 @@ function proxyServer(option){
proxyType = /https/i.test(option.type || DEFAULT_TYPE) ? T_TYPE_HTTPS : T_TYPE_HTTP , proxyType = /https/i.test(option.type || DEFAULT_TYPE) ? T_TYPE_HTTPS : T_TYPE_HTTP ,
proxyPort = option.port || DEFAULT_PORT, proxyPort = option.port || DEFAULT_PORT,
proxyHost = option.hostname || DEFAULT_HOST, proxyHost = option.hostname || DEFAULT_HOST,
proxyRules = option.rule || require('./lib/rule_default'); proxyRules = option.rule || require('./lib/rule_default');
requestHandler.setRules(proxyRules); requestHandler.setRules(proxyRules); //TODO : optimize calling for set rule
self.httpProxyServer = null; self.httpProxyServer = null;
async.series( async.series(

View File

@ -1,25 +1,51 @@
module.exports = { module.exports = {
/* /*
these functions will overwrite the default ones, write your own when necessary. 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(){ summary:function(){
console.log("this is a blank rule for anyproxy"); console.log("this is a blank rule for anyproxy");
}, },
//whether to intercept this request by local logic
//=======================
//when getting a request from user
//收到用户请求之后
//=======================
//是否在本地直接发送响应(不再向服务器发出请求)
//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 //if the return value is true, anyproxy will call dealLocalResponse to get response data and will not send request to remote server anymore
shouldUseLocalResponse : function(req,reqBody){ shouldUseLocalResponse : function(req,reqBody){
return false; return false;
}, },
//如果shouldUseLocalResponse返回true会调用这个函数来获取本地响应内容
//you may deal the response locally instead of sending it to server //you may deal the response locally instead of sending it to server
//this function be called when shouldUseLocalResponse returns true //this function be called when shouldUseLocalResponse returns true
//callback(statusCode,resHeader,responseData) //callback(statusCode,resHeader,responseData)
//e.g. callback(200,{"content-type":"text/html"},"hello world") //e.g. callback(200,{"content-type":"text/html"},"hello world")
dealLocalResponse : function(req,reqBody,callback){ dealLocalResponse : function(req,reqBody,callback){
callback(statusCode,resHeader,responseData) callback(statusCode,resHeader,responseData)
}, },
//=======================
//when ready to send a request to server
//向服务端发出请求之前
//=======================
//替换向服务器发出的请求协议http和https的替换
//replace the request protocol when sending to the real server //replace the request protocol when sending to the real server
//protocol : "http" or "https" //protocol : "http" or "https"
replaceRequestProtocol:function(req,protocol){ replaceRequestProtocol:function(req,protocol){
@ -27,6 +53,7 @@ module.exports = {
return newProtocol; return newProtocol;
}, },
//替换向服务器发出的请求参数option)
//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 //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
//you may return a customized option to replace the original option //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 //you should not write content-length header in options, since anyproxy will handle it for you
@ -35,17 +62,29 @@ module.exports = {
return newOption; return newOption;
}, },
//替换请求的body
//replace the request body //replace the request body
replaceRequestData: function(req,data){ replaceRequestData: function(req,data){
return 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 //replace the statusCode before it's sent to the user
replaceResponseStatusCode: function(req,res,statusCode){ replaceResponseStatusCode: function(req,res,statusCode){
var newStatusCode = statusCode; var newStatusCode = statusCode;
return newStatusCode; return newStatusCode;
}, },
//替换服务器响应的http头
//replace the httpHeader before it's sent to the user //replace the httpHeader before it's sent to the user
//Here header == res.headers //Here header == res.headers
replaceResponseHeader: function(req,res,header){ replaceResponseHeader: function(req,res,header){
@ -53,6 +92,7 @@ module.exports = {
return newHeader; return newHeader;
}, },
//替换服务器响应的数据
//replace the response from the server before it's sent to the user //replace the response from the server before it's sent to the user
//you may return either a Buffer or a string //you may return either a Buffer or a string
//serverResData is a Buffer, you may get its content by calling serverResData.toString() //serverResData is a Buffer, you may get its content by calling serverResData.toString()
@ -60,12 +100,23 @@ module.exports = {
return serverResData; return serverResData;
}, },
//在请求返回给用户前的延迟时间
//add a pause before sending response to user //add a pause before sending response to user
pauseBeforeSendingResponse : function(req,res){ pauseBeforeSendingResponse : function(req,res){
var timeInMS = 1; //delay all requests for 1ms var timeInMS = 1; //delay all requests for 1ms
return timeInMS; return timeInMS;
}, },
//=======================
//https config
//=======================
//是否截获https请求
//should intercept https request, or it will be forwarded to real server //should intercept https request, or it will be forwarded to real server
shouldInterceptHttpsReq :function(req){ shouldInterceptHttpsReq :function(req){
return false; return false;