Skip to content

Commit 0e2f559

Browse files
author
Z User
committed
fix: 完善防盗链方案
- AuthKeyService 添加 shutdown() 方法,关闭 OkHttp 连接池和调度器 - Servers.close() 中调用 authKeyService.shutdown() 释放资源 - anti-hotlink-enabled 默认值改为 true(默认开启防盗链)
1 parent 61277e5 commit 0e2f559

3 files changed

Lines changed: 21 additions & 2 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,16 @@ public void clearCache() {
260260
cache.clear();
261261
}
262262

263+
/**
264+
* 关闭鉴权服务,释放资源。
265+
* 关闭 OkHttp 连接池和调度器,清除缓存。
266+
*/
267+
public void shutdown() {
268+
cache.clear();
269+
client.dispatcher().executorService().shutdown();
270+
client.connectionPool().evictAll();
271+
}
272+
263273
/**
264274
* 使指定文件路径的缓存失效。
265275
* <p>

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,15 @@ public class Servers implements UpdatingServer {
3535
*/
3636
AtomicInteger current = new AtomicInteger(0);
3737

38+
/**
39+
* 防盗链鉴权服务,为null时表示未启用防盗链
40+
*/
41+
AuthKeyService authKeyService;
42+
3843
public Servers(AppConfig config) throws McpatchBusinessException {
3944
this.config = config;
4045

4146
// 如果启用了防盗链,创建鉴权服务
42-
AuthKeyService authKeyService = null;
4347
if (config.antiHotlinkEnabled) {
4448
authKeyService = new AuthKeyService(
4549
config.authApiUrl,
@@ -177,6 +181,11 @@ public void close() throws Exception {
177181
for (UpdatingServer source : servers) {
178182
source.close();
179183
}
184+
185+
// 关闭鉴权服务,释放 OkHttp 连接池等资源
186+
if (authKeyService != null) {
187+
authKeyService.shutdown();
188+
}
180189
}
181190

182191
@FunctionalInterface

src/main/resources/mcpatch.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ enable-chunked-download: true
121121
# 是否启用防盗链鉴权
122122
# 启用后,下载文件时会先向鉴权服务器获取 auth_key,然后拼接到下载 URL 中
123123
# 仅对 HTTP/HTTPS 协议生效,私有协议(mcpatch://)不受影响
124-
anti-hotlink-enabled: false
124+
anti-hotlink-enabled: true
125125

126126
# 鉴权 API 地址
127127
# 用于生成符合阿里云ESA A方案规范的鉴权密钥(auth_key)

0 commit comments

Comments
 (0)