-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.js
More file actions
61 lines (52 loc) · 1.65 KB
/
Copy pathproxy.js
File metadata and controls
61 lines (52 loc) · 1.65 KB
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
export default class ProxyManager {
proxyPool = {};
#user
#pass
constructor(user, pass, proxies) {
// dict
for (const proxy of proxies) {
this.proxyPool[proxy] = {}
}
this.#user = user;
this.#pass = pass;
}
async addProxy(proxy) {
this.proxyPool[proxy] = {}
}
async removeProxy(proxy) {
delete this.proxyPool[proxy]
}
async setCredentials(user, pass) {
this.#user = user;
this.#pass = pass;
}
async getCredentials() {
return { user: this.#user, pass: this.#pass }
}
async getProxy(proxy) {
return { user: this.#user, pass: this.#pass, proxyUrl: Object.keys(this.proxyPool)[proxy] }
}
async getRandomProxy() {
const proxies = Object.keys(this.proxyPool);
if (proxies.length === 0) throw new Error('No proxies available')
const getRandomProxy = (proxies) => proxies[Math.floor(Math.random() * proxies.length)];
let safety = 0;
do {
const proxy = getRandomProxy(proxies)
if ('errorCount' in this.proxyPool[proxy]) if (this.proxyPool[proxy].errorCount > 3) {
delete this.proxyPool[proxy]
safety++
continue
}
return { user: this.#user, pass: this.#pass, proxyUrl: proxy }
} while (safety < 10)
throw new Error('No remaining proxies available')
}
async markError(proxy) {
if (!this.proxyPool[proxy]) {
this.proxyPool[proxy] = {}
this.proxyPool[proxy].errorCount = 0
}
this.proxyPool[proxy].errorCount += 1
}
}