From 8adba59095c16eadfbfa3d0dcee839e5a38e2ae4 Mon Sep 17 00:00:00 2001 From: Haowei Wen Date: Fri, 10 Sep 2021 17:22:59 +0800 Subject: [PATCH] implement #136 --- .../authlibinjector/AuthlibInjector.java | 43 +++++++++++-------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/src/main/java/moe/yushi/authlibinjector/AuthlibInjector.java b/src/main/java/moe/yushi/authlibinjector/AuthlibInjector.java index d5ab123..22e22c7 100644 --- a/src/main/java/moe/yushi/authlibinjector/AuthlibInjector.java +++ b/src/main/java/moe/yushi/authlibinjector/AuthlibInjector.java @@ -33,6 +33,7 @@ import java.io.UncheckedIOException; import java.lang.instrument.Instrumentation; import java.net.HttpURLConnection; import java.net.URL; +import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; @@ -102,15 +103,31 @@ public final class AuthlibInjector { } } - private static Optional getPrefetchedResponse() { - String prefetched = System.getProperty("authlibinjector.yggdrasil.prefetched"); - if (prefetched == null) { - prefetched = System.getProperty("org.to2mbn.authlibinjector.config.prefetched"); - if (prefetched != null) { - log(WARNING, "'-Dorg.to2mbn.authlibinjector.config.prefetched=' is deprecated, use '-Dauthlibinjector.yggdrasil.prefetched=' instead"); + private static Optional getPrefetchedMetadata() { + String metadataFile = System.getProperty("authlibinjector.prefetch.file"); + if (metadataFile != null) { + try { + return Optional.of(new String(Files.readAllBytes(Paths.get(metadataFile)), UTF_8)); + } catch (IOException e) { + log(ERROR, "Unable read metadata file [" + metadataFile + "]: " + e); + throw new InitializationException(e); } } - return Optional.ofNullable(prefetched); + + String encoded = System.getProperty("authlibinjector.yggdrasil.prefetched"); + if (encoded != null) { + log(WARNING, "'-Dauthlibinjector.yggdrasil.prefetched=' is deprecated, use '-Dauthlibinjector.prefetch.file=' instead"); + try { + return Optional.of(new String(Base64.getDecoder().decode(removeNewLines(encoded)), UTF_8)); + } catch (IllegalArgumentException e) { + log(ERROR, "Unable to decode metadata: " + e + "\n" + + "Encoded metadata:\n" + + encoded); + throw new InitializationException(e); + } + } + + return Optional.empty(); } private static APIMetadata fetchAPIMetadata(String apiUrl) { @@ -125,18 +142,10 @@ public final class AuthlibInjector { String metadataResponse; - Optional prefetched = getPrefetchedResponse(); + Optional prefetched = getPrefetchedMetadata(); if (prefetched.isPresent()) { - log(DEBUG, "Prefetched metadata detected"); - try { - metadataResponse = new String(Base64.getDecoder().decode(removeNewLines(prefetched.get())), UTF_8); - } catch (IllegalArgumentException e) { - log(ERROR, "Unable to decode metadata: " + e + "\n" - + "Encoded metadata:\n" - + prefetched.get()); - throw new InitializationException(e); - } + metadataResponse = prefetched.get(); } else {