forked from chill117/proxy-lists
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·300 lines (264 loc) · 7.09 KB
/
cli.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
#!/usr/bin/env node
'use strict';
// To fix "MaxListenersExceededWarning: Possible EventEmitter memory leak detected" warning.
// Setting max listeners to a large yet reasonable number.
process.setMaxListeners(1024);
var _ = require('underscore');
var fs = require('fs');
var path = require('path');
var program = require('commander');
var pkg = require('./package.json');
var ProxyLists = require('./index');
var validOutputFormats = ['json', 'csv', 'txt'];
var proxyFieldNames = ['source', 'ipAddress', 'port', 'country', 'protocols', 'anonymityLevel'];
function list(value) {
return value.split(',');
}
function value(value) {
return value;
}
program
.version(pkg.version)
.description(pkg.description);
program
.command('getProxies')
.option(
'-m, --filter-mode [value]',
'Set the filter mode [strict or loose]',
value,
null
)
.option(
'-a, --anonymity-levels <list>',
'Get proxies with these anonymity levels [' + ProxyLists._anonymityLevels.join(', ') + ']',
list,
null
)
.option(
'-c, --countries <list>',
'Get proxies from these countries [us, ca, cz, ..]',
list,
null
)
.option(
'-C, --countries-black-list <list>',
'Exclude proxies from these countries [de, gb, ..]',
list,
null
)
.option(
'-p, --protocols <list>',
'Get proxies that support these protocols [' + ProxyLists._protocols.join(', ') + ']',
list,
null
)
.option(
'-s, --sources-white-list <list>',
'Get proxies from these sources only [some-source, another-source, somewhere, etc..]',
list,
null
)
.option(
'-x, --sources-black-list <list>',
'Do not get proxies from these sources [some-source, another-source, somewhere, etc..]',
list,
null
)
.option(
'--sources-dir [value]',
'Full path to the sources directory',
value,
ProxyLists.defaultOptions.sourcesDir
)
.option(
'-f, --output-file [value]',
'File to which the output will be written',
value,
'proxies'
)
.option(
'-F, --output-format [value]',
'Format in which the output will be written [' + validOutputFormats.join(', ') + ']',
value,
'txt'
)
.option(
'--series',
'Perform all asynchronous operations in series'
)
.option(
'--sample',
'Get a sample of proxies from each source'
)
.option(
'--stdout',
'Write to STDOUT instead of a file',
value,
false
)
.option(
'-l, --log-file [value]',
'File to which will be logged when writing to stdout',
value,
'proxy-lists.log'
)
.action(function(options) {
var outputFormat = options.outputFormat;
var stdout = options.stdout;
var outputFile = options.outputFile;
if (outputFile.indexOf('/') === -1) {
outputFile = path.join(process.cwd(), outputFile);
}
if (!path.extname(outputFile)) {
outputFile = outputFile + '.' + outputFormat;
}
var outputStream;
if (!stdout) {
outputStream = fs.createWriteStream(outputFile);
} else {
outputFile = 'STDOUT';
outputStream = {
write: function(data) {
process.stdout.write(data + '\n');
},
end: function(cb) {
cb();
},
on: function() {},
};
}
var logFile = options.logFile;
if (logFile.indexOf('/') === -1) {
logFile = path.join(process.cwd(), logFile);
}
var logStream = fs.createWriteStream(logFile);
function log() {
var args = Array.prototype.slice.call(arguments);
var message = args.join(' ');
logStream.write(message + '\n');
}
var numWriting = 0;
var wroteData = false;
function onData(data) {
if (!_.isEmpty(data)) {
numWriting++;
switch (outputFormat) {
case 'json':
data = _.map(data, function(row) {
return JSON.stringify(row);
});
outputStream.write((wroteData ? ',' : '') + data.join(','));
break;
case 'csv':
data = _.map(data, function(row) {
return _.map(proxyFieldNames, function(fieldName) {
return _.isArray(row[fieldName]) ? row[fieldName].join('/') : row[fieldName];
}).join(',');
});
outputStream.write('\n' + data.join('\n'));
break;
case 'txt':
data = _.map(data, function(row) {
return row.ipAddress + ':' + row.port;
});
outputStream.write((wroteData ? '\n' : '') + data.join('\n'));
break;
}
numWriting--;
wroteData = true;
}
tryEndOutput();
}
function tryEndOutput() {
if (canEndOutput()) {
endOutput();
}
}
function canEndOutput() {
return doneScrapingAllSources() && !isWriting();
}
function isWriting() {
return numWriting > 0;
}
function doneScrapingAllSources() {
return !!_.every(sources, function(source) {
return !!sourcesDone[source.name];
});
}
var startOutput = _.once(function() {
log('Writing output to ' + outputFile);
switch (outputFormat) {
case 'json':
outputStream.write('[');
break;
case 'csv':
outputStream.write(proxyFieldNames.join(','));
break;
}
});
var endOutput = _.once(function() {
log('Closing output stream...');
switch (outputFormat) {
case 'json':
outputStream.write(']');
break;
}
outputStream.end(function() {
log('Output stream closed');
log('Closing log stream...');
if (logStream) {
logStream.end(done);
} else {
done();
}
});
});
var done = _.once(function() {
process.exit();
});
log('Getting proxies...');
var listSourcesOptions = _.pick(options, [
'filterMode',
'anonymityLevels',
'countries',
'countriesBlackList',
'protocols',
'sourcesWhiteList',
'sourcesBlackList',
'sourcesDir',
'sample',
'series',
]);
var sources = ProxyLists.listSources(listSourcesOptions);
var sourceOptions = _.omit(listSourcesOptions, 'sourcesWhiteList', 'sourcesBlackList');
var sourcesDone = {};
_.each(sources, function(source) {
try {
ProxyLists.getProxiesFromSource(source.name, sourceOptions)
.on('data', onData)
.on('error', function(error) {
log('Error while scraping', source.name + ':', error);
})
.once('end', function() {
log('Finished scraping from', source.name);
sourcesDone[source.name] = true;
tryEndOutput();
});
} catch (error) {
log(error);
}
});
startOutput();
});
program
.command('updateGeoIpData')
.usage('--license-key <value>\n\nThis module uses geoip-lite to perform geoip-country lookups on IP addresses for\neach proxy. The geoip-lite module ships with the free version of MaxMind\'s geoip\ndatabase. This database stopped being directly included in the module due to\na change on MaxMind\'s side - specifically with their end-user licensing agreements.\nSo it is necessary for each end-user (that\'s you!) to create their own MaxMind\naccount and then generate a license key.\n\nTo sign-up for a MaxMind account:\nhttps://www.maxmind.com/en/geolite2/signup\n\nTo generate a new license key:\nhttps://support.maxmind.com/account-faq/license-keys/how-do-i-generate-a-license-key/')
.requiredOption(
'-l, --license-key <value>',
'Your MaxMind license key'
)
.action(function(options) {
process.argv.push('license_key=' + options.licenseKey);
require(path.join(__dirname, 'node_modules', 'geoip-lite', 'scripts', 'updatedb.js'));
});
program.parse(process.argv);