forked from hideckies/exploit-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserve.ts
58 lines (43 loc) · 1.35 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
import Server from "https:/deno.land/x/lume/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";
const server = new Server({
port: 8000,
root: `${Deno.cwd()}/_site`,
});
// Rate limiting (max 20 requests per 10 seconds)
const maxRequests = 20;
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(1);
return response;
});
// Not found
server.use(notFound({
root: `${Deno.cwd()}/_site`,
page404: "/404",
}));
server.use(expires());
server.start();
console.log("Listening on http://localhost:8000");