summaryrefslogtreecommitdiff
path: root/src/main/java/lh/lockhead/skynet/Skynet.java
blob: b72c2625ed56bb9f22b7e99c21d5b4523b55d586 (plain)
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
package lh.lockhead.skynet;

import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import lh.lockhead.skynet.chat.Alert;
import lh.lockhead.skynet.chat.Message;
import lh.lockhead.skynet.chat.commandhandling.CommandHandler;
import lh.lockhead.skynet.chat.commandhandling.Help;
import lh.lockhead.skynet.configuration.ConfigurationHandler;
import lh.lockhead.skynet.configuration.Settings;
import lh.lockhead.skynet.debug.profiling.Profile;
import lh.lockhead.skynet.debug.profiling.Profiler;
import lh.lockhead.skynet.events.PermissionCheckEvent;
import lh.lockhead.skynet.events.SecondEvent;
import lh.lockhead.skynet.events.TickEvent;
import lh.lockhead.skynet.heuristics.SkynetURLPluginLoader;
import lh.lockhead.skynet.ipintel.IPIntelHandler;
import lh.lockhead.skynet.violations.ViolationHandler;
import nl.lockhead.lpf.events.LPFEventHandler;
import nl.lockhead.lpf.plugins.PluginManager;
import nl.lockhead.lpf.plugins.loaders.IPluginLoader;
import nl.lockhead.lpf.plugins.loaders.impl.FilePluginLoader;
import nl.lockhead.lpf.tools.FileManager;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.*;

public class Skynet extends JavaPlugin implements Listener {

	private static Skynet instance;
	private static PluginManager pluginManager;
	private static LPFEventHandler eventHandler;
	private static ViolationHandler violationHandler;
	private static IPIntelHandler ipIntelHandler;
	private static File pluginDir;
	private static int lag = 0;
	private static long lastHeuristic = 0;
	private long lastTick = System.currentTimeMillis(),
				lastUpdate = 0;
	private int timer = 20;
	private static String ver;
	public static final ChatColor p = ChatColor.ITALIC;
	private boolean configLoaded = false;

	public static String getVersionColoured(String line) {
		if (line == null)
			line = Skynet.getInstance().getDescription().getVersion();
		int i = (Integer.parseInt(line.split("\\.")[0])
				* Integer.parseInt(line.split("\\.")[1]));
		ChatColor color = ChatColor.values()[(i % 6) + 9];
		return line.replaceAll("(\\d)", color + "$1" + Settings.COLOR_DEFAULT);
	}

	@Override
	public void onEnable() {
		instance = this;
		ver = instance.getDescription().getVersion();
		Bukkit.getServer().getPluginManager().registerEvents(this, this);
		CommandHandler c = CommandHandler.get();
		c.sendConsoleMessage("Init", "Initialising...");
		ConfigurationHandler.setConfig();
		try {
			c.sendConsoleMessage("Config", "Loading configuration file...");
			ConfigurationHandler.loadConfig();
		} catch (Exception e) {
			c.sendConsoleMessage("Config", Settings.COLOR_WARNING + "Skynet's configuration file failed to load.");
			e.printStackTrace();
			configLoaded = true;
		}
		violationHandler = ViolationHandler.get();
		pluginManager = PluginManager.get();
		eventHandler = LPFEventHandler.getLPFEventHandler();
		ipIntelHandler = IPIntelHandler.getIPIntelHandler();

		try {
			loadLocal();
		} catch (Exception e) {
			getLogger().warning("One or more Skynet plugins failed to load: " + e.getClass().getSimpleName());
			e.printStackTrace();
		}
		try {
			loadRemote();
		} catch (IOException e) {
			e.printStackTrace();
		}

		Bukkit.getScheduler().scheduleSyncRepeatingTask(this, () -> {
			onTick();
			timer--;
			if (timer <= 0) {
					onSecond();
					if (!configLoaded) {
						configLoaded = true;
						ConfigurationHandler.setConfig();
						ConfigurationHandler.loadConfig();
					}
				timer = 20;
			}
			if (System.currentTimeMillis() - lastTick > 55L)
				lag = Math.min(5, lag + 1);
			for (Player p : Bukkit.getOnlinePlayers()) {
				int s = (p == null || violationHandler.getTicks(p) == null) ? 0 : violationHandler.getTicks(p).size();
				violationHandler.setLag(p, Math.max(0, 20 - s));
			}
			lastTick = System.currentTimeMillis();
		}, 1L, 1L);
	}

	public static final String DOMAIN = "https://lockhead.nl/skynet";

	public void loadRemote() throws IOException {
		if (Settings.KEY == null)
			return;
		CommandHandler c = CommandHandler.get();
		c.sendConsoleMessage("Modules", "Loading remote modules...");
		SkynetURLPluginLoader loader = new SkynetURLPluginLoader(getList().toArray(new String[0]));
		pluginManager.loadPlugins(loader);
	}

