-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsystemProxyMgr.js
152 lines (120 loc) · 4.44 KB
/
systemProxyMgr.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
'use strict'
const child_process = require('child_process');
const networkTypes = ['Ethernet', 'Thunderbolt Ethernet', 'Wi-Fi'];
function execSync(cmd) {
let stdout,
status = 0;
try {
stdout = child_process.execSync(cmd);
} catch (err) {
stdout = err.stdout;
status = err.status;
}
return {
stdout: stdout.toString(),
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
* ------------------------------------------------------------------------
*/
const macProxyManager = {};
macProxyManager.getNetworkType = () => {
for (let i = 0; i < networkTypes.length; i++) {
const 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 = (ip, port, proxyType) => {
if (!ip || !port) {
console.log('failed to set global proxy server.\n ip and port are required.');
return;
}
proxyType = proxyType || 'http';
const networkType = macProxyManager.networkType || macProxyManager.getNetworkType();
return /^http$/i.test(proxyType) ?
// set http proxy
execSync(
'networksetup -setwebproxy ${networkType} ${ip} ${port} && networksetup -setproxybypassdomains ${networkType} 127.0.0.1 localhost'
.replace(/\${networkType}/g, networkType)
.replace('${ip}', ip)
.replace('${port}', port)) :
// set https proxy
execSync('networksetup -setsecurewebproxy ${networkType} ${ip} ${port} && networksetup -setproxybypassdomains ${networkType} 127.0.0.1 localhost'
.replace(/\${networkType}/g, networkType)
.replace('${ip}', ip)
.replace('${port}', port));
};
macProxyManager.disableGlobalProxy = (proxyType) => {
proxyType = proxyType || 'http';
const 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 = () => {
const networkType = macProxyManager.networkType || macProxyManager.getNetworkType();
const result = execSync('networksetup -getwebproxy ${networkType}'.replace('${networkType}', networkType));
return result;
};
/**
* ------------------------------------------------------------------------
* windows proxy manager
*
* netsh does not alter the settings for IE
* ------------------------------------------------------------------------
*/
const winProxyManager = {};
winProxyManager.enableGlobalProxy = (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 = () => execSync('reg add "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings" /v ProxyEnable /t REG_DWORD /d 0 /f');
winProxyManager.getProxyState = () => ''
winProxyManager.getNetworkType = () => ''
module.exports = /^win/.test(process.platform) ? winProxyManager : macProxyManager;