diff --git a/README.md b/README.md index de9c9d4..946f831 100644 --- a/README.md +++ b/README.md @@ -220,10 +220,13 @@ var proxy = require("anyproxy"); !proxy.isRootCAFileExists() && proxy.generateRootCA(); var options = { - type : "http", - port : "8001", - hostname : "localhost", - rule : require("path/to/my/ruleModule.js") + type : "http", + port : 8001, + hostname : "localhost", + 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); diff --git a/lib/rule_default.js b/lib/rule_default.js index 9db2bcf..f4fe3d3 100644 --- a/lib/rule_default.js +++ b/lib/rule_default.js @@ -41,6 +41,7 @@ module.exports = { }, replaceServerResData: function(req,res,serverResData){ + return serverResData; }, pauseBeforeSendingResponse : function(req,res){ diff --git a/lib/util.js b/lib/util.js index 813fafc..60eb3bb 100644 --- a/lib/util.js +++ b/lib/util.js @@ -22,3 +22,10 @@ module.exports.getUserHome = function(){ 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] : ''; + }); + +} diff --git a/package.json b/package.json index b80adeb..4243323 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "anyproxy", - "version": "2.4.2", + "version": "2.4.3", "description": "A fully configurable proxy in NodeJS, which can handle HTTPS requests perfectly.", "main": "proxy.js", "bin": { diff --git a/proxy.js b/proxy.js index 730a30c..2ba856a 100644 --- a/proxy.js +++ b/proxy.js @@ -54,8 +54,11 @@ if(fs.existsSync(process.cwd() + '/rule.js')){ //option //option.type : 'http'(default) or 'https' //option.port : 8001(default) -//option.rule : ruleModule //option.hostname : localhost(default) +//option.rule : ruleModule +//option.webPort : 8002(default) +//option.socketPort : 8003(default) +//option.webConfigPort : 8080(default) function proxyServer(option){ option = option || {}; @@ -63,7 +66,16 @@ function proxyServer(option){ 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 || 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 self.httpProxyServer = null; @@ -101,14 +113,13 @@ function proxyServer(option){ //start web interface function(callback){ - var webServer = new proxyWebServer(); + var webServer = new proxyWebServer(portList); var wss = webServer.wss; - var configServer = new UIConfigServer(DEFAULT_CONFIG_PORT); + var configServer = new UIConfigServer(proxyConfigPort); configServer.on("rule_changed",function() { console.log(arguments); }) - // var wss = proxyWebServer(); callback(null); } @@ -133,7 +144,7 @@ function proxyServer(option){ } } -// doing +// BETA : UIConfigServer function UIConfigServer(port){ var self = this; @@ -146,6 +157,7 @@ function UIConfigServer(port){ }, userKey; + port = port || DEFAULT_CONFIG_PORT; customerRule.shouldUseLocalResponse = function(req,reqBody){ var url = req.url; @@ -213,10 +225,13 @@ function UIConfigServer(port){ inherits(UIConfigServer, events.EventEmitter); -function proxyWebServer(port){ +function proxyWebServer(portList){ 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 var app = express(); @@ -251,7 +266,11 @@ function proxyWebServer(port){ if(req.url == "/"){ 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{ next(); } @@ -264,7 +283,7 @@ function proxyWebServer(port){ var tipText = "web interface started at port " + port; //web socket interface - var wss = new WebSocketServer({port: DEFAULT_WEBSOCKET_PORT}); + var wss = new WebSocketServer({port: webSocketPort}); wss.on("connection",function(ws){}); wss.broadcast = function(data) { for(var i in this.clients){ diff --git a/web/index.html b/web/index.html index fe31283..49449d9 100644 --- a/web/index.html +++ b/web/index.html @@ -14,7 +14,7 @@ Clear Logs(Ctrl+X) Stop Resume - Config Local Response(beta) + Config Local Response(beta)

rule : {{rule}}

@@ -44,6 +44,8 @@
+ +