mirror of
https://github.com/alibaba/anyproxy.git
synced 2025-04-24 08:41:31 +00:00
Merge branch 'linkarys-add_global_proxy'
This commit is contained in:
commit
c91a6f7bc9
@ -120,6 +120,7 @@ var options = {
|
||||
socketPort : 8003, // optional, internal port for web socket, replace this when it is conflict with your own service
|
||||
throttle : 10, // optional, speed limit in kb/s
|
||||
disableWebInterface : false, //optional, set it when you don't want to use the web interface
|
||||
setAsGlobalProxy : false, //set anyproxy as your system proxy
|
||||
silent : false //optional, do not print anything into terminal. do not set it when you are still debugging.
|
||||
};
|
||||
new proxy.proxyServer(options);
|
||||
|
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 for system')
|
||||
.option('install', '[alpha] install node modules')
|
||||
.parse(process.argv);
|
||||
|
||||
@ -91,6 +92,7 @@ if(program.clear){
|
||||
webPort : program.web,
|
||||
rule : ruleModule,
|
||||
disableWebInterface : false,
|
||||
setAsGlobalProxy : program.global,
|
||||
interceptHttps : program.intercept,
|
||||
silent : program.silent
|
||||
});
|
||||
|
166
lib/systemProxyMgr.js
Normal file
166
lib/systemProxyMgr.js
Normal file
@ -0,0 +1,166 @@
|
||||
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
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* proxy for CentOs
|
||||
* ------------------------------------------------------------------------
|
||||
*
|
||||
* 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
|
||||
* ------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/**
|
||||
* 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 = {};
|
||||
|
||||
macProxyManager.getNetworkType = function() {
|
||||
|
||||
for (var i = 0; i < networkTypes.length; i++) {
|
||||
|
||||
var
|
||||
type = networkTypes[i],
|
||||
result = execSync('networksetup -getwebproxy ' + type);
|
||||
|
||||
if (result.status === 0) {
|
||||
macProxyManager.networkType = type;
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error('Unknown network type');
|
||||
};
|
||||
|
||||
macProxyManager.enableGlobalProxy = function(ip, port, proxyType) {
|
||||
|
||||
if (!ip || !port) {
|
||||
console.log('failed to set global proxy server.\n ip and port are required.');
|
||||
return;
|
||||
};
|
||||
|
||||
proxyType = proxyType || 'http';
|
||||
|
||||
var networkType = macProxyManager.networkType || macProxyManager.getNetworkType();
|
||||
|
||||
return /^http$/i.test(proxyType) ?
|
||||
|
||||
// set http proxy
|
||||
execSync(
|
||||
'networksetup -setwebproxy ${networkType} ${ip} ${port}'
|
||||
.replace("${networkType}", networkType)
|
||||
.replace("${ip}", ip)
|
||||
.replace("${port}", port)) :
|
||||
|
||||
// set https proxy
|
||||
execSync('networksetup -setsecurewebproxy ${networkType} ${ip} ${port}'
|
||||
.replace("${networkType}", networkType)
|
||||
.replace("${ip}", ip)
|
||||
.replace("${port}", port));
|
||||
|
||||
};
|
||||
|
||||
macProxyManager.disableGlobalProxy = function(proxyType) {
|
||||
proxyType = proxyType || 'http';
|
||||
|
||||
var networkType = macProxyManager.networkType || macProxyManager.getNetworkType();
|
||||
|
||||
return /^http$/i.test(proxyType) ?
|
||||
|
||||
// set http proxy
|
||||
execSync(
|
||||
'networksetup -setwebproxystate ${networkType} off'
|
||||
.replace("${networkType}", networkType)) :
|
||||
|
||||
// set https proxy
|
||||
execSync(
|
||||
'networksetup -setsecurewebproxystate ${networkType} off'
|
||||
.replace("${networkType}", networkType));
|
||||
};
|
||||
|
||||
macProxyManager.getProxyState = function() {
|
||||
var networkType = macProxyManager.networkType || macProxyManager.getNetworkType();
|
||||
var result = execSync('networksetup -getwebproxy ${networkType}'.replace('${networkType}', networkType));
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* ------------------------------------------------------------------------
|
||||
* windows proxy manager
|
||||
*
|
||||
* netsh does not alter the settings for IE
|
||||
* ------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
var winProxyManager = {};
|
||||
|
||||
winProxyManager.enableGlobalProxy = function(ip, port) {
|
||||
|
||||
if (!ip && !port) {
|
||||
console.log('failed to set global proxy server.\n ip and port are 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');
|
||||
|
||||
};
|
||||
|
||||
winProxyManager.disableGlobalProxy = function() {
|
||||
return execSync('reg add "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings" /v ProxyEnable /t REG_DWORD /d 0 /f');
|
||||
};
|
||||
|
||||
winProxyManager.getProxyState = function() {
|
||||
return '';
|
||||
};
|
||||
|
||||
winProxyManager.getNetworkType = function() {
|
||||
return '';
|
||||
};
|
||||
|
||||
module.exports = /^win/.test(process.platform) ? winProxyManager : macProxyManager;
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "anyproxy",
|
||||
"version": "3.8.1",
|
||||
"version": "3.9.0beta1",
|
||||
"description": "A fully configurable proxy in NodeJS, which can handle HTTPS requests perfectly.",
|
||||
"main": "proxy.js",
|
||||
"bin": {
|
||||
|
44
proxy.js
44
proxy.js
@ -16,6 +16,7 @@ var http = require('http'),
|
||||
logUtil = require("./lib/log"),
|
||||
wsServer = require("./lib/wsServer"),
|
||||
webInterface = require("./lib/webInterface"),
|
||||
SystemProxyMgr = require('./lib/systemProxyMgr'),
|
||||
inherits = require("util").inherits,
|
||||
util = require("./lib/util"),
|
||||
path = require("path"),
|
||||
@ -69,8 +70,6 @@ function proxyServer(option){
|
||||
logUtil.setPrintStatus(false);
|
||||
}
|
||||
|
||||
|
||||
|
||||
if(!!option.interceptHttps){
|
||||
default_rule.setInterceptFlag(true);
|
||||
|
||||
@ -158,11 +157,50 @@ function proxyServer(option){
|
||||
callback(null);
|
||||
},
|
||||
|
||||
//set global proxy
|
||||
function(callback) {
|
||||
if (option.setAsGlobalProxy) {
|
||||
console.log('setting global proxy for you...');
|
||||
if(!/^win/.test(process.platform) && !process.env.SUDO_UID){
|
||||
console.log('sudo password may be required.');
|
||||
}
|
||||
var result = SystemProxyMgr.enableGlobalProxy(ip.address(), proxyPort, proxyType == T_TYPE_HTTP ? "Http" : "Https");
|
||||
if (result.status) {
|
||||
callback(result.stdout);
|
||||
} else {
|
||||
if(/^win/.test(process.platform)){
|
||||
console.log('AnyProxy is now the default proxy for your system. It may take up to 1min to take effect.');
|
||||
} else{
|
||||
console.log('AnyProxy is now the default proxy for your system.');
|
||||
}
|
||||
callback(null);
|
||||
}
|
||||
} else {
|
||||
callback(null);
|
||||
}
|
||||
},
|
||||
|
||||
//server status manager
|
||||
function(callback){
|
||||
|
||||
process.on("exit",function(code){
|
||||
logUtil.printLog('AnyProxy is about to exit with code: ' + code, logUtil.T_ERR);
|
||||
|
||||
if (option.setAsGlobalProxy) {
|
||||
console.log('resigning global proxy...');
|
||||
var result = SystemProxyMgr.disableGlobalProxy(proxyType == T_TYPE_HTTP ? "Http" : "Https");
|
||||
|
||||
if (result.status) {
|
||||
console.log(color.red(result.stdout));
|
||||
} else{
|
||||
console.log('global proxy resigned.');
|
||||
}
|
||||
}
|
||||
|
||||
process.exit();
|
||||
});
|
||||
|
||||
//exit cause ctrl+c
|
||||
process.on("SIGINT", function() {
|
||||
process.exit();
|
||||
});
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user