mirror of
https://github.com/alibaba/anyproxy.git
synced 2025-04-23 16:01:26 +00:00
update doc;update proxy.js param
This commit is contained in:
parent
18501987fb
commit
fc8c0d7992
21
README.md
21
README.md
@ -7,7 +7,7 @@ Feature
|
|||||||
* work as http or https proxy
|
* work as http or https proxy
|
||||||
* fully configurable, you can modify a request at any stage by your own javascript code
|
* fully configurable, you can modify a request at any stage by your own javascript code
|
||||||
* 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)
|
* 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)
|
||||||
* provide a web interface
|
* a web interface is availabe for you to view request details
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
@ -54,6 +54,9 @@ 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.
|
||||||
*/
|
*/
|
||||||
|
summary:function(){
|
||||||
|
console.log("this is a blank rule for anyproxy");
|
||||||
|
},
|
||||||
|
|
||||||
//whether to intercept this request by local logic
|
//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
|
||||||
@ -76,8 +79,7 @@ module.exports = {
|
|||||||
return newProtocol;
|
return newProtocol;
|
||||||
},
|
},
|
||||||
|
|
||||||
//req is user's request sent to the proxy server
|
//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
|
||||||
//option is how the proxy server will send request to the real server. i.e. require("http").request(option,function(){...})
|
|
||||||
//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
|
||||||
replaceRequestOption : function(req,option){
|
replaceRequestOption : function(req,option){
|
||||||
@ -150,8 +152,17 @@ npm install anyproxy --save
|
|||||||
```javascript
|
```javascript
|
||||||
var proxy = require("anyproxy");
|
var proxy = require("anyproxy");
|
||||||
|
|
||||||
!proxy.isRootCAFileExists() && proxy.generateRootCA(); //please manually trust this rootCA
|
//create cert when you want to use https features
|
||||||
new proxy.proxyServer("http","8001", "localhost" ,"path/to/rule/file.js");
|
//please manually trust this rootCA when it is the first time you run it
|
||||||
|
!proxy.isRootCAFileExists() && proxy.generateRootCA();
|
||||||
|
|
||||||
|
var options = {
|
||||||
|
type : "http",
|
||||||
|
port : "8001",
|
||||||
|
hostname : "localhost",
|
||||||
|
rule : require("path/to/my/ruleModule.js")
|
||||||
|
};
|
||||||
|
new proxy.proxyServer(options);
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
25
bin.js
25
bin.js
@ -25,6 +25,27 @@ if(program.clear){
|
|||||||
process.exit(0);
|
process.exit(0);
|
||||||
});
|
});
|
||||||
}else{
|
}else{
|
||||||
new proxy.proxyServer(program.type,program.port, program.host ,program.rule);
|
var ruleModule;
|
||||||
|
|
||||||
|
if(program.rule){
|
||||||
|
if(fs.existsSync(program.rule)){
|
||||||
|
try{ //for abs path
|
||||||
|
ruleModule = require(program.rule);
|
||||||
|
}catch(e){ //for relative path
|
||||||
|
ruleModule = require("./" + program.rule);
|
||||||
|
}
|
||||||
|
console.log(color.green("rule file loaded"));
|
||||||
|
}else{
|
||||||
|
console.log(color.red("can not find rule file"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
new proxy.proxyServer({
|
||||||
|
type : program.type,
|
||||||
|
port : program.port,
|
||||||
|
hostname : program.hostname,
|
||||||
|
rule : ruleModule
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "anyproxy",
|
"name": "anyproxy",
|
||||||
"version": "2.1.2",
|
"version": "2.2.0",
|
||||||
"description": "a charles/fiddler like proxy written in NodeJs, which can handle HTTPS requests and CROS perfectly.",
|
"description": "a charles/fiddler like proxy written in NodeJs, which can handle HTTPS requests and CROS perfectly.",
|
||||||
"main": "proxy.js",
|
"main": "proxy.js",
|
||||||
"bin": {
|
"bin": {
|
||||||
|
56
proxy.js
56
proxy.js
@ -15,13 +15,14 @@ var http = require('http'),
|
|||||||
getPort = require("./lib/getPort"),
|
getPort = require("./lib/getPort"),
|
||||||
requestHandler = require("./lib/requestHandler"),
|
requestHandler = require("./lib/requestHandler"),
|
||||||
Recorder = require("./lib/Recorder"),
|
Recorder = require("./lib/Recorder"),
|
||||||
|
util = require("./lib/util"),
|
||||||
entities = require("entities"),
|
entities = require("entities"),
|
||||||
express = require("express"),
|
express = require("express"),
|
||||||
WebSocketServer= require('ws').Server;
|
WebSocketServer= require('ws').Server;
|
||||||
|
|
||||||
GLOBAL.recorder = new Recorder();
|
GLOBAL.recorder = new Recorder();
|
||||||
|
|
||||||
var T_TYPE_HTTP = 0,
|
var T_TYPE_HTTP = 0,
|
||||||
T_TYPE_HTTPS = 1,
|
T_TYPE_HTTPS = 1,
|
||||||
DEFAULT_PORT = 8001,
|
DEFAULT_PORT = 8001,
|
||||||
DEFAULT_WEB_PORT = 8002,
|
DEFAULT_WEB_PORT = 8002,
|
||||||
@ -29,35 +30,22 @@ var T_TYPE_HTTP = 0,
|
|||||||
DEFAULT_HOST = "localhost",
|
DEFAULT_HOST = "localhost",
|
||||||
DEFAULT_TYPE = T_TYPE_HTTP;
|
DEFAULT_TYPE = T_TYPE_HTTP;
|
||||||
|
|
||||||
function proxyServer(type, port, hostname,ruleFile){
|
//option
|
||||||
var self = this,
|
//option.type : 'http'(default) or 'https'
|
||||||
proxyType = /https/i.test(type || DEFAULT_TYPE) ? T_TYPE_HTTPS : T_TYPE_HTTP ,
|
//option.port : 8001(default)
|
||||||
proxyPort = port || DEFAULT_PORT,
|
//option.rule : ruleModule
|
||||||
proxyHost = hostname || DEFAULT_HOST;
|
//option.hostname : localhost(default)
|
||||||
|
function proxyServer(option){
|
||||||
|
option = option || {};
|
||||||
|
|
||||||
|
var self = this,
|
||||||
|
proxyType = /https/i.test(option.type || DEFAULT_TYPE) ? T_TYPE_HTTPS : T_TYPE_HTTP ,
|
||||||
|
proxyPort = option.port || DEFAULT_PORT,
|
||||||
|
proxyHost = option.hostname || DEFAULT_HOST,
|
||||||
|
proxyRules = option.rule;
|
||||||
|
|
||||||
|
requestHandler.setRules(proxyRules);
|
||||||
self.httpProxyServer = null;
|
self.httpProxyServer = null;
|
||||||
self.close = function(){
|
|
||||||
self.httpProxyServer && self.httpProxyServer.close();
|
|
||||||
console.log(color.green("server closed :" + proxyHost + ":" + proxyPort));
|
|
||||||
}
|
|
||||||
|
|
||||||
startWebServer();
|
|
||||||
|
|
||||||
if(ruleFile){
|
|
||||||
if(fs.existsSync(ruleFile)){
|
|
||||||
try{ //for abs path
|
|
||||||
requestHandler.setRules(require(ruleFile));
|
|
||||||
}catch(e){ //for relative path
|
|
||||||
requestHandler.setRules(require("./" + ruleFile));
|
|
||||||
}
|
|
||||||
console.log(color.green("rule file loaded"));
|
|
||||||
}else{
|
|
||||||
console.log(color.red("can not find rule file"));
|
|
||||||
}
|
|
||||||
}else{
|
|
||||||
rules = require("./lib/rule_default.js");
|
|
||||||
requestHandler.setRules(rules);
|
|
||||||
}
|
|
||||||
|
|
||||||
async.series(
|
async.series(
|
||||||
[
|
[
|
||||||
@ -82,12 +70,18 @@ function proxyServer(type, port, hostname,ruleFile){
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
//listen CONNECT method for https over http
|
||||||
function(callback){
|
function(callback){
|
||||||
//listen CONNECT method for https over http
|
|
||||||
self.httpProxyServer.on('connect',requestHandler.connectReqHandler);
|
self.httpProxyServer.on('connect',requestHandler.connectReqHandler);
|
||||||
|
|
||||||
self.httpProxyServer.listen(proxyPort);
|
self.httpProxyServer.listen(proxyPort);
|
||||||
callback(null);
|
callback(null);
|
||||||
|
},
|
||||||
|
|
||||||
|
//start web interface
|
||||||
|
function(callback){
|
||||||
|
startWebServer();
|
||||||
|
callback(null);
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
|
||||||
@ -104,6 +98,10 @@ function proxyServer(type, port, hostname,ruleFile){
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
self.close = function(){
|
||||||
|
self.httpProxyServer && self.httpProxyServer.close();
|
||||||
|
console.log(color.green("server closed :" + proxyHost + ":" + proxyPort));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function startWebServer(port){
|
function startWebServer(port){
|
||||||
|
@ -27,8 +27,7 @@ module.exports = {
|
|||||||
return newProtocol;
|
return newProtocol;
|
||||||
},
|
},
|
||||||
|
|
||||||
//req is user's request sent to the proxy server
|
//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
|
||||||
//option is how the proxy server will send request to the real server. i.e. require("http").request(option,function(){...})
|
|
||||||
//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
|
||||||
replaceRequestOption : function(req,option){
|
replaceRequestOption : function(req,option){
|
||||||
|
Loading…
x
Reference in New Issue
Block a user