-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
63 lines (51 loc) · 1.53 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
const express = require("express");
const dnsp = require("dns/promises");
const isIpPrivate = require("private-ip");
const fetch = (...args) => import("node-fetch").then(({default: fetch}) => fetch(...args));
const app = express();
const PORT = process.env.PORT || 8080;
app.use(express.urlencoded({ extended: false }));
app.use(express.static("public"));
app.post("/preview", async (req, res) => {
const { link } = req.body;
if(!link || typeof link !== "string") {
return res.send("Missing link");
}
let url;
try {
url = new URL(link);
}
catch(err) {
return res.send("Invalid url");
}
if(!["http:", "https:"].includes(url.protocol)) {
return res.send("Invalid url");
}
let dnsLookup;
try {
dnsLookup = await dnsp.lookup(url.hostname, 4);
}
catch(err) {
return res.send("Could not resolve url");
}
console.log(dnsLookup);
let { address } = dnsLookup;
if(isIpPrivate(address)) {
return res.send("You are not allowed to view this url");
}
try {
let fetchReq = await fetch(link);
fetchReq.body.pipe(res);
}
catch(err) {
res.send("There was an error previewing your url");
}
});
app.get("/flag", (req, res) => {
console.log(req.socket.remoteAddress);
if(req.socket.remoteAddress === "::ffff:127.0.0.1") {
return res.send(process.env.FLAG || "flag{test_flag}");
}
res.send("No flag for you!");
});
app.listen(PORT, () => console.log(`app listening on port ${PORT}`));