-
Notifications
You must be signed in to change notification settings - Fork 1
/
WrappedServer.java
522 lines (467 loc) · 16.2 KB
/
WrappedServer.java
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
package com.sitrica.serverinstances.objects;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.ServerConnectEvent;
import net.md_5.bungee.config.Configuration;
import net.npcnetwork.bungeecord.NpcBungee;
import net.npcnetwork.bungeecord.utils.Utils;
import java.io.*;
import java.net.InetSocketAddress;
import java.nio.file.Files;
import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import com.sitrica.serverinstances.ServerManager;
public class WrappedServer {
private final long started = System.currentTimeMillis();
private InputStream inputStream, errors;
private ProcessBuilder processBuilder;
private boolean isRunning, useSaved, isBlocking;
private OutputStream outputStream;
private final String template;
private String name, motd;
private Process process;
private File folder;
private int port;
private final List<String> commands = new ArrayList<>();
private final Configuration configuration;
private final ServerManager serverManager;
private final NpcBungee instance;
private String Xmx, Xms;
/**
* Create and start a wrapped server
*
* @param instance The Plugin running the server.
* @param existing if an existing server is to be cloned.
* @param useSaved if a saved folder was found, should it be used.
*/
public WrappedServer(NpcBungee instance, WrappedServer existing, boolean useSaved) {
this.serverManager = instance.getServerManager();
this.configuration = instance.getConfiguration();
this.commands.addAll(existing.getCommands());
this.template = existing.getTemplate();
this.folder = existing.getFolder();
this.name = existing.getName();
this.folder = setupFolder();
this.useSaved = useSaved;
this.instance = instance;
this.Xmx = existing.Xmx;
this.Xms = existing.Xms;
if (this.folder == null)
return;
this.processBuilder = setupProcessBuilder();
startup();
}
public WrappedServer(NpcBungee instance, String name, String template, String Xmx, boolean useSaved, int port) {
this.port = port;
this.name = name;
this.instance = instance;
this.template = template;
this.useSaved = useSaved;
this.serverManager = instance.getServerManager();
this.configuration = instance.getConfiguration();
this.Xms = configuration.getString("instances.default-Xms");
this.Xmx = Xmx;
this.folder = setupFolder();
if (this.folder == null)
return;
prepare();
startup();
}
public WrappedServer(NpcBungee instance, String name, String template, boolean useSaved, int port) {
this.port = port;
this.name = name;
this.instance = instance;
this.template = template;
this.useSaved = useSaved;
this.serverManager = instance.getServerManager();
this.configuration = instance.getConfiguration();
this.Xms = configuration.getString("instances.default-Xms");
this.Xmx = configuration.getString("instances.default-Xmx");
this.folder = setupFolder();
if (this.folder == null)
return;
prepare();
startup();
}
public WrappedServer(NpcBungee instance, String name, String template, boolean useSaved) {
this.name = name;
this.instance = instance;
this.template = template;
this.useSaved = useSaved;
this.serverManager = instance.getServerManager();
this.configuration = instance.getConfiguration();
this.Xms = configuration.getString("instances.default-Xms");
this.Xmx = configuration.getString("instances.default-Xmx");
this.folder = setupFolder();
if (this.folder == null)
return;
prepare();
startup();
}
//For starting a brand new server and checking if it exists.
public WrappedServer(NpcBungee instance, String name, String template, String Xmx, String Xms, boolean useSaved) {
this.serverManager = instance.getServerManager();
this.configuration = instance.getConfiguration();
this.useSaved = useSaved;
this.template = template;
this.instance = instance;
this.name = template;
this.Xmx = Xmx;
this.Xms = Xms;
this.folder = setupFolder();
if (this.folder == null)
return;
prepare();
startup();
}
public WrappedServer(NpcBungee instance, String name, String template, List<String> commands, boolean useSaved) {
this.commands.addAll(commands);
this.useSaved = useSaved;
this.template = template;
this.instance = instance;
this.name = name;
this.folder = setupFolder();
this.serverManager = instance.getServerManager();
this.configuration = instance.getConfiguration();
this.Xms = configuration.getString("instances.default-Xms");
this.Xmx = configuration.getString("instances.default-Xmx");
if (this.folder == null)
return;
this.processBuilder = setupProcessBuilder();
startup();
}
private ProcessBuilder setupProcessBuilder() {
File runningFolder = new File(serverManager.getRunningServerFolder(), name);
return new ProcessBuilder(commands).directory(runningFolder);
}
public OutputStream getOutputStream() {
return outputStream;
}
public InputStream getInputStream() {
return inputStream;
}
public InputStream getErrorStream() {
return errors;
}
public List<String> getCommands() {
return commands;
}
public long getStartedTime() {
return started;
}
public Process getProcess() {
return process;
}
public File getFolder() {
return folder;
}
public String getName() {
return name;
}
public String getXmx() {
return validate(Xmx, "default-Xmx");
}
/*
* This will cause the server to restart.
*/
public void setXmx(String xmx) {
Xmx = xmx;
restart(false);
}
public String getXms() {
return validate(Xms, "default-Xms");
}
/*
* This will cause the server to restart.
*/
public void setXms(String xms) {
Xms = xms;
restart(false);
}
public boolean canUseSaved() {
return useSaved;
}
public boolean isRunning() {
return isRunning;
}
public String getTemplate() {
return template;
}
public String getMotd() {
return motd;
}
public int getPort() {
return port;
}
public void setRunning(Boolean running) {
if (this.isRunning && !running) shutdown();
this.isRunning = running;
}
//Check that the output is logical.
private String validate(String input, String node) {
input = input.replaceAll("( |-)", "");
if (!input.contains("M") || !input.contains("G")) input = Integer.parseInt(input) + "M";
if (Integer.parseInt(input.replaceAll("(M|G)", "")) < 50) input = configuration.getString("instances." + node, "250M");
return "-" + input;
}
public File getJar() {
File[] jars = folder.listFiles(file -> file.getName().equalsIgnoreCase(configuration.getString("instances.jar-name", "spigot.jar")));
return (jars == null || jars.length <= 0) ? null : jars[0];
}
@SuppressWarnings("deprecation")
public ServerInfo getServerInfo() {
InetSocketAddress address = new InetSocketAddress("0.0.0.0", getPort());
ProxyServer proxy = instance.getProxy();
return proxy.getServers().values().stream()
.filter(info -> info.getAddress().equals(address))
.findFirst()
.orElse(proxy.constructServerInfo(name, address, getMotd(), false));
}
private File setupFolder() {
int spot = 1;
String nameCopy = name;
while (serverManager.containsName(nameCopy)) {
nameCopy = name + "-" + spot;
spot++;
}
this.name = nameCopy;
File runningFolder = new File(serverManager.getRunningServerFolder(), name);
if (runningFolder.exists()) {
instance.consoleMessage("There was already a server directory under the name: " + name);
if (serverManager.getInstances().stream().anyMatch(instance -> instance.getName().equalsIgnoreCase(name))) {
instance.consoleMessage("There was already a server running under the name: " + name + ". Aborting creation.");
//TODO maybe handle stopping the already running server?
return null;
}
//TODO handle deletion if the user doesn't want it for some reason.
// Recursively delete files since java can't delete non-empty folders
Utils.deleteDirectory(runningFolder);
}
File templateFolder = new File(serverManager.getTemplateFolder() + File.separator + template);
if (!templateFolder.exists() || templateFolder.listFiles() == null || templateFolder.listFiles().length <= 0) {
instance.consoleMessage("The template: \"" + template + "\" was empty or non existant.");
return null;
} else {
this.folder = templateFolder;
if (getJar() == null) {
instance.consoleMessage("The jar file for template: \"" + template + "\" was not found. Make sure the name matches what is in the config.yml");
return null;
}
}
if (serverManager.getInstances().size() >= configuration.getInt("instances.max-servers", 20)) {
instance.consoleMessage("The maximum amount of server instances has been reached!");
return null;
}
File[] files = folder.listFiles(file -> file.getName().endsWith(".properties"));
if (files == null || files.length <= 0) {
instance.consoleMessage("The template: \"" + template + "\" was missing a server.properties file.");
return null;
}
File savedFolder = new File(serverManager.getSavedFolder() + File.separator + name);
if (savedFolder.exists() && useSaved)
folder = savedFolder;
try {
Utils.copyDirectory(folder, runningFolder);
} catch (IOException exception) {
instance.exception(exception, "Failed to copy the directory of template: " + template);
return null;
}
setupPort();
findMotd();
return folder;
}
public void findMotd() {
if (folder == null)
return;
File[] files = folder.listFiles(file -> file.getName().endsWith(".properties"));
Properties properties = new Properties();
InputStream input = null;
try {
input = new FileInputStream(files[0]);
properties.load(input);
this.motd = properties.getProperty("motd").replaceAll(Pattern.quote("%server%"), name);
} catch (IOException exception) {
instance.exception(exception, "There was an error loading the properties of template: " + name);
} finally {
if (input != null) {
try {
input.close();
} catch (IOException exception) {
instance.exception(exception, "There was an error closing the InputStream of the properties reader for template: " + name);
}
}
}
}
public void setupPort() {
if (folder == null)
return;
File[] files = folder.listFiles(file -> file.getName().equals("server.properties"));
Properties properties = new Properties();
InputStream input = null;
Set<Entry<Object, Object>> set = new HashSet<>();
try {
input = new FileInputStream(files[0]);
properties.load(input);
set = properties.entrySet();
} catch (IOException exception) {
instance.exception(exception, "There was an error loading the properties while setting up port of template: " + name);
} finally {
if (input != null) {
try {
input.close();
} catch (IOException exception) {
instance.exception(exception, "There was an error closing the InputStream of the properties reader for template: " + name);
}
}
}
OutputStream output = null;
try {
output = new FileOutputStream(files[0]);
if (port <= 0)
this.port = Utils.findPort(configuration.getInt("instances.minimum-port", 25000), configuration.getInt("instances.maximum-port", 27000));
for (Entry<Object, Object> entry : set) {
if (entry.getKey().equals("server-port")) {
properties.setProperty("server-port", this.port + "");
} else if (entry.getKey().equals("query.port")) {
properties.setProperty("query.port", this.port + "");
} else properties.setProperty((String)entry.getKey(), (String) entry.getValue());
}
properties.store(output, null);
} catch (IOException exception) {
instance.exception(exception, "There was an error loading the properties of template: " + name);
} finally {
if (output != null) {
try {
output.close();
} catch (IOException exception) {
instance.exception(exception, "There was an error closing the OutputStream of the properties reader for template: " + name);
}
}
}
}
private void prepare() {
commands.add("java");
if (Xmx.matches("(\\-Xmx)(.*)")) {
commands.add(getXmx());
} else {
commands.add("-Xmx" + Xmx);
}
if (Xms.matches("(\\-Xms)(.*)")) {
commands.add(getXms());
} else {
commands.add("-Xms" + Xms);
}
boolean isWindows = System.getProperty("os.name").matches("(?i)(.*)(windows)(.*)");
if (isWindows) commands.add("-Djline.terminal=jline.UnsupportedTerminal");
for (String command : configuration.getStringList("instances.command-arguments")) {
commands.add(command);
}
commands.add("-jar");
commands.add(getJar().getName());
if (isWindows) commands.add("--nojline");
processBuilder = setupProcessBuilder();
/*linux screen support
File screen = new File(ServerManager.getRunScriptsFolder(), "start-screen.sh");
Object[] command = screen.exists() ? new String[]{"sh", ServerManager.getRunScriptsFolder() + "/start-screen.sh", name, ServerManager.getRunningServerFolder().getAbsolutePath(), getXmx(), getXms(), getJar().getName()} : (!getJar().getName().matches("^(?i)spigot.*\\.jar") ? new String[]{"screen", "-dmS", name, "java", getXmx(), getXms(), "-jar", getJar().getName()} : new String[]{"screen", "-dmS", name, "java", getXmx(), getXms(), "-Dcom.mojang.eula.agree=true", "-jar", getJar().getName()});
ProcessBuilder processBuilder = new ProcessBuilder(new String[0]);
processBuilder.command((String[])command);
processBuilder.directory(ServerManager.getRunningServerFolder());
getProcessRunner().queueProcess(this.getName(), processBuilder);
*/
}
private void startup() {
if (isRunning)
return;
setRunning(true);
serverManager.starting(this);
instance.consoleMessage("Starting up server " + name + "...");
InetSocketAddress address = new InetSocketAddress("0.0.0.0", getPort());
// If statement keeps the original motd
if (!instance.getProxy().getServers().containsKey(name)) instance.getProxy().getServers().put(name, instance.getProxy().constructServerInfo(name, address, motd, false));
try {
process = processBuilder.start();
inputStream = process.getInputStream();
errors = process.getErrorStream();
outputStream = process.getOutputStream();
serverManager.getProcesses().add(process);
//TODO make a system to read the console of this process
} catch (IOException exception) {
instance.exception(exception, "Failed to start server: " + name);
}
}
public void shutdown() {
if (!isRunning)
return;
this.isRunning = false;
instance.consoleMessage("Stopping server \"" + name + "\"...");
try {
outputStream.write("stop".getBytes());
outputStream.flush();
} catch (IOException exception) {
} finally {
try {
inputStream.close();
outputStream.flush();
outputStream.close();
} catch (IOException e) {
//e.printStackTrace();
}
}
inputStream = null;
outputStream = null;
instance.getProxy().getServers().remove(name);
System.gc();
kill();
instance.getProxy().getScheduler().schedule(instance, () -> {
File delete = new File(serverManager.getRunningServerFolder(), name);
Utils.deleteDirectory(delete);
// Just delete already fuck sakes.
delete.delete();
try {
Files.delete(delete.toPath());
} catch (IOException e) {}
}, 350, TimeUnit.MILLISECONDS);
}
public int getPlayerCount() {
return getServerInfo().getPlayers().size();
}
public void join(ProxiedPlayer player) {
if (isBlocking || !instance.getProxy().getServers().containsKey(name)) return;
player.connect(instance.getProxy().getServers().get(name), ServerConnectEvent.Reason.COMMAND);
}
public void setBlocking(boolean blocking) {
this.isBlocking = blocking;
}
private boolean autoSave = false;
public void setAutoSave(boolean autoSave) {
this.autoSave = autoSave;
}
public boolean isAutoSave() {
return autoSave;
}
public void shutdown(boolean saving) {
if (saving) {
try {
File savedFolder = new File(serverManager.getSavedFolder() + File.separator + name);
if (savedFolder.exists())
Utils.deleteDirectory(savedFolder);
File runningFolder = new File(serverManager.getRunningServerFolder(), name);
Utils.copyDirectory(runningFolder, savedFolder);
} catch (IOException exception) {
instance.exception(exception, "Failed to save the directory of server: " + template);
}
}
shutdown();
}
public void restart(boolean save) {
shutdown(save);
prepare();
startup();
}
public void kill() {
process.destroy();
}
}