mirror of
https://github.com/alibaba/anyproxy.git
synced 2025-04-24 07:01:25 +00:00
add global proxy
This commit is contained in:
parent
225edd2317
commit
5b52859900
2
bin.js
Normal file → Executable file
2
bin.js
Normal file → Executable file
@ -22,6 +22,7 @@ program
|
||||
.option('-i, --intercept', 'intercept(decrypt) https requests when root CA exists')
|
||||
.option('-s, --silent', 'do not print anything into terminal')
|
||||
.option('-c, --clear', 'clear all the tmp certificates')
|
||||
.option('-o, --global', 'set as global proxy')
|
||||
.option('install', '[alpha] install node modules')
|
||||
.parse(process.argv);
|
||||
|
||||
@ -91,6 +92,7 @@ if(program.clear){
|
||||
webPort : program.web,
|
||||
rule : ruleModule,
|
||||
disableWebInterface : false,
|
||||
globalProxy : program.global,
|
||||
interceptHttps : program.intercept,
|
||||
silent : program.silent
|
||||
});
|
||||
|
137
lib/proxyManager.js
Normal file
137
lib/proxyManager.js
Normal file
@ -0,0 +1,137 @@
|
||||
var child_process = require('child_process');
|
||||
|
||||
var networkTypes = ['Ethernet', 'Thunderbolt Ethernet', 'Wi-Fi'];
|
||||
|
||||
function execSync(cmd) {
|
||||
var stdout, status = 0;
|
||||
|
||||
try {
|
||||
stdout = child_process.execSync(cmd);
|
||||
} catch (err) {
|
||||
stdout = err.stdout;
|
||||
status = err.status;
|
||||
}
|
||||
|
||||
return {
|
||||
stdout: stdout.toString(),
|
||||
status: status
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* ------------------------------------------------------------------------
|
||||
* linux like system proxy manager
|
||||
* ------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
var unixLikeSysProxyManager = {};
|
||||
|
||||
unixLikeSysProxyManager.getNetworkType = function() {
|
||||
|
||||
for (var i = 0; i < networkTypes.length; i++) {
|
||||
|
||||
var
|
||||
type = networkTypes[i],
|
||||
result = execSync('networksetup -getwebproxy ' + type);
|
||||
|
||||
if (result.status === 0) {
|
||||
unixLikeSysProxyManager.networkType = type;
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error('Unknown network type');
|
||||
};
|
||||
|
||||
unixLikeSysProxyManager.enableGlobalProxy = function(ip, port) {
|
||||
|
||||
if (!ip && !port) {
|
||||
console.log('proxy server\'s ip and port is required');
|
||||
return;
|
||||
};
|
||||
|
||||
var networkType = unixLikeSysProxyManager.networkType || unixLikeSysProxyManager.getNetworkType();
|
||||
|
||||
var result = execSync(
|
||||
|
||||
// set http proxy
|
||||
'sudo networksetup -setwebproxy ${networkType} ${ip} ${port}; '
|
||||
.replace("${networkType}", networkType)
|
||||
.replace("${ip}", ip)
|
||||
.replace("${port}", port) +
|
||||
|
||||
// set https proxy
|
||||
'sudo networksetup -setsecurewebproxy ${networkType} ${ip} ${port}'
|
||||
.replace("${networkType}", networkType)
|
||||
.replace("${ip}", ip)
|
||||
.replace("${port}", port));
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
unixLikeSysProxyManager.disableGlobalProxy = function() {
|
||||
var networkType = unixLikeSysProxyManager.networkType || unixLikeSysProxyManager.getNetworkType();
|
||||
|
||||
var result = execSync(
|
||||
|
||||
// disable http proxy
|
||||
'networksetup -setwebproxystate ${networkType} off; '
|
||||
.replace(/\$\{networkType\}/g, networkType) +
|
||||
|
||||
// disable https proxy
|
||||
'networksetup -setsecurewebproxystate ${networkType} off'
|
||||
.replace(/\$\{networkType\}/g, networkType));
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
unixLikeSysProxyManager.getProxyState = function() {
|
||||
var networkType = unixLikeSysProxyManager.networkType || unixLikeSysProxyManager.getNetworkType();
|
||||
var result = execSync('networksetup -getwebproxy ${networkType}'.replace('${networkType}', networkType));
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* ------------------------------------------------------------------------
|
||||
* windows proxy manager
|
||||
* ------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
var winSysProxyManager = {};
|
||||
|
||||
winSysProxyManager.enableGlobalProxy = function(ip, port) {
|
||||
|
||||
if (!ip && !port) {
|
||||
console.log('proxy server\'s ip and port is required');
|
||||
return;
|
||||
};
|
||||
|
||||
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) +
|
||||
|
||||
// enable proxy
|
||||
'reg add "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings" /v ProxyEnable /t REG_DWORD /d 1 /f');
|
||||
|
||||
};
|
||||
|
||||
winSysProxyManager.disableGlobalProxy = function() {
|
||||
return execSync('reg add "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings" /v ProxyEnable /t REG_DWORD /d 0 /f');
|
||||
};
|
||||
|
||||
winSysProxyManager.getProxyState = function() {
|
||||
return '';
|
||||
};
|
||||
|
||||
winSysProxyManager.getNetworkType = function() {
|
||||
return '';
|
||||
};
|
||||
|
||||
module.exports = /^win/.test(process.platform) ? winSysProxyManager : unixLikeSysProxyManager;
|
19
proxy.js
19
proxy.js
@ -158,11 +158,30 @@ function proxyServer(option){
|
||||
callback(null);
|
||||
},
|
||||
|
||||
|
||||
function(callback) {
|
||||
|
||||
if (option.globalProxy) {
|
||||
var result = require('./lib/proxyManager').enableGlobalProxy(ip.address(), proxyPort);
|
||||
|
||||
if (result.status) {
|
||||
console.log(color.red(result.stdout));
|
||||
}
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
//server status manager
|
||||
function(callback){
|
||||
|
||||
process.on("exit",function(code){
|
||||
logUtil.printLog('AnyProxy is about to exit with code: ' + code, logUtil.T_ERR);
|
||||
var result = require('./lib/proxyManager').disableGlobalProxy();
|
||||
|
||||
if (result.status) {
|
||||
console.log(color.red(result.stdout));
|
||||
}
|
||||
|
||||
process.exit();
|
||||
});
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user