From a444d8eb7775477a80dab766a6ad6b53922110db Mon Sep 17 00:00:00 2001 From: ottomao Date: Mon, 17 Nov 2014 00:54:41 -0800 Subject: [PATCH] Created What is rule file and how to write (markdown) --- What-is-rule-file-and-how-to-write.md | 72 +++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 What-is-rule-file-and-how-to-write.md diff --git a/What-is-rule-file-and-how-to-write.md b/What-is-rule-file-and-how-to-write.md new file mode 100644 index 0000000..9075c44 --- /dev/null +++ b/What-is-rule-file-and-how-to-write.md @@ -0,0 +1,72 @@ +Anyproxy exposes a lot of interface for you to hack requests. To use them, you have to write a *.js file which is a Node.js module. Here we name this kind of file as **rule file**, which means this file defines the rule to handle all requests. + +How it works +----------------------- +Basically, you should understand how http-proxy works. +Http-proxy is something like a man standing between client and server. When a client is configured with proxy, all requests sent to target server will be forwarded to proxy server. After getting requests, proxy server would try to figure out the request url(host and path), headers, and request body. Then with these data, proxy will send a request to target server on behalf of the client. As you can imagine, the procedure of getting response is also very likely. + +As you can see, the proxy server as a middle man could inspect and modify any byte of an http request without being noticed by client or target server. AnyProxy is exactly the tool for you to customize these requests. + +Here is a figure showing the whole procedure. The bold bubble on the right is interface exposed by anyproxy.(interface in this figure may by outdate, please refer to docs instead of this figure when coding) + +![](https://t.alipayobjects.com/images/T1v8pbXjJqXXXXXXXX.png) + + +Rule file scheme +----------------------- +Rule file should be written in javascript and running in Node.js. The following is a blank rule file with all interface. + +```javascript +module.exports = { + summary:function(){ + return "this is a blank rule for anyproxy"; + }, + + shouldUseLocalResponse : function(req,reqBody){ + return false; + }, + + dealLocalResponse : function(req,reqBody,callback){ + callback(statusCode,resHeader,responseData); + }, + + replaceRequestProtocol:function(req,protocol){ + return protocol; + }, + + replaceRequestOption : function(req,option){ + var newOption = option; + return newOption; + }, + + replaceRequestData: function(req,data){ + return data; + }, + + replaceResponseStatusCode: function(req,res,statusCode){ + return statusCode; + }, + + replaceResponseHeader: function(req,res,header){ + return header; + }, + + replaceServerResDataAsync: function(req,res,serverResData,callback){ + callback(serverResData); + }, + + pauseBeforeSendingResponse : function(req,res){ + return 0; + }, + + shouldInterceptHttpsReq :function(req){ + return false; + } +}; +``` + +[TODO] + +To invoke a rule file +------------------------- +* in shell ``anyproxy --rule /path/to/ruleFile.js`` \ No newline at end of file