Skip to content

Commit 083cdb1

Browse files
committed
restore for later
1 parent ea94ea9 commit 083cdb1

File tree

1 file changed

+38
-20
lines changed
  • src/main/java/io/github/ImpactDevelopment/installer/utils

1 file changed

+38
-20
lines changed

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

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import java.net.URI;
3030
import java.net.URISyntaxException;
3131
import java.nio.charset.StandardCharsets;
32+
import java.security.KeyManagementException;
33+
import java.security.NoSuchAlgorithmException;
3234
import java.security.cert.X509Certificate;
3335

3436
/**
@@ -40,35 +42,51 @@ public static String fetch(String url) {
4042
}
4143

4244
public static byte[] fetchBytes(String url) {
45+
SSLSocketFactory originalSSL = HttpsURLConnection.getDefaultSSLSocketFactory(); // for restoring later
46+
HostnameVerifier originalHost = HttpsURLConnection.getDefaultHostnameVerifier();
4347
try {
44-
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
45-
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
46-
return null;
47-
}
48-
49-
public void checkClientTrusted(X509Certificate[] certs, String authType) {
50-
}
51-
52-
public void checkServerTrusted(X509Certificate[] certs, String authType) {
53-
}
54-
}};
55-
SSLContext sc = SSLContext.getInstance("SSL");
56-
sc.init(null, trustAllCerts, new java.security.SecureRandom());
57-
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
58-
HostnameVerifier allHostsValid = new HostnameVerifier() {
59-
public boolean verify(String hostname, SSLSession session) {
60-
return true;
61-
}
62-
};
63-
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
48+
disableAllVerification();
6449
} catch (Throwable th) {
50+
System.out.println("Unable to disable https");
6551
th.printStackTrace();
6652
}
6753
System.out.println("DOWNLOADING " + url);
6854
try {
6955
return IOUtils.toByteArray(new URI(url));
7056
} catch (IOException | URISyntaxException e) {
7157
throw new RuntimeException("Unable to fetch " + url, e);
58+
} finally {
59+
try {
60+
HttpsURLConnection.setDefaultSSLSocketFactory(originalSSL); // restore to full https verification
61+
HttpsURLConnection.setDefaultHostnameVerifier(originalHost);
62+
} catch (Throwable th) {
63+
System.out.println("Unable to restore https! This could actually be a problem!");
64+
}
7265
}
7366
}
67+
68+
public static void disableAllVerification() throws NoSuchAlgorithmException, KeyManagementException {
69+
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
70+
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
71+
return null;
72+
}
73+
74+
public void checkClientTrusted(X509Certificate[] certs, String authType) {
75+
}
76+
77+
public void checkServerTrusted(X509Certificate[] certs, String authType) {
78+
}
79+
}};
80+
SSLContext sc = SSLContext.getInstance("SSL");
81+
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+
};
88+
89+
HttpsURLConnection.setDefaultSSLSocketFactory(allSSLValid);
90+
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
91+
}
7492
}

0 commit comments

Comments
 (0)