-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
98 lines (59 loc) · 1.54 KB
/
index.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
const db = require("./db");
const path = require("path");
const mime = require("mime");
const contenu = require("contenu");
const iplocation = require("iplocation").default;
async function locate (ip) {
if (ip === "::1") {
return {}
} else {
iplocation(ip).catch(e => {
console.log(e);
})
const l = await iplocation(ip)
console.log(l);
delete l.latitude;
delete l.longitude;
delete l.ip;
return l;
}
}
function partial (_) {
return path.join(__dirname, "..", "..", "views", "partials", _);
}
(async () => {
await db.init();
contenu.routes.admin.get("/plugins/analytics", (req, res) => {
if (req.query.path) {
res.render(path.join(__dirname, "views", "path.ejs"), {
partial,
path: req.query.path,
db
});
} else {
res.render(path.join(__dirname, "views", "index.ejs"), {
partial,
db
});
}
});
contenu.routes.www.after((req, res) => {
res.on("finish", async () => {
if (res.statusCode === 200 || res.statusCode === 304) {
const ip = req.connection.remoteAddress.replace("::ffff:", "");
const location = await locate(ip);
// console.log(ip, location, req.get("Referrer"), req.path);
// console.log(req.path, res.getHeaders());
// console.log("A");
db.addHit({
path: req.path,
date: new Date(),
origin: location,
referrer: req.get("Referrer"),
userAgent: req.get("User-Agent"),
contentType: (res.get("Content-Type") || mime.getType(req.path) || "text/html").split(";")[0].trim()
});
}
});
});
})();