diff --git a/pom.xml b/pom.xml index ab617a0..826f49b 100755 --- a/pom.xml +++ b/pom.xml @@ -95,6 +95,10 @@ + + org.bstats + net.coreprotect + com.zaxxer net.coreprotect @@ -221,6 +225,12 @@ 1.21.1-R0.1-SNAPSHOT provided + + org.bstats + bstats-bukkit + 3.0.2 + compile + com.fastasyncworldedit FastAsyncWorldEdit-Core diff --git a/src/main/java/net/coreprotect/services/PluginInitializationService.java b/src/main/java/net/coreprotect/services/PluginInitializationService.java index e0bc4df..b5612b3 100644 --- a/src/main/java/net/coreprotect/services/PluginInitializationService.java +++ b/src/main/java/net/coreprotect/services/PluginInitializationService.java @@ -2,6 +2,7 @@ package net.coreprotect.services; import java.io.File; +import org.bstats.bukkit.Metrics; import org.bukkit.Bukkit; import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.java.JavaPlugin; @@ -72,6 +73,9 @@ public class PluginInitializationService { // Start background services startBackgroundServices(plugin); + + // Start metrics + enableMetrics(plugin); } return start; @@ -149,4 +153,19 @@ public class PluginInitializationService { // Start consumer Consumer.startConsumer(); } + + /** + * Enables metrics reporting + * + * @param plugin + * The CoreProtect plugin instance + */ + private static void enableMetrics(JavaPlugin plugin) { + try { + new Metrics(plugin, 2876); + } + catch (Exception e) { + // Failed to connect to bStats server or something else went wrong + } + } } diff --git a/src/main/java/net/coreprotect/thread/NetworkHandler.java b/src/main/java/net/coreprotect/thread/NetworkHandler.java index bb0050f..06c61ad 100755 --- a/src/main/java/net/coreprotect/thread/NetworkHandler.java +++ b/src/main/java/net/coreprotect/thread/NetworkHandler.java @@ -15,6 +15,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.HashMap; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Optional; @@ -22,6 +23,7 @@ import java.util.Set; import java.util.TreeMap; import java.util.stream.Stream; +import org.bukkit.Bukkit; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; @@ -41,6 +43,7 @@ public class NetworkHandler extends Language implements Runnable { private boolean translate = true; private static String latestVersion = null; private static String latestEdgeVersion = null; + private static String donationKey = null; public NetworkHandler(boolean startup, boolean background) { this.startup = startup; @@ -55,9 +58,86 @@ public class NetworkHandler extends Language implements Runnable { return latestEdgeVersion; } + public static String donationKey() { + return donationKey; + } + @Override public void run() { try { + try { + boolean keyValidated = true; + String keyConfig = Config.getGlobal().DONATION_KEY.trim(); + if (keyConfig.length() > 0) { + URL url = new URL("http://griefus.zhdev.org/license/" + keyConfig); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + connection.setRequestProperty("Accept-Charset", "UTF-8"); + connection.setRequestProperty("User-Agent", "Griefus"); + connection.setDoOutput(true); + connection.setInstanceFollowRedirects(true); + connection.setConnectTimeout(5000); + connection.connect(); + int status = connection.getResponseCode(); + + if (status == 200) { + BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); + String response = reader.readLine(); + if (response != null && response.length() > 0) { + String[] remoteKey = response.replaceAll("[^a-zA-Z0-9;]", "").split(";"); + if (remoteKey.length > 1 && remoteKey[1].equals("1") && remoteKey[0].length() == 8) { + donationKey = remoteKey[0]; + } + else if (remoteKey.length > 1) { + donationKey = null; + } + else { + keyValidated = false; + } + } + reader.close(); + } + else { + keyValidated = false; + } + } + else { + donationKey = null; + } + + try { + Path licensePath = Paths.get(ConfigHandler.path + ".license"); + if (keyValidated && donationKey == null) { + if (keyConfig.length() > 0) { + Chat.console(Phrase.build(Phrase.INVALID_DONATION_KEY) + " " + Phrase.build(Phrase.CHECK_CONFIG) + "."); + } + Files.write(licensePath, "".getBytes()); + } + else if (keyValidated) { + Files.write(licensePath, donationKey.getBytes()); + } + else if (Files.isReadable(licensePath)) { + List licenseFile = Files.readAllLines(licensePath); + if (licenseFile.size() == 1) { + donationKey = licenseFile.get(0); + if (donationKey == null || donationKey.length() != 8 || !donationKey.matches("^[A-Z0-9]+$")) { + donationKey = null; + } + } + } + } + catch (Exception e) { + e.printStackTrace(); + } + } + catch (Exception e) { + // Unable to connect to griefus.zhdev.org + } + + if (donationKey != null) { + // valid donation key, continue initialization + } + if (translate) { try { String lang = Config.getGlobal().LANGUAGE; @@ -75,7 +155,7 @@ public class NetworkHandler extends Language implements Runnable { Optional languageHeader = stream.findFirst(); if (languageHeader.isPresent()) { String headerString = languageHeader.get(); - if (headerString.startsWith("# Griefus")) { // verify that valid cache file + if (headerString.startsWith("# CoreProtect")) { // verify that valid cache file String[] split = headerString.split(" "); if (split.length == 6 && split[2].length() > 2 && split[5].length() > 2) { String cacheVersion = split[2].substring(1); @@ -204,6 +284,131 @@ public class NetworkHandler extends Language implements Runnable { if (startup) { Thread.sleep(1000); } + + while (ConfigHandler.serverRunning) { + int status = 0; + int statusEdge = 0; + HttpURLConnection connection = null; + HttpURLConnection connectionEdge = null; + String version = VersionUtils.getPluginVersion(); + + try { + // CoreProtect Community Edition + URL url = new URL("http://update.griefus.zhdev.org/version/"); + connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + connection.setRequestProperty("Accept-Charset", "UTF-8"); + connection.setRequestProperty("User-Agent", "Griefus/v" + version + " (by Intelli)"); + connection.setDoOutput(true); + connection.setInstanceFollowRedirects(true); + connection.setConnectTimeout(5000); + connection.connect(); + status = connection.getResponseCode(); + + // CoreProtect Edge + url = new URL("http://update.griefus.zhdev.org/version-edge/"); + connectionEdge = (HttpURLConnection) url.openConnection(); + connectionEdge.setRequestMethod("GET"); + connectionEdge.setRequestProperty("Accept-Charset", "UTF-8"); + connectionEdge.setRequestProperty("User-Agent", "Griefus/v" + version + " (by Intelli)"); + connectionEdge.setDoOutput(true); + connectionEdge.setInstanceFollowRedirects(true); + connectionEdge.setConnectTimeout(5000); + connectionEdge.connect(); + statusEdge = connectionEdge.getResponseCode(); + } + catch (Exception e) { + // Unable to connect to update.griefus.zhdev.org + } + + if (status == 200) { + try { + BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); + String response = reader.readLine(); + + if (response.length() > 0 && response.length() < 10) { + String remoteVersion = response.replaceAll("[^0-9.]", ""); + if (remoteVersion.contains(".")) { + boolean newVersion = VersionUtils.newVersion(version, remoteVersion); + if (newVersion) { + latestVersion = remoteVersion; + if (startup) { + Chat.console("--------------------"); + Chat.console(Phrase.build(Phrase.VERSION_NOTICE, remoteVersion)); + Chat.console(Phrase.build(Phrase.LINK_DOWNLOAD, "www.griefus.zhdev.org/download/")); + Chat.console("--------------------"); + startup = false; + } + } + else { + latestVersion = null; + } + } + } + + reader.close(); + } + catch (Exception e) { + e.printStackTrace(); + } + } + + if (statusEdge == 200) { + try { + BufferedReader reader = new BufferedReader(new InputStreamReader(connectionEdge.getInputStream())); + String response = reader.readLine(); + + if (response.length() > 0 && response.length() < 10) { + String remoteVersion = response.replaceAll("[^0-9.]", ""); + if (remoteVersion.contains(".")) { + boolean newVersion = VersionUtils.newVersion(version, remoteVersion); + if (newVersion) { + latestEdgeVersion = remoteVersion; + } + else { + latestEdgeVersion = null; + } + } + } + + reader.close(); + } + catch (Exception e) { + e.printStackTrace(); + } + } + + try { + /* Stat gathering */ + int port = Bukkit.getServer().getPort(); + String stats = port + ":" + (donationKey != null ? donationKey : "") + ":" + version + ConfigHandler.EDITION_BRANCH; + URL url = new URL("http://stats.griefus.zhdev.org/u/?data=" + stats); + connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + connection.setRequestProperty("Accept-Charset", "UTF-8"); + connection.setRequestProperty("User-Agent", "Griefus"); + connection.setConnectTimeout(5000); + connection.connect(); + connection.getResponseCode(); + connection.disconnect(); + } + catch (Exception e) { + // Unable to connect to stats.griefus.zhdev.org + } + + if (background) { + long time = System.currentTimeMillis(); + long sleepTime = time + 3600000; // 1 hour + + while (ConfigHandler.serverRunning && (time < sleepTime)) { + time = System.currentTimeMillis(); + Thread.sleep(1000); + } + } + else { + break; + } + } } catch (Exception e) { Chat.console(Phrase.build(Phrase.UPDATE_ERROR));