diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/Metadata.java b/HMCL/src/main/java/org/jackhuang/hmcl/Metadata.java index d7966d8276..5b5a149a19 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/Metadata.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/Metadata.java @@ -10,7 +10,9 @@ import org.jetbrains.annotations.Nullable; import java.nio.file.Files; import java.nio.file.Path; +import java.util.ArrayList; import java.util.EnumSet; +import java.util.List; /** * Stores metadata about this application. @@ -160,6 +162,26 @@ public final class Metadata { return DEFAULT_CLIENT_FILE_API; } + /// The built-in sources of the client pack: [#getClientFileApi()] first, then any extra mirrors from the system + /// property `hmcl.client.fileApiMirrors` or the jar attribute of the same name (comma separated). The launcher + /// probes them and downloads from the fastest; a manifest's own list (`fileApiMirrors`) replaces this once known. + public static List getClientFileApis() { + List all = new ArrayList<>(); + all.add(getClientFileApi()); + String extra = System.getProperty("hmcl.client.fileApiMirrors", "").trim(); + if (StringUtils.isBlank(extra)) { + extra = JarUtils.getAttribute("hmcl.client.fileApiMirrors", ""); + } + if (extra != null) { + for (String item : extra.split(",")) { + if (StringUtils.isNotBlank(item)) { + all.add(item.trim()); + } + } + } + return all; + } + /// Absolute URL of the client `server-manifest.json` used for first install and differential updates. public static String getClientManifestUrl() { return getClientFileApi() + "/server-manifest.json"; diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/download/HMCLDownloadProvider.java b/HMCL/src/main/java/org/jackhuang/hmcl/download/HMCLDownloadProvider.java index 63e530fa9d..04d62540b2 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/download/HMCLDownloadProvider.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/download/HMCLDownloadProvider.java @@ -17,6 +17,7 @@ import org.jackhuang.hmcl.setting.DownloadSource; import org.jackhuang.hmcl.task.Task; import org.jackhuang.hmcl.util.RegionUtils; import org.jackhuang.hmcl.util.versioning.GameVersionNumber; +import org.jackhuang.hmcl.modpack.server.ClientMirrors; import org.jetbrains.annotations.NotNullByDefault; import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Unmodifiable; @@ -98,7 +99,25 @@ public final class HMCLDownloadProvider extends DownloadProvider { } if (mirrorFirst == null) { - return DownloadCandidates.of(urls.stream().map(DownloadCandidate::of).toArray(DownloadCandidate[]::new)); + // Official sources stay as they are, but a URL that already points at our own sources (the Java + // runtime files) still gets every one of them, whatever the region. + var plain = new ArrayList(urls.size()); + for (String url : urls) { + DownloadCandidate candidate = DownloadCandidate.of(url); + @Nullable List own = null; + for (MirrorRule rule : rules) { + if (rule instanceof OwnPathRule) { + own = rule.getCandidates(candidate, false); + if (own != null) + break; + } + } + if (own != null) + plain.addAll(own); + else + plain.add(candidate); + } + return DownloadCandidates.of(List.copyOf(plain)); } var candidates = new ArrayList(urls.size() * 3); @@ -132,17 +151,20 @@ public final class HMCLDownloadProvider extends DownloadProvider { public HMCLDownloadProvider(Supplier<@Unmodifiable List> mirrorRoots) { this.mirrorRoots = Objects.requireNonNull(mirrorRoots); + ClientMirrors.registerDefault(Metadata.getClientFileApis()); this.rules = List.of( // The self-hosted client mirror carries the pack's full dependency set. BMCLAPI's // redirect CDNs (sakura.sld.tw, cernet) and the Mojang/Forge origins are TLS-blocked // on many mainland networks, so the VPS must be tried before any of them. - new SimpleRule("https://piston-meta.mojang.com", Metadata.getClientFileApi() + "/piston-meta", "https://bmclapi2.bangbang93.com"), - new SimpleRule("https://libraries.minecraft.net", Metadata.getClientFileApi() + "/libraries", "https://bmclapi2.bangbang93.com/libraries"), - new SimpleRule("https://maven.minecraftforge.net", Metadata.getClientFileApi() + "/forge-maven", "https://bmclapi2.bangbang93.com/maven"), - new SimpleRule("https://launcher.mojang.com", Metadata.getClientFileApi() + "/launcher-mojang", "https://bmclapi2.bangbang93.com"), + new OwnRule("https://piston-meta.mojang.com", "/piston-meta", "https://bmclapi2.bangbang93.com"), + new OwnRule("https://libraries.minecraft.net", "/libraries", "https://bmclapi2.bangbang93.com/libraries"), + new OwnRule("https://maven.minecraftforge.net", "/forge-maven", "https://bmclapi2.bangbang93.com/maven"), + new OwnRule("https://launcher.mojang.com", "/launcher-mojang", "https://bmclapi2.bangbang93.com"), // Forge 1.7.10 installer URLs from the version list point at files.minecraftforge.net. - new SimpleRule("https://files.minecraftforge.net/maven", Metadata.getClientFileApi() + "/forge-maven", "https://bmclapi2.bangbang93.com/maven"), - new SimpleRule("http://files.minecraftforge.net/maven", Metadata.getClientFileApi() + "/forge-maven", "https://bmclapi2.bangbang93.com/maven"), + new OwnRule("https://files.minecraftforge.net/maven", "/forge-maven", "https://bmclapi2.bangbang93.com/maven"), + new OwnRule("http://files.minecraftforge.net/maven", "/forge-maven", "https://bmclapi2.bangbang93.com/maven"), + // Java runtime files: the manifest points them at one source; offer all of them, fastest first. + new OwnPathRule("/java-runtime/objects/"), new MultiRootRule("https://bmclapi2.bangbang93.com", Function.identity()), new MultiRootRule("https://bmclapi.bangbang93.com", Function.identity()), new MultiRootRule("https://launchermeta.mojang.com", Function.identity()), @@ -321,7 +343,9 @@ public final class HMCLDownloadProvider extends DownloadProvider { // Keep the VPS strictly first: spreadPrimary would otherwise rotate BMCLAPI ahead of // it, and BMCLAPI's asset CDN is unreachable on blocked networks (per-file timeouts). var candidates = new ArrayList(mirrors.size() + 2); - candidates.add(DownloadCandidate.of(WebURL.parse(Metadata.getClientFileApi() + "/assets/" + location))); + for (String source : ClientMirrors.ranked()) { + candidates.add(DownloadCandidate.of(WebURL.parse(source + "/assets/" + location))); + } candidates.addAll(spreadPrimary(mirrors.stream().map(DownloadCandidate::of).toList(), officialUrl.href())); candidates.add(DownloadCandidate.of(officialUrl)); return DownloadCandidates.of(List.copyOf(candidates)); @@ -406,6 +430,52 @@ public final class HMCLDownloadProvider extends DownloadProvider { } } + /// Maps an official URL to the same path under every one of our own sources (fastest first, see + /// [ClientMirrors]), then the official URL, then a public mirror as the last resort. + @NotNullByDefault + private record OwnRule(String source, String suffix, @Nullable String fallback) implements MirrorRule { + + @Override + public @Nullable List getCandidates(DownloadCandidate source, boolean mirrorFirst) { + if (source.url() == null || !source.url().href().startsWith(this.source)) + return null; + + String urlWithoutPrefix = source.url().href().substring(this.source.length()); + var candidates = new ArrayList(); + if (!mirrorFirst) + candidates.add(source); + for (String own : ClientMirrors.ranked()) + candidates.add(DownloadCandidate.of(own + suffix + urlWithoutPrefix)); + if (mirrorFirst) + candidates.add(source); + if (fallback != null) + candidates.add(DownloadCandidate.of(fallback + urlWithoutPrefix)); + return candidates; + } + } + + /// A URL that already points at one of our own sources: offer the same path on all of them, fastest first. + @NotNullByDefault + private record OwnPathRule(String path) implements MirrorRule { + + @Override + public @Nullable List getCandidates(DownloadCandidate source, boolean mirrorFirst) { + if (source.url() == null) + return null; + String href = source.url().href(); + for (String own : ClientMirrors.registered()) { + if (href.startsWith(own + path)) { + String rest = href.substring(own.length()); + var candidates = new ArrayList(); + for (String ranked : ClientMirrors.ranked()) + candidates.add(DownloadCandidate.of(ranked + rest)); + return candidates; + } + } + return null; + } + } + @NotNullByDefault private record SimpleRule(String source, @Nullable String first, @Nullable String fallback) implements MirrorRule { public SimpleRule(String source, String target) { diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/server/ClientMirrors.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/server/ClientMirrors.java new file mode 100644 index 0000000000..acd25d506a --- /dev/null +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/server/ClientMirrors.java @@ -0,0 +1,269 @@ +// SPDX-FileCopyrightText: Copyright (C) 2026 The HMCL Authors +// SPDX-License-Identifier: GPL-3.0-or-later WITH AdditionRef-HMCL +package org.jackhuang.hmcl.modpack.server; + +import org.jetbrains.annotations.NotNullByDefault; +import org.jetbrains.annotations.Nullable; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.time.Duration; +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import static org.jackhuang.hmcl.util.logging.Logger.LOG; + +/// The client pack's own download sources (the manifest's `fileApi` and `fileApiMirrors`), ranked by a +/// quick speed probe so each player downloads from the source that is fastest for them. +/// +/// Players are spread across regions and a single origin is a single point of failure, so the manifest +/// may list several equivalent base URLs (the origin, a mainland CDN, an overseas CDN, ...). They +/// serve identical content, so any of them can answer any file request. Before the first download the +/// launcher fetches a small probe file from every source at once and orders them fastest first; a source +/// that cannot be reached is kept at the very end as a last resort. The ranking is cached for a few +/// minutes. With a single source nothing is probed. +@NotNullByDefault +public final class ClientMirrors { + + /// A small file every source should serve (512 KiB of throughput is enough to tell a slow link apart). + static final String PROBE_PATH = "/probe.bin"; + /// Present on every source already; used when {@link #PROBE_PATH} is not there yet. + static final String FALLBACK_PROBE_PATH = "/server-manifest.json"; + static final long PROBE_BYTES = 512L * 1024L; + static final Duration PROBE_TIMEOUT = Duration.ofSeconds(4); + static final long CACHE_NANOS = TimeUnit.MINUTES.toNanos(10); + + /// Sources that finish within this factor (plus a small constant) of the fastest are treated as equally + /// good and keep the manifest's order, so a tiny difference does not flip the preferred source. + static final double TIE_FACTOR = 1.3; + static final long TIE_SLACK_MILLIS = 40; + + /// Measures one source: returns how many milliseconds it took to fetch the probe, or throws when the + /// source does not answer. + @FunctionalInterface + interface Prober { + long probeMillis(String base) throws IOException; + } + + private record Ranking(List bases, long at, List ranked) { + } + + private static volatile List registered = List.of(); + private static @Nullable Ranking cached; + private static volatile Prober prober = new HttpProber(); + + private ClientMirrors() { + } + + /// Replaces the list of sources. The first entry is the origin (preferred when speeds are comparable). + /// Blank entries and trailing slashes are dropped; duplicates are ignored. An empty list is ignored so + /// a manifest without mirrors does not erase what a previous call registered. + public static synchronized void register(@Nullable Collection bases) { + List normalized = normalize(bases); + if (normalized.isEmpty() || normalized.equals(registered)) { + return; + } + registered = normalized; + cached = null; + } + + /// Registers `bases` only when nothing has been registered yet (the launcher's built-in default). + public static synchronized void registerDefault(@Nullable Collection bases) { + if (registered.isEmpty()) { + register(bases); + } + } + + /// The registered sources in manifest order. + public static List registered() { + return registered; + } + + /// The registered sources, fastest first. Probes on first use (and again after the cache expires), so + /// call it from a background thread. Never empty when something is registered. + public static synchronized List ranked() { + List bases = registered; + if (bases.size() <= 1) { + return bases; + } + long now = System.nanoTime(); + Ranking current = cached; + if (current != null && current.bases.equals(bases) && now - current.at < CACHE_NANOS) { + return current.ranked; + } + List result = rank(bases, prober); + LOG.info("Client pack sources ranked by speed: " + result); + cached = new Ranking(bases, now, result); + return result; + } + + /// Forgets the cached ranking so the next [#ranked()] probes again (for example after a download + /// failed on the best source). + public static synchronized void invalidate() { + cached = null; + } + + /// Probes every source at the same time and orders them fastest first. Sources that fail or time out + /// go last in their original order. + static List rank(List bases, Prober prober) { + if (bases.size() <= 1) { + return List.copyOf(bases); + } + + ExecutorService executor = Executors.newFixedThreadPool(bases.size(), runnable -> { + Thread thread = new Thread(runnable, "client-mirror-probe"); + thread.setDaemon(true); + return thread; + }); + long[] millis = new long[bases.size()]; + try { + List> futures = new ArrayList<>(bases.size()); + for (String base : bases) { + futures.add(CompletableFuture.supplyAsync(() -> { + try { + return prober.probeMillis(base); + } catch (IOException | RuntimeException e) { + return -1L; + } + }, executor)); + } + long deadline = System.nanoTime() + PROBE_TIMEOUT.toNanos() + TimeUnit.SECONDS.toNanos(1); + for (int i = 0; i < millis.length; i++) { + try { + long remaining = Math.max(1, deadline - System.nanoTime()); + millis[i] = futures.get(i).get(remaining, TimeUnit.NANOSECONDS); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + millis[i] = -1; + } catch (ExecutionException | TimeoutException e) { + millis[i] = -1; + } + } + } finally { + executor.shutdownNow(); + } + + long best = Long.MAX_VALUE; + for (long m : millis) { + if (m >= 0 && m < best) { + best = m; + } + } + if (best == Long.MAX_VALUE) { + return List.copyOf(bases); // nothing answered: keep the manifest's order + } + + long tieLimit = (long) (best * TIE_FACTOR) + TIE_SLACK_MILLIS; + List tied = new ArrayList<>(); + List slower = new ArrayList<>(); + List failed = new ArrayList<>(); + for (int i = 0; i < bases.size(); i++) { + if (millis[i] < 0) { + failed.add(bases.get(i)); + } else if (millis[i] <= tieLimit) { + tied.add(bases.get(i)); + } else { + slower.add(i); + } + } + slower.sort((a, b) -> Long.compare(millis[a], millis[b])); + + List result = new ArrayList<>(bases.size()); + result.addAll(tied); + for (int index : slower) { + result.add(bases.get(index)); + } + result.addAll(failed); + return List.copyOf(result); + } + + static List normalize(@Nullable Collection bases) { + LinkedHashSet result = new LinkedHashSet<>(); + if (bases != null) { + for (String base : bases) { + if (base == null) { + continue; + } + String trimmed = base.trim(); + while (trimmed.endsWith("/")) { + trimmed = trimmed.substring(0, trimmed.length() - 1); + } + if (!trimmed.isEmpty()) { + result.add(trimmed); + } + } + } + return List.copyOf(result); + } + + /// Test hook: swaps the prober and clears all state. + static synchronized void resetForTest(Prober testProber) { + prober = testProber; + registered = List.of(); + cached = null; + } + + private static final class HttpProber implements Prober { + + private final HttpClient client = HttpClient.newBuilder() + .connectTimeout(PROBE_TIMEOUT) + .followRedirects(HttpClient.Redirect.NORMAL) + .build(); + + @Override + public long probeMillis(String base) throws IOException { + try { + return fetch(base + PROBE_PATH, true); + } catch (NotFound e) { + return fetch(base + FALLBACK_PROBE_PATH, false); + } + } + + private long fetch(String url, boolean ranged) throws IOException { + HttpRequest.Builder builder = HttpRequest.newBuilder(URI.create(url)).timeout(PROBE_TIMEOUT).GET(); + if (ranged) { + builder.header("Range", "bytes=0-" + (PROBE_BYTES - 1)); + } + long start = System.nanoTime(); + try { + HttpResponse response = client.send(builder.build(), HttpResponse.BodyHandlers.ofInputStream()); + int status = response.statusCode(); + if (status == 404) { + response.body().close(); + throw new NotFound(); + } + if (status != 200 && status != 206) { + response.body().close(); + throw new IOException("HTTP " + status + " from " + url); + } + try (InputStream in = response.body()) { + byte[] buffer = new byte[16 * 1024]; + long total = 0; + int n; + while (total < PROBE_BYTES && (n = in.read(buffer)) >= 0) { + total += n; + } + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new IOException(e); + } + return Math.max(1, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start)); + } + + private static final class NotFound extends IOException { + } + } +} diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/server/ServerModpackCompletionTask.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/server/ServerModpackCompletionTask.java index 02a4a4dfb4..49d162b32d 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/server/ServerModpackCompletionTask.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/server/ServerModpackCompletionTask.java @@ -126,6 +126,10 @@ public class ServerModpackCompletionTask extends Task { throw new IOException(e); } + // The pack may list several equivalent sources; probe them and download from the fastest first. + ClientMirrors.register(remoteManifest.getFileApis()); + List sources = ClientMirrors.ranked(); + Map oldAddons = toMap(manifest.getManifest().getAddons()); Map newAddons = toMap(remoteManifest.getAddons()); if (!Objects.equals(oldAddons, newAddons)) { @@ -185,8 +189,15 @@ public class ServerModpackCompletionTask extends Task { if (download) { total++; + List urls = new ArrayList<>(); + for (String source : sources) { + urls.add(org.jackhuang.hmcl.download.DownloadCandidate.of(source + "/overrides/" + file.getPath())); + } + if (urls.isEmpty()) { + urls.add(org.jackhuang.hmcl.download.DownloadCandidate.of(remoteManifest.getFileApi() + "/overrides/" + file.getPath())); + } dependencies.add(new FileDownloadTask( - remoteManifest.getFileApi() + "/overrides/" + file.getPath(), + org.jackhuang.hmcl.download.DownloadCandidates.of(List.copyOf(urls)), actualPath, new FileDownloadTask.IntegrityCheck("SHA-1", file.getHash())) .withCounter("hmcl.modpack.download")); diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/server/ServerModpackManifest.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/server/ServerModpackManifest.java index c2c630e89b..b4e56b943c 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/server/ServerModpackManifest.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/server/ServerModpackManifest.java @@ -29,6 +29,9 @@ public class ServerModpackManifest implements ModpackManifest, Validation { private final String fileApi; private final List files; private final List addons; + /// Other base URLs that serve exactly the same files as [#fileApi] (for example a CDN in front of it), or + /// `null`. The launcher probes all sources and downloads from the fastest; old launchers ignore this field. + private @Nullable List fileApiMirrors; /// The address players are connected to on launch, or `null` when the manifest does not specify one. private final @Nullable String serverAddress; /// Alternative connection routes (for example direct and CDN), or `null` when only [#serverAddress] is used. @@ -92,6 +95,16 @@ public class ServerModpackManifest implements ModpackManifest, Validation { return fileApi; } + /// All sources for this pack's files: [#getFileApi()] first, then the mirrors, without duplicates or blanks. + public List getFileApis() { + List all = new java.util.ArrayList<>(); + all.add(fileApi); + if (fileApiMirrors != null) { + all.addAll(fileApiMirrors); + } + return ClientMirrors.normalize(all); + } + public List getFiles() { return files; } diff --git a/HMCLCore/src/test/java/org/jackhuang/hmcl/modpack/server/ClientMirrorsTest.java b/HMCLCore/src/test/java/org/jackhuang/hmcl/modpack/server/ClientMirrorsTest.java new file mode 100644 index 0000000000..130ea72100 --- /dev/null +++ b/HMCLCore/src/test/java/org/jackhuang/hmcl/modpack/server/ClientMirrorsTest.java @@ -0,0 +1,181 @@ +// SPDX-FileCopyrightText: Copyright (C) 2026 The HMCL Authors +// SPDX-License-Identifier: GPL-3.0-or-later WITH AdditionRef-HMCL +package org.jackhuang.hmcl.modpack.server; + +import com.sun.net.httpserver.HttpServer; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicInteger; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public final class ClientMirrorsTest { + + private final List servers = new ArrayList<>(); + + @AfterEach + void tearDown() { + for (HttpServer server : servers) { + server.stop(0); + } + ClientMirrors.resetForTest(base -> { + throw new IOException("no network in tests"); + }); + } + + /// A fake probe that reports fixed times (negative = unreachable). + private static ClientMirrors.Prober fixed(Map times, AtomicInteger calls) { + return base -> { + calls.incrementAndGet(); + Long t = times.get(base); + if (t == null || t < 0) { + throw new IOException("down"); + } + return t; + }; + } + + @Test + void fastestFirstAndUnreachableLast() { + Map times = Map.of("http://a", 900L, "http://b", 120L, "http://c", -1L, "http://d", 400L); + List ranked = ClientMirrors.rank(List.of("http://a", "http://b", "http://c", "http://d"), fixed(times, new AtomicInteger())); + assertEquals(List.of("http://b", "http://d", "http://a", "http://c"), ranked); + } + + @Test + void comparableSpeedsKeepTheManifestOrder() { + // 100 vs 110 ms is noise, not a reason to prefer the second source. + Map times = Map.of("http://origin", 110L, "http://cdn", 100L); + List ranked = ClientMirrors.rank(List.of("http://origin", "http://cdn"), fixed(times, new AtomicInteger())); + assertEquals(List.of("http://origin", "http://cdn"), ranked); + } + + @Test + void aClearlyFasterSourceWins() { + Map times = Map.of("http://origin", 2000L, "http://cdn", 100L); + List ranked = ClientMirrors.rank(List.of("http://origin", "http://cdn"), fixed(times, new AtomicInteger())); + assertEquals(List.of("http://cdn", "http://origin"), ranked); + } + + @Test + void ifNothingAnswersTheManifestOrderIsKept() { + Map times = Map.of("http://a", -1L, "http://b", -1L); + List ranked = ClientMirrors.rank(List.of("http://a", "http://b"), fixed(times, new AtomicInteger())); + assertEquals(List.of("http://a", "http://b"), ranked); + } + + @Test + void aSingleSourceIsNeverProbed() { + AtomicInteger calls = new AtomicInteger(); + ClientMirrors.resetForTest(fixed(Map.of(), calls)); + ClientMirrors.register(List.of("http://only/")); + assertEquals(List.of("http://only"), ClientMirrors.ranked()); + assertEquals(0, calls.get()); + } + + @Test + void registerNormalizesAndIgnoresBlanksAndDuplicates() { + ClientMirrors.resetForTest(fixed(Map.of(), new AtomicInteger())); + ClientMirrors.register(java.util.Arrays.asList("http://a/", " http://b ", "", null, "http://a")); + assertEquals(List.of("http://a", "http://b"), ClientMirrors.registered()); + ClientMirrors.register(List.of()); // an empty list does not erase what is registered + assertEquals(List.of("http://a", "http://b"), ClientMirrors.registered()); + } + + @Test + void registerDefaultOnlyFillsAnEmptyList() { + ClientMirrors.resetForTest(fixed(Map.of(), new AtomicInteger())); + ClientMirrors.registerDefault(List.of("http://d")); + ClientMirrors.registerDefault(List.of("http://other")); + assertEquals(List.of("http://d"), ClientMirrors.registered()); + ClientMirrors.register(List.of("http://m1", "http://m2")); // a manifest's list replaces the default + assertEquals(List.of("http://m1", "http://m2"), ClientMirrors.registered()); + } + + @Test + void theRankingIsCachedUntilInvalidatedOrTheListChanges() { + AtomicInteger calls = new AtomicInteger(); + ClientMirrors.resetForTest(fixed(Map.of("http://a", 50L, "http://b", 60L), calls)); + ClientMirrors.register(List.of("http://a", "http://b")); + ClientMirrors.ranked(); + ClientMirrors.ranked(); + assertEquals(2, calls.get(), "one probe per source, then cached"); + ClientMirrors.invalidate(); + ClientMirrors.ranked(); + assertEquals(4, calls.get()); + ClientMirrors.register(List.of("http://a", "http://b", "http://c")); + ClientMirrors.ranked(); + assertEquals(7, calls.get(), "a changed list is probed again"); + } + + @Test + void manifestListsTheOriginFirstThenMirrors() { + com.google.gson.Gson gson = org.jackhuang.hmcl.util.gson.JsonUtils.GSON; + ServerModpackManifest manifest = gson.fromJson( + "{\"name\":\"n\",\"author\":\"a\",\"version\":\"1\",\"description\":\"d\",\"fileApi\":\"http://origin/x/\"," + + "\"fileApiMirrors\":[\"http://cdn-cn/x\",\"http://origin/x\",\"https://cdn-global/x/\"],\"files\":[],\"addons\":[]}", + ServerModpackManifest.class); + assertEquals(List.of("http://origin/x", "http://cdn-cn/x", "https://cdn-global/x"), manifest.getFileApis()); + ServerModpackManifest old = gson.fromJson( + "{\"name\":\"n\",\"author\":\"a\",\"version\":\"1\",\"description\":\"d\",\"fileApi\":\"http://origin/x\",\"files\":[],\"addons\":[]}", + ServerModpackManifest.class); + assertEquals(List.of("http://origin/x"), old.getFileApis()); + } + + @Test + void theRealProbeMeasuresARealServerAndFallsBackToTheManifestFile() throws Exception { + HttpServer fast = server(Map.of("/server-manifest.json", 0)); + HttpServer slow = server(Map.of("/server-manifest.json", 300)); + String fastBase = "http://127.0.0.1:" + fast.getAddress().getPort(); + String slowBase = "http://127.0.0.1:" + slow.getAddress().getPort(); + String deadBase = "http://127.0.0.1:1"; // nothing listens here + + ClientMirrors.resetForTest(new ClientMirrorsTestAccess().realProber()); + ClientMirrors.register(List.of(slowBase, deadBase, fastBase)); + assertEquals(List.of(fastBase, slowBase, deadBase), ClientMirrors.ranked()); + } + + private HttpServer server(Map delaysMillis) throws IOException { + HttpServer server = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0); + Map delays = new ConcurrentHashMap<>(delaysMillis); + server.createContext("/", exchange -> { + Integer delay = delays.get(exchange.getRequestURI().getPath()); + if (delay == null) { // the probe file is not there: the client falls back to the manifest + exchange.sendResponseHeaders(404, -1); + exchange.close(); + return; + } + try { + Thread.sleep(delay); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + byte[] body = "{}".getBytes(); + exchange.sendResponseHeaders(200, body.length); + exchange.getResponseBody().write(body); + exchange.close(); + }); + server.start(); + servers.add(server); + assertTrue(server.getAddress().getPort() > 0); + return server; + } + + /// Reaches the real prober without exposing it outside the package. + private static final class ClientMirrorsTestAccess { + ClientMirrors.Prober realProber() throws Exception { + java.lang.reflect.Constructor constructor = Class.forName("org.jackhuang.hmcl.modpack.server.ClientMirrors$HttpProber").getDeclaredConstructor(); + constructor.setAccessible(true); + return (ClientMirrors.Prober) constructor.newInstance(); + } + } +}