-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
52 lines (43 loc) · 1.1 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
const httpAgent = require('socks5-http-client/lib/Agent');
const httpsAgent = require('socks5-https-client/lib/Agent');
const getAgent = (proxy, type, url) => {
const options = {};
let user = null;
let pass = null;
let host = null;
let port = null;
if (proxy.split('@').length > 1) {
user = proxy.split('@')[0].split(':')[0];
pass = proxy.split('@')[0].split(':')[1];
host = proxy.split('@')[1].split(':')[0];
port = proxy.split('@')[1].split(':')[1];
} else {
host = proxy.split(':')[0];
port = proxy.split(':')[1];
}
if (type === 'socks') {
if (url.substr(0, 5) === 'https') {
options.agentClass = httpsAgent;
} else {
options.agentClass = httpAgent;
}
options.agentOptions = {
socksHost: host,
socksPort: port,
};
if (user) {
options.agentOptions.socksUsername = user;
options.agentOptions.socksPassword = pass;
}
}
if (type === 'http') {
options.proxy = `http://${proxy}`;
}
if (type === 'https') {
options.proxy = `https://${proxy}`;
}
return options;
};
module.exports = {
getAgent
};