-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
344 lines (306 loc) · 11.2 KB
/
index.js
File metadata and controls
344 lines (306 loc) · 11.2 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
const axios = require("axios").default;
const dns = require("dns/promises")
const HttpsProxyAgent = require('https-proxy-agent');
const HttpProxyAgent = require('http-proxy-agent');
const pT = require('promise-timeout');
/**
* @author Demon Martin
* @license MIT
* @description A simple proxy chooser to choose proxies from a list, only supports http/https proxies.
*/
class ProxyChooser {
/**
* @type {Array}
* @private
*/
#proxyList;
/**
* @typedef {Object} ProxyChooserOptions
* @property {boolean} [verbose=false] - If true, will log to console
* @property {string} [verboseIdentifier="[proxyChooser]"] - Identifier for verbose
* @property {number} [maxTimeout=1000] - Max timeout for a request
* @property {string} [pingUrl="http://myexternalip.com/raw"] - URL to use to check proxy
* @property {boolean} [forceRetry=false] - Whether function getProxy should continue searching even on error
* @property {number} [maxRetries=100] - Max retries for a request
*/
/**
* @type {ProxyChooserOptions}
* @private
*/
#options;
/**
* @type {Array}
* @private
*/
#cache;
/**
* @param {Array} proxyList - List of proxies to use
* @param {ProxyChooserOptions} options - Options to use
*/
constructor(proxyList, options) {
this.#proxyList = proxyList || [];
this.#options = options || {
verbose: false,
maxTimeout: 1000,
verboseIdentifier: "[proxyChooser]",
pingUrl: "http://myexternalip.com/raw",
forceRetry: false,
maxRetries: 100
};
this.#cache = [];
}
/**
* Validates the options and throws an error if they are invalid.
* @private
*/
validateOptions() {
if (!this.#options) {
this.#options = {
verbose: false,
maxTimeout: 1000,
verboseIdentifier: "[proxyChooser]",
};
}
if (typeof this.#options?.verbose === "undefined") {
this.#options.verbose = false;
}
if (typeof this.#options?.maxTimeout === "undefined") {
this.#options.maxTimeout = 1000;
}
if (typeof this.#options?.verboseIdentifier === "undefined") {
this.#options.verboseIdentifier = "[proxyChooser]";
}
if (typeof this.#options?.pingUrl === "undefined") {
this.#options.pingUrl = "http://myexternalip.com/raw";
}
if (typeof this.#options?.forceRetry === "undefined") {
this.#options.forceRetry = false;
}
if (
typeof this.#options?.maxTimeout !== "number" ||
typeof this.#options?.verbose !== "boolean" ||
typeof this.#options?.verboseIdentifier !== "string" ||
typeof this.#options?.pingUrl !== "string" ||
typeof this.#options?.forceRetry !== "boolean"
) {
throw new Error("Invalid options");
}
return true;
}
/**
* Resets the proxy list.
* @returns {boolean} Whether it failed or not
*/
resetList() {
if (this.#proxyList?.length != 0) {
this.#proxyList = [];
} else {
return false;
}
return true;
}
/**
* Adds proxies to the proxy list.
* @param {Array} proxies - List of proxies to add
* @returns {boolean} Whether it failed or not
*/
addProxies(proxies) {
this.validateOptions();
if (typeof proxies == "undefined" || !Array.isArray(proxies) || proxies.length === 0) {
return false;
}
this.#proxyList = [...this.#proxyList, ...proxies];
return true;
}
/**
* Determines the type of the given proxy.
* @param {string} proxy - Proxy to check
* @returns {string} Proxy type
*/
proxyType(proxy) {
if (!proxy) {
throw new Error("Proxy cannot be undefined");
}
const directPattern = /^[\w.]+:\d+$/;
const authPattern = /^([^:@]+):([^:@]+)@([^\s@:]+):(\d+)$/
if (directPattern.test(proxy)) {
return "direct";
} else if (authPattern.test(proxy)) {
return "auth";
} else {
return "invalid";
}
}
/**
* Converts the given proxy to a specific format.
* @param {string} proxy - Proxy to convert
* @returns {Promise<string>} Converted proxy
*/
async convertProxy(proxy) {
if (!proxy) {
throw new Error("Proxy cannot be undefined");
}
// Regex to check if Hostname is IP or not
const hasNumber = /^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)(\.(?!$)|$)){4}$/;
// Split proxy into host and port, name and password
let proxyHost, proxyPort, proxyAuth, proxyPassword;
let proxyTypeCheck = this.proxyType(proxy);
if (proxyTypeCheck == "direct") {
// if proxy is host:port
[proxyHost, proxyPort] = proxy.split(":");
} else if (proxyTypeCheck == "auth") {
// if proxy is auth:password@host:port
[proxyAuth, proxyHost] = proxy.split("@");
[proxyAuth, proxyPassword] = proxyAuth.split(":");
[proxyHost, proxyPort] = proxyHost.split(":");
} else {
throw new Error("Invalid proxy format: " + proxy);
}
// If proxyHost is not an IP, resolve it to an IP
if (!hasNumber.test(proxyHost)) {
try {
const proxyIP = await dns.lookup(proxyHost);
proxyHost = proxyIP.address;
} catch (error) {
throw new Error(
`Failed to resolve IP address for ${proxyHost}.`
);
}
}
// If proxy is auth:password@host:port, return auth:password@ip:port
if (proxyTypeCheck == "auth") {
return `${proxyAuth}:${proxyPassword}@${proxyHost}:${proxyPort}`;
}
// If proxy is host:port, return ip:port
return `${proxyHost}:${proxyPort}`;
}
/**
* Gets the ping between the client and the pingUrl.
* @returns {Promise<number>} Ping in ms
*/
async getPing() {
this.validateOptions();
try {
if (this.#options.verbose) {
console.log(`${this.#options.verboseIdentifier} Getting ping for ${this.#options.pingUrl}`)
}
let StartTime = Date.now()
await axios({
method: "get",
url: this.#options.pingUrl,
timeout: 30 * 10000
});
let EndTime = Date.now()
if (this.#options.verbose) {
console.log(`${this.#options.verboseIdentifier} Ping for ${this.#options.pingUrl} is ${EndTime - StartTime}ms`)
}
return EndTime - StartTime;
} catch (error) {
throw new Error(`Failed to get ping for ${this.#options.pingUrl} | Axios Error: ${error}`);
}
}
/**
* Tests the given proxy for connectivity.
* @param {string} proxy - Proxy to test
* @returns {Promise<boolean>} Whether the proxy is working or not
*/
async testProxy(proxy) {
this.validateOptions()
if (!proxy) {
throw new Error("Proxy cannot be undefined");
}
proxy = await this.convertProxy(proxy);
try {
let startTime = Date.now();
const controller = new AbortController();
const axiosRequest = axios({
method: "get",
url: this.#options.pingUrl,
httpsAgent: new HttpsProxyAgent(`http://${proxy}`),
httpAgent: new HttpProxyAgent(`http://${proxy}`),
timeout: this.#options.maxTimeout,
signal: controller.signal
});
try {
const timeoutPromise = await pT.timeout(axiosRequest, this.#options.maxTimeout);
} catch (error) {
try { controller.abort() } catch { }
if (error instanceof pT.TimeoutError) {
throw new Error(`Request timed out after ${this.#options.maxTimeout}ms`);
} else {
throw new Error(error);
}
}
let endTime = Date.now();
if ((endTime - startTime) > this.#options.maxTimeout) {
throw new Error(`Request timed out after ${this.#options.maxTimeout}ms`);
}
if (this.#options.verbose) {
console.log(
`${this.#options.verboseIdentifier} Proxy ${proxy} is working. Ping: ${(endTime - startTime)}ms`
);
}
return true;
} catch (e) {
if (this.#options.verbose) {
console.log(
`${this.#options.verboseIdentifier} Proxy ${proxy} is not working. | ${e}`
);
}
return false;
}
}
/**
* Resets the cache of tested proxies.
* @returns {boolean} Whether the cache was reset or not
*/
resetCache() {
this.#cache = [];
return true;
}
/**
* Gets the next working proxy from the proxy list.
* @returns {Promise<string|null>} Next working proxy or null if no proxy is available
*/
async getProxy(retries = 0) {
this.validateOptions();
try {
const proxyList = this.#proxyList.filter(cproxy => !this.#cache.includes(cproxy));
const options = this.#options;
const proxy = await this.convertProxy(proxyList[Math.floor(Math.random() * proxyList.length)]);
if (!this.#cache.includes(proxy)) {
this.#cache.push(proxy);
} else {
if (this.#cache.length === this.#proxyList.length) {
throw new Error(`All proxies have been tested and failed, please reset Cache using cacheReset()`)
}
}
if (options.verbose) {
console.log(
`${options.verboseIdentifier} Trying proxy ${proxy}`
);
}
let workingProxy = await this.testProxy(proxy);
if (workingProxy) {
return proxy;
} else {
if (retries < options.maxRetries) {
return this.getProxy(retries + 1);
} else {
throw new Error(`Max retries (${options.maxRetries}) exceeded`);
}
}
} catch (error) {
if (this.#options.forceRetry) {
if (this.#options.verbose) {
console.log(`${this.#options.verboseIdentifier} ${error} | Retrying... (forceRetry enabled)`)
}
this.resetCache();
return this.getProxy(retries + 1);
} else {
throw new Error(error);
}
}
}
}
module.exports = ProxyChooser;