Skip to content

Commit 665775b

Browse files
author
Z User
committed
fix: 使用 header() 替代 addHeader() 确保 User-Agent 唯一性
- AuthKeyService 和 HttpProtocol 中自定义 headers 添加方式从 addHeader() 改为 header() - addHeader() 是追加模式,会导致多个 User-Agent 头,使鉴权服务器看到的 UA 与配置不一致 - header() 是替换模式,确保配置文件中的 User-Agent 是唯一的权威值 - 同时修复 anti-hotlink-enabled 默认值为 true(与 mcpatch.yml 配置保持一致)
1 parent 0e2f559 commit 665775b

3 files changed

Lines changed: 9 additions & 5 deletions

File tree

src/main/java/com/github/balloonupdate/mcpatch/client/config/AppConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public AppConfig(Map<String, Object> map) {
188188
long chunkSize = getLong(map, "chunk-size", null, 1024L * 1024);
189189
int maxChunks = getInt(map, "max-chunks", null, 16);
190190
boolean enableChunkedDownload = getBoolean(map, "enable-chunked-download", null, true);
191-
boolean antiHotlinkEnabled = getBoolean(map, "anti-hotlink-enabled", null, false);
191+
boolean antiHotlinkEnabled = getBoolean(map, "anti-hotlink-enabled", null, true);
192192
String authApiUrl = getString(map, "anti-hotlink-auth-url", null, "https://auth-api.mxzysoa.com/generate-auth-url");
193193
int authExpireTime = getInt(map, "anti-hotlink-expire-time", null, 3600);
194194
String authUid = getString(map, "anti-hotlink-uid", null, "0");

src/main/java/com/github/balloonupdate/mcpatch/client/network/AuthKeyService.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,12 @@ private AuthKeyEntry requestAuthKey(String filePath) throws McpatchBusinessExcep
189189
.url(url)
190190
.get();
191191

192-
// 添加自定义headers(与下载请求保持一致,包括 User-Agent 等)
192+
// 使用 header()(替换模式)而非 addHeader()(追加模式),
193+
// 确保配置文件中的 User-Agent 等自定义 headers 是唯一的、权威的值,
194+
// 不会与 OkHttp 或系统默认值产生重复
193195
if (httpHeaders != null) {
194196
for (Map.Entry<String, String> e : httpHeaders.entrySet()) {
195-
reqBuilder.addHeader(e.getKey(), e.getValue());
197+
reqBuilder.header(e.getKey(), e.getValue());
196198
}
197199
}
198200

src/main/java/com/github/balloonupdate/mcpatch/client/network/impl/HttpProtocol.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,11 @@ private Request buildRequest(String url, Range range, Map<String, String> header
280280
req.addHeader(e.getKey(), e.getValue());
281281
}
282282

283-
// 添加自定义headers
283+
// 使用 header()(替换模式)而非 addHeader()(追加模式),
284+
// 确保配置文件中的 User-Agent 等自定义 headers 是唯一的、权威的值,
285+
// 不会与 OkHttp 或系统默认值产生重复
284286
for (Map.Entry<String, String> e : config.httpHeaders.entrySet()) {
285-
req.addHeader(e.getKey(), e.getValue());
287+
req.header(e.getKey(), e.getValue());
286288
}
287289

288290
return req.build();

0 commit comments

Comments
 (0)