mirror of
https://github.com/alibaba/anyproxy.git
synced 2025-05-19 05:28:26 +00:00
add support for all ports
This commit is contained in:
parent
e6da97a098
commit
f5448677ac
11
README.md
11
README.md
@ -220,10 +220,13 @@ var proxy = require("anyproxy");
|
|||||||
!proxy.isRootCAFileExists() && proxy.generateRootCA();
|
!proxy.isRootCAFileExists() && proxy.generateRootCA();
|
||||||
|
|
||||||
var options = {
|
var options = {
|
||||||
type : "http",
|
type : "http",
|
||||||
port : "8001",
|
port : 8001,
|
||||||
hostname : "localhost",
|
hostname : "localhost",
|
||||||
rule : require("path/to/my/ruleModule.js")
|
rule : require("path/to/my/ruleModule.js"),
|
||||||
|
webPort : 8002, // port for web interface
|
||||||
|
socketPort : 8003, // internal port for web socket, replace this when it is conflict with your own service
|
||||||
|
webConfigPort : 8080 // internal port for web config(beta), replace this when it is conflict with your own service
|
||||||
};
|
};
|
||||||
new proxy.proxyServer(options);
|
new proxy.proxyServer(options);
|
||||||
|
|
||||||
|
@ -41,6 +41,7 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
replaceServerResData: function(req,res,serverResData){
|
replaceServerResData: function(req,res,serverResData){
|
||||||
|
return serverResData;
|
||||||
},
|
},
|
||||||
|
|
||||||
pauseBeforeSendingResponse : function(req,res){
|
pauseBeforeSendingResponse : function(req,res){
|
||||||
|
@ -22,3 +22,10 @@ module.exports.getUserHome = function(){
|
|||||||
return process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
|
return process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module.exports.simpleRender = function(str, object, regexp){
|
||||||
|
return String(str).replace(regexp || (/\{\{([^{}]+)\}\}/g), function(match, name){
|
||||||
|
if (match.charAt(0) == '\\') return match.slice(1);
|
||||||
|
return (object[name] != null) ? object[name] : '';
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "anyproxy",
|
"name": "anyproxy",
|
||||||
"version": "2.4.2",
|
"version": "2.4.3",
|
||||||
"description": "A fully configurable proxy in NodeJS, which can handle HTTPS requests perfectly.",
|
"description": "A fully configurable proxy in NodeJS, which can handle HTTPS requests perfectly.",
|
||||||
"main": "proxy.js",
|
"main": "proxy.js",
|
||||||
"bin": {
|
"bin": {
|
||||||
|
39
proxy.js
39
proxy.js
@ -54,8 +54,11 @@ if(fs.existsSync(process.cwd() + '/rule.js')){
|
|||||||
//option
|
//option
|
||||||
//option.type : 'http'(default) or 'https'
|
//option.type : 'http'(default) or 'https'
|
||||||
//option.port : 8001(default)
|
//option.port : 8001(default)
|
||||||
//option.rule : ruleModule
|
|
||||||
//option.hostname : localhost(default)
|
//option.hostname : localhost(default)
|
||||||
|
//option.rule : ruleModule
|
||||||
|
//option.webPort : 8002(default)
|
||||||
|
//option.socketPort : 8003(default)
|
||||||
|
//option.webConfigPort : 8080(default)
|
||||||
function proxyServer(option){
|
function proxyServer(option){
|
||||||
option = option || {};
|
option = option || {};
|
||||||
|
|
||||||
@ -63,7 +66,16 @@ 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 || default_rule;
|
proxyRules = option.rule || default_rule,
|
||||||
|
proxyWebPort = option.webPort || DEFAULT_WEB_PORT, //port for web interface
|
||||||
|
socketPort = option.socketPort || DEFAULT_WEBSOCKET_PORT, //port for websocket
|
||||||
|
proxyConfigPort = option.webConfigPort || DEFAULT_CONFIG_PORT; //port to ui config server
|
||||||
|
|
||||||
|
var portList = {
|
||||||
|
proxyWebPort : proxyWebPort,
|
||||||
|
socketPort : socketPort,
|
||||||
|
proxyConfigPort : proxyConfigPort
|
||||||
|
};
|
||||||
|
|
||||||
requestHandler.setRules(proxyRules); //TODO : optimize calling for set rule
|
requestHandler.setRules(proxyRules); //TODO : optimize calling for set rule
|
||||||
self.httpProxyServer = null;
|
self.httpProxyServer = null;
|
||||||
@ -101,14 +113,13 @@ function proxyServer(option){
|
|||||||
|
|
||||||
//start web interface
|
//start web interface
|
||||||
function(callback){
|
function(callback){
|
||||||
var webServer = new proxyWebServer();
|
var webServer = new proxyWebServer(portList);
|
||||||
var wss = webServer.wss;
|
var wss = webServer.wss;
|
||||||
|
|
||||||
var configServer = new UIConfigServer(DEFAULT_CONFIG_PORT);
|
var configServer = new UIConfigServer(proxyConfigPort);
|
||||||
configServer.on("rule_changed",function() {
|
configServer.on("rule_changed",function() {
|
||||||
console.log(arguments);
|
console.log(arguments);
|
||||||
})
|
})
|
||||||
// var wss = proxyWebServer();
|
|
||||||
|
|
||||||
callback(null);
|
callback(null);
|
||||||
}
|
}
|
||||||
@ -133,7 +144,7 @@ function proxyServer(option){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// doing
|
// BETA : UIConfigServer
|
||||||
function UIConfigServer(port){
|
function UIConfigServer(port){
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
@ -146,6 +157,7 @@ function UIConfigServer(port){
|
|||||||
},
|
},
|
||||||
userKey;
|
userKey;
|
||||||
|
|
||||||
|
port = port || DEFAULT_CONFIG_PORT;
|
||||||
|
|
||||||
customerRule.shouldUseLocalResponse = function(req,reqBody){
|
customerRule.shouldUseLocalResponse = function(req,reqBody){
|
||||||
var url = req.url;
|
var url = req.url;
|
||||||
@ -213,10 +225,13 @@ function UIConfigServer(port){
|
|||||||
inherits(UIConfigServer, events.EventEmitter);
|
inherits(UIConfigServer, events.EventEmitter);
|
||||||
|
|
||||||
|
|
||||||
function proxyWebServer(port){
|
function proxyWebServer(portList){
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
port = port || DEFAULT_WEB_PORT;
|
portList = portList || {};
|
||||||
|
port = portList.proxyWebPort || DEFAULT_WEB_PORT;
|
||||||
|
webSocketPort = portList.socketPort || DEFAULT_WEBSOCKET_PORT;
|
||||||
|
proxyConfigPort = portList.proxyConfigPort;
|
||||||
|
|
||||||
//web interface
|
//web interface
|
||||||
var app = express();
|
var app = express();
|
||||||
@ -251,7 +266,11 @@ function proxyWebServer(port){
|
|||||||
|
|
||||||
if(req.url == "/"){
|
if(req.url == "/"){
|
||||||
res.setHeader("Content-Type", "text/html");
|
res.setHeader("Content-Type", "text/html");
|
||||||
res.end(indexHTML.replace("{{rule}}",requestHandler.getRuleSummary()) );
|
res.end(util.simpleRender(indexHTML, {
|
||||||
|
rule : requestHandler.getRuleSummary(),
|
||||||
|
webSocketPort : webSocketPort,
|
||||||
|
proxyConfigPort : proxyConfigPort
|
||||||
|
}));
|
||||||
}else{
|
}else{
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
@ -264,7 +283,7 @@ function proxyWebServer(port){
|
|||||||
var tipText = "web interface started at port " + port;
|
var tipText = "web interface started at port " + port;
|
||||||
|
|
||||||
//web socket interface
|
//web socket interface
|
||||||
var wss = new WebSocketServer({port: DEFAULT_WEBSOCKET_PORT});
|
var wss = new WebSocketServer({port: webSocketPort});
|
||||||
wss.on("connection",function(ws){});
|
wss.on("connection",function(ws){});
|
||||||
wss.broadcast = function(data) {
|
wss.broadcast = function(data) {
|
||||||
for(var i in this.clients){
|
for(var i in this.clients){
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
<a href="#" class="J_clearBtn"><span class="topBtn">Clear Logs(Ctrl+X)</span></a>
|
<a href="#" class="J_clearBtn"><span class="topBtn">Clear Logs(Ctrl+X)</span></a>
|
||||||
<a href="#" class="J_statusBtn"><span class="topBtn">Stop</span></a>
|
<a href="#" class="J_statusBtn"><span class="topBtn">Stop</span></a>
|
||||||
<a href="#" class="J_statusBtn btn_disable"><span class="topBtn">Resume</span></a>
|
<a href="#" class="J_statusBtn btn_disable"><span class="topBtn">Resume</span></a>
|
||||||
<a href="http://localhost:8080"><span class="topBtn">Config Local Response(beta)</span></a>
|
<a href="http://localhost:{{proxyConfigPort}}"><span class="topBtn">Config Local Response(beta)</span></a>
|
||||||
</div>
|
</div>
|
||||||
<div class="ruleDesc">
|
<div class="ruleDesc">
|
||||||
<h4>rule : <strong>{{rule}}</strong></h4>
|
<h4>rule : <strong>{{rule}}</strong></h4>
|
||||||
@ -44,6 +44,8 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<input type="hidden" id="socketPort" value="{{webSocketPort}}" />
|
||||||
|
|
||||||
<script type="text/template" id="main_table_row">
|
<script type="text/template" id="main_table_row">
|
||||||
<td class="data_id"><%= _id %></td>
|
<td class="data_id"><%= _id %></td>
|
||||||
<td><%= method %></td>
|
<td><%= method %></td>
|
||||||
|
@ -176,7 +176,8 @@ seajs.use(['$','Underscore' ,'Backbone'], function($, _, Backbone) {
|
|||||||
alert("WebSocket is required. Please use a modern browser.");
|
alert("WebSocket is required. Please use a modern browser.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var dataSocket = new WebSocket("ws://127.0.0.1:8003");
|
var socketPort = $("#socketPort").val() || "8003",
|
||||||
|
dataSocket = new WebSocket("ws://127.0.0.1:" + socketPort);
|
||||||
dataSocket.onopen = function(){}
|
dataSocket.onopen = function(){}
|
||||||
|
|
||||||
dataSocket.onmessage = function(event){
|
dataSocket.onmessage = function(event){
|
||||||
|
Loading…
x
Reference in New Issue
Block a user