forked from gregtour/cybertanks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.js
63 lines (56 loc) · 1.85 KB
/
admin.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
var crypto = require('crypto');
var qs = require('querystring');
var stats, statlog;
function GET_POST(req, resp, callback) {
if (req.method === 'POST') {
var body = '';
req.on('data', function (data) {
body += data;
});
req.on('end', function () {
var post = qs.parse(body);
callback(post);
});
} else {
callback(null);
}
}
// /admin page request.
function adminPage(req, resp) {
GET_POST(req, resp, function (post) {
if (post && post['key']) {
var hash = crypto.createHash('sha256');
hash.update(post['key'] + process.env.SALT);
var keyhash = hash.digest('hex');
if (keyhash === process.env.ADMIN_KEY) {
resp.writeHead(200, {"Content-Type": "text/html"});
resp.write("<!doctype html><h1>Admin Panel</h1><h2>Welcome</h2>");
resp.write("<h3>Current Users</h3>" + stats.currentUsers);
resp.write("<h3>Total Users</h3>" + stats.totalUsers);
var log = "";
stats.log.map((value) => log += "<p>" + value + "</p>\n");
resp.write("<h3>Log</h3>"+log+"</p>");
resp.end();
} else {
var ip = req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress;
console.log("Unauthorized admin login attempt: " + ip + " " + keyhash);
statlog("Unauthorized admin login attempt: " + ip + " " + keyhash);
resp.writeHead(401, {"Content-Type": "text/html"});
resp.write("<!doctype html><h1>Unauthorized.</h1>");
resp.end();
}
} else {
resp.writeHead(200, {"Content-Type": "text/html"});
resp.write("<!doctype html><form action='/admin' method='post'>Administrator Password: <input name='key' type='password'><input type='submit' value='Enter'></form>");
resp.end();
}
});
};
module.exports = function (statsObject, statLogFn) {
stats = statsObject;
statlog = statLogFn;
return adminPage;
}