mirror of
https://github.com/alibaba/anyproxy.git
synced 2025-07-07 08:39:30 +00:00
Created What is rule file and how to write (markdown)
parent
6645a228d1
commit
a444d8eb77
72
What-is-rule-file-and-how-to-write.md
Normal file
72
What-is-rule-file-and-how-to-write.md
Normal file
@ -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)
|
||||
|
||||

|
||||
|
||||
|
||||
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``
|
Loading…
x
Reference in New Issue
Block a user