-
-
Notifications
You must be signed in to change notification settings - Fork 279
/
system-proxy.js
150 lines (130 loc) · 4.52 KB
/
system-proxy.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
import util from 'util';
import os from 'os';
import path from 'path';
import {exec as _exec, spawn} from 'child_process';
import Registry from 'winreg';
import getLogger from '../logger';
const logger = getLogger('system-proxy');
const exec = util.promisify(_exec);
class SystemProxy {
static async setProxy(ip, port) {
throw new Error('You have to implement the method setProxy!');
}
static async unsetProxy() {
throw new Error('You have to implement the method unsetProxy!');
}
}
// TODO: Add path http_proxy and https_proxy
// TODO: Support for non-gnome
class LinuxSystemProxy extends SystemProxy {
static async setProxy(ip, port) {
await exec('gsettings set org.gnome.system.proxy mode manual');
await exec(`gsettings set org.gnome.system.proxy.http host ${ip}`);
await exec(`gsettings set org.gnome.system.proxy.http port ${port}`);
await exec(`gsettings set org.gnome.system.proxy.https host ${ip}`);
await exec(`gsettings set org.gnome.system.proxy.https port ${port}`);
}
static async unsetProxy() {
await exec('gsettings set org.gnome.system.proxy mode none');
}
}
// TODO: Support for lan connections too
// TODO: move scripts to ../scripts/darwin
class DarwinSystemProxy extends SystemProxy {
static async setProxy(ip, port) {
const wifiAdaptor = (await exec(`sh -c "networksetup -listnetworkserviceorder | grep \`route -n get 0.0.0.0 | grep 'interface' | cut -d ':' -f2\` -B 1 | head -n 1 | cut -d ' ' -f2"`)).stdout.trim();
await exec(`networksetup -setwebproxy '${wifiAdaptor}' ${ip} ${port}`);
await exec(`networksetup -setsecurewebproxy '${wifiAdaptor}' ${ip} ${port}`);
}
static async unsetProxy() {
const wifiAdaptor = (await exec(`sh -c "networksetup -listnetworkserviceorder | grep \`route -n get 0.0.0.0 | grep 'interface' | cut -d ':' -f2\` -B 1 | head -n 1 | cut -d ' ' -f2"`)).stdout.trim();
await exec(`networksetup -setwebproxystate '${wifiAdaptor}' off`);
await exec(`networksetup -setsecurewebproxystate '${wifiAdaptor}' off`);
}
}
class WindowsSystemProxy extends SystemProxy{
static async setProxy(ip, port) {
const regKey = new Registry({
hive: Registry.HKCU,
key: '\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings'
});
await Promise.all([
WindowsSystemProxy._asyncRegSet(regKey, 'MigrateProxy', Registry.REG_DWORD, 1),
WindowsSystemProxy._asyncRegSet(regKey, 'ProxyEnable', Registry.REG_DWORD, 1),
WindowsSystemProxy._asyncRegSet(regKey, 'ProxyHttp1.1', Registry.REG_DWORD, 0),
WindowsSystemProxy._asyncRegSet(regKey, 'ProxyServer', Registry.REG_SZ, `${ip}:${port}`),
WindowsSystemProxy._asyncRegSet(regKey, 'ProxyOverride', Registry.REG_SZ, '*.local;<local>'),
]);
await WindowsSystemProxy._resetWininetProxySettings();
}
static async unsetProxy() {
const regKey = new Registry({
hive: Registry.HKCU,
key: '\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings'
});
await Promise.all([
WindowsSystemProxy._asyncRegSet(regKey, 'ProxyEnable', Registry.REG_DWORD, 0),
WindowsSystemProxy._asyncRegSet(regKey, 'ProxyServer', Registry.REG_SZ, ``),
]);
await WindowsSystemProxy._resetWininetProxySettings();
}
static _asyncRegSet(regKey, name, type, value) {
return new Promise((resolve, reject) => {
regKey.set(name, type, value, e => {
if (e) {
reject(e);
} else {
resolve();
}
})
});
}
static _resetWininetProxySettings() {
return new Promise((resolve, reject) => {
const scriptPath = path.join(__dirname, '..', 'scripts', 'windows', 'wininet-reset-settings.ps1');
const child = spawn("powershell.exe", [scriptPath]);
child.stdout.setEncoding('utf8');
child.stdout.on("data", (data) => {
if (data.includes('True')) {
resolve();
} else {
reject(data);
}
});
child.stderr.on("data", (err) => {
reject(err);
});
child.stdin.end();
});
}
}
function getSystemProxy() {
switch (os.platform()) {
case 'darwin':
return DarwinSystemProxy;
case 'linux':
return LinuxSystemProxy;
case 'win32':
case 'win64':
return WindowsSystemProxy;
case 'unknown os':
default:
throw new Error(`UNKNOWN OS TYPE ${os.platform()}`);
}
}
export async function setProxy(ip, port) {
try {
const systemProxy = getSystemProxy();
await systemProxy.setProxy(ip, port);
} catch (error) {
logger.debug(`[SYSTEM PROXY] error on SetProxy (${error})`)
}
}
export async function unsetProxy() {
try {
const systemProxy = getSystemProxy();
await systemProxy.unsetProxy();
} catch (error) {
logger.debug(`[SYSTEM PROXY] error on UnsetProxy (${error})`)
}
}