From ccb6b817de4908140ae9b27f7d75f9f0343da275 Mon Sep 17 00:00:00 2001 From: OttoMao Date: Tue, 4 Nov 2014 17:23:52 +0800 Subject: [PATCH] allow user to close the web interface --- README.md | 1 + bin.js | 3 +- package.json | 2 +- proxy.js | 94 ++++++++++++++++++++++++++++------------------------ 4 files changed, 55 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index 06f6cad..efc710c 100644 --- a/README.md +++ b/README.md @@ -242,6 +242,7 @@ var options = { socketPort : 8003, // optional, internal port for web socket, replace this when it is conflict with your own service webConfigPort : 8088, // optional, internal port for web config(beta), replace this when it is conflict with your own service throttle : 10, // optional, speed limit in kb/s + disableWebInterface : false //option, set it when you don't want to use the web interface }; new proxy.proxyServer(options); diff --git a/bin.js b/bin.js index 7131b89..8ce0d65 100644 --- a/bin.js +++ b/bin.js @@ -48,7 +48,8 @@ if(program.clear){ hostname : program.hostname, dbFile : program.file, throttle : program.throttle, - rule : ruleModule + rule : ruleModule, + disableWebInterface:false }); } diff --git a/package.json b/package.json index a152dcf..4540a3c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "anyproxy", - "version": "2.7.3", + "version": "2.7.4", "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 c20342b..8cdf2a3 100644 --- a/proxy.js +++ b/proxy.js @@ -65,18 +65,20 @@ if(fs.existsSync(process.cwd() + '/rule.js')){ //option.webConfigPort : 8088(default) //option.dbFile : null(default) //option.throttle : null(default) +//option.disableWebInterface 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 || 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 - + 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, + 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 + disableWebInterface = !!option.disableWebInterface ; + if(option.dbFile){ GLOBAL.recorder = new Recorder({filename: option.dbFile}); }else{ @@ -124,45 +126,51 @@ function proxyServer(option){ //start web interface function(callback){ - - //web interface - var args = [proxyWebPort, socketPort, proxyConfigPort, requestHandler.getRuleSummary(), ip.address()]; - var child_webServer = fork(path.join(__dirname,"./webServer.js"),args); - child_webServer.on("message",function(data){ - if(data.type == "reqBody" && data.id){ - child_webServer.send({ - type : "body", - id : data.id, - body : GLOBAL.recorder.getBody(data.id) - }); - } - }); - - //kill web server when father process exits - process.on("exit",function(){ - child_webServer.kill(); - }); - - GLOBAL.recorder.on("update",function(data){ - child_webServer.send({ - type: "update", - body: data + if(disableWebInterface){ + console.log('web interface is disabled'); + callback(null); + }else{ + + //web interface + var args = [proxyWebPort, socketPort, proxyConfigPort, requestHandler.getRuleSummary(), ip.address()]; + var child_webServer = fork(path.join(__dirname,"./webServer.js"),args); + child_webServer.on("message",function(data){ + if(data.type == "reqBody" && data.id){ + child_webServer.send({ + type : "body", + id : data.id, + body : GLOBAL.recorder.getBody(data.id) + }); + } }); - }); - var configServer = new UIConfigServer(proxyConfigPort); - configServer.on("rule_changed",function() { - // console.log(arguments); - }) + //TODO : uncaught exception + //kill web server when father process exits + process.on("exit",function(){ + child_webServer.kill(); + }); - var tipText,webUrl; - webUrl = "http://" + ip.address() + ":" + proxyWebPort +"/"; - tipText = "web interface started at : " + webUrl; - console.log(color.green(tipText)); + GLOBAL.recorder.on("update",function(data){ + child_webServer.send({ + type: "update", + body: data + }); + }); - tipText = "[alpha]qr code to for iOS client: " + webUrl + "qr"; - console.log(color.green(tipText)); - callback(null); + var configServer = new UIConfigServer(proxyConfigPort); + configServer.on("rule_changed",function() { + // console.log(arguments); + }); + + var tipText,webUrl; + webUrl = "http://" + ip.address() + ":" + proxyWebPort +"/"; + tipText = "web interface started at : " + webUrl; + console.log(color.green(tipText)); + + tipText = "[alpha]qr code to for iOS client: " + webUrl + "qr"; + console.log(color.green(tipText)); + callback(null); + } } ],