Disable SSL validation for YaSOnlineServer.
Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
parent
93c8ab96f1
commit
76c8efb0c2
|
@ -43,6 +43,7 @@ import java.net.URL;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.security.cert.X509Certificate;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
@ -50,6 +51,10 @@ import java.util.Scanner;
|
||||||
import java.util.jar.JarFile;
|
import java.util.jar.JarFile;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
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.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
|
@ -561,4 +566,28 @@ public class Utils {
|
||||||
return null;
|
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) {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -93,6 +93,8 @@ public class YaSOnlineServer extends DownloadServer {
|
||||||
*/
|
*/
|
||||||
private String getDownloadURLFromMapData(int beatmapSetID) throws IOException {
|
private String getDownloadURLFromMapData(int beatmapSetID) throws IOException {
|
||||||
try {
|
try {
|
||||||
|
Utils.setSSLCertValidation(false);
|
||||||
|
|
||||||
// read JSON
|
// read JSON
|
||||||
String search = String.format(DOWNLOAD_URL, beatmapSetID);
|
String search = String.format(DOWNLOAD_URL, beatmapSetID);
|
||||||
JSONObject json = Utils.readJsonObjectFromUrl(new URL(search));
|
JSONObject json = Utils.readJsonObjectFromUrl(new URL(search));
|
||||||
|
@ -114,6 +116,8 @@ public class YaSOnlineServer extends DownloadServer {
|
||||||
} catch (MalformedURLException | UnsupportedEncodingException e) {
|
} catch (MalformedURLException | UnsupportedEncodingException e) {
|
||||||
ErrorHandler.error(String.format("Problem retrieving download URL for beatmap '%d'.", beatmapSetID), e, true);
|
ErrorHandler.error(String.format("Problem retrieving download URL for beatmap '%d'.", beatmapSetID), e, true);
|
||||||
return null;
|
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 {
|
public DownloadNode[] resultList(String query, int page, boolean rankedOnly) throws IOException {
|
||||||
DownloadNode[] nodes = null;
|
DownloadNode[] nodes = null;
|
||||||
try {
|
try {
|
||||||
|
Utils.setSSLCertValidation(false);
|
||||||
|
|
||||||
// read JSON
|
// read JSON
|
||||||
String search;
|
String search;
|
||||||
boolean isSearch;
|
boolean isSearch;
|
||||||
|
@ -181,6 +187,8 @@ public class YaSOnlineServer extends DownloadServer {
|
||||||
this.totalResults = maxServerID;
|
this.totalResults = maxServerID;
|
||||||
} catch (MalformedURLException | UnsupportedEncodingException e) {
|
} catch (MalformedURLException | UnsupportedEncodingException e) {
|
||||||
ErrorHandler.error(String.format("Problem loading result list for query '%s'.", query), e, true);
|
ErrorHandler.error(String.format("Problem loading result list for query '%s'.", query), e, true);
|
||||||
|
} finally {
|
||||||
|
Utils.setSSLCertValidation(true);
|
||||||
}
|
}
|
||||||
return nodes;
|
return nodes;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user