-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathapp.js
More file actions
137 lines (115 loc) · 2.81 KB
/
app.js
File metadata and controls
137 lines (115 loc) · 2.81 KB
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
import fs from "fs";
import net from "net";
import path from "path";
import express from "express";
import rateLimit from "express-rate-limit";
import morgan from "morgan";
import { LRUCache } from "lru-cache";
import { query as dotsQuery } from "./dots/index.js";
const cache = new LRUCache({
max: 500,
ttl: 30 * 1000,
});
const VALID_TYPES = new Set([
"a",
"aaaa",
"cname",
"mx",
"naptr",
"ns",
"ptr",
"soa",
"srv",
"txt",
"rdns",
"tls",
"http",
"whois",
"geo",
]);
const DOMAIN_RE =
/^(?:[_a-zA-Z0-9](?:[_a-zA-Z0-9-]{0,61}[_a-zA-Z0-9])?\.)*[a-zA-Z]{2,}$/;
const app = express();
app.disable("x-powered-by");
app.use(morgan("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(process.cwd(), "dist")));
app.use(express.static(path.join(process.cwd(), "public")));
app.get("/healthz", (req, res) => {
res.sendStatus(200);
});
const queryLimiter = rateLimit({
windowMs: 60 * 1000,
max: 60,
standardHeaders: true,
legacyHeaders: false,
skip: (req) => {
const { type, addr } = req.body;
if (typeof type !== "string" || typeof addr !== "string") return false;
return cache.get(`${type}:${addr.toLowerCase()}`) !== undefined;
},
});
app.post("/", queryLimiter, async (req, res, next) => {
const { type, addr } = req.body;
if (typeof type !== "string" || !VALID_TYPES.has(type)) {
return res.sendStatus(400);
}
if (
typeof addr !== "string" ||
addr.length > 253 ||
(!net.isIP(addr) && !DOMAIN_RE.test(addr))
) {
return res.sendStatus(400);
}
try {
const cacheKey = `${type}:${addr.toLowerCase()}`;
let records = cache.get(cacheKey);
if (records === undefined) {
records = await dotsQuery(type, addr);
cache.set(cacheKey, records);
}
res.json({ records });
} catch (err) {
handleError(err, res, next);
}
});
const distIndexPath = path.join(process.cwd(), "dist", "index.html");
let indexHtml = null;
try {
indexHtml = fs.readFileSync(distIndexPath, "utf-8");
indexHtml = indexHtml.replace(
"__MAPBOX_TOKEN__",
process.env.MAPBOX_TOKEN || "",
);
} catch {}
app.get("*any", (req, res) => {
if (req.path === "/" && req.query.addr) {
res.redirect(`/${req.query.addr}`);
return;
}
if (!indexHtml) {
return res
.status(503)
.send("Run 'npm run build' first, or use 'npm run dev' for development.");
}
res.type("html").send(indexHtml);
});
const handleError = (err, res, next) => {
switch (err.code) {
case "ENOTFOUND":
case "ENODATA":
case "TIMEOUT":
res.json({ records: [] });
break;
case "BADQUERY":
res.sendStatus(400);
break;
default:
next(err);
}
};
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Listening on port ${PORT}`);
});