forked from reliactyldev/Reliactyl-2.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
161 lines (120 loc) · 4.65 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
/* eslint-disable camelcase */
'use strict'
// Hey! Use comments for everything you do.
// Load packages.
const fs = require('fs')
const yaml = require('js-yaml')
const express = require('express')
const bodyParser = require('body-parser')
const ejs = require('ejs')
const session = require('express-session')
const expressWs = require('express-ws')
const rateLimit = require('express-rate-limit')
// 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
)
// 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.
expressWs(app) // Creates app.ws() function, and does websocket stuff;
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.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,
cookie: {
secure: process.env.website.secure
},
store: new SqliteStore({
client: session_db,
expired: {
clear: true,
intervalMs: 900000
}
})
}))
app.use(async (req, res, next) => {
if (req.session.data) {
const blacklist_status = await process.db.blacklistStatus(req.session.data.userinfo.id)
if (blacklist_status && !req.session.data.panelinfo.root_admin) {
delete req.session.data
functions.doRedirect(req, res, process.pagesettings.redirectactions.blacklisted)
return
}
}
next()
})
const listener = app.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 = fs.readdirSync('./handlers').filter(file => file.endsWith('.js') && file !== 'pages.js') // Gets a list of all files in the "handlers" folder. Doesn't add any "pages.js" to the array.
apifiles.push('pages.js') // Adds "pages.js" to the end of the array. (so it loads last, because it has a "*" request)
apifiles.forEach(file => { // Loops all files in the "handlers" folder.
const apifile = require(`./handlers/${file}`) // Loads the file.
if (typeof apifile.load === 'function') apifile.load(app, ifValidAPI, ejs) // Gives "app" to the file.
})
})
/*
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
};