-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.js
77 lines (67 loc) · 2.47 KB
/
main.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
/* eslint-disable camelcase */
const getUserIP = require('get-user-ip')
const bodyData = require('body-data')
const adapter = require('./database/adapter')
const { isUrlRegExp, refererHandler } = require('./utils')
/**
*
* @param { import('http').IncomingMessage } req
* @param { import('http').ServerResponse } res
* @returns
*/
// eslint-disable-next-line max-statements
module.exports = async (req, res) => {
try {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Cache-Control', 'no-store, max-age=0, must-revalidate')
res.setHeader('Content-Type', 'application/json;charset=utf-8')
const db = await adapter()
if (!db) {
res.statusCode = 400
res.end(JSON.stringify({ msg: 'No matching database found' }))
return
}
const request_url = refererHandler(req.url)
const data = await bodyData(req)
const ip = getUserIP(req, ['headers.cf-connecting-ip'])
// eslint-disable-next-line no-console
console.log({ ip, ua: req.headers['user-agent'], data })
const referer = req.headers.referer
// eslint-disable-next-line no-console
console.log('referer', referer)
if (!isUrlRegExp.test(referer)) {
res.statusCode = 400
res.end(JSON.stringify({ msg: 'Missing Referer header' }))
return
}
const { host } = new URL(referer)
// Get statistics in bulk
if (request_url === '/visits') {
if (!data.urls) {
res.statusCode = 400
return res.end(JSON.stringify({ msg: 'Missing `urls` parameter' }))
}
const urls_raw = req.method === 'GET' ? data.urls.split(',') : data.urls
// Limit access to 20 bars only
const urls = urls_raw
.filter((url) => isUrlRegExp.test(url) && new URL(url).host === host)
.slice(0, 20)
.map((url) => [[refererHandler(url)], url])
const fromEntries = Object.fromEntries(urls)
const entries = Object.entries(fromEntries)
const result_visits = await db.visits(entries)
res.statusCode = 200
return res.end(JSON.stringify(result_visits))
}
const result_counter = await db.counter(ip, host, refererHandler(referer))
// eslint-disable-next-line no-console
console.log(result_counter)
res.statusCode = 200
res.end(JSON.stringify(result_counter))
} catch (error) {
// eslint-disable-next-line no-console
console.error(error)
res.statusCode = 500
res.end(JSON.stringify({ msg: 'Bad Request', github: 'https://github.com/Lete114/Visit-Stat' }))
}
}