This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex.js
195 lines (158 loc) · 4.92 KB
/
index.js
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* eslint-disable camelcase */
'use strict';
// Hey! Use comments for everything you do.
// Load packages.
const fs = require('fs');
const glob = require('glob');
const yaml = require('js-yaml');
const express = require('express');
const bodyParser = require('body-parser');
const ejs = require('ejs');
const session = require('express-session');
const http = require('http');
const expressWs = require('express-ws');
const rateLimit = require('express-rate-limit');
// Load prototypes
Date.prototype.addDays = function (days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
};
// Load settings.
process.env = yaml.load(fs.readFileSync('./settings.yml', 'utf8'));
if (process.env.pterodactyl.domain.slice(-1) === '/')
process.env.pterodactyl.domain = process.env.pterodactyl.domain.slice(0, -1);
process.api_messages = yaml.load(fs.readFileSync('./api_messages.yml', 'utf8'));
// Loads database.
const db = require('./db.js');
const Sqlite = require('better-sqlite3');
const SqliteStore = require('better-sqlite3-session-store')(session);
const session_db = new Sqlite('sessions.db');
// Loads functions.
const functions = require('./functions.js');
// Loads page settings.
process.pagesettings = yaml.load(
fs.readFileSync('./frontend/pages.yml', 'utf8')
); // Loads "settings.yml" and loads the yaml file as a JSON.
setInterval(() => {
process.pagesettings = yaml.load(
fs.readFileSync('./frontend/pages.yml', 'utf8')
); // This line of code is suppose to update any new pages.yml settings every minute.
}, 60000);
const path = require('path');
// Makes "process.db" have the database functions.
process.db = db;
// Make "process.functions" have the custom functions..
process.functions = functions;
// Start express website.
const app = express(); // Creates express object.
process.rateLimit = rateLimit;
app.use(
express.json({
// Some settings for express.
inflate: true,
limit: '500kb',
reviver: null,
strict: true,
// type: 'application/json',
verify: undefined,
})
);
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.set('views', path.join(__dirname, 'frontend', 'pages'));
app.use((err, req, res, next) => {
if (err instanceof SyntaxError && err.status === 400 && 'body' in err) {
// https://stackoverflow.com/questions/53048642/node-js-handle-body-parser-invalid-json-error
// console.error(err);
res.status(400);
return res.send({
error: 'An error has occured when trying to handle the request.',
});
}
next();
});
app.use(
session({
secret: process.env.website.secret,
resave: true,
saveUninitialized: true,
store: new SqliteStore({
client: session_db,
expired: {
clear: true,
intervalMs: 900000,
},
}),
})
);
app.use(async (req, res, next) => {
if (req.session.data) {
if (req.session.data.dbinfo?.discord_id) {
const blacklist_status = await process.db.blacklistStatusByDiscordID(
req.session.data.dbinfo.discord_id
);
if (
blacklist_status !== 'false' &&
!req.session.data.panelinfo.root_admin
) {
delete req.session.data;
functions.doRedirect(
req,
res,
process.pagesettings.redirectactions.blacklisted
);
return;
}
}
}
next();
});
const server = http.createServer(app);
expressWs(app, server); // Creates app.ws() function, and does websocket stuff;
const listener = server.listen(process.env.website.port, function () {
// Listens the website at a port.
console.log(
`[WEBSITE] The application is now listening on port ${
listener.address().port
}.`
); // Message sent when the port is successfully listening and the website is ready.
const apiFiles = glob.sync('./handlers/**/**/*.js');
for (const file of apiFiles) {
const api = require(file);
if (typeof api.load === 'function') api.load(app, ifValidAPI, ejs);
}
});
/*
ifValidAPI(req, res, permission);
req = request
res = response
permissions = permission from settings.yml.
*/
function ifValidAPI(req, res, permission) {
const auth = req.headers.authorization;
if (auth) {
if (auth.startsWith('Bearer ') && auth !== 'Bearer ') {
const validkeys = Object.entries(process.env.api).filter(
(key) => key[0] === auth.slice('Bearer '.length)
);
if (validkeys.length === 1) {
const validkey = validkeys[0][1];
if (permission) {
if (validkey[permission]) {
return true;
}
res.status(403);
res.send({
error: process.pagesettings.apimessages.missingAPIPermissions,
}); // Gets missingAPIPermissions message.
return false;
}
return true;
}
}
}
res.status(403);
res.send({ error: process.pagesettings.apimessages.invalidAPIkey }); // Gets invalidAPIkey message.
return false;
}