Skip to content

Commit 2941868

Browse files
committed
refactor: 优化地理位置检测和源选择逻辑,简化代码结构
1 parent 75849a5 commit 2941868

File tree

2 files changed

+22
-53
lines changed

2 files changed

+22
-53
lines changed

src/main/java/i18nupdatemod/util/AssetUtil.java

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.List;
2121
import java.util.Map;
2222
import java.util.concurrent.*;
23+
import java.util.stream.Collectors;
2324

2425
public class AssetUtil {
2526
private static final String CFPA_ASSET_ROOT = "http://downloader1.meitangdehulu.com:22943/";
@@ -45,51 +46,30 @@ public static String getString(String url) throws IOException, URISyntaxExceptio
4546
}
4647

4748
public static String getFastestUrl() {
48-
List<String> urls = new ArrayList<>();
49-
50-
// 根据地理位置选择源列表
51-
if (LocationDetectUtil.isMainlandChina()) {
52-
// 中国大陆:测速选择最快的国内源
53-
urls.addAll(MIRRORS);
54-
urls.add(CFPA_ASSET_ROOT);
55-
Log.info("Inside mainland China: Testing mirrors...");
56-
} else {
57-
// 海外用户:直接使用 GitHub 源
58-
urls.add(GITHUB);
59-
Log.info("Outside mainland China: Using GitHub source...");
49+
// 海外用户直接用 GitHub,不测速
50+
if (!LocationDetectUtil.isMainlandChina()) {
51+
Log.info("Outside mainland China: Using GitHub source");
52+
return GITHUB;
6053
}
6154

62-
ExecutorService executor = Executors.newFixedThreadPool(Math.max(urls.size(), 10));
63-
try {
64-
List<CompletableFuture<String>> futures = new ArrayList<>();
65-
for (String url : urls) {
66-
futures.add(CompletableFuture.supplyAsync(() -> {
67-
try {
68-
return testUrlConnection(url);
69-
} catch (IOException e) {
70-
return null;
71-
}
72-
}, executor));
73-
}
74-
75-
String fastest = null;
76-
while (!futures.isEmpty()) {
77-
fastest = (String) CompletableFuture.anyOf(futures.toArray(new CompletableFuture[0])).join();
78-
futures.removeIf(CompletableFuture::isDone);
79-
if (fastest != null) {
80-
for (CompletableFuture<String> f : futures) {
81-
f.cancel(true);
82-
}
83-
Log.info("Using fastest url: %s", fastest);
84-
return fastest;
85-
}
86-
}
55+
// 中国大陆:测速选择最快的国内源
56+
Log.info("Inside mainland China: Testing mirrors...");
57+
List<String> urls = new ArrayList<>(MIRRORS);
58+
urls.add(CFPA_ASSET_ROOT);
8759

60+
ExecutorService executor = Executors.newCachedThreadPool();
61+
try {
62+
String fastest = executor.invokeAny(
63+
urls.stream().map(url -> (Callable<String>) () -> testUrlConnection(url)).collect(Collectors.toList()),
64+
10, TimeUnit.SECONDS
65+
);
66+
executor.shutdownNow();
67+
Log.info("Using fastest url: %s", fastest);
68+
return fastest;
69+
} catch (Exception e) {
70+
executor.shutdownNow();
8871
Log.info("All sources unreachable, using CFPA_ASSET_ROOT as fallback");
8972
return CFPA_ASSET_ROOT;
90-
91-
} finally {
92-
executor.shutdownNow();
9373
}
9474
}
9575

src/main/java/i18nupdatemod/util/LocationDetectUtil.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,7 @@ private static Boolean tryApi(String name, String url) {
5151

5252
private static boolean parseResponse(String response) {
5353
response = response.trim();
54-
// Kugou API JSON: {"flag": 1} 或 {"flag": true}
55-
if (response.contains("\"flag\":1") || response.contains("\"flag\": 1") ||response.contains("\"flag\":true") || response.contains("\"flag\": true")) {
56-
return true;
57-
}
58-
// IP.SB API JSON: {"country_code": "CN"}
59-
if (response.contains("\"country_code\":\"CN\"") || response.contains("\"country_code\": \"CN\"")) {
60-
return true;
61-
}
62-
return false;
63-
}
64-
65-
public static void resetCache() {
66-
cached = null;
54+
// Kugou: {"flag":1/true} | IP.SB: {"country_code":"CN"}
55+
return response.matches(".*(\"flag\".*[\":] *(1|true)|\"country_code\".*\"CN\").*");
6756
}
6857
}

0 commit comments

Comments
 (0)