Skip to content

Commit

Permalink
refactor: refine boundHost
Browse files Browse the repository at this point in the history
  • Loading branch information
avwo committed Dec 21, 2020
1 parent 55b3309 commit 8cbaf85
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 38 deletions.
10 changes: 5 additions & 5 deletions bin/whistle.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ program
.option('-C, --copy [storageDir]', 'copy the configuration of the specified directory to a new directory', String, undefined)
.option('-c, --dnsCache [time]', 'set the cache time of DNS (60000ms by default)', String, undefined)
.option('-H, --host [boundHost]', 'set the bound host (INADDR_ANY by default)', String, undefined)
.option('-p, --port [proxyPort]', 'set the proxy port (' + config.port + ' by default)', parseInt, undefined)
.option('-P, --uiport [uiport]', 'set the webui port', parseInt, undefined)
.option('-p, --port [proxyPort]', 'set the proxy port (' + config.port + ' by default)', String, undefined)
.option('-P, --uiport [uiport]', 'set the webui port', String, undefined)
.option('-m, --middlewares [script path or module name]', 'set the express middlewares loaded at startup (as: xx,yy/zz.js)', String, undefined)
.option('-M, --mode [mode]', 'set the starting mode (as: pureProxy|debug|multiEnv|capture|disableH2|network|rules|plugins|prod)', String, undefined)
.option('-t, --timeout [ms]', 'set the request timeout (' + config.timeout + 'ms by default)', parseInt, undefined)
Expand All @@ -108,9 +108,9 @@ program
.option('-R, --reqCacheSize [reqCacheSize]', 'set the cache size of request data (600 by default)', String, undefined)
.option('-F, --frameCacheSize [frameCacheSize]', 'set the cache size of webSocket and socket\'s frames (512 by default)', String, undefined)
.option('-A, --addon [pluginPaths]', 'add custom plugin paths', String, undefined)
.option('--socksPort [socksPort]', 'set the socksv5 server port', parseInt, undefined)
.option('--httpPort [httpPort]', 'set the http server port', parseInt, undefined)
.option('--httpsPort [httpsPort]', 'set the https server port', parseInt, undefined)
.option('--socksPort [socksPort]', 'set the socksv5 server port', String, undefined)
.option('--httpPort [httpPort]', 'set the http server port', String, undefined)
.option('--httpsPort [httpsPort]', 'set the https server port', String, undefined)
.option('--no-global-plugins', 'do not load any globally installed plugins')
.option('--no-prev-options', 'do not reuse the previous options when restarting')
.option('--inspect [[host:]port]', 'activate inspector on host:port (127.0.0.1:9229 by default)')
Expand Down
15 changes: 8 additions & 7 deletions biz/init.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
var http = require('http');
var ui = require('./webui/lib');

var LOCALHOST = '127.0.0.1';
var util = require('../lib/util');

