Files
Hyperia/server/index.js
2025-11-24 00:56:15 -06:00

229 lines
7.3 KiB
JavaScript

const express = require('express');
const cors = require('cors');
const cookieParser = require('cookie-parser');
const http = require('http');
const fs = require('fs');
const chalk = require('chalk');
const moment = require('moment');
const path = require('path');
import { initWebSocket } from './ws.js'
import Database from "./db/db.js"
import AuthHandler from './auth.js';
import handlers from "./handlers.js";
class Server {
db;
auth;
UIPath = path.join(__dirname, './ui')
DBPath = path.join(__dirname, './db')
registerRoutes(router) {
// router.post('/api/location', handlers.updateLocation)
router.post('/login', this.auth.login)
router.get('/signout', this.auth.logout)
router.get('/signup', this.verifyToken, this.get)
router.post('/signup', this.verifyToken, this.newUserSubmission)
router.get('/db/images/*', this.getUserImage)
router.get('/*', this.get)
return router
}
verifyToken = (req, res, next) => {
const { token } = req.query;
if (!token) {
return res.status(400).json({ error: 'Token is required' });
}
let fromDB = this.db.tokens.get(token)
if (!fromDB) {
return res.status(403).json({ error: 'Invalid or expired token' });
} else if(fromDB.used) {
return res.status(403).json({ error: 'Invalid or expired token' });
}
next()
}
newUserSubmission = async (req, res) => {
const { token } = req.query;
try {
let tokenData = db.tokens.get(token)
if(tokenData.used) throw new global.ServerError(400, "Token alredy used!")
await db.members.add(req.body, tokenData.uuid)
db.tokens.markUsed(token)
global.db.saveData()
return res.status(200).json({});
} catch(e) {
console.log(e)
return res.status(e.status).json({ error: 'Error adding new member' });
}
}
authMiddleware = (req, res, next) => {
const authHeader = req.headers.authorization;
if (!authHeader) {
return res.status(401).json({ error: 'Authorization token required.' });
}
const [scheme, token] = authHeader.split(' ');
if (scheme !== 'Bearer' || !token) {
return res.status(401).json({ error: 'Malformed authorization header.' })
}
try {
const payload = this.auth.verify(token);
req.user = payload;
return next();
} catch (err) {
return res.status(403).json({ error: 'Invalid or expired token.' });
}
}
getUserImage = async (req, res) => {
function getFileByNumber(dir, number) {
const files = fs.readdirSync(dir);
const match = files.find(file => {
const base = path.parse(file).name; // filename without extension
return base === String(number);
});
return match ? path.join(dir, match) : null;
}
let filePath = getFileByNumber(path.join(this.DBPath, "images"), path.basename(req.url))
res.sendFile(filePath)
}
get = async (req, res) => {
let url = req.url
let publicSite = () => {
let filePath;
if(url.startsWith("/_")) {
filePath = path.join(this.UIPath, url);
} else if(url.includes("75820185")) {
filePath = path.join(this.UIPath, "public", url.split("75820185")[1]);
} else {
filePath = path.join(this.UIPath, "public", "index.html");
}
res.sendFile(filePath);
}
let privateSite = () => {
let filePath;
if(url.startsWith("/_")) {
filePath = path.join(this.UIPath, url);
} else if(url.includes("75820185")) {
filePath = path.join(this.UIPath, "site", url.split("75820185")[1]);
} else {
filePath = path.join(this.UIPath, "site", "index.html");
}
res.sendFile(filePath);
}
if(!this.auth.isLoggedInUser(req, res)) {
publicSite()
} else {
privateSite()
}
}
logRequest(req, res, next) {
const formattedDate = moment().format('M.D');
const formattedTime = moment().format('h:mma');
if(req.url.includes("/api/")) {
console.logclean(chalk.blue(` ${req.method} ${req.url} | ${formattedDate} ${formattedTime}`));
} else {
if(req.url === "/")
console.logclean(chalk.gray(` ${req.method} ${req.url} | ${formattedDate} ${formattedTime}`));
}
next();
}
logResponse(req, res, next) {
const originalSend = res.send;
res.send = function (body) {
if(res.statusCode >= 400) {
console.logclean(chalk.blue( `<-${chalk.red(res.statusCode)}- ${req.method} ${req.url} | ${chalk.red(body)}`));
} else {
console.logclean(chalk.blue(`<-${res.statusCode}- ${req.method} ${req.url}`));
}
originalSend.call(this, body);
};
next();
}
constructor() {
this.db = new Database()
global.db = this.db
this.auth = new AuthHandler()
const app = express();
app.use(cors({ origin: '*' }));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(this.logRequest);
app.use(this.logResponse);
let router = express.Router();
this.registerRoutes(router)
app.use('/', router);
const server = http.createServer(app);
initWebSocket(server);
const PORT = 3003;
server.listen(PORT, () => {
console.logclean("\n")
console.logclean(chalk.yellow("*************** Hyperia ***************"))
console.logclean(chalk.yellowBright(`Server is running on port ${PORT}: http://localhost`));
console.logclean(chalk.yellow("***************************************"))
console.logclean("\n")
});
process.on('SIGINT', async () => {
console.logclean(chalk.red('Closing server...'));
console.logclean(chalk.green('Database connection closed.'));
process.exit(0);
});
Object.preventExtensions(this);
}
}
const _log = console.log;
console.logclean = function (...args) {
_log.call(console, ...args);
}
// console.log = function (...args) {
// // Get the caller location
// const stack = new Error().stack.split("\n")[2];
// const match = stack.match(/(\/.*:\d+:\d+)/);
// let location = match ? match[1] : "unknown";
// // Remove CWD prefix
// while (location.startsWith("/")) {
// location = location.slice(1);
// }
// location = "/" + location
// let cwd = process.cwd();
// if (location.startsWith(cwd)) {
// location = location.slice(cwd.length);
// if (location.startsWith("/")) location = location.slice(1);
// }
// _log.call(console, `[${location}]`, ...args);
// };
global.ServerError = class extends Error {
constructor(status, msg) {
super(msg);
this.status = status;
}
}
const server = new Server()