2018-08-01 20:09:51 +03:00
|
|
|
import http, { ServerResponse } from "http";
|
2019-04-13 01:44:18 +03:00
|
|
|
import { GlobalPlugin, IPluginOptions, logger } from "knub";
|
2018-08-18 19:51:28 +03:00
|
|
|
import { GuildArchives } from "../data/GuildArchives";
|
2018-08-05 00:45:35 +03:00
|
|
|
import { sleep } from "../utils";
|
2019-01-13 17:29:26 +02:00
|
|
|
import moment from "moment-timezone";
|
2018-08-01 20:09:51 +03:00
|
|
|
|
|
|
|
const DEFAULT_PORT = 9920;
|
2018-08-18 19:51:28 +03:00
|
|
|
const archivesRegex = /^\/(spam-logs|archives)\/([a-z0-9\-]+)\/?$/i;
|
2018-08-01 20:09:51 +03:00
|
|
|
|
|
|
|
function notFound(res: ServerResponse) {
|
|
|
|
res.statusCode = 404;
|
|
|
|
res.end("Not Found");
|
|
|
|
}
|
|
|
|
|
2019-03-04 21:44:04 +02:00
|
|
|
interface ILogServerPluginConfig {
|
|
|
|
port: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class LogServerPlugin extends GlobalPlugin<ILogServerPluginConfig> {
|
2019-01-13 17:29:26 +02:00
|
|
|
public static pluginName = "log_server";
|
2019-01-03 06:15:28 +02:00
|
|
|
|
2018-08-18 19:51:28 +03:00
|
|
|
protected archives: GuildArchives;
|
2018-08-01 20:09:51 +03:00
|
|
|
protected server: http.Server;
|
|
|
|
|
2019-03-04 21:44:04 +02:00
|
|
|
protected getDefaultOptions(): IPluginOptions<ILogServerPluginConfig> {
|
|
|
|
return {
|
|
|
|
config: {
|
|
|
|
port: DEFAULT_PORT,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-08-05 00:45:35 +03:00
|
|
|
async onLoad() {
|
2018-08-18 19:51:28 +03:00
|
|
|
this.archives = new GuildArchives(null);
|
2018-08-01 20:09:51 +03:00
|
|
|
|
|
|
|
this.server = http.createServer(async (req, res) => {
|
2018-08-18 19:51:28 +03:00
|
|
|
const pathMatch = req.url.match(archivesRegex);
|
|
|
|
if (!pathMatch) return notFound(res);
|
2018-08-01 20:09:51 +03:00
|
|
|
|
2018-08-18 19:51:28 +03:00
|
|
|
const logId = pathMatch[2];
|
|
|
|
|
|
|
|
if (pathMatch[1] === "spam-logs") {
|
|
|
|
res.statusCode = 301;
|
|
|
|
res.setHeader("Location", `/archives/${logId}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pathMatch) {
|
|
|
|
const log = await this.archives.find(logId);
|
2018-08-01 20:09:51 +03:00
|
|
|
if (!log) return notFound(res);
|
|
|
|
|
2019-01-13 17:29:26 +02:00
|
|
|
let body = log.body;
|
|
|
|
|
|
|
|
// Add some metadata at the end of the log file (but only if it doesn't already have it directly in the body)
|
|
|
|
if (log.body.indexOf("Log file generated on") === -1) {
|
|
|
|
const createdAt = moment(log.created_at).format("YYYY-MM-DD [at] HH:mm:ss [(+00:00)]");
|
|
|
|
body += `\n\nLog file generated on ${createdAt}`;
|
|
|
|
|
|
|
|
if (log.expires_at !== null) {
|
|
|
|
const expiresAt = moment(log.expires_at).format("YYYY-MM-DD [at] HH:mm:ss [(+00:00)]");
|
|
|
|
body += `\nExpires at ${expiresAt}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-01 20:09:51 +03:00
|
|
|
res.setHeader("Content-Type", "text/plain; charset=UTF-8");
|
2019-01-13 17:29:26 +02:00
|
|
|
res.end(body);
|
2018-08-01 20:09:51 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-03-04 21:44:04 +02:00
|
|
|
const port = this.getConfig().port;
|
2018-08-05 00:45:35 +03:00
|
|
|
let retried = false;
|
|
|
|
|
|
|
|
this.server.on("error", async (err: any) => {
|
|
|
|
if (err.code === "EADDRINUSE" && !retried) {
|
2019-04-13 01:44:18 +03:00
|
|
|
logger.info("Got EADDRINUSE, retrying in 2 sec...");
|
2018-08-05 00:45:35 +03:00
|
|
|
retried = true;
|
|
|
|
await sleep(2000);
|
2019-03-04 21:44:04 +02:00
|
|
|
this.server.listen(port);
|
2018-08-05 00:45:35 +03:00
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-03-04 21:44:04 +02:00
|
|
|
this.server.listen(port);
|
2018-08-01 20:09:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async onUnload() {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
this.server.close(() => resolve());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|