diff options
| author | Cow <cow@volloeko.de> | 2022-10-22 14:06:49 +0200 |
|---|---|---|
| committer | Cow <cow@volloeko.de> | 2022-10-22 14:06:49 +0200 |
| commit | feea22f251cd188d91b843ff9348e057e0f9e91d (patch) | |
| tree | 10f09a15e028d4301ae093e3d6827dbdbb39e904 /src/main/java/de/cowtipper/cowlection/util | |
| parent | 0e2e8859a2652a781a381267d9d327d29df92c68 (diff) | |
| download | Cowlection-feea22f251cd188d91b843ff9348e057e0f9e91d.tar.gz Cowlection-feea22f251cd188d91b843ff9348e057e0f9e91d.tar.bz2 Cowlection-feea22f251cd188d91b843ff9348e057e0f9e91d.zip | |
Added Let's Encrypt support for ancient Java versions
Diffstat (limited to 'src/main/java/de/cowtipper/cowlection/util')
| -rw-r--r-- | src/main/java/de/cowtipper/cowlection/util/ApiUtils.java | 66 |
1 files changed, 43 insertions, 23 deletions
diff --git a/src/main/java/de/cowtipper/cowlection/util/ApiUtils.java b/src/main/java/de/cowtipper/cowlection/util/ApiUtils.java index 663936f..1397a36 100644 --- a/src/main/java/de/cowtipper/cowlection/util/ApiUtils.java +++ b/src/main/java/de/cowtipper/cowlection/util/ApiUtils.java @@ -14,9 +14,14 @@ import de.cowtipper.cowlection.data.*; import de.cowtipper.cowlection.error.ApiAskPolitelyErrorEvent; import de.cowtipper.cowlection.error.ApiHttpErrorEvent; import de.cowtipper.cowlection.error.ApiHttpErrorException; +import net.minecraft.util.EnumChatFormatting; import net.minecraftforge.common.MinecraftForge; import org.apache.http.HttpStatus; +import sun.security.provider.certpath.SunCertPathBuilderException; +import sun.security.validator.ValidatorException; +import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.SSLHandshakeException; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; @@ -194,31 +199,46 @@ public class ApiUtils { } private static BufferedReader makeApiCall(String url) throws IOException { - HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); - connection.setConnectTimeout(5000); - connection.setReadTimeout(8000); - connection.addRequestProperty("User-Agent", "Forge Mod " + Cowlection.MODNAME + "/" + Cowlection.VERSION + " (" + Cowlection.GITURL + ")"); - - connection.getResponseCode(); - if (connection.getResponseCode() == HttpStatus.SC_NO_CONTENT) { // http status 204 - return null; - } else if (connection.getResponseCode() == HttpStatus.SC_BAD_GATEWAY && url.startsWith("https://api.hypixel.net/")) { // http status 502 (cloudflare) - throw new ApiHttpErrorException("Couldn't contact Hypixel API (502 Bad Gateway). API might be down, check https://status.hypixel.net for info.", "https://status.hypixel.net"); - } else if (connection.getResponseCode() == HttpStatus.SC_SERVICE_UNAVAILABLE) { // http status 503 Service Unavailable - int queryParamStart = url.indexOf('?', 10); - String baseUrl = queryParamStart > 0 ? url.substring(0, queryParamStart) : url; - throw new ApiHttpErrorException("Couldn't contact the API (503 Service unavailable). API might be down, or you might be blocked by Cloudflare, check if you can reach: " + baseUrl, url); - } else if (connection.getResponseCode() == HttpStatus.SC_BAD_GATEWAY && url.startsWith("https://moulberry.codes/")) { // http status 502 (cloudflare) - throw new ApiHttpErrorException("Couldn't contact Moulberry's API (502 Bad Gateway). API might be down, check if " + LOWEST_BINS + " is reachable.", LOWEST_BINS); - } else { - BufferedReader reader; - InputStream errorStream = connection.getErrorStream(); - if (errorStream != null) { - reader = new BufferedReader(new InputStreamReader(errorStream)); + try { + HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); + if (CredentialStorage.sslContext != null && connection instanceof HttpsURLConnection) { + ((HttpsURLConnection) connection).setSSLSocketFactory(CredentialStorage.sslContext.getSocketFactory()); + } + connection.setConnectTimeout(5000); + connection.setReadTimeout(8000); + connection.addRequestProperty("User-Agent", "Forge Mod " + Cowlection.MODNAME + "/" + Cowlection.VERSION + " (" + Cowlection.GITURL + ")"); + + connection.getResponseCode(); + if (connection.getResponseCode() == HttpStatus.SC_NO_CONTENT) { // http status 204 + return null; + } else if (connection.getResponseCode() == HttpStatus.SC_BAD_GATEWAY && url.startsWith("https://api.hypixel.net/")) { // http status 502 (cloudflare) + throw new ApiHttpErrorException("Couldn't contact Hypixel API (502 Bad Gateway). API might be down, check https://status.hypixel.net for info.", "https://status.hypixel.net"); + } else if (connection.getResponseCode() == HttpStatus.SC_SERVICE_UNAVAILABLE) { // http status 503 Service Unavailable + int queryParamStart = url.indexOf('?', 10); + String baseUrl = queryParamStart > 0 ? url.substring(0, queryParamStart) : url; + throw new ApiHttpErrorException("Couldn't contact the API (503 Service unavailable). API might be down, or you might be blocked by Cloudflare, check if you can reach: " + baseUrl, url); + } else if (connection.getResponseCode() == HttpStatus.SC_BAD_GATEWAY && url.startsWith("https://moulberry.codes/")) { // http status 502 (cloudflare) + throw new ApiHttpErrorException("Couldn't contact Moulberry's API (502 Bad Gateway). API might be down, check if " + LOWEST_BINS + " is reachable.", LOWEST_BINS); } else { - reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); + BufferedReader reader; + InputStream errorStream = connection.getErrorStream(); + if (errorStream != null) { + reader = new BufferedReader(new InputStreamReader(errorStream)); + } else { + reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); + } + return reader; + } + } catch (SSLHandshakeException e) { + Throwable cause = e.getCause(); + if (cause instanceof ValidatorException && cause.getCause() instanceof SunCertPathBuilderException) { + throw new ApiHttpErrorException("" + EnumChatFormatting.DARK_RED + EnumChatFormatting.BOLD + " ! " + + EnumChatFormatting.RED + "Java is outdated and doesn't support Let's Encrypt certificates (out of the box). A game restart might fix this issue. If the problem persists, open a ticket on the Cowshed discord server.", Cowlection.INVITE_URL); + } else { + // not a newer https related issue, thus rethrow exception: + throw e; } - return reader; } } + } |
