Skip to content

Commit

Permalink
Merge pull request #87 from Andre601/fix/86-http-classes-not-present
Browse files Browse the repository at this point in the history
First attempt at fixing #86
  • Loading branch information
Andre601 authored Jul 9, 2021
2 parents a071ba5 + ebad677 commit 9be98da
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 99 deletions.
9 changes: 9 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@
<version>4.8.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.0</version>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -112,6 +117,10 @@
<pattern>net.kyori</pattern>
<shadedPattern>com.andre601.oneversionremake.core.dependencies.adventure</shadedPattern>
</relocation>
<relocation>
<pattern>okhttp3</pattern>
<shadedPattern>com.andre601.oneversionremake.core.dependencies.okhttp</shadedPattern>
</relocation>
</relocations>
<filters>
<filter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public OneVersionRemake(PluginCore pluginCore){
this.protocolVersionResolver = new ProtocolVersionResolver(this, pluginCore.getPath());
this.parser = new Parser(this);

start();
load();
}

public ProxyLogger getProxyLogger(){
Expand Down Expand Up @@ -128,7 +128,7 @@ public <T> List<T> getPlayers(Class<T> clazz, List<String> lines, List<Integer>
}
}

private void start(){
private void load(){
loadVersion();
printBanner();

Expand All @@ -139,19 +139,34 @@ private void start(){
return;
}

if(!loadFile())
return;
if(!getProtocolVersionResolver().hasFile() || getConfigHandler().getBoolean(true, "Settings", "UpdateVersions")){
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.");
}
}
}

private void enable(){
List<Integer> 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;
}

Expand Down Expand Up @@ -212,25 +227,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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,14 @@

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;
Expand All @@ -40,47 +37,33 @@
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;
private final File path;
private final Path path;

private ConfigurationNode node = null;

public ProtocolVersionResolver(OneVersionRemake core, Path path){
this.core = core;

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 boolean loadFile(){
if(!path.isDirectory() && !path.mkdirs()){
if(!path.toFile().isDirectory() && !path.toFile().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 setupConfigurate();
return updateCache();
}

public boolean setupConfigurate(){
Expand All @@ -98,57 +81,55 @@ 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"))
.build();

HttpResponse<InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());

if(response.statusCode() != 200){
String CONNECTION_ERR = "Unable to establish connection! Status-Code %d (%s)";

switch(response.statusCode()){
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(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;
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 false;
}

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);
ResponseBody body = response.body();
if(body == null){
logger.warn("GitHub.com returned an invalid/empty body!");
return false;
}

return null;
Files.copy(body.byteStream(), file, StandardCopyOption.REPLACE_EXISTING);

return setupConfigurate();
}catch(IOException ex){
logger.warn("Encountered IOException while performing a request!", ex);
return false;
}
}

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<plugin.version>3.8.1</plugin.version>
<plugin.version>3.8.2</plugin.version>
<plugin.description>Only allow specific client versions on your Network.</plugin.description>

<maven.compiler.target>11</maven.compiler.target>
Expand Down

0 comments on commit 9be98da

Please sign in to comment.