forked from ChickenAI/multizlogin
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathproxyService.js
More file actions
68 lines (59 loc) · 1.89 KB
/
proxyService.js
File metadata and controls
68 lines (59 loc) · 1.89 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
62
63
64
65
66
67
68
// proxyService.js
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const proxiesFilePath = path.join(__dirname, 'zalo_data', 'proxies.json');
const MAX_ACCOUNTS_PER_PROXY = 3;
class ProxyService {
constructor() {
this.RAW_PROXIES = [];
try {
const data = fs.readFileSync(proxiesFilePath, 'utf8');
this.RAW_PROXIES = JSON.parse(data);
} catch (err) {
console.error('Không thể đọc file proxies.json:', err);
this.RAW_PROXIES = [];
}
this.PROXIES = this.RAW_PROXIES.map((p) => ({ url: p, usedCount: 0, accounts: [] }));
}
getAvailableProxyIndex() {
for (let i = 0; i < this.PROXIES.length; i++) {
if (this.PROXIES[i].usedCount < MAX_ACCOUNTS_PER_PROXY) {
return i;
}
}
return -1;
}
addProxy(proxyUrl) {
try {
new URL(proxyUrl);
const newProxy = { url: proxyUrl, usedCount: 0, accounts: [] };
this.PROXIES.push(newProxy);
this.RAW_PROXIES.push(proxyUrl);
fs.writeFileSync(proxiesFilePath, JSON.stringify(this.RAW_PROXIES, null, 2));
return newProxy;
} catch (err) {
throw new Error('Proxy URL không hợp lệ');
}
}
removeProxy(proxyUrl) {
const index = this.PROXIES.findIndex((p) => p.url === proxyUrl);
if (index === -1) {
throw new Error('Không tìm thấy proxy');
}
this.PROXIES.splice(index, 1);
const rawIndex = this.RAW_PROXIES.indexOf(proxyUrl);
if (rawIndex !== -1) {
this.RAW_PROXIES.splice(rawIndex, 1);
}
fs.writeFileSync(proxiesFilePath, JSON.stringify(this.RAW_PROXIES, null, 2));
return true;
}
getPROXIES() {
return this.PROXIES;
}
}
const proxyService = new ProxyService();
export { proxyService };