-
Notifications
You must be signed in to change notification settings - Fork 0
/
GlobalServer.java
355 lines (316 loc) · 13.2 KB
/
GlobalServer.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
package de.cubeside.globalserver;
import de.cubeside.globalserver.command.AccountAddAllowedChannelCommand;
import de.cubeside.globalserver.command.AccountInfoCommand;
import de.cubeside.globalserver.command.AccountRemoveAllowedChannelCommand;
import de.cubeside.globalserver.command.AccountSetPasswordCommand;
import de.cubeside.globalserver.command.AccountSetRestrictedCommand;
import de.cubeside.globalserver.command.AccountsCommand;
import de.cubeside.globalserver.command.CreateAccountCommand;
import de.cubeside.globalserver.command.HelpCommand;
import de.cubeside.globalserver.command.ListCommand;
import de.cubeside.globalserver.command.ServersCommand;
import de.cubeside.globalserver.command.StopCommand;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.io.IoBuilder;
import org.yaml.snakeyaml.TypeDescription;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
public class GlobalServer {
static {
System.setProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager");
PrintStream logger = IoBuilder.forLogger("System.out").setLevel(Level.INFO).buildPrintStream();
PrintStream errorLogger = IoBuilder.forLogger("System.err").setLevel(Level.ERROR).buildPrintStream();
System.setOut(logger);
System.setErr(errorLogger);
}
public final static Logger LOGGER = LogManager.getLogger("Server");
private ServerListener listener;
private static Console console;
private boolean running;
private File configFile = new File("config.yml");
private Yaml configYaml;
private ServerConfig serverConfig;
private HashMap<String, ClientConfig> clientConfigs;
private ArrayList<ClientConnection> pendingConnections;
private ArrayList<ClientConnection> connections;
private HashMap<String, ServerCommand> commands;
private final Object sync = new Object();
public GlobalServer() {
console = new JLineConsole(this);
LOGGER.info("Starting GlobalServer...");
Constructor constructor = new Constructor(ServerConfig.class);
TypeDescription serverConfigDescription = new TypeDescription(ServerConfig.class);
serverConfigDescription.addPropertyParameters("clientConfigs", ClientConfig.class);
constructor.addTypeDescription(serverConfigDescription);
configYaml = new Yaml(constructor);
if (configFile.exists()) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(configFile), Charset.forName("UTF-8")))) {
serverConfig = configYaml.loadAs(reader, ServerConfig.class);
saveConfig();
} catch (Exception e) {
LOGGER.error("Could not parse config!", e);
}
}
if (serverConfig == null) {
serverConfig = new ServerConfig();
LOGGER.info("Generating new config!");
if (!configFile.exists()) {
saveConfig();
}
}
clientConfigs = new HashMap<>();
for (ClientConfig cc : serverConfig.getClientConfigs()) {
clientConfigs.put(cc.getLogin(), cc);
}
pendingConnections = new ArrayList<>();
connections = new ArrayList<>();
commands = new HashMap<>();
addCommand(new HelpCommand());
addCommand(new StopCommand());
addCommand(new ServersCommand());
addCommand(new ListCommand());
addCommand(new AccountsCommand());
addCommand(new AccountInfoCommand());
addCommand(new CreateAccountCommand());
addCommand(new AccountSetPasswordCommand());
addCommand(new AccountSetRestrictedCommand());
addCommand(new AccountAddAllowedChannelCommand());
addCommand(new AccountRemoveAllowedChannelCommand());
}
public Collection<ServerCommand> getCommands() {
return Collections.unmodifiableCollection(commands.values());
}
public Collection<String> getCommandNames() {
return Collections.unmodifiableCollection(commands.keySet());
}
public ServerCommand getCommand(String name) {
return commands.get(name.toLowerCase());
}
private void addCommand(ServerCommand command) {
commands.put(command.getCommand().toLowerCase().trim(), command);
}
public void addAccount(String login, String password) {
Objects.requireNonNull(login, "login must not be null");
Objects.requireNonNull(password, "password must not be null");
if (clientConfigs.containsKey(login)) {
throw new IllegalArgumentException("Login name in use: " + login);
}
ClientConfig cfg = new ClientConfig(login, password, false, new HashSet<>());
clientConfigs.put(login, cfg);
serverConfig.getClientConfigs().add(cfg);
saveConfig();
}
public void saveConfig() {
String output = configYaml.dumpAsMap(serverConfig);
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(configFile), Charset.forName("UTF-8")))) {
writer.write(output);
} catch (Exception e) {
LOGGER.error("Could not save config!", e);
}
}
public Collection<ClientConfig> getAccounts() {
return Collections.unmodifiableCollection(clientConfigs.values());
}
public ClientConfig getAccount(String name) {
return clientConfigs.get(name);
}
public List<ClientConnection> getConnections() {
return Collections.unmodifiableList(connections);
}
public void run() {
running = true;
int port = serverConfig.getPort();
if (port <= 0) {
port = 25701;
serverConfig.setPort(port);
saveConfig();
}
try {
listener = new ServerListener(this, port);
listener.start();
} catch (IOException e) {
LOGGER.error("Could not bind to " + port + ": " + e.getMessage());
return;
}
synchronized (sync) {
while (running) {
for (ClientConnection cc : connections) {
cc.sendPing();
}
try {
sync.wait(20000);
} catch (InterruptedException e) {
// ignore
}
}
}
}
public static void main(String[] args) {
new GlobalServer().run();
LogManager.shutdown();
}
public void processCommand(String line) {
line = line.trim();
if (line.length() == 0) {
return;
}
int firstSpace = line.indexOf(' ');
String cmd = (firstSpace < 0 ? line : line.substring(0, firstSpace)).toLowerCase().trim();
String args = firstSpace < 0 ? "" : line.substring(firstSpace + 1);
synchronized (sync) {
ServerCommand command = commands.get(cmd);
if (command != null) {
ArrayList<String> splitArgs = new ArrayList<>();
for (String s : args.trim().split(" ++")) {
String s2 = s.trim();
if (s2.length() > 0) {
splitArgs.add(s2);
}
}
command.execute(this, new ArgsParser(splitArgs.toArray(new String[splitArgs.size()])));
} else {
LOGGER.info("Unknown command: " + cmd);
}
}
}
public void stopServer() {
listener.shutdown();
synchronized (sync) {
running = false;
sync.notify();
for (ClientConnection cc : new ArrayList<>(pendingConnections)) {
cc.closeConnection();
}
pendingConnections.clear();
for (ClientConnection cc : new ArrayList<>(connections)) {
cc.closeConnection();
}
connections.clear();
}
console.stop();
}
public void addPendingConnection(ClientConnection connection) {
synchronized (sync) {
pendingConnections.add(connection);
}
}
public void removeConnection(ClientConnection connection) {
synchronized (sync) {
boolean wasOnline = connections.remove(connection);
pendingConnections.remove(connection);
if (wasOnline) {
processServerOffline(connection);
}
}
}
public ClientConfig processLogin(ClientConnection connection, String account, byte[] password, byte[] saltServer, byte[] saltClient) throws IOException {
synchronized (sync) {
ClientConfig config = clientConfigs.get(account);
if (config == null || !config.checkPassword(password, saltServer, saltClient)) {
LOGGER.info("Login failed for '" + account + "'.");
pendingConnections.remove(connection);
connection.sendLoginResultAndActivateEncryption(false, null);
return null;
}
LOGGER.info("Login successfull for '" + account + "'.");
for (ClientConnection cc : connections) {
if (cc.getAccount().equals(account)) {
cc.closeConnection();
removeConnection(cc);
break;
}
}
connection.setClient(config);
pendingConnections.remove(connection);
connections.add(connection);
connection.sendLoginResultAndActivateEncryption(true, config);
// send online servers
for (ClientConnection cc : connections) {
if (cc != connection) {
cc.sendServerOnline(account);
connection.sendServerOnline(cc.getAccount());
Collection<OnlinePlayer> players = cc.getPlayers();
for (OnlinePlayer e : players) {
connection.sendPlayerOnline(cc.getAccount(), e.getUuid(), e.getName(), e.getJoinTime());
}
}
}
return config;
}
}
public void processPlayerOnline(ClientConnection connection, UUID uuid, String name, long joinTime) {
synchronized (sync) {
if (connection.addPlayer(uuid, name, joinTime)) {
for (ClientConnection cc : connections) {
if (cc != connection) {
cc.sendPlayerOnline(connection.getAccount(), uuid, name, joinTime);
}
}
}
}
}
public void processPlayerOffline(ClientConnection connection, UUID uuid) {
synchronized (sync) {
if (connection.removePlayer(uuid)) {
for (ClientConnection cc : connections) {
if (cc != connection) {
cc.sendPlayerOffline(connection.getAccount(), uuid);
}
}
}
}
}
private void processServerOffline(ClientConnection connection) {
for (ClientConnection cc : connections) {
if (cc != connection) {
cc.sendServerOffline(connection.getAccount());
}
}
}
public void processData(ClientConnection connection, String channel, UUID targetUuid, String targetServer, byte[] data, boolean allowRestricted, boolean toAllUnrestrictedServers) {
boolean fromRestricted = connection.getClient().isRestricted();
if (!fromRestricted || connection.getClient().getAllowedChannels().contains(channel)) {
synchronized (sync) {
for (ClientConnection cc : connections) {
if (cc != connection) {
boolean toRestricted = cc.getClient().isRestricted();
if (!toRestricted || cc.getClient().getAllowedChannels().contains(channel)) {
boolean explicitServer = targetServer != null && cc.getAccount().equals(targetServer);
if (targetServer == null || explicitServer) {
boolean explicitPlayer = targetUuid != null && cc.hasPlayer(targetUuid);
if (toAllUnrestrictedServers || targetUuid == null || explicitPlayer) {
if (allowRestricted || (!fromRestricted && !toRestricted) || explicitServer || explicitPlayer) {
cc.sendData(connection.getAccount(), channel, targetUuid, targetServer, data);
}
}
}
}
}
}
}
}
}
public static Console getConsole() {
return console;
}
}