-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrotating_proxy_manager.js
More file actions
104 lines (98 loc) · 3.55 KB
/
rotating_proxy_manager.js
File metadata and controls
104 lines (98 loc) · 3.55 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const EventEmitter = require('events');
const Database = require('./database');
const RotatingProxy = require('./rotating_proxy');
class RotatingProxyManager extends EventEmitter {
constructor(proxyArr, dbPath, newInstance = false) {
super();
this.nextProxyReturned = true;
this.proxyArr = proxyArr;
this.database = new Database(dbPath+'/sqlite.db', newInstance, database => {
let addProxyPromises = [];
for (let i = 0; i < proxyArr.length; i++) {
let proxy = proxyArr[i];
addProxyPromises.push(database.addProxy(proxy.toString(), proxy.waitMin, proxy.waitMax));
}
Promise.all(addProxyPromises).then(() => {
this.emit('ready');
}).catch((err) => {
throw new Error('Error adding proxy to database: ' + err);
});
});
}
nextProxy(callback, skipWait = false) {
if (this.proxyArr.length == 0) {
throw new Error('No more proxies to use.');
}
if (!this.nextProxyReturned) {
setTimeout(() => {
this.nextProxy(callback, skipWait);
}, 0);
return;
}
this.nextProxyReturned = false;
this.database.nextProxy((err, dbProxy) => {
if (err) {
callback(err);
return;
}
let proxy = new RotatingProxy(dbProxy.proxy, dbProxy.min_wait, dbProxy.max_wait, dbProxy.updated_at);
let timeToWait = proxy.timeToWait;
if (skipWait || dbProxy.num_uses == 0) {
timeToWait = 0;
}
setTimeout(() => {
this.nextProxyReturned = true;
callback(null, proxy);
}, timeToWait);
});
}
randomProxy(callback, skipWait = false) {
if (this.proxyArr.length == 0) {
throw new Error('No more proxies to use.');
}
if (!this.nextProxyReturned) {
setTimeout(() => {
this.randomProxy(callback, skipWait);
}, 0);
return;
}
this.nextProxyReturned = false;
this.database.getProxies((err, proxies) => {
if (err) {
callback(err);
return;
}
let proxyIndex = Math.floor(Math.random() * (this.proxyArr.length));
let dbProxy = proxies[proxyIndex];
let proxy = new RotatingProxy(dbProxy.proxy, dbProxy.min_wait, dbProxy.max_wait, dbProxy.updated_at);
let timeToWait = proxy.timeToWait;
if (skipWait || dbProxy.num_uses == 0) {
timeToWait = 0;
}
this.database.incrementProxy(dbProxy.id, () => {
setTimeout(() => {
this.nextProxyReturned = true;
callback(null, proxy);
}, timeToWait);
});
});
}
blockProxy(proxy, blockUntil, callback) {
this.database.blockProxy(proxy, blockUntil, callback);
}
getProxies(callback) {
let proxies = [];
this.database.getProxies((err, dbProxies) => {
if (err) {
callback(err);
return;
}
for (let i = 0; i < dbProxies.length; i++) {
let dbProxy = dbProxies[i];
proxies.push(new RotatingProxy(dbProxy.proxy, dbProxy.min_wait, dbProxy.max_wait, dbProxy.updated_at));
}
callback(null, proxies);
});
}
}
module.exports = RotatingProxyManager;