1 | const fs = require('fs');
|
2 | const path = require('path');
|
3 | const Detector = require('./detector');
|
4 | const fetch = require('node-fetch');
|
5 | const json = JSON.parse(fs.readFileSync(path.resolve(`${ __dirname }/apps.json`)));
|
6 | class Detect
|
7 | {
|
8 | constructor (url, data, context)
|
9 | {
|
10 | this.apps = [];
|
11 | this.detector = new Detector();
|
12 | this.detector.apps = json.apps;
|
13 | this.detector.driver.displayApps = (detected, meta) =>
|
14 | {
|
15 | this.meta = meta;
|
16 | Object.keys(detected).forEach((appName) =>
|
17 | {
|
18 | const app = detected[ appName ];
|
19 | const categories = [];
|
20 | app.props.cats.forEach((id) =>
|
21 | {
|
22 | const category = {};
|
23 | category[ id ] = json.categories[ id ].name;
|
24 | categories.push(category);
|
25 | });
|
26 | if (!this.apps.some(detectedApp => detectedApp.name === app.name))
|
27 | {
|
28 | let icon = this.datauri('./icons/' + (app.props.icon || 'default.svg'));
|
29 | this.apps.push({
|
30 | name: app.name,
|
31 | confidence: app.confidenceTotal.toString(),
|
32 | version: app.version || null,
|
33 | icon,
|
34 | website: app.props.website,
|
35 | categories,
|
36 | });
|
37 | }
|
38 | });
|
39 | };
|
40 | }
|
41 | datauri(file)
|
42 | {
|
43 | var bitmap = fs.readFileSync(path.resolve(`${ __dirname }/${ file }`));
|
44 | let uri = new Buffer(bitmap).toString('base64');
|
45 | let ext = file.slice(-3).toLowerCase();
|
46 | let mime = ext == 'png' ? 'data:image/png;base64,' : ext == 'jpg' ?
|
47 | 'data:image/jpeg;base64,' : 'data:image/svg+xml;base64,';
|
48 | return mime + uri;
|
49 | }
|
50 | async identify(url, data)
|
51 | {
|
52 | await this.detector.analyze(url, data)
|
53 | return this.apps;
|
54 | }
|
55 | regexMatches(string, reg)
|
56 | {
|
57 | return (string.match(reg) || []).map(e => e.replace(reg, '$1').replace(/\s+/gm, ' '))
|
58 | };
|
59 | async url(url)
|
60 | {
|
61 | let process = await fetch(url);
|
62 | let html = await process.text();
|
63 | let headers = await process.headers.raw();
|
64 | let cookie = await process.headers.get('set-cookie');
|
65 | let scriptsRegex = /(?:\<script.*?src=(?:\'|\")(.*?)(?:\'|\"))/igms;
|
66 | let scripts = this.regexMatches(html, scriptsRegex);
|
67 | return this.identify(url, { html, url, headers, cookie, scripts });
|
68 | }
|
69 | }
|
70 | module.exports = Detect; |
\ | No newline at end of file |