From c7589e4e3c0d078723b27d6f577661b1bbce3f23 Mon Sep 17 00:00:00 2001 From: huangyuhui Date: Sun, 4 Feb 2018 12:33:09 +0800 Subject: [PATCH] Fix official optifine download --- .../hmcl/auth/yggdrasil/YggdrasilAccount.java | 4 +- .../hmcl/download/RemoteVersion.java | 2 +- .../optifine/OptiFineDownloadFormatter.java | 57 ------------------- .../optifine/OptiFineRemoteVersion.java | 37 ++++++++++++ .../optifine/OptiFineVersionList.java | 18 +++++- .../org/jackhuang/hmcl/util/NetworkUtils.java | 9 ++- 6 files changed, 61 insertions(+), 66 deletions(-) delete mode 100644 HMCLCore/src/main/java/org/jackhuang/hmcl/download/optifine/OptiFineDownloadFormatter.java create mode 100644 HMCLCore/src/main/java/org/jackhuang/hmcl/download/optifine/OptiFineRemoteVersion.java diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/yggdrasil/YggdrasilAccount.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/yggdrasil/YggdrasilAccount.java index 4de9fddc3..74668fc1f 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/yggdrasil/YggdrasilAccount.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/yggdrasil/YggdrasilAccount.java @@ -265,8 +265,8 @@ public final class YggdrasilAccount extends Account { return "YggdrasilAccount[username=" + getUsername() + "]"; } - private static final String BASE_URL = "http://localhost:8080/authserver/"; //"https://authserver.mojang.com/"; - private static final String BASE_PROFILE = "http://localhost:8080/sessionserver/session/minecraft/profile/"; //"https://sessionserver.mojang.com/session/minecraft/profile/"; + private static final String BASE_URL = "https://authserver.mojang.com/"; + private static final String BASE_PROFILE = "https://sessionserver.mojang.com/session/minecraft/profile/"; private static final URL ROUTE_AUTHENTICATE = NetworkUtils.toURL(BASE_URL + "authenticate"); private static final URL ROUTE_REFRESH = NetworkUtils.toURL(BASE_URL + "refresh"); private static final URL ROUTE_VALIDATE = NetworkUtils.toURL(BASE_URL + "validate"); diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/RemoteVersion.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/RemoteVersion.java index 2e5152006..2280ae6e9 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/RemoteVersion.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/RemoteVersion.java @@ -27,7 +27,7 @@ import java.util.Objects; * * @author huangyuhui */ -public final class RemoteVersion implements Comparable> { +public class RemoteVersion implements Comparable> { private final String gameVersion; private final String selfVersion; diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/optifine/OptiFineDownloadFormatter.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/optifine/OptiFineDownloadFormatter.java deleted file mode 100644 index f2ac81f38..000000000 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/optifine/OptiFineDownloadFormatter.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Hello Minecraft! Launcher. - * Copyright (C) 2017 huangyuhui - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see {http://www.gnu.org/licenses/}. - */ -package org.jackhuang.hmcl.download.optifine; - -import org.jackhuang.hmcl.task.TaskResult; -import org.jackhuang.hmcl.util.NetworkUtils; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** - * - * @author huangyuhui - */ -public final class OptiFineDownloadFormatter extends TaskResult { - - private final String url; - - public OptiFineDownloadFormatter(String url) { - this.url = url; - } - - @Override - public void execute() throws Exception { - String result = null; - String content = NetworkUtils.doGet(NetworkUtils.toURL(url)); - Matcher m = PATTERN.matcher(content); - while (m.find()) - result = m.group(1); - if (result == null) - throw new IllegalStateException("Cannot find version in " + content); - setResult("http://optifine.net/downloadx?f=OptiFine" + result); - } - - @Override - public String getId() { - return ID; - } - - public static final String ID = "optifine_formatter"; - private static final Pattern PATTERN = Pattern.compile("\"downloadx\\?f=OptiFine(.*)\""); -} diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/optifine/OptiFineRemoteVersion.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/optifine/OptiFineRemoteVersion.java new file mode 100644 index 000000000..a28073e34 --- /dev/null +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/optifine/OptiFineRemoteVersion.java @@ -0,0 +1,37 @@ +/* + * Hello Minecraft! Launcher. + * Copyright (C) 2017 huangyuhui + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see {http://www.gnu.org/licenses/}. + */ +package org.jackhuang.hmcl.download.optifine; + +import org.jackhuang.hmcl.download.RemoteVersion; + +import java.util.function.Supplier; + +public class OptiFineRemoteVersion extends RemoteVersion { + private final Supplier url; + + public OptiFineRemoteVersion(String gameVersion, String selfVersion, Supplier url, Void tag) { + super(gameVersion, selfVersion, "", tag); + + this.url = url; + } + + @Override + public String getUrl() { + return url.get(); + } +} diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/optifine/OptiFineVersionList.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/optifine/OptiFineVersionList.java index 659cec84b..71c485b31 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/download/optifine/OptiFineVersionList.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/download/optifine/OptiFineVersionList.java @@ -22,6 +22,7 @@ import org.jackhuang.hmcl.download.RemoteVersion; import org.jackhuang.hmcl.download.VersionList; import org.jackhuang.hmcl.task.GetTask; import org.jackhuang.hmcl.task.Task; +import org.jackhuang.hmcl.util.Lang; import org.jackhuang.hmcl.util.NetworkUtils; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -30,6 +31,7 @@ import org.w3c.dom.NodeList; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import java.io.ByteArrayInputStream; +import java.io.IOException; import java.util.Collection; import java.util.Collections; import java.util.regex.Matcher; @@ -42,6 +44,8 @@ import java.util.regex.Pattern; public final class OptiFineVersionList extends VersionList { private static final Pattern PATTERN = Pattern.compile("OptiFine (.*?) "); + private static final Pattern LINK_PATTERN = Pattern.compile("\"downloadx\\?f=OptiFine(.*)\""); + public static final OptiFineVersionList INSTANCE = new OptiFineVersionList(); private OptiFineVersionList() { @@ -89,7 +93,9 @@ public final class OptiFineVersionList extends VersionList { gameVersion = matcher.group(1); if (gameVersion == null) continue; - versions.put(gameVersion, new RemoteVersion<>(gameVersion, version, url, null)); + + String finalURL = url; + versions.put(gameVersion, new OptiFineRemoteVersion(gameVersion, version, Lang.hideException(() -> getLink(finalURL)), null)); } } } @@ -97,4 +103,14 @@ public final class OptiFineVersionList extends VersionList { }; } + public static String getLink(String url) throws IOException { + String result = null; + String content = NetworkUtils.doGet(NetworkUtils.toURL(url)); + Matcher m = LINK_PATTERN.matcher(content); + while (m.find()) + result = m.group(1); + if (result == null) + throw new IllegalStateException("Cannot find version in " + content); + return "http://optifine.net/downloadx?f=OptiFine" + result; + } } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/NetworkUtils.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/NetworkUtils.java index bdd6d7c2b..a5546bd24 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/NetworkUtils.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/NetworkUtils.java @@ -75,20 +75,19 @@ public final class NetworkUtils { SSLContext c = SSLContext.getInstance("SSL"); c.init(null, new X509TrustManager[] { XTM }, new SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(c.getSocketFactory()); - } catch (GeneralSecurityException e) { + } catch (GeneralSecurityException ignore) { } HttpsURLConnection.setDefaultHostnameVerifier(HNV); } - private static Supplier userAgentSupplier = () -> RandomUserAgent.randomUserAgent(); + private static Supplier userAgentSupplier = RandomUserAgent::randomUserAgent; public static String getUserAgent() { return userAgentSupplier.get(); } public static void setUserAgentSupplier(Supplier userAgentSupplier) { - Objects.requireNonNull(userAgentSupplier); - NetworkUtils.userAgentSupplier = userAgentSupplier; + NetworkUtils.userAgentSupplier = Objects.requireNonNull(userAgentSupplier); } public static HttpURLConnection createConnection(URL url, Proxy proxy) throws IOException { @@ -103,7 +102,7 @@ public final class NetworkUtils { } public static String doGet(URL url) throws IOException { - return IOUtils.readFullyAsString(url.openConnection().getInputStream()); + return IOUtils.readFullyAsString(createConnection(url, Proxy.NO_PROXY).getInputStream()); } public static String doGet(URL url, Proxy proxy) throws IOException {