From fd2f7e4fbc394486775d00552f98fd7d14279ef8 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Sat, 29 Jun 2019 13:38:20 +1200 Subject: [PATCH] Improve startup checks for invalid settings --- .../libraryaddict/disguise/LibsDisguises.java | 10 +- .../disguise/utilities/LibsPremium.java | 122 ++++++++++-------- .../utilities/metrics/MetricsInitalizer.java | 18 ++- .../utilities/plugin/PluginInformation.java | 60 +++++++++ 4 files changed, 155 insertions(+), 55 deletions(-) create mode 100644 src/main/java/me/libraryaddict/disguise/utilities/plugin/PluginInformation.java diff --git a/src/main/java/me/libraryaddict/disguise/LibsDisguises.java b/src/main/java/me/libraryaddict/disguise/LibsDisguises.java index 10c9c835..275d9cd9 100644 --- a/src/main/java/me/libraryaddict/disguise/LibsDisguises.java +++ b/src/main/java/me/libraryaddict/disguise/LibsDisguises.java @@ -53,7 +53,7 @@ public class LibsDisguises extends JavaPlugin { getLogger().info("Build Date: " + pluginYml.getString("build-date")); - LibsPremium.check(getDescription().getVersion()); + LibsPremium.check(getDescription().getVersion(), getFile()); if (!LibsPremium.isPremium()) { getLogger().severe("You must purchase the plugin to use support for 1.14!"); @@ -243,6 +243,14 @@ public class LibsDisguises extends JavaPlugin { continue; } + // Invalidate invalid distribution + if (LibsPremium.isPremium() && LibsPremium.getPaidInformation() != null && + LibsPremium.getPaidInformation().getDownloadID().equals("-1895736039")) { + throw new IllegalStateException( + "Error while checking pi rate on startup! Please re-download the jar from SpigotMC before " + + "reporting this error!"); + } + disguiseType.setWatcherClass(watcherClass); if (DisguiseValues.getDisguiseValues(disguiseType) != null) { diff --git a/src/main/java/me/libraryaddict/disguise/utilities/LibsPremium.java b/src/main/java/me/libraryaddict/disguise/utilities/LibsPremium.java index 1bfa5280..41404bbd 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/LibsPremium.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/LibsPremium.java @@ -1,67 +1,38 @@ package me.libraryaddict.disguise.utilities; -import com.comphenix.protocol.PacketType; import me.libraryaddict.disguise.LibsDisguises; +import me.libraryaddict.disguise.utilities.plugin.PluginInformation; import me.libraryaddict.disguise.utilities.reflection.ReflectionManager; +import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.craftbukkit.libs.org.apache.commons.io.IOUtils; import java.io.File; +import java.io.IOException; import java.net.URL; import java.net.URLClassLoader; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; /** * Created by libraryaddict on 2/06/2017. */ public class LibsPremium { - private static class PluginInformation { - private String userID; - private boolean premium; - private String version; - private String buildNumber; - private String buildDate; + private static Boolean thisPluginIsPaidFor; + /** + * Information of the actively running plugin + */ + private static PluginInformation pluginInformation; + /** + * Information of the plugin used to activate premium, if exists + */ + private static PluginInformation paidInformation; - public PluginInformation(String userID, boolean premium, String version, String buildNumber, String buildDate) { - this.userID = userID; - this.premium = premium; - this.version = version; - this.buildNumber = buildNumber; - this.buildDate = buildDate; - } - - public String getUserID() { - return userID; - } - - public boolean isPremium() { - return premium; - } - - public String getVersion() { - return version; - } - - public String getBuildNumber() { - return buildNumber; - } - - public String getBuildDate() { - return buildDate; - } - - public Date getParsedBuildDate() { - try { - return new SimpleDateFormat("dd/MM/yyyy HH:mm").parse(getBuildDate()); - } - catch (ParseException e) { - return null; - } - } + public static PluginInformation getPluginInformation() { + return pluginInformation; } - private static Boolean thisPluginIsPaidFor; + public static PluginInformation getPaidInformation() { + return paidInformation; + } /** * @return Account ID if downloaded through SpigotMC @@ -92,8 +63,11 @@ public class LibsPremium { return !userID.contains("__USER__"); } + /** + * Returns true if this plugin is premium + */ public static Boolean isPremium() { - return thisPluginIsPaidFor == null ? !getUserID().contains("__USER__") : thisPluginIsPaidFor; + return thisPluginIsPaidFor == null ? isPremium(getUserID()) : thisPluginIsPaidFor; } /** @@ -143,11 +117,15 @@ public class LibsPremium { Boolean premium; String userId = null; + String downloadId = null; + String resourceId = null; if (oldJarFile) { premium = (Boolean) c.getMethod("isPremium").invoke(null); } else { userId = (String) c.getMethod("getUserID").invoke(null); + resourceId = (String) c.getMethod("getResourceID").invoke(null); + downloadId = (String) c.getMethod("getDownloadID").invoke(null); premium = isPremium(userId); } @@ -172,7 +150,8 @@ public class LibsPremium { String pluginVersion = config.getString("version"); - return new PluginInformation(userId, premium, pluginVersion, pluginBuildNumber, pluginBuildDate); + return new PluginInformation(userId, resourceId, downloadId, premium, pluginVersion, pluginBuildNumber, + pluginBuildDate); } } @@ -211,7 +190,8 @@ public class LibsPremium { plugin.getBuildDate()); if (plugin.isPremium()) { - if (!isValidVersion(version, plugin.getVersion()) || plugin.getUserID() == null) { + if (!isValidVersion(version, plugin.getVersion()) || plugin.getUserID() == null || + plugin.getDownloadID() == null) { DisguiseUtilities.getLogger().warning( "You have an old Lib's Disguises jar (" + file.getName() + " " + fileInfo + ") in the LibsDisguises folder! For security purposes, please replace this with a" + @@ -220,6 +200,8 @@ public class LibsPremium { continue; } + paidInformation = plugin; + thisPluginIsPaidFor = true; // Found a premium Lib's Disguises jar (v5.2.6, build #40, created 16/02/2019) DisguiseUtilities.getLogger().info("Found a premium Lib's Disguises jar (" + fileInfo + ")"); @@ -251,12 +233,42 @@ public class LibsPremium { total += Character.getNumericValue(c); } - return String.format("%s(%s)", userID, total); + return String.format("%s (%s)", userID, total); } - public static void check(String version) { + public static void check(String version, File file) { thisPluginIsPaidFor = isPremium(); + try { + pluginInformation = getInformation(file); + } + catch (Exception e) { + String pluginBuildDate = "??/??/????"; + + YamlConfiguration config = new YamlConfiguration(); + + try { + config.loadFromString(IOUtils.toString(LibsDisguises.getInstance().getResource("plugin.yml"), "UTF-8")); + + // If plugin.yml contains a build-date + if (config.contains("build-date")) { + pluginBuildDate = config.getString("build-date"); + } + } + catch (InvalidConfigurationException | IOException ex) { + ex.printStackTrace(); + } + + String buildNo = LibsDisguises.getInstance().getBuildNo(); + + if (buildNo != null && buildNo.matches("[0-9]+")) { + buildNo = "#" + buildNo; + } + + pluginInformation = new PluginInformation(getUserID(), getResourceID(), getDownloadID(), + isPremium(getUserID()), version, buildNo, pluginBuildDate); + } + if (!isPremium() || !LibsDisguises.getInstance().isReleaseBuild()) { doSecondaryCheck(version); } else { @@ -264,7 +276,11 @@ public class LibsPremium { } if (isPremium()) { - DisguiseUtilities.getLogger().info("Premium enabled, thank you for supporting Lib's Disguises!"); + boolean prem = + getPaidInformation() == null ? getPluginInformation().isLegit() : getPaidInformation().isLegit(); + + DisguiseUtilities.getLogger() + .info("Premium enabled, thank you for supporting Lib's Disguises!" + (!prem ? "!" : "")); } } } diff --git a/src/main/java/me/libraryaddict/disguise/utilities/metrics/MetricsInitalizer.java b/src/main/java/me/libraryaddict/disguise/utilities/metrics/MetricsInitalizer.java index 8fac54e8..9d5ef09d 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/metrics/MetricsInitalizer.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/metrics/MetricsInitalizer.java @@ -6,6 +6,7 @@ import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.TargetedDisguise; import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.LibsPremium; +import me.libraryaddict.disguise.utilities.plugin.PluginInformation; import java.util.Collection; import java.util.HashMap; @@ -42,7 +43,22 @@ public class MetricsInitalizer { final String premiumType; if (LibsPremium.isPremium()) { - if (plugin.isReleaseBuild()) { + PluginInformation info = LibsPremium.getPaidInformation(); + + if (info == null) { + info = LibsPremium.getPluginInformation(); + } + + boolean customPremium = !info.getUserID().matches("[0-9]+") || info.getUserID().equals("1") || + !LibsPremium.getResourceID().equals("32453") || !info.getDownloadID().matches("-?[0-9]+"); + + if (customPremium) { + if (plugin.isReleaseBuild() && LibsPremium.getPaidInformation() == null) { + premiumType = "Custom Plugin"; + } else { + premiumType = "Custom Builds"; + } + } else if (plugin.isReleaseBuild()) { premiumType = "Paid Plugin"; } else { premiumType = "Paid Builds"; diff --git a/src/main/java/me/libraryaddict/disguise/utilities/plugin/PluginInformation.java b/src/main/java/me/libraryaddict/disguise/utilities/plugin/PluginInformation.java new file mode 100644 index 00000000..7a267baa --- /dev/null +++ b/src/main/java/me/libraryaddict/disguise/utilities/plugin/PluginInformation.java @@ -0,0 +1,60 @@ +package me.libraryaddict.disguise.utilities.plugin; + +import me.libraryaddict.disguise.utilities.LibsPremium; + +/** + * Created by libraryaddict on 20/06/2019. + */ +public class PluginInformation { + private String userID; + private String resourceID; + private String downloadID; + private boolean premium; + private String version; + private String buildNumber; + private String buildDate; + + public PluginInformation(String userID, String resourceID, String downloadID, boolean premium, String version, + String buildNumber, String buildDate) { + this.userID = userID; + this.resourceID = resourceID; + this.downloadID = downloadID; + this.premium = premium; + this.version = version; + this.buildNumber = buildNumber; + this.buildDate = buildDate; + } + + public String getUserID() { + return userID; + } + + public String getResourceID() { + return resourceID; + } + + public String getDownloadID() { + return downloadID; + } + + public boolean isPremium() { + return premium; + } + + public String getVersion() { + return version; + } + + public String getBuildNumber() { + return buildNumber; + } + + public String getBuildDate() { + return buildDate; + } + + public boolean isLegit() { + return getUserID().matches("[0-9]+") && LibsPremium.getResourceID().equals("32453") && + getDownloadID().matches("-?[0-9]+"); + } +}