Sitelet https://github.com/alibaba/anyproxy/commit/fc8c0d799273d7462f70b369fd2a7785b7b616d8
Skip to content

Commit fc8c0d7

Browse files
author
加里
committed
update doc;update proxy.js param
1 parent 1850198 commit fc8c0d7

5 files changed

Lines changed: 69 additions & 40 deletions

File tree

‎README.md‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Feature
77
* work as http or https proxy
88
* fully configurable, you can modify a request at any stage by your own javascript code
99
* when working as https proxy, it can generate and intercept https requests for any domain without complaint by browser (after you trust its root CA)
10-
* provide a web interface
10+
* a web interface is availabe for you to view request details
1111

1212
![screenshot](http://gtms03.alicdn.com/tps/i3/TB1ddyqGXXXXXbXXpXXihxC1pXX-1000-549.jpg_640x640q90.jpg)
1313

@@ -54,6 +54,9 @@ module.exports = {
5454
/*
5555
these functions will overwrite the default ones, write your own when necessary.
5656
*/
57+
summary:function(){
58+
console.log("this is a blank rule for anyproxy");
59+
},
5760

5861
//whether to intercept this request by local logic
5962
//if the return value is true, anyproxy will call dealLocalResponse to get response data and will not send request to remote server anymore
@@ -76,8 +79,7 @@ module.exports = {
7679
return newProtocol;
7780
},
7881

79-
//req is user's request sent to the proxy server
80-
//option is how the proxy server will send request to the real server. i.e. require("http").request(option,function(){...})
82+
//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
8183
//you may return a customized option to replace the original option
8284
//you should not write content-length header in options, since anyproxy will handle it for you
8385
replaceRequestOption : function(req,option){
@@ -150,8 +152,17 @@ npm install anyproxy --save
150152
```javascript
151153
var proxy = require("anyproxy");
152154

153-
!proxy.isRootCAFileExists() && proxy.generateRootCA(); //please manually trust this rootCA
154-
new proxy.proxyServer("http","8001", "localhost" ,"path/to/rule/file.js");
155+
//create cert when you want to use https features
156+
//please manually trust this rootCA when it is the first time you run it
157+
!proxy.isRootCAFileExists() && proxy.generateRootCA();
158+
159+
var options = {
160+
type : "http",
161+
port : "8001",
162+
hostname : "localhost",
163+
rule : require("path/to/my/ruleModule.js")
164+
};
165+
new proxy.proxyServer(options);
155166

156167
```
157168

‎bin.js‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,27 @@ if(program.clear){
2525
process.exit(0);
2626
});
2727
}else{
28-
new proxy.proxyServer(program.type,program.port, program.host ,program.rule);
28+
var ruleModule;
29+
30+
if(program.rule){
31+
if(fs.existsSync(program.rule)){
32+
try{ //for abs path
33+
ruleModule = require(program.rule);
34+
}catch(e){ //for relative path
35+
ruleModule = require("./" + program.rule);
36+
}
37+
console.log(color.green("rule file loaded"));
38+
}else{
39+
console.log(color.red("can not find rule file"));
40+
}
41+
}
42+
43+
new proxy.proxyServer({
44+
type : program.type,
45+
port : program.port,
46+
hostname : program.hostname,
47+
rule : ruleModule
48+
});
49+
}
50+
2951

30-
}

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "anyproxy",
3-
"version": "2.1.2",
3+
"version": "2.2.0",
44
"description": "a charles/fiddler like proxy written in NodeJs, which can handle HTTPS requests and CROS perfectly.",
55
"main": "proxy.js",
66
"bin": {

‎proxy.js‎

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,49 +15,37 @@ var http = require('http'),
1515
getPort = require("./lib/getPort"),
1616
requestHandler = require("./lib/requestHandler"),
1717
Recorder = require("./lib/Recorder"),
18+
util = require("./lib/util"),
1819
entities = require("entities"),
1920
express = require("express"),
2021
WebSocketServer= require('ws').Server;
2122

2223
GLOBAL.recorder = new Recorder();
2324

24-
var T_TYPE_HTTP = 0,
25+
var T_TYPE_HTTP = 0,
2526
T_TYPE_HTTPS = 1,
2627
DEFAULT_PORT = 8001,
2728
DEFAULT_WEB_PORT = 8002,
2829
DEFAULT_WEBSOCKET_PORT = 8003,
2930
DEFAULT_HOST = "localhost",
3031
DEFAULT_TYPE = T_TYPE_HTTP;
3132

32-
function proxyServer(type, port, hostname,ruleFile){
33-
var self = this,
34-
proxyType = /https/i.test(type || DEFAULT_TYPE) ? T_TYPE_HTTPS : T_TYPE_HTTP ,
35-
proxyPort = port || DEFAULT_PORT,
36-
proxyHost = hostname || DEFAULT_HOST;
37-
33+
//option
34+
//option.type : 'http'(default) or 'https'
35+
//option.port : 8001(default)
36+
//option.rule : ruleModule
37+
//option.hostname : localhost(default)
38+
function proxyServer(option){
39+
option = option || {};
40+
41+
var self = this,
42+
proxyType = /https/i.test(option.type || DEFAULT_TYPE) ? T_TYPE_HTTPS : T_TYPE_HTTP ,
43+
proxyPort = option.port || DEFAULT_PORT,
44+
proxyHost = option.hostname || DEFAULT_HOST,
45+
proxyRules = option.rule;
46+
47+
requestHandler.setRules(proxyRules);
3848
self.httpProxyServer = null;
39-
self.close = function(){
40-
self.httpProxyServer && self.httpProxyServer.close();
41-
console.log(color.green("server closed :" + proxyHost + ":" + proxyPort));
42-
}
43-
44-
startWebServer();
45-
46-
if(ruleFile){
47-
if(fs.existsSync(ruleFile)){
48-
try{ //for abs path
49-
requestHandler.setRules(require(ruleFile));
50-
}catch(e){ //for relative path
51-
requestHandler.setRules(require("./" + ruleFile));
52-
}
53-
console.log(color.green("rule file loaded"));
54-
}else{
55-
console.log(color.red("can not find rule file"));
56-
}
57-
}else{
58-
rules = require("./lib/rule_default.js");
59-
requestHandler.setRules(rules);
60-
}
6149

6250
async.series(
6351
[
@@ -82,12 +70,18 @@ function proxyServer(type, port, hostname,ruleFile){
8270
}
8371
},
8472

73+
//listen CONNECT method for https over http
8574
function(callback){
86-
//listen CONNECT method for https over http
8775
self.httpProxyServer.on('connect',requestHandler.connectReqHandler);
8876

8977
self.httpProxyServer.listen(proxyPort);
9078
callback(null);
79+
},
80+
81+
//start web interface
82+
function(callback){
83+
startWebServer();
84+
callback(null);
9185
}
9286
],
9387

@@ -104,6 +98,10 @@ function proxyServer(type, port, hostname,ruleFile){
10498
}
10599
);
106100

101+
self.close = function(){
102+
self.httpProxyServer && self.httpProxyServer.close();
103+
console.log(color.green("server closed :" + proxyHost + ":" + proxyPort));
104+
}
107105
}
108106

109107
function startWebServer(port){

‎rule_sample/rule__blank.js‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ module.exports = {
2727
return newProtocol;
2828
},
2929

30-
//req is user's request sent to the proxy server
31-
//option is how the proxy server will send request to the real server. i.e. require("http").request(option,function(){...})
30+
//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
3231
//you may return a customized option to replace the original option
3332
//you should not write content-length header in options, since anyproxy will handle it for you
3433
replaceRequestOption : function(req,option){

0 commit comments

Comments
 (0)