enable global proxy depends on options.type

This commit is contained in:
沈辰 2016-04-05 20:04:11 +08:00
parent bdca110890
commit c3e67ebcb1
2 changed files with 31 additions and 27 deletions

View File

@ -20,7 +20,7 @@ function execSync(cmd) {
/** /**
* proxy for Centos * proxy for CentOs
* ------------------------------------------------------------------------ * ------------------------------------------------------------------------
* *
* file: ~/.bash_profile * file: ~/.bash_profile
@ -69,46 +69,50 @@ macProxyManager.getNetworkType = function() {
throw new Error('Unknown network type'); throw new Error('Unknown network type');
}; };
macProxyManager.enableGlobalProxy = function(ip, port) { macProxyManager.enableGlobalProxy = function(ip, port, proxyType) {
if (!ip && !port) { if (!ip || !port) {
console.log('proxy server\'s ip and port is required'); console.log('proxy server\'s ip and port are required');
return; return;
}; };
proxyType = proxyType || 'http';
var networkType = macProxyManager.networkType || macProxyManager.getNetworkType(); var networkType = macProxyManager.networkType || macProxyManager.getNetworkType();
var result = execSync( return /^http$/i.test(proxyType) ?
// set http proxy // set http proxy
'sudo networksetup -setwebproxy ${networkType} ${ip} ${port}; ' execSync(
'networksetup -setwebproxy ${networkType} ${ip} ${port}'
.replace("${networkType}", networkType) .replace("${networkType}", networkType)
.replace("${ip}", ip) .replace("${ip}", ip)
.replace("${port}", port) + .replace("${port}", port)) :
// set https proxy // set https proxy
'sudo networksetup -setsecurewebproxy ${networkType} ${ip} ${port}' execSync('networksetup -setsecurewebproxy ${networkType} ${ip} ${port}'
.replace("${networkType}", networkType) .replace("${networkType}", networkType)
.replace("${ip}", ip) .replace("${ip}", ip)
.replace("${port}", port)); .replace("${port}", port));
return result;
}; };
macProxyManager.disableGlobalProxy = function() { macProxyManager.disableGlobalProxy = function(proxyType) {
proxyType = proxyType || 'http';
var networkType = macProxyManager.networkType || macProxyManager.getNetworkType(); var networkType = macProxyManager.networkType || macProxyManager.getNetworkType();
var result = execSync( return /^http$/i.test(proxyType) ?
// disable http proxy // set http proxy
'sudo networksetup -setwebproxystate ${networkType} off; ' execSync(
.replace(/\$\{networkType\}/g, networkType) + 'networksetup -setwebproxystate ${networkType} off'
.replace("${networkType}", networkType)) :
// disable https proxy // set https proxy
'sudo networksetup -setsecurewebproxystate ${networkType} off' execSync(
.replace(/\$\{networkType\}/g, networkType)); 'networksetup -setsecurewebproxystate ${networkType} off'
.replace("${networkType}", networkType));
return result;
}; };
macProxyManager.getProxyState = function() { macProxyManager.getProxyState = function() {
@ -133,7 +137,7 @@ var winProxyManager = {};
winProxyManager.enableGlobalProxy = function(ip, port) { winProxyManager.enableGlobalProxy = function(ip, port) {
if (!ip && !port) { if (!ip && !port) {
console.log('proxy server\'s ip and port is required'); console.log('proxy server\'s ip and port are required');
return; return;
}; };

View File

@ -162,7 +162,7 @@ function proxyServer(option){
function(callback) { function(callback) {
if (option.globalProxy) { if (option.globalProxy) {
var result = require('./lib/proxyManager').enableGlobalProxy(ip.address(), proxyPort); var result = require('./lib/proxyManager').enableGlobalProxy(ip.address(), proxyPort, proxyType == T_TYPE_HTTP ? "Http" : "Https");
if (result.status) { if (result.status) {
callback(result.stdout); callback(result.stdout);
@ -184,7 +184,7 @@ function proxyServer(option){
logUtil.printLog('AnyProxy is about to exit with code: ' + code, logUtil.T_ERR); logUtil.printLog('AnyProxy is about to exit with code: ' + code, logUtil.T_ERR);
if (option.globalProxy) { if (option.globalProxy) {
var result = require('./lib/proxyManager').disableGlobalProxy(); var result = require('./lib/proxyManager').disableGlobalProxy(proxyType == T_TYPE_HTTP ? "Http" : "Https");
//error occur //error occur
if (result.status) { if (result.status) {