allow user to close the web interface

This commit is contained in:
OttoMao 2014-11-04 17:23:52 +08:00
parent 93aa79b02a
commit ccb6b817de
4 changed files with 55 additions and 45 deletions

View File

@ -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);

3
bin.js
View File

@ -48,7 +48,8 @@ if(program.clear){
hostname : program.hostname,
dbFile : program.file,
throttle : program.throttle,
rule : ruleModule
rule : ruleModule,
disableWebInterface:false
});
}

View File

@ -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": {

View File

@ -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);
}
}
],