-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan.js
executable file
·115 lines (94 loc) · 3.65 KB
/
scan.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
'use strict';
const puppeteer = require('puppeteer');
const consoleOutput = require('./src/tools/console');
const linkTool = require('./src/tools/link');
const configuration = require('./src/configuration');
const ResponseCollection = require('./src/responseCollection/ResponseCollection');
const RuleAssessor = require('./src/responseCollection/RuleAssessor');
let config;
try {
config = configuration.process();
console.log('Configuration processed: ' + JSON.stringify(config, null, 4) + '\n');
} catch (e) {
console.error(e.message);
process.exit(1);
}
const responseCollection = new ResponseCollection();
const ruleAssessor = new RuleAssessor(config, responseCollection);
const printFailedResponses = () => {
const failedResponses = responseCollection.getFailedResponses();
if (failedResponses.length > 0) {
consoleOutput.writeln('\r\n============= Failed urls ================');
responseCollection.getFailedResponses().forEach(response => {
config.output_information = [
'referer',
'content-type'
];
consoleOutput.printResponse(response, config);
});
}
}
process.on('SIGINT', function() {
printFailedResponses();
process.exit();
});
(async () => {
const browser = await puppeteer.launch({headless: true});
const page = await browser.newPage();
const getLinks = async function () {
return await page.evaluate(() => {
const anchors = Array.from(document.querySelectorAll('a'));
return anchors.map(anchor => anchor.getAttribute("href"));
});
};
const login = async function () {
try {
await page.goto(linkTool.getFullUrl(config.login.url, config.base_url));
await page.click(config.login.selector.username);
await page.keyboard.type(config.login.data.username);
await page.click(config.login.selector.password);
await page.keyboard.type(config.login.data.password);
await page.click(config.login.selector.submit_button);
} catch (e) {
console.log(e);
consoleOutput.writeln('Error on login: <error>' + e.message + '</error>');
}
};
const followLink = async function (link) {
try {
await page.goto(link);
const links = await getLinks();
for (let i = 0; i < links.length; i++) {
if (links[i] === null) {
continue;
}
let fullLink = linkTool.getFullUrl(links[i], link);
// if the link was not already followed
if (ruleAssessor.shouldBeFollowed(fullLink, link)) {
await followLink(fullLink);
}
}
} catch (e) {
consoleOutput.writeln('Error following link <info>' + link + '</info>: <error>' + e.message + '</error>');
}
};
page.on('response', response => {
if (ruleAssessor.shouldBeFollowed(response.url(), page.url())) {
response.referer = page.url();
response['content-type'] = response.headers()['content-type'];
responseCollection.add(response);
}
});
responseCollection.on('response_added', (response) => {
consoleOutput.printResponse(response, config);
});
if (config.login) {
await login();
}
await followLink(config.base_url);
for (let i = 0; i < config.extra_links.length; i++) {
await followLink(linkTool.getFullUrl(config.extra_links[i], config.base_url));
}
await browser.close();
printFailedResponses();
})();