anyproxy/lib/systemProxyMgr.js

167 lines
4.5 KiB
JavaScript
Raw Normal View History

2016-04-03 02:04:27 +08:00
var child_process = require('child_process');
var networkTypes = ['Ethernet', 'Thunderbolt Ethernet', 'Wi-Fi'];
function execSync(cmd) {
2016-04-04 22:52:31 +08:00
var stdout, status = 0;
try {
stdout = child_process.execSync(cmd);
} catch (err) {
stdout = err.stdout;
status = err.status;
}
return {
stdout: stdout.toString(),
status: status
};
2016-04-03 02:04:27 +08:00
}
/**
* proxy for CentOs
2016-04-03 02:04:27 +08:00
* ------------------------------------------------------------------------
2016-04-04 22:42:20 +08:00
*
* file: ~/.bash_profile
*
* http_proxy=http://proxy_server_address:port
* export no_proxy=localhost,127.0.0.1,192.168.0.34
* export http_proxy
2016-04-03 02:04:27 +08:00
* ------------------------------------------------------------------------
*/
2016-04-04 22:42:20 +08:00
/**
* proxy for Ubuntu
* ------------------------------------------------------------------------
*
* file: /etc/environment
* more info: http://askubuntu.com/questions/150210/how-do-i-set-systemwide-proxy-servers-in-xubuntu-lubuntu-or-ubuntu-studio
*
* http_proxy=http://proxy_server_address:port
* export no_proxy=localhost,127.0.0.1,192.168.0.34
* export http_proxy
* ------------------------------------------------------------------------
*/
/**
* ------------------------------------------------------------------------
* mac proxy manager
* ------------------------------------------------------------------------
*/
var macProxyManager = {};
2016-04-03 02:04:27 +08:00
2016-04-04 22:42:20 +08:00
macProxyManager.getNetworkType = function() {
2016-04-03 02:04:27 +08:00
2016-04-04 22:52:31 +08:00
for (var i = 0; i < networkTypes.length; i++) {
2016-04-03 02:04:27 +08:00
2016-04-04 22:52:31 +08:00
var
type = networkTypes[i],
result = execSync('networksetup -getwebproxy ' + type);
2016-04-03 02:04:27 +08:00
2016-04-04 22:52:31 +08:00
if (result.status === 0) {
macProxyManager.networkType = type;
return type;
}
}
2016-04-03 02:04:27 +08:00
2016-04-04 22:52:31 +08:00
throw new Error('Unknown network type');
2016-04-03 02:04:27 +08:00
};
macProxyManager.enableGlobalProxy = function(ip, port, proxyType) {
2016-04-03 02:04:27 +08:00
if (!ip || !port) {
2016-04-05 22:09:07 +08:00
console.log('failed to set global proxy server.\n ip and port are required.');
2016-04-04 22:52:31 +08:00
return;
};
2016-04-03 02:04:27 +08:00
proxyType = proxyType || 'http';
2016-04-04 22:52:31 +08:00
var networkType = macProxyManager.networkType || macProxyManager.getNetworkType();
2016-04-03 02:04:27 +08:00
return /^http$/i.test(proxyType) ?
2016-04-03 02:04:27 +08:00
2016-04-04 22:52:31 +08:00
// set http proxy
execSync(
'networksetup -setwebproxy ${networkType} ${ip} ${port}'
.replace("${networkType}", networkType)
.replace("${ip}", ip)
.replace("${port}", port)) :
2016-04-03 02:04:27 +08:00
2016-04-04 22:52:31 +08:00
// set https proxy
execSync('networksetup -setsecurewebproxy ${networkType} ${ip} ${port}'
.replace("${networkType}", networkType)
.replace("${ip}", ip)
.replace("${port}", port));
2016-04-03 02:04:27 +08:00
};
macProxyManager.disableGlobalProxy = function(proxyType) {
proxyType = proxyType || 'http';
2016-04-03 02:04:27 +08:00
var networkType = macProxyManager.networkType || macProxyManager.getNetworkType();
2016-04-03 02:04:27 +08:00
return /^http$/i.test(proxyType) ?
2016-04-03 02:04:27 +08:00
// set http proxy
execSync(
'networksetup -setwebproxystate ${networkType} off'
.replace("${networkType}", networkType)) :
2016-04-03 02:04:27 +08:00
// set https proxy
execSync(
'networksetup -setsecurewebproxystate ${networkType} off'
.replace("${networkType}", networkType));
2016-04-03 02:04:27 +08:00
};
2016-04-04 22:42:20 +08:00
macProxyManager.getProxyState = function() {
2016-04-04 22:52:31 +08:00
var networkType = macProxyManager.networkType || macProxyManager.getNetworkType();
var result = execSync('networksetup -getwebproxy ${networkType}'.replace('${networkType}', networkType));
2016-04-03 02:04:27 +08:00
2016-04-04 22:52:31 +08:00
return result;
2016-04-03 02:04:27 +08:00
};
/**
* ------------------------------------------------------------------------
* windows proxy manager
2016-04-04 22:52:31 +08:00
*
* netsh does not alter the settings for IE
2016-04-03 02:04:27 +08:00
* ------------------------------------------------------------------------
*/
2016-04-04 22:42:20 +08:00
var winProxyManager = {};
2016-04-03 02:04:27 +08:00
2016-04-04 22:42:20 +08:00
winProxyManager.enableGlobalProxy = function(ip, port) {
2016-04-03 02:04:27 +08:00
2016-04-04 22:52:31 +08:00
if (!ip && !port) {
2016-04-05 22:09:07 +08:00
console.log('failed to set global proxy server.\n ip and port are required.');
2016-04-04 22:52:31 +08:00
return;
};
2016-04-03 02:04:27 +08:00
2016-04-04 22:52:31 +08:00
return execSync(
// set proxy
'reg add "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings" /v ProxyServer /t REG_SZ /d ${ip}:${port} /f & '
.replace("${ip}", ip)
.replace("${port}", port) +
2016-04-03 02:04:27 +08:00
2016-04-04 22:52:31 +08:00
// enable proxy
'reg add "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings" /v ProxyEnable /t REG_DWORD /d 1 /f');
2016-04-03 02:04:27 +08:00
};
2016-04-04 22:42:20 +08:00
winProxyManager.disableGlobalProxy = function() {
2016-04-04 22:52:31 +08:00
return execSync('reg add "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings" /v ProxyEnable /t REG_DWORD /d 0 /f');
2016-04-03 02:04:27 +08:00
};
2016-04-04 22:42:20 +08:00
winProxyManager.getProxyState = function() {
2016-04-04 22:52:31 +08:00
return '';
2016-04-03 02:04:27 +08:00
};
2016-04-04 22:42:20 +08:00
winProxyManager.getNetworkType = function() {
2016-04-04 22:52:31 +08:00
return '';
2016-04-03 02:04:27 +08:00
};
2016-04-04 22:42:20 +08:00
module.exports = /^win/.test(process.platform) ? winProxyManager : macProxyManager;