2022-09-17 22:21:35 +03:00
|
|
|
require("dotenv").config();
|
2022-09-16 22:40:43 +03:00
|
|
|
const
|
2023-05-25 17:47:54 +03:00
|
|
|
{ def_theme, forum_name, description, limits, global_ratelimit: RLS, discord_auth, host } = require("../config.json"),
|
2022-09-09 15:34:12 +03:00
|
|
|
{ UserModel, BanModel } = require("./models"),
|
2022-08-29 19:31:59 +03:00
|
|
|
port = process.env.PORT || 3000,
|
|
|
|
mongoose = require("mongoose"),
|
|
|
|
express = require('express'),
|
|
|
|
fs = require("fs"),
|
2023-05-09 13:28:45 +03:00
|
|
|
{ join } = require("path"),
|
2022-09-17 22:21:35 +03:00
|
|
|
app = express(),
|
|
|
|
{ mw: IP } = require('request-ip'),
|
2023-05-09 13:28:45 +03:00
|
|
|
{ RL, themes } = require('./lib'),
|
2022-09-17 22:21:35 +03:00
|
|
|
SES = require('express-session'),
|
|
|
|
MS = require("connect-mongo"),
|
|
|
|
DB = mongoose.connect(process.env.MONGO_DB_URL)
|
|
|
|
.then(async m => {
|
|
|
|
console.log("Database is connected with", (app.ips = await BanModel.find({})).length, "banned IPs");
|
|
|
|
return m.connection.getClient()
|
|
|
|
});
|
2022-08-31 14:44:28 +03:00
|
|
|
|
2022-08-29 19:31:59 +03:00
|
|
|
app.ips = [];
|
2022-09-09 16:29:36 +03:00
|
|
|
|
2022-03-13 16:16:46 +03:00
|
|
|
app.set("view engine", "ejs");
|
2022-09-17 16:27:01 +03:00
|
|
|
app.set("limits", limits);
|
2022-08-29 19:31:59 +03:00
|
|
|
|
2023-05-25 17:47:54 +03:00
|
|
|
for (const theme of fs.readdirSync(join(__dirname, "themes")))
|
|
|
|
app.use(`/themes/${theme}`, express.static(join(__dirname, "themes", theme, "public")));
|
2023-05-25 17:23:31 +03:00
|
|
|
|
|
|
|
|
2023-05-25 17:47:54 +03:00
|
|
|
app.use(express.static(join(__dirname, "public")), express.json(), express.urlencoded({ extended: true }), IP(),
|
2023-05-08 17:16:12 +03:00
|
|
|
SES({ secret: process.env.SECRET, store: MS.create({ clientPromise: DB, stringify: false }), resave: false, saveUninitialized: false }),
|
2022-08-29 19:31:59 +03:00
|
|
|
async (req, res, next) => {
|
2022-09-16 22:52:55 +03:00
|
|
|
if (app.ips.includes(req.clientIp)) return res.status(403).send("You are banned from this forum.");
|
|
|
|
|
2022-09-16 22:26:03 +03:00
|
|
|
req.user = req.session.userID ? await UserModel.findOneAndUpdate({ id: req.session.userID }, {
|
|
|
|
lastSeen: Date.now(), $addToSet: { ips: req.clientIp }
|
2022-09-24 01:39:06 +03:00
|
|
|
}) : null;
|
2022-10-10 00:04:25 +03:00
|
|
|
|
2023-05-09 13:28:45 +03:00
|
|
|
let theme = req.user?.theme || def_theme;
|
|
|
|
|
|
|
|
if (!themes.some(t => t.codename === theme.codename))
|
|
|
|
theme = def_theme;
|
2023-05-08 17:16:12 +03:00
|
|
|
|
|
|
|
|
2023-05-25 17:23:31 +03:00
|
|
|
res.reply = (page, options = {}, status = 200) => {
|
|
|
|
const road = join(__dirname, "themes", theme.codename, "views", `${page}.ejs`);
|
|
|
|
const renderpage = fs.existsSync(road) ? road : join(__dirname, "themes", "common", "views", `${page}.ejs`);
|
|
|
|
return res.status(status).render(renderpage, {
|
|
|
|
dataset: {
|
|
|
|
themes, theme, forum_name, description,
|
|
|
|
getFile: file => join(__dirname, "themes", file),
|
|
|
|
},
|
|
|
|
user: req.user,
|
|
|
|
...options
|
|
|
|
});
|
|
|
|
}
|
2022-08-27 10:31:16 +03:00
|
|
|
|
|
|
|
res.error = (type, error) => res.reply("error", { type, error }, type);
|
|
|
|
|
2022-08-24 22:10:23 +03:00
|
|
|
if (req.user?.deleted) {
|
2022-08-29 19:31:59 +03:00
|
|
|
req.session.destroy();
|
|
|
|
return res.error(403, "Your account has been deleted.");
|
2022-08-24 22:09:21 +03:00
|
|
|
}
|
2022-10-09 21:23:31 +03:00
|
|
|
|
|
|
|
if (req.user && req.user.state == "APPROVAL" && !req.user.admin && !req.url.startsWith("/auth/email")) return res.error(403, "Your account is not approved yet.");
|
|
|
|
|
2022-08-11 03:12:40 +03:00
|
|
|
next();
|
2022-09-17 22:21:35 +03:00
|
|
|
}
|
2022-08-29 19:31:59 +03:00
|
|
|
);
|
|
|
|
|
2023-05-25 19:28:05 +03:00
|
|
|
if (RLS.enabled) app.use(RL(RLS.windowMs, RLS.max));
|
|
|
|
|
2022-09-17 20:17:18 +03:00
|
|
|
if (discord_auth)
|
2023-05-25 17:58:47 +03:00
|
|
|
app.set("DISCORD_AUTH_URL", `https://discord.com/api/oauth2/authorize?client_id=${process.env.DISCORD_ID}&redirect_uri=${host}%2Fauth%2Fdiscord&response_type=code&scope=identify`);
|
2022-09-17 20:17:18 +03:00
|
|
|
|
2023-05-25 17:47:54 +03:00
|
|
|
for (const file of fs.readdirSync(join(__dirname, "routes")))
|
2022-08-29 19:31:59 +03:00
|
|
|
app.use("/" + file.replace(".js", ""), require(`./routes/${file}`));
|
2022-03-21 23:53:22 +03:00
|
|
|
|
2023-05-08 17:16:12 +03:00
|
|
|
app.all("*", (req, res) => res.error(404, "This page does not exist on this forum."));
|
2022-03-21 23:53:22 +03:00
|
|
|
|
2023-05-25 17:23:31 +03:00
|
|
|
app.listen(port, () => console.log(`${forum_name} on port:`, port));
|