-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
87 lines (76 loc) · 2.23 KB
/
index.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
const http = require('http');
const errors = [
'Bad proxy string',
'Proxy offline'
];
const proxy_check = p => {
return new Promise((resolve, reject) => {
let proxy = {
host: '',
port: 0,
proxyAuth: ''
};
if (typeof p === 'object') {
if (Array.isArray(p)) {
if (typeof p[0] === 'object') {
proxy = p[0];
} else if (typeof p === 'string') {
p = p[0];
} else {
return reject(errors[0]);
}
} else {
proxy = p;
}
}
if (typeof p === 'string') {
if (p.indexOf('@') + 1) {
proxy.proxyAuth = p.split('@')[0];
const host_port = p.split('@')[1];
if (host_port.indexOf(':') + 1) {
proxy.host = host_port.split(':')[0];
proxy.port = host_port.split(':')[1];
}
} else {
if (p.indexOf(':') + 1) {
proxy.host = p.split(':')[0];
proxy.port = p.split(':')[1];
}
}
}
const proxy_options = {
method: 'CONNECT',
path: 'www.google.com:443',
timeout: 1000,
agent: false
};
if (proxy.host) {
proxy_options.host = proxy.host;
}
if (proxy.port) {
proxy_options.port = proxy.port;
}
if (proxy.proxyAuth) {
proxy_options.headers = {
'Proxy-Authorization': 'Basic ' + Buffer.from(proxy.proxyAuth).toString('base64')
};
}
const req = http.request(proxy_options);
req.on('connect', res => {
req.destroy();
if (res.statusCode === 200) {
return resolve(true);
} else {
return reject(errors[1]);
}
});
req.on('timeout', () => {
req.destroy();
});
req.on('error', err => {
return reject((err && err.code) || errors[1]);
});
req.end();
});
}
module.exports = proxy_check;