-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
index.js
372 lines (301 loc) · 9.16 KB
/
index.js
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
'use strict';
var _ = require('underscore');
var DataSourcer = require('data-sourcer');
var geoip = require('geoip-lite');
var net = require('net');
var path = require('path');
var debug = {
error: require('debug')('proxy-lists:error'),
};
var ProxyLists = module.exports = {
DataSourcer: DataSourcer,
defaultOptions: {
/*
The filter mode determines how some options will be used to exclude proxies.
For example if using this option `anonymityLevels: ['elite']`:
'strict' mode will only allow proxies that have the 'anonymityLevel' property equal to 'elite'; ie. proxies that are missing the 'anonymityLevel' property will be excluded.
'loose' mode will allow proxies that have the 'anonymityLevel' property of 'elite' as well as those that are missing the 'anonymityLevel' property.
*/
filterMode: 'strict',
/*
Whether or not to emit only unique proxies (HOST:PORT).
*/
unique: true,
/*
Get proxies for the specified countries.
To get all proxies, regardless of country, set this option to NULL.
See:
https://en.wikipedia.org/wiki/ISO_3166-1
Only USA and Canada:
['us', 'ca']
*/
countries: null,
/*
Exclude proxies from the specified countries.
To exclude Germany and Great Britain:
['de', 'gb']
*/
countriesBlackList: null,
/*
Get proxies that use the specified protocols.
To get all proxies, regardless of protocol, set this option to NULL.
To get proxies with specified protocols:
['socks4', 'socks5']
*/
protocols: null,
/*
Anonymity level.
To get all proxies, regardless of anonymity level, set this option to NULL.
To get proxies with specified anonymity-levels:
['elite', 'anonymous']
*/
anonymityLevels: null,
/*
Include proxy sources by name.
Only 'freeproxylists':
['freeproxylists']
*/
sourcesWhiteList: null,
/*
Exclude proxy sources by name.
All proxy sources except 'freeproxylists':
['freeproxylists']
*/
sourcesBlackList: null,
/*
Full path to the sources directory.
*/
sourcesDir: path.join(__dirname, 'sources'),
/*
Set to TRUE to have all asynchronous operations run in series.
*/
series: false,
/*
Options to pass to puppeteer when creating a new browser instance.
*/
browser: {
headless: true,
slowMo: 0,
timeout: 10000,
},
/*
Default request module options. For example you could pass the 'proxy' option in this way.
See for more info:
https://github.com/request/request#requestdefaultsoptions
*/
defaultRequestOptions: null,
/*
Use a queue to limit the number of simultaneous HTTP requests.
*/
requestQueue: {
/*
The maximum number of simultaneous requests.
*/
concurrency: 7,
/*
The time (in milliseconds) between each request. Set to 0 for no delay.
*/
delay: 0,
},
},
_protocols: ['http', 'https', 'socks4', 'socks5'],
_anonymityLevels: ['transparent', 'anonymous', 'elite'],
// Sources that were added via ProxyLists.addSource(name, source)
_sources: [],
// Get proxies from all sources.
getProxies: function(options) {
options = options || {};
options = _.defaults(options || {}, this.defaultOptions);
var emitter = DataSourcer.prototype.prepareSafeEventEmitter();
var onData = emitter.emit.bind(emitter, 'data');
var onError = emitter.emit.bind(emitter, 'error');
var onEnd = emitter.emit.bind(emitter, 'end');
var sourcerOptions = this.toSourcerOptions(options);
var dataSourcer = this.prepareDataSourcer(options);
sourcerOptions.process = this.processProxy.bind(this);
var proxyMap = options.unique ? new Map() : null;
dataSourcer.getData(sourcerOptions)
.on('data', function(proxies) {
if (proxyMap) {
var uniques = _.filter(proxies, function(proxy) {
var hostname = proxy.ipAddress + ':' + proxy.port;
if (proxyMap.has(hostname)) return false;
proxyMap.set(hostname, true);
return true;
});
if (uniques.length > 0) {
onData(uniques);
}
} else {
onData(proxies);
}
})
.on('error', onError)
.on('end', function() {
try {
if (proxyMap) {
proxyMap.clear();
proxyMap = null;
}
dataSourcer.close(function(error) {
if (error) onError(error);
onEnd();
});
} catch (error) {
onError(error);
return onEnd();
}
});
return emitter;
},
// Get proxies from a single source.
getProxiesFromSource: function(name, options) {
options = options || {};
var emitter = DataSourcer.prototype.prepareSafeEventEmitter();
var onData = emitter.emit.bind(emitter, 'data');
var onError = emitter.emit.bind(emitter, 'error');
var onEnd = emitter.emit.bind(emitter, 'end');
var sourcerOptions = this.toSourcerOptions(options);
var dataSourcer = this.prepareDataSourcer(options);
sourcerOptions.process = this.processProxy.bind(this);
dataSourcer.getDataFromSource(name, sourcerOptions)
.on('data', onData)
.on('error', onError)
.on('end', function() {
try {
dataSourcer.close(function(error) {
if (error) onError(error);
onEnd();
});
} catch (error) {
onError(error);
return onEnd();
}
});
return emitter;
},
listSources: function(options) {
options = options || {};
var sourcerOptions = this.toSourcerOptions(options);
var dataSourcer = this.prepareDataSourcer(options);
return dataSourcer.listSources(sourcerOptions);
},
addSource: function(name, source, options) {
options = options || {};
var alreadyAdded = !!_.findWhere(this._sources, { name: name });
if (alreadyAdded) {
throw new Error('Source already exists: "' + name + '"');
}
var dataSourcer = this.prepareDataSourcer(options);
dataSourcer.addSource(name, source);
this._sources.push({
name: name,
definition: source,
});
},
prepareDataSourcer: function(options) {
options = _.defaults(options || {}, {
getDataMethodName: 'getProxies',
sourcesDir: path.join(__dirname, 'sources'),
});
var dataSourcer = new DataSourcer(options);
_.each(this._sources, function(source) {
dataSourcer.addSource(source.name, source.definition);
});
return dataSourcer;
},
processProxy: function(proxy) {
if (!this.isValidProxy(proxy)) return null;
proxy.port = parseInt(proxy.port);
proxy.country = this.lookupIpAddressCountry(proxy.ipAddress);
return proxy;
},
lookupIpAddressCountry: function(ipAddress) {
if (!_.isString(ipAddress)) {
throw new Error('Invalid argument ("ipAddress"): String expected');
}
var country;
try {
var geo = geoip.lookup(ipAddress);
country = geo && geo.country && geo.country.toLowerCase();
} catch (error) {
debug.error(error);
}
return country || null;
},
toSourcerOptions: function(options) {
options = options || {};
var sourcerOptions = _.omit(options,
'filterMode',
'countries',
'countriesBlackList',
'protocols',
'anonymityLevels'
);
sourcerOptions.filter = {
mode: options.filterMode || this.defaultOptions.filterMode,
include: {},
exclude: {},
};
_.each({
country: 'countries',
protocols: 'protocols',
anonymityLevel: 'anonymityLevels',
}, function(oldKey, newKey) {
var optionValue = options[oldKey];
if (!_.isUndefined(optionValue) && !_.isNull(optionValue) && _.isArray(optionValue)) {
sourcerOptions.filter.include[newKey] = _.invoke(optionValue, 'toLowerCase');
}
});
_.each({
country: 'countriesBlackList',
}, function(oldKey, newKey) {
var optionValue = options[oldKey];
if (!_.isUndefined(optionValue) && !_.isNull(optionValue) && _.isArray(optionValue)) {
sourcerOptions.filter.exclude[newKey] = _.invoke(optionValue, 'toLowerCase');
}
});
return sourcerOptions;
},
isValidProxy: function(proxy, options) {
options = _.defaults(options || {}, {
validateIp: true
});
// 'ipAddress' is required.
if (!proxy.ipAddress) return false;
// Valid 'ipAddress' is optional.
if (options.validateIp && !this.isValidIpAddress(proxy.ipAddress)) return false;
// 'port' is required.
if (!proxy.port) return false;
// Valid port is required.
if (!this.isValidPort(proxy.port)) return false;
// 'protocols' is not required, but if it's set it should be valid.
if (!_.isUndefined(proxy.protocols) && !_.isNull(proxy.protocols)) {
if (!this.isValidProxyProtocols(proxy.protocols)) return false;
}
// 'anonymityLevel' is not required, but if it's set it should be valid.
if (!_.isUndefined(proxy.anonymityLevel) && !_.isNull(proxy.anonymityLevel)) {
if (!this.isValidAnonymityLevel(proxy.anonymityLevel)) return false;
}
// Valid proxy.
return true;
},
isValidPort: function(port) {
var asInt = parseInt(port);
return !_.isNaN(asInt) && asInt.toString() === port.toString();
},
isValidProxyProtocols: function(protocols) {
return _.isArray(protocols) && _.every(protocols, function(protocol) {
return ProxyLists.isValidProxyProtocol(protocol);
});
},
isValidProxyProtocol: function(protocol) {
return _.isString(protocol) && _.contains(this._protocols, protocol);
},
isValidAnonymityLevel: function(anonymityLevel) {
return _.isString(anonymityLevel) && _.contains(this._anonymityLevels, anonymityLevel);
},
isValidIpAddress: function(ipAddress) {
return net.isIP(ipAddress) !== 0;
}
};