-
Notifications
You must be signed in to change notification settings - Fork 3
/
Helper.java
408 lines (365 loc) · 13.5 KB
/
Helper.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
/*
* D3Backend
* Copyright (C) 2015 - 2017 Dries007 & Double Door Development
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.doubledoordev.backend.util;
import com.flowpowered.nbt.Tag;
import com.flowpowered.nbt.stream.NBTInputStream;
import com.flowpowered.nbt.stream.NBTOutputStream;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import net.doubledoordev.backend.Main;
import net.doubledoordev.backend.server.Server;
import net.doubledoordev.backend.util.methodCaller.IMethodCaller;
import net.doubledoordev.backend.util.methodCaller.WebSocketCaller;
import org.apache.logging.log4j.util.Strings;
import org.glassfish.grizzly.websockets.WebSocket;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.net.*;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import static net.doubledoordev.backend.util.Constants.GSON;
import static net.doubledoordev.backend.util.Constants.JSONPARSER;
import static net.doubledoordev.backend.util.Settings.SETTINGS;
/**
* Public static helper methods
* Passed to template engine
*
* @author Dries007
*/
@SuppressWarnings("UnusedDeclaration")
public class Helper
{
private static final Map<String, String> UUID_USERNMAME_MAP = new HashMap<>();
private static final SimpleDateFormat BAN_SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static final SimpleDateFormat HTML_TIME_FORMAT = new SimpleDateFormat("'<b>'HH:mm'</b>':ss");
public static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("#.##");
private static final char[] symbols = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
private static final int MAX_REDIRECTS = 25;
private Helper()
{
}
public static String getStackTrace(final Throwable throwable)
{
final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw, true);
throwable.printStackTrace(pw);
return sw.getBuffer().toString();
}
/**
* Checks to see if a port/hostname combo is available through opening a socked and closing it again
*
* @param hostname the hostname, if null this is bypassed
* @param port the port to check
* @return true if available
*/
public static boolean isPortAvailable(String hostname, int port)
{
if (port == -1) return false;
try
{
ServerSocket socket = new ServerSocket();
socket.bind(hostname == null || hostname.length() == 0 ? new InetSocketAddress(port) : new InetSocketAddress(hostname, port));
socket.close();
return true;
}
catch (IOException e)
{
return false;
}
}
public static String randomString(int length)
{
return new String(randomCharArray(length));
}
public static char[] randomCharArray(int length)
{
if (length < 1) throw new IllegalArgumentException("length < 1: " + length);
final char[] buf = new char[length];
for (int idx = 0; idx < buf.length; ++idx) buf[idx] = symbols[Constants.RANDOM.nextInt(symbols.length)];
return buf;
}
/**
* @return set of all MC versions we can grab from mojang
*/
public static Collection<String> getAllMCVersions()
{
return Cache.getMcVersions().keySet();
}
public static String getForgeVersionJson()
{
return GSON.toJson(Cache.getForgeVersions());
}
public static int getTotalRamUsed()
{
int total = 0;
for (Server server : Settings.SETTINGS.getOnlineServers()) total += server.getJvmData().ramMax;
return total;
}
public static int getTotalDiskspaceUsed()
{
int total = 0;
for (Server server : Settings.SETTINGS.getServers()) total += server.getDiskspaceUse()[2];
return total;
}
public static String getNowInBanFormat()
{
return BAN_SIMPLE_DATE_FORMAT.format(new Date());
}
public static Tag<?> readRawNBT(File file, boolean compressed)
{
Tag<?> tag = null;
try
{
InputStream is = new FileInputStream(file);
NBTInputStream ns = new NBTInputStream(is, compressed);
try
{
tag = ns.readTag();
}
finally
{
try
{
ns.close();
}
catch (IOException ignored)
{
}
}
}
catch (Exception ignored)
{
}
return tag;
}
public static void writeRawNBT(File file, boolean compressed, Tag<?> tag)
{
try
{
OutputStream is = new FileOutputStream(file);
NBTOutputStream ns = new NBTOutputStream(is, compressed);
try
{
ns.writeTag(tag);
}
finally
{
try
{
ns.close();
}
catch (IOException ignored)
{
}
}
}
catch (Exception ignored)
{
}
}
public static String getUsernameFromUUID(String uuid)
{
if (uuid.endsWith(".dat")) uuid = uuid.substring(0, uuid.lastIndexOf('.'));
uuid = uuid.replace("-", "");
if (uuid.length() != 32) return null;
if (!UUID_USERNMAME_MAP.containsKey(uuid))
{
// This variable is used so it will only ever check any UUID once. Even if its name is null.
String name = null;
try
{
URL url = new URL(String.format("https://api.mojang.com/user/profiles/%s/names", uuid));
URLConnection uc = url.openConnection();
uc.setUseCaches(false);
uc.setDefaultUseCaches(false);
uc.addRequestProperty("User-Agent", "minecraft");
InputStream in = uc.getInputStream();
name = JSONPARSER.parse(new InputStreamReader(in)).getAsJsonArray().get(0).getAsJsonObject().get("name").getAsString();
in.close();
}
catch (Exception ignored)
{
}
UUID_USERNMAME_MAP.put(uuid, name);
}
return UUID_USERNMAME_MAP.get(uuid);
}
public static boolean usingHttps()
{
return !(Strings.isBlank(SETTINGS.certificatePath) || Strings.isBlank(SETTINGS.certificatePass));
}
public static boolean hasUpdate()
{
return Cache.hasUpdate();
}
public static String getUpdateVersion()
{
return Cache.getUpdateVersion();
}
public static int getGlobalPlayers()
{
int p = 0;
for (Server server : SETTINGS.getOnlineServers())
{
p += server.getOnlinePlayers();
}
return p;
}
/**
* Default arguments: "%2d days, ", "%2d hours, ", "%2d min and", "%2 sec"
*/
public static String getOnlineTime(long startTime, String dayString, String hoursString, String minuteString, String secondsString)
{
StringBuilder sb = new StringBuilder(30);
long time = System.currentTimeMillis() - startTime;
boolean alwaysShowNext = false;
if (Strings.isNotBlank(dayString) && time > 1000 * 60 * 60 * 24)
{
alwaysShowNext = true;
sb.append(String.format(dayString, time / (1000 * 60 * 60 * 24)));
time %= 1000 * 60 * 60 * 24;
}
if (Strings.isNotBlank(hoursString) && (alwaysShowNext || time > 1000 * 60 * 60))
{
alwaysShowNext = true;
sb.append(String.format(hoursString, time / (1000 * 60 * 60)));
time %= 1000 * 60 * 60;
}
if (Strings.isNotBlank(minuteString) && (alwaysShowNext || time > 1000 * 60))
{
sb.append(String.format(minuteString, time / (1000 * 60)));
time %= 1000 * 60;
}
sb.append(String.format(secondsString, time / (1000)));
return sb.toString();
}
public static String getOnlineTime(String dayString, String hoursString, String minuteString, String secondsString)
{
return getOnlineTime(Main.STARTTIME, dayString, hoursString, minuteString, secondsString);
}
public static String getReadOnlyProperties()
{
JsonArray array = new JsonArray();
if (SETTINGS.fixedPorts) array.add(new JsonPrimitive(Server.SERVER_PORT));
if (SETTINGS.fixedIP) array.add(new JsonPrimitive(Server.SERVER_IP));
array.add(new JsonPrimitive(Server.QUERY_PORT));
array.add(new JsonPrimitive(Server.QUERY_ENABLE));
return array.toString();
}
public static IMethodCaller invokeWithRefectionMagic(WebSocket caller, Object instance, String methodName, ArrayList<String> args) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
{
for (java.lang.reflect.Method method : instance.getClass().getDeclaredMethods())
{
if (!method.getName().equalsIgnoreCase(methodName)) continue; // Name match
boolean userMethodCaller = method.getParameterTypes().length != 0 && method.getParameterTypes()[0].isAssignableFrom(IMethodCaller.class); // See if first type is IMethodCaller
int size = args.size() + (userMethodCaller ? 1 : 0); // deferments wanted parameter length
if (method.getParameterTypes().length == size) // parameter length match
{
try
{
Object parms[] = new Object[size];
if (userMethodCaller) parms[0] = new WebSocketCaller(caller);
for (int i = userMethodCaller ? 1 : 0; i < method.getParameterTypes().length; i++)
{
parms[i] = TypeHellhole.convert(method.getParameterTypes()[i], args.get(i - (userMethodCaller ? 1 : 0)));
}
method.invoke(instance, parms);
return userMethodCaller ? (IMethodCaller) parms[0] : null;
}
catch (ClassCastException ignored)
{
ignored.printStackTrace();
}
}
}
throw new NoSuchMethodException(methodName);
}
public static String getVersionString()
{
return Main.version;
}
public static String formatDate(long time)
{
return SIMPLE_DATE_FORMAT.format(time);
}
public static String getServerTime()
{
return HTML_TIME_FORMAT.format(new Date());
}
public static String stripColor(String txt)
{
return txt.replaceAll("(?i)\u00a7[0-9a-fk-or]", "");
}
public static URL getFinalURL(String url) throws IOException
{
for (int i = 0; i < MAX_REDIRECTS; i++)
{
HttpURLConnection con = null;
try
{
con = (HttpURLConnection) new URL(url).openConnection();
con.setInstanceFollowRedirects(false);
con.connect();
if (con.getHeaderField("Location") == null)
{
return new URL(url.replace("?cookieTest=1", ""));
}
url = con.getHeaderField("Location");
}
catch (IOException e)
{
return new URL(url.replace("?cookieTest=1", ""));
}
finally
{
if (con != null) con.disconnect();
}
}
throw new IOException("Redirect limit (" + MAX_REDIRECTS + ") exceeded on url: " + url);
}
public static void doWebMethodCall(WebSocket socket, String text, Object o)
{
try
{
JsonObject object = JSONPARSER.parse(text).getAsJsonObject();
String name = object.get("method").getAsString();
ArrayList<String> args = new ArrayList<>();
if (object.has("args")) for (JsonElement arg : object.getAsJsonArray("args")) args.add(arg.getAsString());
IMethodCaller methodCaller = Helper.invokeWithRefectionMagic(socket, o, name, args);
if (methodCaller == null)
{
WebSocketHelper.sendOk(socket);
socket.close();
}
}
catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e)
{
WebSocketHelper.sendError(socket, e);
socket.close();
}
}
public static String getRelativeString(File root, File target)
{
if (root.equals(target)) return ".";
return target.toString().substring(root.toString().length() + 1).replace('\\', '/');
}
}