-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathprogram.js
413 lines (372 loc) · 14.8 KB
/
program.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const systemLocale = getDefaultLocale(); // should be before scrap-site (before lighthouse require)
import { program } from 'commander';
import config from './config.js';
import color from './color.js';
import os from 'os';
import expandHomedir from 'expand-home-dir';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const packageJsonPath = path.join(__dirname, '..', 'package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"));
const defaultDocs = [
'doc',
'docx',
'xls',
'xlsx',
'ppt',
'pptx',
'pdf',
'rar',
'zip'];
const fieldsCustom = {};
const list = val => {
return val ? val.split(',') : [];
};
/* const collect = (value, previous) => {
const res = value.match(/(.*?)=(.*)/);
const name = res[1];
const extractor = res[2];
return previous.concat([{name, extractor}]);
}; */
const fieldsCustomCollect = (value, previous) => {
const res = value.match(/(.*?)=(.*)/);
const name = res[1];
const extractor = res[2];
fieldsCustom[name] = extractor;
return fieldsCustom;
};
function getConfigVal(name, def) {
let val = undefined;
// objects like 'influxdb.maxSendCount'
if (name.includes('.')) {
const parts = name.split('.');
let conf = config;
for (let part of parts) {
conf = conf[part];
if (conf === undefined) {
conf = def;
break;
}
}
if (typeof conf === 'object') val = def;
else val = conf;
}
else if (config[name] === undefined) val = def;
else val = config[name];
// console.log(`config: ${name}: `, val);
return val;
}
function createDirIfNotExists(path) {
const exists = fs.existsSync(path);
if (exists && fs.statSync(path).isFile()) {
throw new Error(`File exists, cannot save here: ${path}`);
return false;
}
if (!exists) fs.mkdirSync(path, {recursive: true});
return path;
}
// only ru and en, default en
function getDefaultLocale() {
const locale = new Intl.DateTimeFormat().resolvedOptions().locale;
// console.log('locale:', new Intl.DateTimeFormat().resolvedOptions())
const map = {
'en-US': 'en',
'ru-RU': 'ru',
'en': 'en',
'ru': 'ru',
};
return map[locale] || 'en';
}
program.postParse = async () => {
// lang
if (!['en', 'fr', 'de', 'ru'].includes(program.lang)) program.lang = systemLocale;
// --json when --upload
if (program.upload) program.json = true;
// no remove json when --no-json
if (!program.json) program.removeJson = false;
if (program.delay > 0 && program.concurrency !== 1) {
console.log('Force set concurrency to 1, must be 1 when delay is set');
program.concurrency = 1;
}
if (program.docsExtensions === undefined) {
program.docsExtensions = getConfigVal('docsExtensions', defaultDocs);
}
if (program.preset === 'lighthouse' || program.preset === 'lighthouse-all') {
program.lighthouse = true;
}
// c = 2, when lighthouse or screenshot -> c = 1
if (program.concurrency === undefined) {
program.concurrency = getConfigVal('concurrency', Math.min(10, os.cpus().length));
if (config.maxConcurrency && program.concurrency > config.maxConcurrency) {
program.concurrency = config.maxConcurrency;
}
}
if (config.maxRequests && (!program.maxRequests || program.maxRequests > config.maxRequests)) {
program.maxRequests = config.maxRequests;
}
if (program.lighthouse) {
program.concurrency = 1;
}
if (program.screenshot) {
program.concurrency = 1;
}
if (program.concurrency > os.cpus().length) {
program.concurrency = os.cpus().length;
}
if (program.urls) {
if (program.urls.length > 1) program.urlList = true;
}
else program.urls = [];
if (program.urlList) {
program.maxDepth = 1;
program.limitDomain = false;
program.ignoreRobotsTxt = true;
// program.defaultFilter = 'depth>1';
}
// influxdb config fron ~/.site-audit-seo.conf.js
program.influxdb = getConfigVal('influxdb', false);
if (program.influxdb && program.influxdbMaxSend) {
program.influxdb.maxSendCount = program.influxdbMaxSend;
}
program.outDir = expandHomedir(program.outDir);
createDirIfNotExists(program.outDir);
}
program.option('-u --urls <urls>', 'Comma separated url list for scan', list).
option('-p, --preset <preset>',
'Table preset (minimal, seo, seo-minimal, headers, parse, lighthouse, lighthouse-all)',
getConfigVal('preset', 'seo')).
option('-t, --timeout <timeout>',
'Timeout for page request, in ms',
getConfigVal('timeout', 20000)).
option('-e, --exclude <fields>',
'Comma separated fields to exclude from results', list).
option('-d, --max-depth <depth>', 'Max scan depth',
getConfigVal('maxDepth', 10)).
option('-c, --concurrency <threads>',
`Threads number (default: ${Math.min(10, os.cpus().length)})`).
option('--lighthouse', 'Appends base Lighthouse fields to preset').
option('--delay <ms>', 'Delay between requests', parseInt, 0).
option('-f, --fields <json>',
'Field in format --field \'title=$("title").text()\'', fieldsCustomCollect,
[]).
option('--default-filter <defaultFilter>', 'Default filter when JSON viewed, example: depth>1').
option('--no-skip-static', `Scan static files`).
option('--no-limit-domain', `Scan not only current domain`).
option('--docs-extensions <ext>',
`Comma-separated extensions that will be add to table (default: ${defaultDocs.join(
',')})`, list).
option('--follow-xml-sitemap', `Follow sitemap.xml`,
getConfigVal('followXmlSitemap', false)).
option('--ignore-robots-txt', `Ignore disallowed in robots.txt`,
getConfigVal('ignoreRobotsTxt', false)).
option('--url-list', `assume that --url contains url list, will set -d 1 --no-limit-domain --ignore-robots-txt`,
getConfigVal('ignoreRobotsTxt', false)).
option('--remove-selectors <selectors>', `CSS selectors for remove before screenshot, comma separated`,
'.matter-after,#matter-1,[data-slug]').
option('-m, --max-requests <num>', `Limit max pages scan`,
parseInt, getConfigVal('maxRequests', 0)).
option('--influxdb-max-send <num>', `Limit send to InfluxDB`,
getConfigVal('influxdb.maxSendCount', 5)).
option('--no-headless', `Show browser GUI while scan`,
!getConfigVal('headless', true)).
option('--remove-csv', `Delete csv after json generate`,
getConfigVal('removeCsv', true)).
option('--remove-json', `Delete json after serve`,
getConfigVal('removeJson', true)).
option('--no-remove-csv', `No delete csv after generate`).
option('--no-remove-json', `No delete json after serve`).
option('--out-dir <dir>', `Output directory`,
getConfigVal('outDir', '~/site-audit-seo/')).
option('--out-name <name>', `Output file name, default: domain`).
option('--csv <path>', `Skip scan, only convert existing csv to json`).
option('--json', `Save as JSON`, getConfigVal('json', true)).
option('--no-json', `No save as JSON`, !getConfigVal('json', true)).
option('--upload', `Upload JSON to public web`,
getConfigVal('upload', false)).
option('--no-color', `No console colors`).
option('--partial-report <partialReport>', ``).
option('--lang <lang>', `Language (en, ru, default: system language)`,
getConfigVal('lang', undefined)).
option('--no-console-validate', `Don't output validate messages in console`).
option('--disable-plugins <plugins>', `Comma-separated plugin list`, list, []).
option('--screenshot', `Save page screenshot`, getConfigVal('screenshot', false)).
name('site-audit-seo').
version(packageJson.version).
usage('-u https://example.com')
program.getOptions = () => {
const opts = {
fieldsPreset: program.preset, // варианты: default, seo, headers, minimal
fieldsExclude: program.exclude, // исключить поля
maxDepth: program.maxDepth, // chrome-crawler, глубина сканирования
maxConcurrency: parseInt(program.concurrency), // chrome-crawler, параллельно открываемые вкладки
lighthouse: program.lighthouse, // сканировать через lighthouse
delay: parseInt(program.delay), // задержка между запросами
skipStatic: program.skipStatic, // не пропускать подгрузку браузером статики (картинки, css, js)
followSitemapXml: program.followXmlSitemap, // chrome-crawler, чтобы найти больше страниц
limitDomain: program.limitDomain, // не пропускать подгрузку браузером статики (картинки, css, js)
urlList: program.urlList, // метка, что передаётся страница со списком url
maxRequest: program.maxRequests, // chrome-crawler, для тестов
headless: program.headless, // chrome-crawler, на десктопе открывает браузер визуально
docsExtensions: program.docsExtensions, // расширения, которые будут добавлены в таблицу
outDir: program.outDir, // папка, куда сохраняются csv
outName: program.outName, // имя файла
color: program.color, // раскрашивать консоль
lang: program.lang, // язык
fields: program.fields, // дополнительные поля, --fields 'title=$("title").text()'
defaultFilter: program.defaultFilter, //
removeCsv: program.removeCsv, // удалять csv после генерации
removeJson: program.removeJson, // удалять json после поднятия сервера
json: program.json, // сохранять json файл
upload: program.upload, // выгружать json на сервер
consoleValidate: program.consoleValidate, // выводить данные валидации в консоль
obeyRobotsTxt: !program.ignoreRobotsTxt, // chrome-crawler, не учитывать блокировки в robots.txt
influxdb: program.influxdb, // конфиг influxdb
screenshot: program.screenshot, // делать ли скриншот
removeSelectors: program.removeSelectors, // удалить селекторы перед скриншотом
urls: program.urls, // адреса для одиночного сканирования
timeout: program.timeout, // таймаут запроса одной страницы
disablePlugins: program.disablePlugins,
partialReport: program.partialReport,
};
return opts;
}
program.outBrief = (options) => {
let brief = [
{
name: 'Preset',
value: program.preset,
comment: '--preset [minimal, seo, seo-minimal, headers, parse, lighthouse, lighthouse-all]',
},
{
name: 'Threads',
value: program.concurrency,
comment: '-c threads' +
(program.concurrency > 1 && program.lighthouse ?
`, ${color.yellow}recommended to set -c 1 when using lighthouse${color.reset}`
: '') +
(config.maxConcurrency ? `, server max: ${config.maxConcurrency}` : ''),
},
{
name: 'Lighthouse',
value: (program.lighthouse ? 'yes' : 'no'),
comment: (program.lighthouse ? '' : '--lighthouse')
},
{
name: 'Delay',
value: program.delay,
comment: '--delay ms',
},
{
name: 'Timeout',
value: program.timeout,
comment: '--timeout ms',
},
{
name: 'Ignore robots.txt',
value: (program.ignoreRobotsTxt ? 'yes' : 'no'),
comment: (!program.ignoreRobotsTxt ? '--ignore-robots-txt' : ''),
},
{
name: 'Follow sitemap.xml',
value: (program.followXmlSitemap ? 'yes' : 'no'),
comment: (!program.followXmlSitemap ? '--follow-xml-sitemap' : ''),
},
{
name: 'Max depth',
value: program.maxDepth,
comment: '-d 8',
},
{
name: 'Max requests',
value: program.maxRequests ? program.maxRequests : 'unlimited',
comment: '-m 123' + (config.maxRequests ? `, server max: ${config.maxRequests}` : ''),
},
{
name: 'Language',
value: program.lang,
comment: '--lang [en, fr, de, ru]',
},
/*{
name: 'Docs extensions',
value: program.docsExtensions.join(','),
comment: '--docs-extensions zip,rar',
},*/
];
if (options.screenshot) {
brief = [...brief, ...[
{
name: 'Screenshot',
value: (program.screenshot ? 'yes' : 'no'),
comment: (program.screenshot ? '' : '--screenshot')
},
{
name: 'Remove selectors',
value: (program.removeSelectors ? program.removeSelectors : 'no'),
comment: (program.removeSelectors ? '' : '--remove-selectors .banner')
},
]]
}
const fieldsArg = Object.entries(fieldsCustom).map(([key, value]) => `${key} (${value})`).join(', ');
if (fieldsArg) {
brief = [...brief, ...[
{
name: 'Fields',
// join fieldsCustom object to string "key (value), key (value)"
value: fieldsArg,
//value: fieldsCustom.map((f, name) => `${name} (${f})`),
comment: '',
},
]]
}
if (options.partialReport) {
brief = [...brief, ...[
{
name: 'Previous report',
value: options.partialReport,
comment: '--partial-report data/report/...json',
},
]];
}
if (options.influxdb) {
brief = [...brief, ...[
{
name: 'InfluxDB max send',
value: program.influxdbMaxSend,
comment: '--influxdb-max-send 10',
},
]];
}
// only for command
if (!options.webService) {
brief = [...brief, ...[
{
name: 'Headless',
value: (program.headless ? 'yes' : 'no'),
comment: (program.headless ? '--no-headless' : ''),
},
{
name: 'Save as JSON',
value: (program.json ? 'yes' : 'no'),
comment: (program.json ? '--no-json' : ''),
},
{
name: 'Upload JSON',
value: (program.upload ? 'yes' : 'no'),
comment: (!program.upload ? '--upload' : ''),
}
]];
}
console.log('');
for (let line of brief) {
const nameCol = line.name.padEnd(20, ' ');
const valueCol = `${line.value}`.padEnd(10, ' ');
const comment = line.comment ? ` ${line.comment}` : '';
console.log(color.white + nameCol + valueCol + color.reset + comment);
if (options.socket) options.socket.emit('status' + (options.socket.uid || ''), '<pre>' + nameCol + valueCol + comment + '</pre>');
}
console.log('');
if (options.socket) options.socket.emit('status' + (options.socket.uid || ''), ' ');
}
export default program;