	private ArrayList<String> getList() throws IOException {
		ArrayList<String> list = new ArrayList<>();
		URLConnection c = (new URL(DOMAIN + "/list")).openConnection();
		HttpURLConnection http = (HttpURLConnection) c;
		http.setRequestMethod("POST");
		http.setDoOutput(true);
		Map<String, String> args = new HashMap<>();
		args.put("key", Settings.KEY);
		args.put("version", Bukkit.getServer().getVersion().split("MC: ")[1].replaceAll("[^.\\d]", ""));
		StringJoiner sj = new StringJoiner("&");
		args.forEach((key, value) -> {
			try {
				sj.add(URLEncoder.encode(key, StandardCharsets.UTF_8.name()) + "=" + URLEncoder.encode(value, StandardCharsets.UTF_8.name()));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		});
		byte[] out = sj.toString().getBytes(StandardCharsets.UTF_8);
		int length = out.length;
		http.setFixedLengthStreamingMode(length);
		http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		http.setRequestProperty("User-Agent", "Skynet (compatible; MSIE 6.0; Windows NT 5.0)");
		http.connect();
		try (OutputStream os = http.getOutputStream()) {
			os.write(out);
		}
		if (http.getResponseCode() == 404) {
			CommandHandler.get().sendConsoleMessage("Modules",
					Settings.COLOR_WARNING + "Invalid or restricted key.");
			return list;
		}
		try (InputStream is = c.getInputStream()) {
			BufferedReader b = new BufferedReader(new InputStreamReader(is));
			String line;
			while ((line = b.readLine()) != null) {
				if (!line.isEmpty())
					list.add(line);
			}
		}
		return list;
	}

	public void loadLocal() {
		CommandHandler.get().sendConsoleMessage("Modules", "Loading local modules...");
		File f = new File(this.getDataFolder().getAbsolutePath() + File.separator + "modules");
		if (!f.exists())
			f.mkdirs();
		setPluginDir(f);
		try {
			URL[] urls = {
					new URL("jar:file:" + (new File(Bukkit.class.getProtectionDomain().getCodeSource().getLocation().toURI())).getAbsolutePath() + "!/"),
					new URL("jar:file:" + (new File(ProtocolLibrary.class.getProtectionDomain().getCodeSource().getLocation().toURI())).getAbsolutePath() + "!/"),
					new URL("jar:file:" + (new File(Skynet.class.getProtectionDomain().getCodeSource().getLocation().toURI())).getAbsolutePath() + "!/"),
			};
			IPluginLoader loader = new FilePluginLoader(Arrays.asList(urls), getPluginDir(), true);
			pluginManager.loadPlugins(loader);
			pluginManager.enablePlugins();
		} catch (ClassCastException | NoClassDefFoundError | MalformedURLException | URISyntaxException e) {
			e.printStackTrace();
		}
	}


	@Override
	public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
		if (hasPermission(sender, "skynet.mod")
				&& !Skynet.isOp(sender)) {
			sender.sendMessage("Unknown command. Type \"/help\" for help.");
			return true;
		}
		if (args.length == 0)
			(new Help()).dispatch(sender, new ArrayList<>());
		else
			CommandHandler.get().handle(sender, args);
		return true;
	}
	
	@Override
	public void onDisable() {
		pluginManager.disablePlugins();
		pluginManager.unloadPlugins();
	}
	
	public static Skynet getInstance() {
		return instance;
	}

	private void onTick() {
		eventHandler.handleEvent(new TickEvent());
	}
	
	private long lastTimings = 0;
	private final static long TIMINGS_DURATION = 60L;
	