module.exports = function init(proxy, callback) {
var config = proxy.config;
ui.init(proxy);
if (config.customUIPort) {
var server = http.createServer();
ui.setupServer(server);
if (config.host === LOCALHOST) {
server.listen(config.uiport, LOCALHOST, callback);
} else {
server.listen(config.uiport, callback);
}
util.getBoundIp(config.uihost, function(host) {
if (host) {
server.listen(config.uiport, host, callback);
} else {
server.listen(config.uiport, callback);
}
});
} else {
callback();
}
Expand Down
51 changes: 38 additions & 13 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,21 @@ function createHash(str) {
return shasum.digest('hex');
}

function getHostPort(str) {
if (!/^(?:([\w.-]+):)?(\d+)$/.test(str)) {
return;
}
var host = RegExp.$1;
var port = parseInt(RegExp.$2, 10);
if (!port) {
return;
}
return {
host: host,
port: port
};
}

function isPort(port) {
return /^\d+$/.test(port) && port > 0;
}
Expand Down Expand Up @@ -577,28 +592,38 @@ exports.extend = function(newConf) {
if (typeof customHandler === 'function') {
config.customHandler = customHandler;
}
if (isPort(newConf.port)) {
config.uiport = config.port = newConf.port;
}
if (isPort(newConf.realPort) && config.realPort != config.port) {
config.realPort = newConf.realPort;
}
if (isPort(newConf.uiport)) {
config.customUIPort = newConf.uiport != config.port;
config.uiport = newConf.uiport;
var port = getHostPort(newConf.port);
if (port) {
config.host = port.host;
config.uiport = config.port = port.port;
}
var uiPort = getHostPort(newConf.uiport);
if (uiPort) {
config.customUIPort = uiPort.port != config.port;
config.uiport = uiPort.port;
config.uihost = uiPort.host;
}
if (isPort(newConf.socksPort)) {
config.socksPort = newConf.socksPort;
var socksPort = getHostPort(newConf.socksPort);
if (socksPort) {
config.socksPort = socksPort.port;
config.socksHost = socksPort.host;
}
if (isPort(newConf.httpPort)) {
config.httpPort = newConf.httpPort;
var httpPort = getHostPort(newConf.httpPort);
if (httpPort) {
config.httpPort = httpPort.port;
config.httpHost = httpPort.host;
}
if (isPort(newConf.httpsPort)) {
config.httpsPort = newConf.httpsPort;
var httpsPort = getHostPort(newConf.httpsPort);
if (httpsPort) {
config.httpsPort = httpsPort.port;
config.httpsHost = httpsPort.host;
}
config.addon = getPaths(newConf.addon);
if (newConf.host && typeof newConf.host === 'string') {
config.host = newConf.host;
config.defaultHost = newConf.host;
}

if (typeof newConf.authKey === 'string') {
Expand Down
6 changes: 3 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function proxy(callback) {
callback.call(server, proxyEvents);
}
};
util.getBoundIp(function(host) {
util.getBoundIp(config.proxyHost, function(host) {
util.checkPort(!config.INADDR_ANY && !host && config.port, function() {
server.listen(config.port, host, execCallback);
});
Expand All @@ -102,7 +102,7 @@ function proxy(callback) {
tunnelProxy(optionServer, proxyEvents);
upgradeProxy(optionServer);
optionServer.on('clientError', handleClientError);
util.getBoundIp(function(host) {
util.getBoundIp(config[opts ? 'httpsHost' : 'httpHost'], function(host) {
util.checkPort(!config.INADDR_ANY && !host && port, function() {
optionServer.listen(port, host, execCallback);
});
Expand Down Expand Up @@ -155,7 +155,7 @@ function proxy(callback) {
client.end();
});
proxyEvents.socksServer = socksServer;
util.getBoundIp(function(host) {
util.getBoundIp(config.socksHost, function(host) {
boundHost = host || '127.0.0.1';
util.checkPort(!config.INADDR_ANY && !host && config.socksPort, function() {
socksServer.listen(config.socksPort, host, execCallback);
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ function loadPlugin(plugin, callback) {
if (ports) {
return callback(null, ports);
}
util.getBoundIp(function(host) {
util.getBoundIp(config.proxyHost, function(host) {
conf.host = host || LOCALHOST;
p.fork({
data: config.getPluginData(plugin.moduleName),
Expand Down
25 changes: 16 additions & 9 deletions lib/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2776,31 +2776,38 @@ exports.checkPluginReqOnce = function(req, raw) {
};

exports.checkPort = function(port, host, cb) {
if (!port) {
return cb();
}
if (typeof host !== 'string') {
cb = host;
host = '127.0.0.1';
}
if (!port) {
return cb();
}
var server = http.createServer();
server.listen(port, host, function() {
server.close(cb);
});
};

var boundIpDefer;
exports.getBoundIp = function(cb) {
var boundIpDeferMap = {};
exports.getBoundIp = function(host, cb) {
if (typeof host === 'function') {
cb = host;
host = null;
}
host = host || config.defaultHost;
if (!host || net.isIP(host)) {
return cb(host);
}
var boundIpDefer = boundIpDeferMap[host];
if (boundIpDefer) {
return boundIpDefer.done(cb);
}
var defer = Q.defer();
boundIpDefer = defer.promise;
boundIpDeferMap[host] = boundIpDefer;
boundIpDefer.done(cb);
if (!config.host || net.isIP(config.host)) {
return defer.resolve(config.host);
}
dns.lookup(config.host, function(err, ip) {
dns.lookup(host, function(err, ip) {
if (err) {
throw err;
}
Expand Down

0 comments on commit 8cbaf85

Please sign in to comment.