Updated update checker

Fixes 403 error
This commit is contained in:
Stijn Bannink 2021-11-21 16:19:53 +01:00 committed by GitHub
parent 148f990061
commit 57662de2da
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,7 +1,5 @@
package nl.SBDeveloper.V10Lift.sbutils; package nl.sbdeveloper.vehiclesplus.sbutils;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
@ -10,7 +8,6 @@ import org.bukkit.plugin.java.JavaPlugin;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
import java.io.*; import java.io.*;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.nio.channels.Channels; import java.nio.channels.Channels;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
@ -20,16 +17,18 @@ import java.util.function.BiConsumer;
/** /**
* Update class for SBDevelopment * Update class for SBDevelopment
*
* @author Stijn [SBDeveloper] * @author Stijn [SBDeveloper]
* @since 05-03-2020 * @version 2.1 [19-11-2021] - This class supports both the v2 Spiget and v2 SBDUpdate API
* @version 2.0 [26-12-2020] - This class supports the v2 Update API
* *
* <p>&copy; Stijn Bannink [stijnbannink23@gmail.com] - All rights reserved.</p> * <p>&copy; Stijn Bannink [stijnbannink23@gmail.com] - All rights reserved.</p>
* @since 05-03-2020
*/ */
public class UpdateManager { public class UpdateManager {
private static final JsonParser parser = new JsonParser();
private static final String SPIGOT_API = "https://api.spigotmc.org/legacy/update.php?resource=%d"; private static final String SPIGOT_API = "https://api.spiget.org/v2/resources/%s/versions/latest";
private static final String SPIGOT_DOWNLOAD = "http://api.spiget.org/v2/resources/%s/download"; private static final String SPIGOT_DOWNLOAD = "https://api.spiget.org/v2/resources/%s/download";
private static final String SBDPLUGINS_API = "https://updates.sbdplugins.nl/api/v2/plugins/%d"; private static final String SBDPLUGINS_API = "https://updates.sbdplugins.nl/api/v2/plugins/%d";
private static final String SBDPLUGINS_DOWNLOAD = "https://updates.sbdplugins.nl/api/v2/download/%d"; private static final String SBDPLUGINS_DOWNLOAD = "https://updates.sbdplugins.nl/api/v2/download/%d";
@ -74,6 +73,7 @@ public class UpdateManager {
/** /**
* Handle the response given by check(); * Handle the response given by check();
*
* @param versionResponse The response * @param versionResponse The response
* @return The updatemanager * @return The updatemanager
*/ */
@ -110,8 +110,6 @@ public class UpdateManager {
if (in == null) return; if (in == null) return;
String version;
String inputLine; String inputLine;
StringBuilder response = new StringBuilder(); StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) { while ((inputLine = in.readLine()) != null) {
@ -119,18 +117,7 @@ public class UpdateManager {
} }
in.close(); in.close();
JsonParser parser = new JsonParser(); String version = parser.parse(response.toString()).getAsJsonObject().get(type == CheckType.SPIGOT ? "name" : "version").getAsString();
if (type == CheckType.SPIGOT) {
JsonArray array = parser.parse(response.toString()).getAsJsonArray();
version = array.get(0).getAsJsonObject().get("name").getAsString();
} else {
JsonObject object = parser.parse(response.toString()).getAsJsonObject();
version = object.get("version").getAsString();
}
if (version == null) return; if (version == null) return;
Version onlineVersion = new Version(version); Version onlineVersion = new Version(version);
@ -168,8 +155,9 @@ public class UpdateManager {
//https://stackoverflow.com/questions/921262/how-to-download-and-save-a-file-from-internet-using-java //https://stackoverflow.com/questions/921262/how-to-download-and-save-a-file-from-internet-using-java
int response; int response;
InputStream stream; InputStream stream;
HttpsURLConnection connection;
if (type == CheckType.SBDPLUGINS) { if (type == CheckType.SBDPLUGINS) {
HttpURLConnection connection = (HttpURLConnection) new URL(String.format(SBDPLUGINS_DOWNLOAD, this.resourceID)).openConnection(); connection = (HttpsURLConnection) new URL(String.format(SBDPLUGINS_DOWNLOAD, this.resourceID)).openConnection();
String urlParameters = "license=" + license + "&port=" + Bukkit.getPort(); String urlParameters = "license=" + license + "&port=" + Bukkit.getPort();
byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8); byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);
@ -185,16 +173,13 @@ public class UpdateManager {
DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.write(postData); wr.write(postData);
wr.close(); wr.close();
response = connection.getResponseCode();
stream = connection.getInputStream();
} else { } else {
HttpsURLConnection connection = (HttpsURLConnection) new URL(String.format(SPIGOT_DOWNLOAD, this.resourceID)).openConnection(); connection = (HttpsURLConnection) new URL(String.format(SPIGOT_DOWNLOAD, this.resourceID)).openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0"); connection.setRequestProperty("User-Agent", "Mozilla/5.0");
}
response = connection.getResponseCode(); response = connection.getResponseCode();
stream = connection.getInputStream(); stream = connection.getInputStream();
}
if (response != 200) { if (response != 200) {
BufferedReader in = new BufferedReader(new InputStreamReader(stream)); BufferedReader in = new BufferedReader(new InputStreamReader(stream));
@ -249,7 +234,9 @@ public class UpdateManager {
} }
private File getPluginFile() { private File getPluginFile() {
if (!(this.plugin instanceof JavaPlugin)) { return null; } if (!(this.plugin instanceof JavaPlugin)) {
return null;
}
try { try {
Method method = JavaPlugin.class.getDeclaredMethod("getFile"); Method method = JavaPlugin.class.getDeclaredMethod("getFile");
method.setAccessible(true); method.setAccessible(true);
@ -278,18 +265,18 @@ public class UpdateManager {
private final String version; private final String version;
public final String get() {
return this.version;
}
private Version(String version) { private Version(String version) {
if(version == null) if (version == null)
throw new IllegalArgumentException("Version can not be null"); throw new IllegalArgumentException("Version can not be null");
if(!version.matches("[0-9]+(\\.[0-9]+)*")) if (!version.matches("[0-9]+(\\.[0-9]+)*"))
throw new IllegalArgumentException("Invalid version format"); throw new IllegalArgumentException("Invalid version format");
this.version = version; this.version = version;
} }
public final String get() {
return this.version;
}
private VersionResponse check(Version that) { private VersionResponse check(Version that) {
String[] thisParts = this.get().split("\\."); String[] thisParts = this.get().split("\\.");
String[] thatParts = that.get().split("\\."); String[] thatParts = that.get().split("\\.");
@ -298,9 +285,9 @@ public class UpdateManager {
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
int thisPart = i < thisParts.length ? Integer.parseInt(thisParts[i]) : 0; int thisPart = i < thisParts.length ? Integer.parseInt(thisParts[i]) : 0;
int thatPart = i < thatParts.length ? Integer.parseInt(thatParts[i]) : 0; int thatPart = i < thatParts.length ? Integer.parseInt(thatParts[i]) : 0;
if(thisPart < thatPart) if (thisPart < thatPart)
return VersionResponse.FOUND_NEW; return VersionResponse.FOUND_NEW;
if(thisPart > thatPart) if (thisPart > thatPart)
return VersionResponse.THIS_NEWER; return VersionResponse.THIS_NEWER;
} }
return VersionResponse.LATEST; return VersionResponse.LATEST;