forked from hideckies/exploit-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.ts
64 lines (47 loc) · 1.52 KB
/
serve.ts
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
import Server from "https://deno.land/x/lume@v1.19.3/core/server.ts";
// import expires from "https:/deno.land/x/lume/middlewares/expires.ts";
import notFound from "https://deno.land/x/lume@v1.19.3/middlewares/not_found.ts";
import { sleep } from "https://deno.land/x/sleep@v1.2.1/mod.ts";
// function expires(): import("https://deno.land/x/lume@v1.19.3/core/server.ts").Middleware {
// throw new Error("Function not implemented.");
// }
const server = new Server({
port: 8000,
root: `${Deno.cwd()}/_site`,
});
// Rate limiting (max 15 requests per 10 seconds)
const maxRequests = 15;
const interval = 10000;
const rateLimitter = {
requestCount: 0,
lastResetTime: Date.now(),
};
server.use(async (request, next, conn) => {
const response = await next(request);
const url = request.url;
const remoteAddr = conn.remoteAddr;
const currentTime = Date.now();
// Reset rateLimitter after elapsing interval
if (currentTime - rateLimitter.lastResetTime > interval) {
rateLimitter.requestCount = 0;
rateLimitter.lastResetTime = currentTime;
}
// Rate limiting
if (rateLimitter.requestCount < maxRequests) {
rateLimitter.requestCount += 1;
} else {
console.log(`Rate limiting for ${JSON.stringify(remoteAddr)}. URL: ${url}. Sleep 30 seconds.`);
await sleep(30);
}
await sleep(2);
return response;
})
// Expires
// server.use(expires());
// Not found
server.use(notFound({
root: `${Deno.cwd()}/_site`,
page404: "/404",
}));
server.start();
console.log("Listening on http://localhost:8000");