From de8bb2bd662f8fff9653065be4a8ad321a0e4f16 Mon Sep 17 00:00:00 2001 From: Dan Mulloy Date: Fri, 29 Jan 2016 23:28:10 -0500 Subject: [PATCH] Switch to HTTPS for Spigot update checking Should fix the 403 errors --- .../comphenix/protocol/ProtocolLibrary.java | 4 ++ .../protocol/updater/BukkitUpdater.java | 3 +- .../protocol/updater/SpigotUpdater.java | 58 +++++++++++-------- .../comphenix/protocol/updater/Updater.java | 9 ++- 4 files changed, 43 insertions(+), 31 deletions(-) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLibrary.java b/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLibrary.java index 4e36142e..390285aa 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLibrary.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLibrary.java @@ -754,4 +754,8 @@ public class ProtocolLibrary extends JavaPlugin { public static Logger getStaticLogger() { return logger; } + + public static void disableUpdates() { + UPDATES_DISABLED = true; + } } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/updater/BukkitUpdater.java b/ProtocolLib/src/main/java/com/comphenix/protocol/updater/BukkitUpdater.java index b0ec0f92..72342403 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/updater/BukkitUpdater.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/updater/BukkitUpdater.java @@ -22,7 +22,6 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.plugin.Plugin; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.JSONValue; @@ -79,7 +78,7 @@ public class BukkitUpdater extends Updater { * @param type Specify the type of update this will be. See {@link UpdateType} * @param announce True if the program should announce the progress of new updates in console */ - public BukkitUpdater(Plugin plugin, int id, File file, UpdateType type, boolean announce) { + public BukkitUpdater(ProtocolLibrary plugin, int id, File file, UpdateType type, boolean announce) { super(plugin, type, announce); this.file = file; diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/updater/SpigotUpdater.java b/ProtocolLib/src/main/java/com/comphenix/protocol/updater/SpigotUpdater.java index 8f3831c5..5c5f963e 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/updater/SpigotUpdater.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/updater/SpigotUpdater.java @@ -21,23 +21,22 @@ import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; - -import org.bukkit.plugin.Plugin; +import java.util.logging.Level; import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.error.Report; +import com.comphenix.protocol.utility.Closer; import com.google.common.base.Charsets; /** * Adapted version of the Bukkit updater for use with Spigot resources - * * @author dmulloy2 */ public final class SpigotUpdater extends Updater { private String remoteVersion; - public SpigotUpdater(Plugin plugin, UpdateType type, boolean announce) { + public SpigotUpdater(ProtocolLibrary plugin, UpdateType type, boolean announce) { super(plugin, type, announce); } @@ -70,34 +69,45 @@ public final class SpigotUpdater extends Updater { result = UpdateResult.NO_UPDATE; } } catch (Throwable ex) { - ProtocolLibrary.getErrorReporter().reportDetailed( - SpigotUpdater.this, Report.newBuilder(REPORT_CANNOT_UPDATE_PLUGIN).error(ex).callerParam(this)); - } finally { - // Invoke the listeners on the main thread - for (Runnable listener : listeners) { - plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, listener); - } - } + if (ProtocolLibrary.getConfiguration().isDebug()) { + ProtocolLibrary.getErrorReporter().reportDetailed( + SpigotUpdater.this, Report.newBuilder(REPORT_CANNOT_UPDATE_PLUGIN).error(ex).callerParam(this)); + } else { + plugin.getLogger().log(Level.WARNING, "Failed to check for updates: " + ex); + } + + ProtocolLibrary.disableUpdates(); + } finally { + // Invoke the listeners on the main thread + for (Runnable listener : listeners) { + plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, listener); + } + } } } - private static final String RESOURCE_URL = "https://www.spigotmc.org/resources/protocollib.1997/"; - private static final String API_URL = "http://www.spigotmc.org/api/general.php"; + private static final String PROTOCOL = "https://"; + private static final String RESOURCE_URL = PROTOCOL + "www.spigotmc.org/resources/protocollib.1997/"; + private static final String API_URL = PROTOCOL + "www.spigotmc.org/api/general.php"; private static final String ACTION = "POST"; private static final int ID = 1997; private static final byte[] API_KEY = ("key=98BE0FE67F88AB82B4C197FAF1DC3B69206EFDCC4D3B80FC83A00037510B99B4&resource=" + ID).getBytes(Charsets.UTF_8); - private String getSpigotVersion() throws IOException { - HttpURLConnection con = (HttpURLConnection) new URL(API_URL).openConnection(); - con.setDoOutput(true); - con.setRequestMethod(ACTION); - con.getOutputStream().write(API_KEY); - String version = new BufferedReader(new InputStreamReader(con.getInputStream())).readLine(); - if (version.length() <= 7) { - return version; - } + public String getSpigotVersion() throws IOException { + Closer closer = Closer.create(); - return null; + try { + HttpURLConnection con = (HttpURLConnection) new URL(API_URL).openConnection(); + con.setDoOutput(true); + con.setRequestMethod(ACTION); + con.getOutputStream().write(API_KEY); + + InputStreamReader isr = closer.register(new InputStreamReader(con.getInputStream())); + BufferedReader br = closer.register(new BufferedReader(isr)); + return br.readLine(); + } finally { + closer.close(); + } } } \ No newline at end of file diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/updater/Updater.java b/ProtocolLib/src/main/java/com/comphenix/protocol/updater/Updater.java index fa14f2a3..fa47c935 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/updater/Updater.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/updater/Updater.java @@ -20,8 +20,7 @@ import java.io.File; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; -import org.bukkit.plugin.Plugin; - +import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.error.ReportType; import com.comphenix.protocol.utility.MinecraftVersion; import com.comphenix.protocol.utility.Util; @@ -32,7 +31,7 @@ import com.google.common.base.Preconditions; */ public abstract class Updater { - protected Plugin plugin; + protected ProtocolLibrary plugin; protected String versionName; protected String versionLink; @@ -49,7 +48,7 @@ public abstract class Updater { public static final ReportType REPORT_CANNOT_UPDATE_PLUGIN = new ReportType("Cannot update ProtocolLib."); - protected Updater(Plugin plugin, UpdateType type, boolean announce) { + protected Updater(ProtocolLibrary plugin, UpdateType type, boolean announce) { this.plugin = plugin; this.type = type; this.announce = announce; @@ -266,7 +265,7 @@ public abstract class Updater { } } - public static Updater create(Plugin plugin, int id, File file, UpdateType type, boolean announce) { + public static Updater create(ProtocolLibrary plugin, int id, File file, UpdateType type, boolean announce) { if (Util.isUsingSpigot()) { return new SpigotUpdater(plugin, type, announce); } else {