-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
257 lines (195 loc) · 7.17 KB
/
index.ts
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import { ServerProcess } from './src/ServerProcess';
import * as versions from './src/VersionData';
import * as cfg from './src/Config';
import * as ph from './src/PropertiesHandler';
import * as bck from './src/Backups'
import { PluginManager, Plugin as PluginType } from './src/PluginManager';
import { Server } from './src/ServerRunner';
import Logger from './src/Logger';
import { sha256 } from './src/Util';
import express from 'express';
import bodyParser from 'body-parser';
import multer from 'multer';
import path from 'node:path'
import * as fs from 'fs';
const app: express.Application = express();
app.use('/public',express.static('public'));
app.use('/assets',express.static('assets'));
app.use(bodyParser.json());
if (!fs.existsSync('./server')) fs.mkdirSync('./server');
let server: Server = new Server(versions.getNumericalVersionFromSoftVersion(cfg.current_server_choice!.version),
cfg.current_server_choice!.software ?? 'paper',
cfg.current_server_choice!.server_name ?? 'Granite');
let plugins: PluginManager = new PluginManager(server);
var storage = multer.memoryStorage()
var upload = multer({ storage: storage })
let props: any = {};
try {
ph.loadProperties(server.JarFolder + '/server.properties');
} catch (e) {
server.Ready.then(() => {
props = ph.loadProperties(server.JarFolder + '/server.properties');
})
}
//console.log(props);
//props['max-players'] = 100;
//console.log(ph.saveProperties(server.JarFolder + '/server.properties', props));
const changeServer = (name: string) => {
server.stop();
cfg.plusWSport();
let newConf = {
...cfg.Config,
}
newConf.current_server_choice = newConf.servers.find(s => s.server_name === name);
if (!newConf.current_server_choice)
return;
cfg.updateConfig(newConf);
const serverConstructorArgs = [
newConf.current_server_choice.version.split('-')[1],
newConf.current_server_choice.software as any,
newConf.current_server_choice.server_name,
];
//server = new Server('1.18', 'paper', '2myepicserver');
server = new Server(serverConstructorArgs[0], serverConstructorArgs[1], serverConstructorArgs[2]);
plugins = new PluginManager(server);
server.Ready.then(() => {
props = ph.loadProperties(server.JarFolder + '/server.properties');
})
}
app.set('view engine', 'ejs');
app.get('/', (req, res) => {
res.redirect(301, '/dashboard')
})
app.get('/dashboard', (req, res) => {
Logger.ipAddressed(req.ip, `Sending dashboard.`)
let data = {
ws_port: cfg.getSocketPort(),
...cfg.getConfig(),
props: JSON.stringify(props),
plugins: plugins.getString(),
servers: cfg.Config.servers,
versions: JSON.stringify(versions.Versions),
is_server_online: server.isOnline()
}
res.render('dashboard.ejs', data);
});
app.post('/server/change', (req, res) => {
let servername = req.body.name;
Logger.ipAddressed(req.ip, `Server change to ${servername} from ${server.Name}`)
changeServer(servername);
res.sendStatus(200);
})
app.post('/server/new', (req, res) => {
let servername = req.body.name;
let version = req.body.version;
let software = req.body.software;
let port = req.body.port;
Logger.ipAddressed(req.ip, `New server creation of ${servername} ${software}-${version} :${port}`)
cfg.addNewServer({
server_name: servername,
version: version,
software: software,
port: port
})
res.sendStatus(200);
})
app.post('/server/plugin/upload', upload.single('file'), (req, res) => {
// console.log(req.file, req.body, req.body.name);
let b64 = req.file?.buffer.toString('base64') || ''
Logger.ipAddressed(req.ip, `Plugin ${req.body.name} uploaded - sha256 of ${sha256(b64)}`)
plugins.addPlugin(req.body.name, b64);
res.sendStatus(200)
});
app.post('/server/plugin/delete/:name', (req, res) => {
if (server.isOnline())
return res.sendStatus(400);
const name = req.params.name;
Logger.ipAddressed(req.ip, `Plugin ${req.body.name} deleted}`)
console.log(name);
plugins.removePlugin(name);
res.sendStatus(200);
})
app.post('/server/plugin/enable/:name', (req, res) => {
if (server.isOnline())
return res.sendStatus(400);
const name = req.params.name;
Logger.ipAddressed(req.ip, `Plugin ${req.body.name} enabled}`)
console.log(name);
plugins.enablePlugin(name);
})
app.post('/server/plugin/disable/:name', (req, res) => {
if (server.isOnline())
return res.sendStatus(400);
const name = req.params.name;
Logger.ipAddressed(req.ip, `Plugin ${req.body.name} disabled}`)
plugins.disablePlugin(name);
})
app.post('/server/info', (req, res) => {
let data = {
version: server.Version,
software: server.Software,
name: server.Name,
is_online: server.isOnline()
}
Logger.ipAddressed(req.ip, `Sent server info - ${server.Name}`)
res.json(data);
})
app.post('/server/property', (req, res) => {
if (!req.body) {
console.log(req.body)
res.sendStatus(400);
return;
}
const property = req.body.name;
const value = req.body.value;
Logger.ipAddressed(req.ip, `${property} set to ${value}`)
props[property] = value;
ph.saveProperties(server.JarFolder + '/server.properties', props);
res.sendStatus(200);
})
app.get('/server/download/:dimension', (req, res) => {
const dimension = req.params.dimension;
Logger.ipAddressed(req.ip, `Download requested for ${dimension}`)
let name = `world${dimension === 'overworld' ? '' : '_' + dimension}`;
if (name === 'world_end') name = 'world_the_end';
if (dimension === 'full-server') name = '' // download the full server directory, instead of a subdirectory
console.log(`Downloading ${name} (${dimension})...`);
server.downloadWorld(name).then((path) => {
if (path == null) {
Logger.ipAddressed(req.ip, `Failed to download ${name}`)
res.send('failed to download')
return
}
Logger.ipAddressed(req.ip, `Downloaded ${name} to ${path}`)
console.log(`Downloaded ${name} to ${path}`);
res.download(path);
})
});
app.post('/server/start', (req, res) => {
Logger.ipAddressed(req.ip, `Start requested for ${server.Name}`)
if (bck.eligible(cfg.Config)) {
Logger.ipAddressed(req.ip, `${server.Name} eligible for backup`)
bck.backup({
serverDetails: {
name: server.Name,
version: `${server.Software}-${server.Version}`,
},
destfolder: path.join(__dirname, cfg.Config.backups.location, `${server.Name} ${server.Software}-${server.Version}`)
})
cfg.updateBackupTime()
}
server.start();
res.sendStatus(200);
});
app.post('/server/stop', (req, res) => {
Logger.ipAddressed(req.ip, `${server.Name} stopped.`)
server.stop();
res.sendStatus(200);
});
app.get('/favicon.ico', (req, res) => {
res.sendFile(__dirname + '/assets/Polished.ico');
res.sendStatus(200);
});
app.listen(cfg.frontend_port, '0.0.0.0', () => {
console.log('Granite started on port ' + cfg.frontend_port);
});