From 219b49aa167feedc689b38e2b2684affd5bc8d2b Mon Sep 17 00:00:00 2001 From: Andre601 <11576465+Andre601@users.noreply.github.com> Date: Sat, 10 Jul 2021 00:20:43 +0200 Subject: [PATCH 1/2] First attempt at fixing #86 ~ Switch to OkHttp for Request handling and use CompletableFuture --- core/pom.xml | 9 ++ .../core/OneVersionRemake.java | 60 ++++---- .../core/proxy/ProtocolVersionResolver.java | 133 ++++++++---------- pom.xml | 2 +- 4 files changed, 105 insertions(+), 99 deletions(-) diff --git a/core/pom.xml b/core/pom.xml index e90b7c7..f326ae1 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -68,6 +68,11 @@ 4.8.1 provided + + com.squareup.okhttp3 + okhttp + 4.9.0 + @@ -112,6 +117,10 @@ net.kyori com.andre601.oneversionremake.core.dependencies.adventure + + okhttp3 + com.andre601.oneversionremake.core.dependencies.okhttp + diff --git a/core/src/main/java/com/andre601/oneversionremake/core/OneVersionRemake.java b/core/src/main/java/com/andre601/oneversionremake/core/OneVersionRemake.java index 32c7e7a..d9d5938 100644 --- a/core/src/main/java/com/andre601/oneversionremake/core/OneVersionRemake.java +++ b/core/src/main/java/com/andre601/oneversionremake/core/OneVersionRemake.java @@ -28,6 +28,8 @@ import java.io.InputStream; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; import java.util.*; public class OneVersionRemake{ @@ -47,7 +49,7 @@ public OneVersionRemake(PluginCore pluginCore){ this.protocolVersionResolver = new ProtocolVersionResolver(this, pluginCore.getPath()); this.parser = new Parser(this); - start(); + load(); } public ProxyLogger getProxyLogger(){ @@ -128,7 +130,7 @@ public List getPlayers(Class clazz, List lines, List } } - private void start(){ + private void load(){ loadVersion(); printBanner(); @@ -139,19 +141,44 @@ private void start(){ return; } - if(!loadFile()) - return; + if(!getProtocolVersionResolver().hasFile() || getConfigHandler().getBoolean(true, "Settings", "UpdateVersions")){ + getProtocolVersionResolver().loadFile().whenComplete((inputStream, ex) -> { + if(ex != null){ + getProxyLogger().warn("Unable to update versions.json! Encountered an exception.", ex); + return; + } + + try{ + Files.copy(inputStream, getProtocolVersionResolver().getFile(), StandardCopyOption.REPLACE_EXISTING); + getProxyLogger().info("Updated versions.json!"); + + enable(); + }catch(IOException ex1){ + getProxyLogger().warn("Unable to update versions.json! Encountered IOException.", ex1); + } + }); + }else{ + if(getProtocolVersionResolver().setupConfigurate()){ + getProxyLogger().info("Loaded versions.json!"); + + enable(); + }else{ + getProxyLogger().warn("Unable to load versions.json! Check previous lines for errors and warnings."); + } + } + } + private void enable(){ List protocols = configHandler.getIntList("Protocol", "Versions"); boolean versionsSet; if(protocols.isEmpty()){ printWarning(); - + versionsSet = false; }else{ getProxyLogger().info("Loaded the following Protocol Version(s):"); getProxyLogger().info(getProtocolVersionResolver().getFriendlyNames(protocols, false)); - + versionsSet = true; } @@ -212,25 +239,4 @@ private void loadVersion(){ version = "UNKNOWN"; } } - - private boolean loadFile(){ - if(!getProtocolVersionResolver().hasFile() || getConfigHandler().getBoolean(true, "Settings", "UpdateVersions")){ - getProxyLogger().info("Fetching latest versions.json from GitHub.com..."); - if(getProtocolVersionResolver().loadFile()){ - getProxyLogger().info("Updated versions.json!"); - return true; - }else{ - getProxyLogger().warn("Unable to update versions.json! Check previous lines for errors and warnings."); - } - }else{ - if(getProtocolVersionResolver().setupConfigurate()){ - getProxyLogger().info("Loaded versions.json!"); - return true; - }else{ - getProxyLogger().warn("Unable to load versions.json! Check previous lines for errors and warnings."); - } - } - - return false; - } } diff --git a/core/src/main/java/com/andre601/oneversionremake/core/proxy/ProtocolVersionResolver.java b/core/src/main/java/com/andre601/oneversionremake/core/proxy/ProtocolVersionResolver.java index 6f7c3eb..576e6cb 100644 --- a/core/src/main/java/com/andre601/oneversionremake/core/proxy/ProtocolVersionResolver.java +++ b/core/src/main/java/com/andre601/oneversionremake/core/proxy/ProtocolVersionResolver.java @@ -20,39 +20,37 @@ import com.andre601.oneversionremake.core.OneVersionRemake; import com.andre601.oneversionremake.core.interfaces.ProxyLogger; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; +import okhttp3.ResponseBody; import org.spongepowered.configurate.ConfigurationNode; import org.spongepowered.configurate.gson.GsonConfigurationLoader; import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.http.HttpClient; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; +import java.util.Collections; import java.util.Comparator; import java.util.List; +import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; import java.util.stream.Stream; public class ProtocolVersionResolver{ - private final OneVersionRemake core; - private final HttpClient client = HttpClient.newHttpClient(); + private final OkHttpClient CLIENT = new OkHttpClient(); private final ProxyLogger logger; - private final Path file; + public final Path file; private final File path; private ConfigurationNode node = null; public ProtocolVersionResolver(OneVersionRemake core, Path path){ - this.core = core; - this.logger = core.getProxyLogger(); this.file = path.resolve("versions.json"); @@ -63,24 +61,17 @@ public boolean hasFile(){ return file.toFile().exists(); } - public boolean loadFile(){ + public Path getFile(){ + return file; + } + + public CompletableFuture loadFile(){ if(!path.isDirectory() && !path.mkdirs()){ logger.warn("Could not create folder for plugin!"); - return false; - } - - try(InputStream is = updateCache()){ - if(is == null){ - return false; - } - - Files.copy(is, file, StandardCopyOption.REPLACE_EXISTING); - }catch(IOException ex){ - logger.warn("Unable to create versions.json! Encountered IOException.", ex); - return false; + return CompletableFuture.supplyAsync(() -> null); } - return setupConfigurate(); + return updateCache(); } public boolean setupConfigurate(){ @@ -98,58 +89,58 @@ public boolean setupConfigurate(){ return true; } - public InputStream updateCache(){ - try{ - HttpRequest request = HttpRequest.newBuilder() - .GET() - .header("User-Agent", "OneVersionRemake") - .uri(new URI("https://raw.githubusercontent.com/Andre601/OneVersionRemake/master/versions.json")) + public CompletableFuture updateCache(){ + return CompletableFuture.supplyAsync(() -> { + String url = "https://raw.githubusercontent.com/Andre601/OneVersionRemake/master/versions.json"; + Request request = new Request.Builder() + .url(url) + .addHeader("User-Agent", "OneVersionRemake") .build(); + + try(Response response = CLIENT.newCall(request).execute()){ + if(!response.isSuccessful()){ + logger.warn(String.format( + "Unable to establish connection! Status-Code %d (%s) received!", + response.code(), + response.message() + )); - HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofInputStream()); - - if(response.statusCode() != 200){ - String CONNECTION_ERR = "Unable to establish connection! Status-Code %d (%s)"; + switch(response.code()){ + case 404: + logger.warn(String.format( + "The requested site (%s) does not exist. Please report this to the developer on Discord!", + url + )); + break; - switch(response.statusCode()){ - case 404: - logger.warn(String.format(CONNECTION_ERR, response.statusCode(), "Site not available")); - logger.warn("Please report this to the Developer!"); - return null; - - case 429: - logger.warn(String.format(CONNECTION_ERR, response.statusCode(), "Rate Limited")); - logger.warn("You connect too many times in a short period. Please delay any further restarts."); - return null; - - case 500: - logger.warn(String.format(CONNECTION_ERR, response.statusCode(), "Internal Server Error")); - logger.warn("The Server (GitHub) had an unexpected error when handling the request. Try again later."); - return null; - - default: - logger.warn("Encountered unknown Response code " + response.statusCode()); - logger.warn("Inform the developer about this on their Discord. This is NOT a bug however!"); - return null; - } - } + case 429: + logger.warn("Encountered a Rate Limit. Please delay any future Proxy Restarts to avoid this."); + break; + + case 500: + logger.warn("The Site (GitHub.com) encountered an error when handling the request. Try again later..."); + break; + + default: + logger.warn("This is an unknown error by the plugin! Please report this to the developer on Discord!"); + break; + } - return response.body(); - }catch(Exception ex){ - if(ex instanceof URISyntaxException){ - core.getProxyLogger().warn("Unable to establish connection to retrieve Protocol Versions! URI was invalid!"); - }else - if(ex instanceof IOException){ - core.getProxyLogger().warn("Unable to establish connection to retrieve Protocol Versions! Request was non-successful!"); - }else - if(ex instanceof InterruptedException){ - core.getProxyLogger().warn("Unable to establish connection to retrieve Protocol Versions! Request was interrupted!"); - }else{ - core.getProxyLogger().warn("Unable to establish connection to retrieve Protocol Versions! Received unknown Exception!", ex); + return null; + } + + ResponseBody body = response.body(); + if(body == null){ + logger.warn("GitHub.com returned an invalid/empty body!"); + return null; + } + + return body.byteStream(); + }catch(IOException ex){ + logger.warn("Encountered IOException while performing a request!", ex); + return null; } - - return null; - } + }); } public String getFriendlyNames(List protocols, boolean majorOnly){ diff --git a/pom.xml b/pom.xml index 31c9a99..dd2d09c 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ UTF-8 - 3.8.1 + 3.8.2 Only allow specific client versions on your Network. 11 From ebad677ef5736cf3760513f47cc8b8cbbed7ef1a Mon Sep 17 00:00:00 2001 From: Andre601 <11576465+Andre601@users.noreply.github.com> Date: Sat, 10 Jul 2021 00:54:48 +0200 Subject: [PATCH 2/2] Don't use CompletableFuture --- .../core/OneVersionRemake.java | 24 ++--- .../core/proxy/ProtocolVersionResolver.java | 100 ++++++++---------- 2 files changed, 51 insertions(+), 73 deletions(-) diff --git a/core/src/main/java/com/andre601/oneversionremake/core/OneVersionRemake.java b/core/src/main/java/com/andre601/oneversionremake/core/OneVersionRemake.java index d9d5938..b504951 100644 --- a/core/src/main/java/com/andre601/oneversionremake/core/OneVersionRemake.java +++ b/core/src/main/java/com/andre601/oneversionremake/core/OneVersionRemake.java @@ -28,8 +28,6 @@ import java.io.InputStream; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; -import java.nio.file.Files; -import java.nio.file.StandardCopyOption; import java.util.*; public class OneVersionRemake{ @@ -142,25 +140,15 @@ private void load(){ } if(!getProtocolVersionResolver().hasFile() || getConfigHandler().getBoolean(true, "Settings", "UpdateVersions")){ - getProtocolVersionResolver().loadFile().whenComplete((inputStream, ex) -> { - if(ex != null){ - getProxyLogger().warn("Unable to update versions.json! Encountered an exception.", ex); - return; - } - - try{ - Files.copy(inputStream, getProtocolVersionResolver().getFile(), StandardCopyOption.REPLACE_EXISTING); - getProxyLogger().info("Updated versions.json!"); - - enable(); - }catch(IOException ex1){ - getProxyLogger().warn("Unable to update versions.json! Encountered IOException.", ex1); - } - }); + if(getProtocolVersionResolver().loadFile()){ + getProxyLogger().info("Updated versions.json!"); + enable(); + }else{ + getProxyLogger().warn("Unable to update versions.json! Check previous lines for warnings and errors."); + } }else{ if(getProtocolVersionResolver().setupConfigurate()){ getProxyLogger().info("Loaded versions.json!"); - enable(); }else{ getProxyLogger().warn("Unable to load versions.json! Check previous lines for errors and warnings."); diff --git a/core/src/main/java/com/andre601/oneversionremake/core/proxy/ProtocolVersionResolver.java b/core/src/main/java/com/andre601/oneversionremake/core/proxy/ProtocolVersionResolver.java index 576e6cb..3291911 100644 --- a/core/src/main/java/com/andre601/oneversionremake/core/proxy/ProtocolVersionResolver.java +++ b/core/src/main/java/com/andre601/oneversionremake/core/proxy/ProtocolVersionResolver.java @@ -27,16 +27,12 @@ import org.spongepowered.configurate.ConfigurationNode; import org.spongepowered.configurate.gson.GsonConfigurationLoader; -import java.io.File; import java.io.IOException; -import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; -import java.util.Collections; import java.util.Comparator; import java.util.List; -import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -45,8 +41,8 @@ public class ProtocolVersionResolver{ private final ProxyLogger logger; - public final Path file; - private final File path; + private final Path file; + private final Path path; private ConfigurationNode node = null; @@ -54,21 +50,17 @@ public ProtocolVersionResolver(OneVersionRemake core, Path path){ this.logger = core.getProxyLogger(); this.file = path.resolve("versions.json"); - this.path = path.toFile(); + this.path = path; } public boolean hasFile(){ return file.toFile().exists(); } - public Path getFile(){ - return file; - } - - public CompletableFuture loadFile(){ - if(!path.isDirectory() && !path.mkdirs()){ + public boolean loadFile(){ + if(!path.toFile().isDirectory() && !path.toFile().mkdirs()){ logger.warn("Could not create folder for plugin!"); - return CompletableFuture.supplyAsync(() -> null); + return false; } return updateCache(); @@ -89,58 +81,56 @@ public boolean setupConfigurate(){ return true; } - public CompletableFuture updateCache(){ - return CompletableFuture.supplyAsync(() -> { - String url = "https://raw.githubusercontent.com/Andre601/OneVersionRemake/master/versions.json"; - Request request = new Request.Builder() - .url(url) - .addHeader("User-Agent", "OneVersionRemake") - .build(); - - try(Response response = CLIENT.newCall(request).execute()){ - if(!response.isSuccessful()){ - logger.warn(String.format( - "Unable to establish connection! Status-Code %d (%s) received!", - response.code(), - response.message() - )); - - switch(response.code()){ - case 404: - logger.warn(String.format( - "The requested site (%s) does not exist. Please report this to the developer on Discord!", - url - )); - break; - + public boolean updateCache(){ + String url = "https://raw.githubusercontent.com/Andre601/OneVersionRemake/master/versions.json"; + Request request = new Request.Builder() + .url(url) + .addHeader("User-Agent", "OneVersionRemake") + .build(); + + try(Response response = CLIENT.newCall(request).execute()){ + if(!response.isSuccessful()){ + logger.warn(String.format( + "Unable to establish connection! Status-Code %d (%s) received!", + response.code(), + response.message() + )); + switch(response.code()){ + case 404: + logger.warn(String.format( + "The requested site (%s) does not exist. Please report this to the developer on Discord!", + url + )); + break; + case 429: logger.warn("Encountered a Rate Limit. Please delay any future Proxy Restarts to avoid this."); break; - + case 500: logger.warn("The Site (GitHub.com) encountered an error when handling the request. Try again later..."); break; - + default: logger.warn("This is an unknown error by the plugin! Please report this to the developer on Discord!"); break; - } - - return null; } - - ResponseBody body = response.body(); - if(body == null){ - logger.warn("GitHub.com returned an invalid/empty body!"); - return null; - } - - return body.byteStream(); - }catch(IOException ex){ - logger.warn("Encountered IOException while performing a request!", ex); - return null; + return false; } - }); + + ResponseBody body = response.body(); + if(body == null){ + logger.warn("GitHub.com returned an invalid/empty body!"); + return false; + } + + Files.copy(body.byteStream(), file, StandardCopyOption.REPLACE_EXISTING); + + return setupConfigurate(); + }catch(IOException ex){ + logger.warn("Encountered IOException while performing a request!", ex); + return false; + } } public String getFriendlyNames(List protocols, boolean majorOnly){