1
0
Fork 0
cloudservices/src/api/routes/Root.ts

78 lines
3.0 KiB
TypeScript

import os from 'os';
import jwt from 'jsonwebtoken';
import { TextChannel } from 'eris';
import { Server } from '..';
import { RichEmbed, Route } from '../../class';
export default class Root extends Route {
constructor(server: Server) {
super(server, { path: '/', deprecated: false, maintenance: false });
}
public bind() {
this.router.get('/', async (_req, res) => {
try {
const date = new Date();
date.setSeconds(-process.uptime());
const accounts = await this.server.client.db.Account.find().lean().exec();
const administrators = accounts.filter((account) => account.root === true).length;
const technicians = accounts.filter((account) => account?.permissions?.director === true).length;
const staff = accounts.filter((account) => account?.permissions?.staff === true).length;
const response = {
csd: {
nodeVersion: process.version,
uptime: process.uptime(),
},
server: {
userCount: accounts.length,
administratorCount: administrators,
technicianCount: technicians,
staffCount: staff,
stats: {
uptime: os.uptime(),
loadAverages: os.loadavg(),
hostname: os.hostname(),
ipv4Address: os.networkInterfaces().enp0s3.filter((r) => r.family === 'IPv4')[0].address,
operatingSystem: {
platform: os.platform(),
release: os.release(),
},
cpu: {
model: os.cpus()[0].model,
clockSpeed: os.cpus()[0].speed / 1000,
coreCount: os.cpus().length,
},
},
},
};
res.status(200).json({ code: this.constants.codes.SUCCESS, message: response });
} catch (error) {
this.handleError(error, res);
}
});
// eslint-disable-next-line consistent-return
this.router.get('/verify', async (req, res) => {
if (req.query.t) {
try {
res.setHeader('Access-Control-Allow-Origin', '*');
const token = <any> jwt.verify(req.query.t.toString(), this.server.client.config.keyPair.privateKey);
const check = await this.server.storage.get<boolean>(token);
if (!check) return res.sendStatus(401);
const embed = new RichEmbed();
embed.setTitle('Referral Authorization');
embed.addField('Referred User', token.referredUserAndDiscrim, true);
embed.addField('Referrer User', token.referrerUsername, true);
embed.addField('Referral Code', token.referralCode, true);
const channel = <TextChannel> this.server.client.guilds.get('446067825673633794').channels.get('580950455581147146');
res.sendStatus(200);
await this.server.storage.set(token, true);
return channel.createMessage({ content: `<@${token.staffUserID}>`, embed });
} catch {
return res.sendStatus(401);
}
}
});
}
}