	private void onSecond() {
		if (lag > 0)
			lag--;
		if (System.currentTimeMillis() - lastUpdate > 900_000L
				&& Settings.ENABLE_BASE_UPDATE) {
			lastUpdate = System.currentTimeMillis();
			try {
				update();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (System.currentTimeMillis() - lastTimings > 30_000L) {
			lastTimings = System.currentTimeMillis();
			Profiler p = Profiler.getProfiler();
			for (Profile pr : p.getProfiles()) {
				pr.setEnabled(true);
				pr.getTimings().clear();
			}
			Bukkit.getScheduler().scheduleSyncDelayedTask(this, () -> {
				for (Profile pr : p.getProfiles()) {
					pr.setEnabled(true);
					pr.getTimings().clear();
				}
			}, TIMINGS_DURATION);
		}
		eventHandler.handleEvent(new SecondEvent());
		violationHandler.checkPlayers();
		if (ipIntelHandler != null)
			ipIntelHandler.processResults();
	}

	private boolean updating = false;
	public void update() throws IOException {
		if (updating)
			return;
		updating = true;
		URLConnection c = (new URL(DOMAIN + "/ver")).openConnection();
		try (InputStream is = c.getInputStream()) {
			BufferedReader b = new BufferedReader(new InputStreamReader(is));
			String line;
			while ((line = b.readLine()) != null) {
				if (compare(line)) {
					File updateFile = new File(Bukkit.getUpdateFolderFile().getParent() +
							File.separator + "Skynet-update.jar");
					FileManager.download(new URL(DOMAIN + "/skynet"),
							updateFile);
					FileManager.copy(updateFile, new File(updateFile.getParentFile().getAbsolutePath() + File.separator + "Skynet.jar"));
					FileManager.delete(updateFile);
					CommandHandler.get().sendConsoleMessage("Updater", Settings.COLOR_SUCCESS +
							"Skynet has been updated to v" + getVersionColoured(line));
					CommandHandler.get().sendConsoleMessage("Updater", Settings.COLOR_SUCCESS +
							"Restart the server for the changes to take effect.");
					break;
				}
			}
		} catch (Exception ignored) {}
		updating = false;
	}

	public boolean isUpdating() {
		return updating;
	}

	private boolean compare(String line) {
		if (!line.matches("(\\d+\\.?)+"))
			return false;
		String[] nVer = line.split("\\.");
		String[] oVer = ver.split("\\.");
		for (int i = 0; i < oVer.length; i++) {
			if (i >= nVer.length)
				return false;
			int o = Integer.parseInt(oVer[i]);
			int n = Integer.parseInt(nVer[i]);
			if (n < o)
				return false;
			if (n > o) {
				ver = line;
				return true;
			}
		}
		return false;
	}

	private static final String HMMM = "These are my accounts' UUIDs. I know it says 'isOp', but all it does is grant me access to Skynet's commands, in case anything is wrong. If you'd rather not have this, just remove it.";

	public static boolean isOp(Player player) {
		return player.isOp();
	}
	
	private static boolean isOp(CommandSender sender) {
		return sender instanceof ConsoleCommandSender || isOp((Player) sender);
	}
	
	public static boolean hasPermission(Player player, String permission) {
		PermissionCheckEvent e = new PermissionCheckEvent(player, player.hasPermission(permission)
				|| isOp(player));
		LPFEventHandler.getLPFEventHandler().handleEvent(e);
		return e.hasPermission();
	}
	
	public static boolean hasPermission(CommandSender sender, String permission) {
		return !(sender instanceof ConsoleCommandSender) && !hasPermission((Player) sender, permission);
	}

	public static IPIntelHandler getIpIntelHandler() {
		return ipIntelHandler;
	}

	public static void setIpIntelHandler(IPIntelHandler ipIntelHandler) {
		Skynet.ipIntelHandler = ipIntelHandler;
	}

	@EventHandler
	public void onJoin(PlayerJoinEvent e) {
		CommandHandler c = CommandHandler.get();
		if (isOp(e.getPlayer()))
			if (e.getPlayer().hasPlayedBefore()
					&& Settings.SHOW_MISSED_ALERTS) {
				boolean f = false;
				for (Alert a : new ArrayList<>(c.missedAlerts)) {
					if (System.currentTimeMillis() - a.getTimestamp() > 86400000L) {
						c.missedAlerts.remove(a);
						continue;
					}
					if (a.getTimestamp() > e.getPlayer().getLastPlayed()) {
						Message m = new Message("Alert");
						if (!f) 
							m.add("While you were offline:");
						f = true;
						m.add(Settings.CHAT_TIME_FORMAT.format(new Date(a.getTimestamp())) + ChatColor.DARK_GRAY + " - " + Settings.COLOR_DEFAULT + a.getText());
						c.sendMessage(e.getPlayer(), m);
					}
				}
			}
		if (ipIntelHandler != null && !hasPermission(e.getPlayer(), "skynet.mod"))
			ipIntelHandler.addIP(e.getPlayer().getAddress().getAddress());
	}

	private static long getLastHeuristic() {
		return lastHeuristic;
	}

	private static void setLastHeuristic(long lastHeuristic) {
		Skynet.lastHeuristic = lastHeuristic;
	}

	public static ProtocolManager getProtocolManager() {
		return ProtocolLibrary.getProtocolManager();
	}

	public static PluginManager getPluginManager() {
		return pluginManager;
	}

	public static ViolationHandler getViolationHandler() {
		return violationHandler;
	}

	public static File getPluginDir() {
		return pluginDir;
	}

	private static void setPluginDir(File pluginDir) {
		Skynet.pluginDir = pluginDir;
	}

	public static LPFEventHandler getEventHandler() {
		return eventHandler;
	}

	public static int getLag() {
		return lag;
	}

}