-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
81 lines (73 loc) · 2.43 KB
/
index.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
const express = require('express');
const bodyParser = require("body-parser");
const genericWebsite = require('@srnd/codecup-genericwebsite');
const puppeteer = require('puppeteer');
const port = process.env.PORT || 8080;
const flag = process.env.FLAG || 'test';
const tpl = genericWebsite.randomTemplate(flag);
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.get('/', (req, res) => { // simple search form
res.send(tpl('Welcome',`
<h1>Website Screenshotter</h1>
<form action = "/search" method = "POST">
<input placeholder = "codeday.org" type = "text" name = "search" align = "justify"/><br><br>
<input type = "submit" value="Search" />
</form>
<div style="height: 10px"></div>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<button onclick='swal( "Hint" , "Hacker: I know your address \\n Me: Bruh I know it too" )'>hint</button>
<div style="height: 150px"></div>`));
});
var screenshot = '';
var url = '';
let globalBrowser = false;
app.post('/search', async (req,res) => {
url = req.body.search;
if (!/^https?:\/\//i.test(url)) { // adds http:// if not
url = 'http://' + url;
}
if (!globalBrowser) {
globalBrowser = await puppeteer.launch({
args: ["--no-sandbox"]
});
}
const page = await globalBrowser.newPage();
await page.setViewport({
width:1280,
height:720,
});
try {
await page.goto(url);
await page.waitForTimeout(1600);
screenshot = await page.screenshot({ encoding: 'base64' }); // encode image in b64 to include in html
res.send(tpl('Result',`
<style>
img {
width: 100%;
height: 100%;
}
</style>
<h1>Website Screenshotter</h1>
<form action = "/search" method = "POST">
<input placeholder = "codeday.org" type = "text" name = "search" align = "justify"/><br><br>
<input type = "submit" value="Search" />
</form>
<div style="height: 10px"></div>
<img src="data:image/png;base64, ${screenshot}" />
<div style="height: 150px"></div>`));
} catch(err) { // display err
res.send(tpl('Error',`
<h1>Website Screenshotter</h1>
<form action = "/search" method = "POST">
<input placeholder = "codeday.org" type = "text" name = "search" align = "justify"/><br><br>
<input type = "submit" value="Search" />
</form>
<div style="height: 10px"></div>
${err}
<div style="height: 150px"></div>`));
}
page.close();
});
app.listen(port, () => console.log(`Listening on http://0.0.0.0:${port}/`));