-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuppeteercluster.js
160 lines (139 loc) · 4.82 KB
/
puppeteercluster.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
const { Cluster } = require('puppeteer-cluster');
class PuppeteerCluster {
constructor(appAnalytics, options) {
this.cluster = null;
this.options = Object.assign(
{},
{
userAgent: 'wappalyzer-puppeteer',
waitDuration: 1000 * 15,
puppeteerClusterOptions: {
concurrency: Cluster.CONCURRENCY_CONTEXT,
maxConcurrency: 4,
puppeteerOptions: {
headless: true,
ignoreHTTPSErrors: true
}
}
},
options
);
this.appAnalytics = appAnalytics;
}
async startCluster() {
if (this.cluster !== null) {
return;
}
this.cluster = await Cluster.launch(this.options.puppeteerClusterOptions);
await this.cluster.task(async ({ page, data: { url, myContext } }) =>
myContext.visitInternal(page, url)
);
}
async closeCluster() {
if (this.cluster === null) {
return;
}
await this.cluster.idle();
await this.cluster.close();
this.cluster = null;
}
async visitInternal(page, url) {
try {
let headers = [];
let scripts = [];
let links = [];
let statusCode = null;
let contentType = null;
let resources = [];
await page.setRequestInterception(true);
page.on('request', req => {
req.continue();
});
page.on('response', res => {
if (res.status() === 301 || res.status() === 302) {
return;
}
const resHeaders = res.headers();
if (resources.length === 0) {
statusCode = res.status();
contentType = resHeaders['content-type'];
Object.keys(resHeaders).forEach(key => {
if (Array.isArray(resHeaders[key])) {
headers[key] = resHeaders[key];
} else {
headers[key] = [resHeaders[key]];
}
});
}
resources.push(res.url());
if (
resHeaders['content-type'] &&
(resHeaders['content-type'].indexOf('javascript') !== -1 ||
resHeaders['content-type'].indexOf('application/') !== -1)
) {
scripts.push(res.url());
}
});
// navigate
await page.setUserAgent(this.options.userAgent);
//console.time('pageload');
try {
if (this.options.waitDuration) {
await Promise.race([
page.goto(url, {
timeout: this.options.waitDuration,
waitUntil: 'networkidle2'
}),
new Promise(x => setTimeout(x, this.options.waitDuration))
]);
} else {
await page.goto(url, {
waitUntil: 'networkidle2'
});
}
} catch (err) {
console.log(err.toString(), 'puppeteer', 'error');
}
//console.timeEnd('pageload');
// get links
const list = await page.evaluateHandle(() =>
Array.from(document.getElementsByTagName('a')).map(a => ({
href: a.href,
hostname: a.hostname,
pathname: a.pathname,
hash: a.hash,
protocol: a.protocol
}))
);
links = await list.jsonValue();
// get cookies
let cookies = await page.cookies();
cookies = cookies.map(e => {
e.key = e.name;
return e;
});
// get html
const html = await page.content();
// do analytics
const result = await this.appAnalytics.runAnalytics(
page,
html,
scripts,
cookies,
headers
);
// close the page to free up memory
await page.close();
return result;
} catch (err) {
throw err;
}
}
async analyze(visiturl) {
if (this.cluster === null) {
return Promise.reject(new Error('You should start the cluster before analyze'));
}
return await this.cluster.execute({ url: visiturl, myContext: this });
}
}
module.exports = PuppeteerCluster;