-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns_lookups.js
61 lines (56 loc) · 1.77 KB
/
dns_lookups.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
const axios = require("axios").create({ proxy: false });
const dns = require("dns");
const privateIp = require("private-ip");
const { headers } = require("./get");
const logger = require("./logger");
// do not pass user input unsanitized! this is only for demo purposes.
const getNext = (nextLocation) => {
dns.lookup(nextLocation, {}, (err, address, family) => {
if (err || address === null || family === null) {
logger.error(err);
} else {
// don't do this
const loc = !address.startsWith("http") ? "http://" + address : address;
axios
.get(loc)
.then((response) => {
logger.info("Requested " + loc);
logger.debug(response);
})
.catch((err) => {
logger.error("UH OH: request (to " + loc + ") failed: ");
logger.error(err.errno);
logger.error(err.config);
});
}
});
};
const requestHost = (hostHeader, response) => {
dns.lookup(hostHeader, {}, (err, address) => {
if (err || address === null) {
logger.error(err);
} else if (privateIp(address)) {
const loc = !address.startsWith("http") ? "http://" + address : address;
axios
.get(loc)
.then((response) => {
logger.info("Requested " + loc);
logger.debug(response);
})
.catch((err) => {
logger.error("UH OH: request (to " + loc + ") failed");
logger.error(err.errno);
logger.error(err.config);
});
} else {
let msg = "requested host did not resolve to private ip";
response.writeHead(400, headers);
response.end(msg);
}
});
};
const patch = (request, response) => {
requestHost(request.headers.host, response);
};
module.exports.getNext = getNext;
module.exports.patch = patch;