-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.js
396 lines (384 loc) · 11.2 KB
/
http.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import { createServer } from 'http'
import http from 'http'
import { Buffer } from 'buffer'
import { promises } from 'fs'
import qs from 'querystring'
import { URL } from 'url'
import { isText } from 'istextorbinary'
import * as pathModule from 'path'
import { fileURLToPath } from 'url'
const __dirname = pathModule.dirname(fileURLToPath(import.meta.url))
/**
* HTML escape a string
* @param {String} s string to HTML escape
* @returns {String} escaped string
*/
export const escapeHTML = s =>
s.replace(/[^0-9A-Za-z ]/g, c => '&#' + c.charCodeAt(0) + ';')
/**
* HTML escape an object
* @param {Object} obj object to HTML escape
* @returns {Object} object with HTML escaped strings
*/
export const objEscapeHTML = obj =>
Object.entries(obj).reduce((obj, e) => {
obj[e[0]] = escapeHTML(e[1])
return obj
}, {})
/**
* Flatten an array and return the first non-array element
* @param {Array} a
* @returns {any}
*/
export const arrToFlat = a => (Array.isArray(a) ? arrToFlat(a[0]) : a)
/**
* flatten arrays in an object - returns the first non-array element for each value in the object
* @param {Object<Array>} obj object with arrays to flatten as values
* @returns {Object<Array>} object with flattened arrays as values
*/
export const flatten = obj =>
Object.entries(obj).reduce((obj, e) => {
obj[e[0]] = arrToFlat(e[1])
return obj
}, {})
let renderFns = {}
async function genRenderFn(filePath) {
let buffer = await promises.readFile(filePath)
if (!isText(filePath, buffer)) {
renderFns[filePath] = () => buffer
return
}
let text = buffer.toString('utf8')
let matches = []
text = text
.replace(/\\/g, '\\\\')
.replace(/\`/g, '\\`')
.replace(/\$/g, '\\$')
.replace(/\{\{.*?\}\}/gs, match => {
match = match.slice(2, -2)
matches.push(match)
return '${' + match + '}'
})
renderFns[filePath] = eval(`({${matches.join(',')}}) => \`${text}\``)
}
async function render(path, data = null) {
if (data === null) data = {}
let filePath
if (data === true)
filePath = pathModule.join(__dirname, 'public', 'static', path)
else
filePath = pathModule.join(__dirname, 'public', path)
if (!filePath.startsWith(pathModule.join(__dirname, 'public'))) return null
if (!renderFns[filePath])
if (serverOptions.whitelistPaths)
return null
try {
await genRenderFn(filePath)
} catch (e) {
return null
}
if (serverOptions.escapeRender && !data.noEscape)
data = objEscapeHTML(data)
return renderFns[filePath](data)
}
async function* getFiles(dir) {
const dirents = await promises.readdir(dir, { withFileTypes: true })
for (const dirent of dirents) {
const res = pathModule.resolve(dir, dirent.name)
if (dirent.isDirectory()) {
yield* getFiles(res)
} else {
yield res
}
}
}
async function generateWhitelistPaths() {
const basePath = pathModule.join(
__dirname,
'public'
)
for await (const f of getFiles('./public')) {
await genRenderFn(f)
}
}
/** @param {string} path */
async function getMIMEtype(path) {
return {
'.html': 'text/html',
'.css': 'text/css',
'.js': 'text/javascript',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon',
'.json': 'application/json',
'.pdf': 'application/pdf',
'.txt': 'text/plain',
'.mp4': 'video/mp4',
'.webm': 'video/webm',
'.mp3': 'audio/mpeg',
'.wav': 'audio/wav',
'.ogg': 'audio/ogg',
'.woff': 'font/woff',
'.woff2': 'font/woff2',
'.eot': 'application/vnd.ms-fontobject',
'.otf': 'font/opentype',
'.ttf': 'font/truetype',
'.zip': 'application/zip',
'.rar': 'application/x-rar-compressed',
'.7z': 'application/x-7z-compressed',
'.tar': 'application/x-tar',
'.gz': 'application/x-gzip',
'.bz2': 'application/x-bzip2',
'.xz': 'application/x-xz'
}[path.slice(path.lastIndexOf('.'))] || 'text/plain'
}
export class Request {
/** @param {http.IncomingMessage} req */
constructor(req) {
this.req = req
this.ip = req.socket.remoteAddress
this.url = new URL(req.url, `http://${req.headers.host}`)
this.path = this.url.pathname
this.query = this.url.search
this.params = flatten(
Object.fromEntries(this.url.searchParams.entries())
)
}
/** @returns {Promise<qs.ParsedUrlQuery>} post data */
async getPostData() {
const buffers = []
for await (const chunk of this.req) {
buffers.push(chunk)
}
const data = Buffer.concat(buffers).toString()
if (serverOptions.flattenData)
return flatten(qs.parse(data))
return qs.parse(data)
}
/**
* @param {String} name
* @returns {Promise<String>} cookie value
*/
async getCookie(name) {
const cookies = this.req.headers.cookie
if (!cookies) return null
const cookie = cookies
.split(';')
.find(c => c.trim().startsWith(name + '='))
if (!cookie) return null
const cookieSplit = cookie.split('=')
cookieSplit.shift()
return cookieSplit.join('=')
}
}
export class Response {
/** @param {http.ServerResponse} res */
constructor(res) {
this.res = res
}
/**
* @param {String} name valid cookie name
* @param {String} value valid cookie value
* @param {Date | Number} expires `Date` object or unix timestamp
* @param {String} path path must start with this string
* @param {Boolean} secure only send cookie over https
* @param {Boolean} httpOnly forbid client side javascript to access cookie
* @param {String} domain domain at which the cookie is accessible
* @param {Number} maxAge seconds until expiration - precedence over expires
* @param {String} sameSite from what origin: { `Strict`: only when coming from this site , `Lax`: Strict + following a link , `None`: from everywhere }
*/
async setCookie(
name,
value,
expires = null,
path = null,
secure = false,
httpOnly = true,
domain = null,
maxAge = null,
sameSite = null
) {
let cookie =
`${name || ''}=${value || ''}` +
(expires != null ? `; Expires=${new Date(expires).toUTCString()}` : '') +
(maxAge != null ? `; Max-Age=${maxAge}` : '') +
(domain != null ? `; Domain=${domain}` : '') +
(path != null ? `; Path=${path}` : '') +
(secure ? '; Secure' : '') +
(httpOnly ? '; HttpOnly' : '') +
(sameSite != null ? `; SameSite=${sameSite}` : '')
this.res.setHeader(
'Set-Cookie',
cookie
)
}
/** @param {String} location */
async redirect(location) {
this.res.writeHead(302, {
Location: location
})
this.res.end()
}
/**
* render file at relative path, replacing `{{}}` with data
*
* renders files from `/public/` or `/public/static/`
*
* If you have set escapeRender to true, set `data.noEscape` to true to prevent escaping for this particular render.
* You can always escape data by using `objEscapeHTML(data)`
* @param {String} path relative path to file
* @param {Object | Boolean} data data to replace `{{}}` with - if set to `true` will render static file (`/public/static/`)
* @param {Number} code response status code
*/
async render(path, data = null, code = 200) {
let r = await render(path, data)
if (r) {
this.res.writeHead(code, {
'Content-Type': await getMIMEtype(path) + '; charset=utf-8'
})
this.res.end(r)
} else {
throw `Requested file: ${path} not found`
}
}
async return(string, contentType = 'text/plain', code = 200) {
this.res.writeHead(code, { 'Content-Type': contentType + '; charset=utf-8' })
this.res.end(string)
}
}
let getCBs = {}
/**
* Set get handler
* @param {String} path
* @param {{(req: Request, res: Response)=>{}}} callback
*/
export function onGet(path, callback) {
getCBs[path] = callback
}
let postCBs = {}
/**
* Set post handler
* @param {String} path
* @param {{(req: Request, res: Response)=>{}}} callback
*/
export function onPost(path, callback) {
postCBs[path] = callback
}
/**
* @param {Request} req
* @param {Response} res
* @param {Object.<String,{(req: Request, res: Response)=>{}}>} CBobj
*/
async function answer(req, res, CBobj) {
let cb = CBobj[req.path] || CBobj.default
if (cb) {
cb(req, res)
} else {
if (req.path === '/') req.path = '/index.html'
if (!req.path.includes('.')) req.path += '.html'
try {
await res.render(req.path, true)
} catch {
if (CBobj.err404)
CBobj.err404(req, res)
else
try {
await res.render('404.html', true)
} catch {
res.res.writeHead(404)
res.res.end()
}
}
}
}
/**
* execute get handler
* @param {Request} req
* @param {Response} res
* @param {String} path if specified, will be used instead of `req.path`
*/
export async function get(req, res, path = null) {
if (path) req.path = path
answer(req, res, getCBs)
}
/**
* execute post handler
* @param {Request} req
* @param {Response} res
* @param {String} path if specified, will be used instead of `req.path`
*/
export async function post(req, res, path = null) {
if (path) req.path = path
answer(req, res, postCBs)
}
class RequestCounter extends Array {
tick() {
this.push(Date.now())
while (this[0] < Date.now() - 1000) this.shift()
if (this.length > serverOptions.maxRequestsPerSecond)
this.timeoutUntil = Date.now() + serverOptions.DDOStimeoutMinutes * 60 * 1000
}
isInvalid() {
return this.timeoutUntil && this.timeoutUntil > Date.now()
}
}
const reqIPs = {}
/** @param {http.IncomingMessage} req @param {http.ServerResponse} res */
async function handleReq(req, res) {
if (!reqIPs[req.socket.remoteAddress])
reqIPs[req.socket.remoteAddress] = new RequestCounter()
reqIPs[req.socket.remoteAddress].tick()
if (reqIPs[req.socket.remoteAddress].isInvalid()) {
console.log(`access denied to ${req.socket.remoteAddress} for spamming`)
res.writeHead(429, {'Retry-After': serverOptions.DDOStimeoutMinutes / 60})
res.end()
return
}
let request = new Request(req)
let response = new Response(res)
if (req.method === 'GET') return get(request, response)
if (req.method === 'POST') return post(request, response)
return get(request, response)
}
/**
* @typedef {Object} serverOptions
* @prop {Boolean} flattenData flatten get and post data
* @prop {Boolean} escapeRender HTML escape render data
* @prop {Boolean} whitelistPaths generate render functions for each file on start and reject all requests to other files
* @prop {Number} maxRequestsPerSecond maximum number of requests per second before DDOS protection kicks in
* @prop {Number} DDOStimeoutMinutes minutes of timeout when banned by DDOS protection
*/
export const serverOptions = {
flattenData: true,
escapeRender: true,
whitelistPaths: true,
maxRequestsPerSecond: 20,
DDOStimeoutMinutes: 5
}
/**
* Start the http server
*
* `options` are passed as an object like this:
* @example
* {
* flattenData: true, // flatten get and post data
* escapeRender: true, //HTML escape render data
* whitelistPaths: true, // generate render functions for each file on start and reject all requests to other files
* maxRequestsPerSecond: 20, // maximum number of requests per second before DDOS protection kicks in
* DDOStimeoutMinutes: 5 // minutes of timeout when banned by DDOS protection
* }
* @param {Number} port
* @param {serverOptions} options
* @returns {Promise<http.Server>}
*/
export async function start(port = 80, options = {}) {
Object.assign(serverOptions, options)
if (serverOptions.whitelistPaths)
await generateWhitelistPaths()
let httpServer = createServer(handleReq)
httpServer.listen(port)
return httpServer
}