-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxyManager.js
More file actions
56 lines (44 loc) · 1.34 KB
/
Copy pathproxyManager.js
File metadata and controls
56 lines (44 loc) · 1.34 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
// proxyManager.js
const fs = require("fs");
const path = require("path");
class ProxyManager {
constructor(path = "proxy.txt") {
this.proxies = fs
.readFileSync(path, "utf-8")
.split("\n")
.map((line) => line.trim())
.filter((line) => line.length > 0 && !line.startsWith("#"))
.map((line) => this.normalizeProxy(line))
.filter((p) => p !== null);
this.queue = [...this.proxies];
}
normalizeProxy(line) {
// Full url just keep it
if (line.startsWith("http://") || line.startsWith("https://")) {
return line;
}
const parts = line.split(":");
if (parts.length === 4) {
// BrightData format: host:port:user:pass
const [host, port, user, pass] = parts;
return `http://${user}:${pass}@${host}:${port}`;
}
if (parts.length === 2) {
// ip:port
const [ip, port] = parts;
return `http://${ip}:${port}`;
}
console.warn(`[ProxyManager] Unknown proxy format: ${line}`);
return null;
}
getNext() {
if (this.queue.length === 0) this.queue = [...this.proxies];
const proxy = this.queue.shift();
if (proxy) this.queue.push(proxy);
return proxy;
}
getAll() {
return [...this.proxies];
}
}
module.exports = new ProxyManager(path.resolve(__dirname, "proxy.txt"));