Skip to content

Commit

Permalink
Merge pull request #4 from DrParadox7/1.7.10
Browse files Browse the repository at this point in the history
Add config to fix ignore HTTPS certificates when necessary (initial commit, will rework a bit)
  • Loading branch information
HRudyPlayZ authored Dec 2, 2023
2 parents 2862ebf + 5ffba3a commit 87175b1
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/main/java/com/hrudyplayz/mcinstanceloader/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class Config {
public static int closeGameTimer;
public static int amountOfDisplayedErrors;
public static String[] successMessage;
public static boolean allowSSLCertificateBypass;

public static String CATEGORY_BEHAVIOR = "Behavior";
public static String CATEGORY_GUI = "GUI";
Expand All @@ -49,6 +50,7 @@ public static void createConfigFile() {
skipFileDisabling = config.getBoolean("Skip file disabling", CATEGORY_BEHAVIOR, false, "Whether to skip the step that disables the pack.mcinstance file and deletes the temp folder. Useful for pack devs.");
deleteInsteadOfRenaming = config.getBoolean("Delete MCInstance directly", CATEGORY_BEHAVIOR, false, "Wheter to delete the pack.mcinstance file instead of renaming it.");
disableStopModRepostsCheck = config.getBoolean("Disable StopModReposts check", CATEGORY_BEHAVIOR, false, "Whether to disable the StopModReposts check, used to prevent the use of malware sites. It's recommended to keep it enabled.");
allowSSLCertificateBypass = config.getBoolean("Allow SSL Certificate bypass", CATEGORY_BEHAVIOR, false, "Whether to allow the bypass of SSL Certificates if this cause problems. This may fix download issues in some launchers.");
disableCache = config.getBoolean("Disable the cache system", CATEGORY_BEHAVIOR, false, "Whether to disable the cache system, forcing every resource to be downloaded regardless of the cached value.");

useHttpProxy = config.getBoolean("Use proxy", CATEGORY_BEHAVIOR, false, "This will enable proxy when downing files.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import java.io.File;
import java.io.IOException;
import java.net.*;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

Expand All @@ -11,6 +15,11 @@
import com.hrudyplayz.mcinstanceloader.Config;
import com.hrudyplayz.mcinstanceloader.Main;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

/**
An helper class to download files from the internet.
Expand All @@ -23,6 +32,8 @@ public class WebHelper {
public static String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) twitch-desktop-electron-platform/1.0.0 Chrome/73.0.3683.121 Electron/5.0.12 Safari/537.36 desklight/8.51.0";
public static String REFERER = "https://www.google.com";

public static Boolean checkSSLCertificates = true;

/**
Downloads a specific file from an internet address and saves it to a given location.
Expand Down Expand Up @@ -70,19 +81,28 @@ private static boolean downloadFile(String fileURL, String savePath, boolean don

File file = new File(savePath);
FileUtils.copyInputStreamToFile(connection.getInputStream(), file);
file.canRead();
if (doneIOException)
LogHelper.info("Successfully downloaded file on the second attempt.");
return true;
}
catch (IOException e) {
if (!doneIOException) {
LogHelper.info("An error occured while downloading the file, trying again...");
LogHelper.info("An error occurred while downloading the file, trying again...");
try {
TimeUnit.MILLISECONDS.sleep(1000);
}
catch (InterruptedException ignore) {}

if (Config.allowSSLCertificateBypass && checkSSLCertificates) {
LogHelper.info("Attempting to resolve issues by bypassing SSL certificates.");
disableSSLCertificateChecking();
}

return downloadFile(fileURL, savePath, true);
}
else {

Main.errorContext = "There was an issue writing to file.";
e.printStackTrace();
return false;
Expand Down Expand Up @@ -146,4 +166,37 @@ public static boolean downloadFile(String fileURL, String savePath, String[] fol

return downloadFile(fileURL, savePath);
}

/**
* Disables the SSL certificate checking for new instances of {@link HttpsURLConnection} This has been created to
* aid testing on a local box, not for use on production.
*/
private static void disableSSLCertificateChecking() {
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}

@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
// Not implemented
}

@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
// Not implemented
}
} };

try {
SSLContext sc = SSLContext.getInstance("TLS");

sc.init(null, trustAllCerts, new java.security.SecureRandom());

HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (KeyManagementException | NoSuchAlgorithmException e) {
e.printStackTrace();
}
checkSSLCertificates = false;
}
}

0 comments on commit 87175b1

Please sign in to comment.