mirror of
https://github.com/HMCL-dev/HMCL.git
synced 2025-03-07 17:36:52 +08:00
* Fix #2375 * Fix * Fix * Simply codes.
This commit is contained in:
parent
5defff2bb0
commit
333fd0ef80
@ -22,8 +22,7 @@ import com.google.gson.annotations.SerializedName;
|
|||||||
import org.jackhuang.hmcl.download.DownloadProvider;
|
import org.jackhuang.hmcl.download.DownloadProvider;
|
||||||
import org.jackhuang.hmcl.task.FileDownloadTask;
|
import org.jackhuang.hmcl.task.FileDownloadTask;
|
||||||
import org.jackhuang.hmcl.task.FileDownloadTask.IntegrityCheck;
|
import org.jackhuang.hmcl.task.FileDownloadTask.IntegrityCheck;
|
||||||
import org.jackhuang.hmcl.util.gson.JsonUtils;
|
import org.jackhuang.hmcl.util.io.HttpRequest;
|
||||||
import org.jackhuang.hmcl.util.io.NetworkUtils;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
@ -102,7 +101,7 @@ public class AuthlibInjectorDownloader implements AuthlibInjectorArtifactProvide
|
|||||||
Optional.ofNullable(latest.checksums.get("sha256"))
|
Optional.ofNullable(latest.checksums.get("sha256"))
|
||||||
.map(checksum -> new IntegrityCheck("SHA-256", checksum))
|
.map(checksum -> new IntegrityCheck("SHA-256", checksum))
|
||||||
.orElse(null))
|
.orElse(null))
|
||||||
.run();
|
.run();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new IOException("Failed to download authlib-injector", e);
|
throw new IOException("Failed to download authlib-injector", e);
|
||||||
}
|
}
|
||||||
@ -112,10 +111,7 @@ public class AuthlibInjectorDownloader implements AuthlibInjectorArtifactProvide
|
|||||||
|
|
||||||
private AuthlibInjectorVersionInfo getLatestArtifactInfo() throws IOException {
|
private AuthlibInjectorVersionInfo getLatestArtifactInfo() throws IOException {
|
||||||
try {
|
try {
|
||||||
return JsonUtils.fromNonNullJson(
|
return HttpRequest.GET(downloadProvider.get().injectURL(LATEST_BUILD_URL)).getJson(AuthlibInjectorVersionInfo.class);
|
||||||
NetworkUtils.doGet(
|
|
||||||
new URL(downloadProvider.get().injectURL(LATEST_BUILD_URL))),
|
|
||||||
AuthlibInjectorVersionInfo.class);
|
|
||||||
} catch (JsonParseException e) {
|
} catch (JsonParseException e) {
|
||||||
throw new IOException("Malformed response", e);
|
throw new IOException("Malformed response", e);
|
||||||
}
|
}
|
||||||
|
@ -17,12 +17,9 @@
|
|||||||
*/
|
*/
|
||||||
package org.jackhuang.hmcl.auth.authlibinjector;
|
package org.jackhuang.hmcl.auth.authlibinjector;
|
||||||
|
|
||||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
|
||||||
import static java.util.Collections.emptyMap;
|
import static java.util.Collections.emptyMap;
|
||||||
import static org.jackhuang.hmcl.util.Lang.tryCast;
|
import static org.jackhuang.hmcl.util.Lang.tryCast;
|
||||||
import static org.jackhuang.hmcl.util.Logging.LOG;
|
import static org.jackhuang.hmcl.util.Logging.LOG;
|
||||||
import static org.jackhuang.hmcl.util.io.IOUtils.readFullyAsByteArray;
|
|
||||||
import static org.jackhuang.hmcl.util.io.IOUtils.readFullyWithoutClosing;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
@ -35,6 +32,8 @@ import java.util.Optional;
|
|||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
import org.jackhuang.hmcl.auth.yggdrasil.YggdrasilService;
|
import org.jackhuang.hmcl.auth.yggdrasil.YggdrasilService;
|
||||||
|
import org.jackhuang.hmcl.util.io.HttpRequest;
|
||||||
|
import org.jackhuang.hmcl.util.io.IOUtils;
|
||||||
import org.jackhuang.hmcl.util.javafx.ObservableHelper;
|
import org.jackhuang.hmcl.util.javafx.ObservableHelper;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@ -77,7 +76,7 @@ public class AuthlibInjectorServer implements Observable {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
AuthlibInjectorServer server = new AuthlibInjectorServer(url);
|
AuthlibInjectorServer server = new AuthlibInjectorServer(url);
|
||||||
server.refreshMetadata(readFullyWithoutClosing(conn.getInputStream()));
|
server.refreshMetadata(IOUtils.readFullyAsStringWithClosing(conn.getInputStream()));
|
||||||
return server;
|
return server;
|
||||||
} finally {
|
} finally {
|
||||||
conn.disconnect();
|
conn.disconnect();
|
||||||
@ -159,12 +158,11 @@ public class AuthlibInjectorServer implements Observable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void refreshMetadata() throws IOException {
|
public void refreshMetadata() throws IOException {
|
||||||
refreshMetadata(readFullyAsByteArray(new URL(url).openStream()));
|
refreshMetadata(HttpRequest.GET(url).getString());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void refreshMetadata(byte[] rawResponse) throws IOException {
|
private void refreshMetadata(String text) throws IOException {
|
||||||
long timestamp = System.currentTimeMillis();
|
long timestamp = System.currentTimeMillis();
|
||||||
String text = new String(rawResponse, UTF_8);
|
|
||||||
try {
|
try {
|
||||||
setMetadataResponse(text, timestamp);
|
setMetadataResponse(text, timestamp);
|
||||||
} catch (JsonParseException e) {
|
} catch (JsonParseException e) {
|
||||||
|
@ -29,7 +29,6 @@ import java.io.OutputStream;
|
|||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.SocketTimeoutException;
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
@ -218,16 +217,21 @@ public abstract class HttpRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static String getStringWithRetry(ExceptionalSupplier<String, IOException> supplier, int retryTimes) throws IOException {
|
private static String getStringWithRetry(ExceptionalSupplier<String, IOException> supplier, int retryTimes) throws IOException {
|
||||||
SocketTimeoutException exception = null;
|
Throwable exception = null;
|
||||||
for (int i = 0; i < retryTimes; i++) {
|
for (int i = 0; i < retryTimes; i++) {
|
||||||
try {
|
try {
|
||||||
return supplier.get();
|
return supplier.get();
|
||||||
} catch (SocketTimeoutException e) {
|
} catch (Throwable e) {
|
||||||
exception = e;
|
exception = e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (exception != null)
|
if (exception != null) {
|
||||||
throw exception;
|
if (exception instanceof IOException) {
|
||||||
|
throw (IOException) exception;
|
||||||
|
} else {
|
||||||
|
throw new IOException(exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
throw new IOException("retry 0");
|
throw new IOException("retry 0");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,6 +44,12 @@ public final class IOUtils {
|
|||||||
return result.toByteArray();
|
return result.toByteArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String readFullyAsStringWithClosing(InputStream stream) throws IOException {
|
||||||
|
ByteArrayOutputStream result = new ByteArrayOutputStream(Math.max(stream.available(), 32));
|
||||||
|
copyTo(stream, result);
|
||||||
|
return result.toString("UTF-8");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read all bytes to a buffer from given input stream, and close the input stream finally.
|
* Read all bytes to a buffer from given input stream, and close the input stream finally.
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user