-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathserver.js
More file actions
223 lines (183 loc) · 4.42 KB
/
server.js
File metadata and controls
223 lines (183 loc) · 4.42 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import { handler } from './build/handler.js';
import express from 'express';
import newrelic from 'newrelic';
import { RateLimiterMemory } from 'rate-limiter-flexible';
import requestIp from 'request-ip';
class InvalidRequestError extends Error {}
class TooManyRequestsError extends Error {}
const default_limiter = new RateLimiterMemory({
points: 1000,
duration: 60,
blockDuration: 60,
});
const search_limiter = new RateLimiterMemory({
points: 30,
duration: 60,
blockDuration: 3 * 60,
});
const github_limiter = new RateLimiterMemory({
points: 5,
duration: 60,
blockDuration: 10 * 60,
});
const github_long_limiter = new RateLimiterMemory({
points: 30,
duration: 10 * 60 * 60,
blockDuration: 10 * 60 * 60,
});
const ROUTES = [
{
method: 'POST',
endpoint: '/maps/{map}/edit',
regex: /^\/maps\/([^/]+)\/edit$/,
extract: (m) => ({ map: m[1] }),
},
{
method: 'GET',
endpoint: '/maps/{map}/{file}',
regex: /^\/maps\/([^/]+)\/([^/]+)$/,
extract: (m) => ({ map: m[1], file: m[2] }),
},
{
method: 'GET',
endpoint: '/maps/random',
regex: /^\/maps\/random$/,
extract: () => ({}),
},
{
method: 'GET',
endpoint: '/maps/{map}',
regex: /^\/maps\/([^/]+)$/,
extract: (m) => ({ map: m[1] }),
},
{
method: 'GET',
endpoint: '/maps',
regex: /^\/maps$/,
extract: () => ({}),
},
{
method: 'GET',
endpoint: '/journal/{id}',
regex: /^\/journal\/([^/]+)$/,
extract: () => ({}),
},
{
method: 'POST',
endpoint: '/request/new',
regex: /^\/request\/new$/,
extract: () => ({}),
},
{
method: 'GET',
endpoint: '/search',
regex: /^\/search$/,
extract: () => ({}),
},
{
method: 'GET',
endpoint: '/statistics',
regex: /^\/statistics$/,
extract: () => ({}),
},
{
method: 'GET',
endpoint: '/',
regex: /^\/$/,
extract: () => ({}),
},
];
function normalize_path(input)
{
let path = input.split('?')[0].split('#')[0];
if (!path.startsWith('/'))
path = '/' + path;
path = path.replaceAll(/\/{2,}/g, '/');
if (path.length > 1 && path.endsWith('/'))
path = path.slice(0, -1);
if (path.endsWith('/__data.json'))
path = path.slice(0, -12);
return path;
}
function match_endpoint(method, path)
{
for (const route of ROUTES)
{
if (route.method !== method)
continue;
const match = route.regex.exec(path);
if (match)
{
const params = route.extract(match);
return {
route: route.endpoint,
map: params.map ?? null,
file: params.file ?? null,
};
}
}
return {
route: null,
map: null,
file: null,
};
}
const app = express();
app.set('trust proxy', true);
app.use(async (req, res, next) =>
{
const method = req.method;
const path = normalize_path(req.path || '/');
const endpoint = match_endpoint(method, path);
try
{
if (endpoint.route !== null)
{
const name = `${method} ${path}`;
newrelic.setTransactionName(name);
newrelic.addCustomAttribute('custom_name', name);
newrelic.addCustomAttribute('custom_method', method);
newrelic.addCustomAttribute('custom_path', path);
newrelic.addCustomAttribute('custom_ip', 'no_ip');
newrelic.addCustomAttribute('custom_endpoint', `${method} ${endpoint.route}`);
newrelic.addCustomAttribute('custom_map', endpoint.map);
newrelic.addCustomAttribute('custom_file', endpoint.file);
newrelic.addCustomAttribute('custom_error', null);
newrelic.addCustomAttribute('custom_status', null);
}
const limiters = [default_limiter];
if (endpoint.route === '/search')
limiters.push(search_limiter);
else if (endpoint.route === '/request/new' || endpoint.route === '/maps/{map}/edit')
limiters.push(github_limiter, github_long_limiter);
const ip = requestIp.getClientIp(req) || req.ip;
if (!ip)
throw new InvalidRequestError('No IP');
if (endpoint.route !== null)
newrelic.addCustomAttribute('custom_ip', ip);
try
{
for (const limiter of limiters)
await limiter.consume(ip);
}
catch
{
throw new TooManyRequestsError('Too many requests');
}
return next();
}
catch (error)
{
console.error(error);
if (endpoint.route !== null)
newrelic.addCustomAttribute('custom_error', error.message);
if (error instanceof InvalidRequestError)
return res.status(400).send(error.message);
if (error instanceof TooManyRequestsError)
return res.status(429).send(error.message);
return res.status(500).send('Internal server error');
}
});
app.use(handler);
const PORT = 3000;
app.listen(PORT, () => console.log(`Server listening on port ${PORT}`));