Skip to content

Commit 87fa791

Browse files
committed
https to https fallback
1 parent 2a07e1f commit 87fa791

File tree

3 files changed

+34
-28
lines changed

3 files changed

+34
-28
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
### Added
1010
- The ability to override CLI argument defaults from a properties file in the jar. This means the Donator Installer can be generated dynamically by the website.
11+
- A fallback from unauthenticated HTTPS to HTTPS. I do not understand why, but it made it work for someone.
1112

1213
## [0.8.3] - 2020-02-21
1314

src/main/java/io/github/ImpactDevelopment/installer/Args.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,14 @@ private static Properties getProperties(String filename) throws IOException {
178178

179179
// Convert a string value to a primitive type
180180
// Won't work for non-primitive types, but luckily all our args are primitives
181-
private static Object toType(Class<?> type, String value ) {
182-
if(Boolean.class == type || Boolean.TYPE == type) return Boolean.parseBoolean(value);
183-
if(Byte.class == type || Byte.TYPE == type) return Byte.parseByte(value);
184-
if(Short.class == type || Short.TYPE == type) return Short.parseShort(value);
185-
if(Integer.class == type || Integer.TYPE == type) return Integer.parseInt(value);
186-
if(Long.class == type || Long.TYPE == type) return Long.parseLong(value);
187-
if(Float.class == type || Float.TYPE == type) return Float.parseFloat(value);
188-
if(Double.class == type || Double.TYPE == type) return Double.parseDouble(value);
181+
private static Object toType(Class<?> type, String value) {
182+
if (Boolean.class == type || Boolean.TYPE == type) return Boolean.parseBoolean(value);
183+
if (Byte.class == type || Byte.TYPE == type) return Byte.parseByte(value);
184+
if (Short.class == type || Short.TYPE == type) return Short.parseShort(value);
185+
if (Integer.class == type || Integer.TYPE == type) return Integer.parseInt(value);
186+
if (Long.class == type || Long.TYPE == type) return Long.parseLong(value);
187+
if (Float.class == type || Float.TYPE == type) return Float.parseFloat(value);
188+
if (Double.class == type || Double.TYPE == type) return Double.parseDouble(value);
189189
return value;
190190
}
191191
}

src/main/java/io/github/ImpactDevelopment/installer/utils/Fetcher.java

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@
2525
import org.apache.commons.io.IOUtils;
2626

2727
import javax.net.ssl.*;
28-
import java.io.IOException;
2928
import java.net.URI;
30-
import java.net.URISyntaxException;
3129
import java.nio.charset.StandardCharsets;
3230
import java.security.KeyManagementException;
3331
import java.security.NoSuchAlgorithmException;
@@ -44,17 +42,28 @@ public static String fetch(String url) {
4442
public static byte[] fetchBytes(String url) {
4543
SSLSocketFactory originalSSL = HttpsURLConnection.getDefaultSSLSocketFactory(); // for restoring later
4644
HostnameVerifier originalHost = HttpsURLConnection.getDefaultHostnameVerifier();
47-
try {
48-
disableAllVerification();
49-
} catch (Throwable th) {
50-
System.out.println("Unable to disable https");
51-
th.printStackTrace();
52-
}
5345
System.out.println("DOWNLOADING " + url);
5446
try {
55-
return IOUtils.toByteArray(new URI(url));
56-
} catch (IOException | URISyntaxException e) {
57-
throw new RuntimeException("Unable to fetch " + url, e);
47+
try {
48+
return IOUtils.toByteArray(new URI(url));
49+
} catch (Throwable th) {
50+
th.printStackTrace();
51+
}
52+
disableHTTPSPart1();
53+
try {
54+
return IOUtils.toByteArray(new URI(url));
55+
} catch (Throwable th) {
56+
th.printStackTrace();
57+
}
58+
disableHTTPSPart2();
59+
try {
60+
return IOUtils.toByteArray(new URI(url));
61+
} catch (Throwable th) {
62+
th.printStackTrace();
63+
throw new RuntimeException("Unable to fetch " + url, th);
64+
}
65+
} catch (NoSuchAlgorithmException | KeyManagementException e) {
66+
throw new RuntimeException(e);
5867
} finally {
5968
try {
6069
HttpsURLConnection.setDefaultSSLSocketFactory(originalSSL); // restore to full https verification
@@ -65,7 +74,7 @@ public static byte[] fetchBytes(String url) {
6574
}
6675
}
6776

68-
public static void disableAllVerification() throws NoSuchAlgorithmException, KeyManagementException {
77+
public static void disableHTTPSPart1() throws NoSuchAlgorithmException, KeyManagementException {
6978
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
7079
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
7180
return null;
@@ -79,14 +88,10 @@ public void checkServerTrusted(X509Certificate[] certs, String authType) {
7988
}};
8089
SSLContext sc = SSLContext.getInstance("SSL");
8190
sc.init(null, trustAllCerts, new java.security.SecureRandom());
82-
SSLSocketFactory allSSLValid = sc.getSocketFactory();
83-
HostnameVerifier allHostsValid = new HostnameVerifier() {
84-
public boolean verify(String hostname, SSLSession session) {
85-
return true;
86-
}
87-
};
91+
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
92+
}
8893

89-
HttpsURLConnection.setDefaultSSLSocketFactory(allSSLValid);
90-
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
94+
public static void disableHTTPSPart2() {
95+
HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> true);
9196
}
9297
}

0 commit comments

Comments
 (0)