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
|
const express = require("express"); // from npm registry
const fs = require("fs"); // built-in
const middlewares = {
account: require("./middlewares/account.js"),
server: require("./middlewares/server.js"),
};
module.exports = exports = class TMWA {
constructor(config, api, challenge, rate_limit) {
// XXX: having to pass a reference to `api` is weird, we should instead
// store config in this.config and make the middlewares (somehow)
// access this.config. the problem is that we can't pass arguments
// to middlewares, so we might have to curry them
this.api = api;
this.api.locals.tmwa = config;
this.api.locals.tmwa.status = "OfflineTemporarily"; // XXX: storing these in the class feels wrong, I don't think it should be exported... maybe we could use some more curry?
this.api.locals.tmwa.num_online = 0;
this.timeout = null;
this.router = express.Router(["caseSensitive", "strict"]);
this.router.get("/server", middlewares.server);
this.router.all("/account", rate_limit, challenge); // flood limit + captcha
this.router.all("/account", express.json(), middlewares.account);
tmwa_poll(this); // first heartbeat
console.info("Loaded TMWA router");
return this.router;
}
};
const tmwa_poll = (_this) => {
fs.readFile("./online.txt", "utf8", (err, data) => {
const lines = data.split("\n");
if (err || lines.length < 2) {
console.error("TMWA: encountered an error while retrieving online.txt", err);
_this.timeout = setTimeout(() => tmwa_poll(_this), 30000); // <= it failed, so check again later
return;
}
const last_online = Date.parse(lines[0].match(/\((.+)\)/)[1] + ` ${_this.api.locals.tmwa.timezone}`);
if (Date.now() - last_online < 30000) {
const num = lines[lines.length - 2].match(/([0-9]+) users are online./);
_this.api.locals.tmwa.status = "Online";
_this.api.locals.tmwa.num_online = num ? num[1] : 0;
} else {
_this.api.locals.tmwa.status = "OfflineTemporarily";
_this.api.locals.tmwa.num_online = 0;
}
_this.timeout = setTimeout(() => tmwa_poll(_this), 2000);
});
};
|