Disable SSL validation for YaSOnlineServer.

Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
Jeffrey Han 2016-10-13 01:17:59 -04:00
parent 93c8ab96f1
commit 76c8efb0c2
2 changed files with 37 additions and 0 deletions

View File

@ -43,6 +43,7 @@ import java.net.URL;
import java.nio.ByteBuffer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
@ -50,6 +51,10 @@ import java.util.Scanner;
import java.util.jar.JarFile;
import javax.imageio.ImageIO;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.json.JSONArray;
import org.json.JSONException;
@ -561,4 +566,28 @@ public class Utils {
return null;
}
}
/**
* Switches validation of SSL certificates on or off by installing a default
* all-trusting {@link TrustManager}.
* @param enabled whether to validate SSL certificates
* @author neu242 (http://stackoverflow.com/a/876785)
*/
public static void setSSLCertValidation(boolean enabled) {
// create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
@Override public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; }
@Override public void checkClientTrusted(X509Certificate[] certs, String authType) {}
@Override public void checkServerTrusted(X509Certificate[] certs, String authType) {}
}
};
// install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, enabled ? null : trustAllCerts, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {}
}
}

View File

@ -93,6 +93,8 @@ public class YaSOnlineServer extends DownloadServer {
*/
private String getDownloadURLFromMapData(int beatmapSetID) throws IOException {
try {
Utils.setSSLCertValidation(false);
// read JSON
String search = String.format(DOWNLOAD_URL, beatmapSetID);
JSONObject json = Utils.readJsonObjectFromUrl(new URL(search));
@ -114,6 +116,8 @@ public class YaSOnlineServer extends DownloadServer {
} catch (MalformedURLException | UnsupportedEncodingException e) {
ErrorHandler.error(String.format("Problem retrieving download URL for beatmap '%d'.", beatmapSetID), e, true);
return null;
} finally {
Utils.setSSLCertValidation(true);
}
}
@ -121,6 +125,8 @@ public class YaSOnlineServer extends DownloadServer {
public DownloadNode[] resultList(String query, int page, boolean rankedOnly) throws IOException {
DownloadNode[] nodes = null;
try {
Utils.setSSLCertValidation(false);
// read JSON
String search;
boolean isSearch;
@ -181,6 +187,8 @@ public class YaSOnlineServer extends DownloadServer {
this.totalResults = maxServerID;
} catch (MalformedURLException | UnsupportedEncodingException e) {
ErrorHandler.error(String.format("Problem loading result list for query '%s'.", query), e, true);
} finally {
Utils.setSSLCertValidation(true);
}
return nodes;
}