第73批:黑名单增量重载(CRC32指纹对比+文件级增删改)+补全http.json
Auto Release / build (push) Successful in 2m14s

This commit is contained in:
youpaishidifu
2026-11-03 19:25:52 +08:00
parent 2687c4224b
commit b835860b2e
2 changed files with 75 additions and 9 deletions
+48 -4
View File
@@ -50,6 +50,10 @@ public class AIChatManager {
// ★ 第61批-续1:endpoint 找不到的状态变化日志静默 // ★ 第61批-续1:endpoint 找不到的状态变化日志静默
private boolean prevEndpointNull = false; private boolean prevEndpointNull = false;
// ★ 第73批-增量重载:黑名单目录文件管理
private final Map<String, List<String>> blacklistFileKeywords = new HashMap<>(); // 文件名 -> 关键词列表
private final Map<String, String> blacklistFileHashes = new HashMap<>(); // 文件名 -> CRC32 指纹
// 默认模型 // 默认模型
private String defaultModelFull = ""; private String defaultModelFull = "";
@@ -262,23 +266,56 @@ public class AIChatManager {
} }
} }
// ★ 第73批:自动扫描 openai/黑名单/ 目录下的所有 json 文件 // ★ 第73批-增量重载:扫描 openai/黑名单/ 目录下的所有 json 文件
// 原理:用 CRC32 指纹对比文件内容。文件没变→内存不动、闭嘴;
// 新增文件→加载并打印一次;文件变了→清掉该文件旧关键词、加载新关键词、打印一次;
// 文件被删→从内存移除该文件的关键词。
File blacklistDir = new File(openaiDir, "黑名单"); File blacklistDir = new File(openaiDir, "黑名单");
if (blacklistDir.exists() && blacklistDir.isDirectory()) { if (blacklistDir.exists() && blacklistDir.isDirectory()) {
File[] blFiles = blacklistDir.listFiles((dir, name) -> name.endsWith(".json")); File[] blFiles = blacklistDir.listFiles((dir, name) -> name.endsWith(".json"));
if (blFiles != null) { if (blFiles != null) {
Set<String> currentFileNames = new HashSet<>();
for (File blFile : blFiles) { for (File blFile : blFiles) {
currentFileNames.add(blFile.getName());
String fileName = blFile.getName();
try { try {
String jsonContent = Files.readString(blFile.toPath(), StandardCharsets.UTF_8); String jsonContent = Files.readString(blFile.toPath(), StandardCharsets.UTF_8);
String newHash = crc32(jsonContent);
String oldHash = blacklistFileHashes.get(fileName);
if (newHash.equals(oldHash)) {
// 文件内容没变,内存里已加载的关键词不动,不打印
continue;
}
// 文件新增或内容变动:加载新关键词
List<String> newKeywords = new ArrayList<>();
JsonArray blArray = JsonParser.parseString(jsonContent).getAsJsonArray(); JsonArray blArray = JsonParser.parseString(jsonContent).getAsJsonArray();
for (JsonElement el : blArray) { for (JsonElement el : blArray) {
if (el.isJsonPrimitive()) { if (el.isJsonPrimitive()) {
blacklist.add(el.getAsString()); newKeywords.add(el.getAsString());
} }
} }
plugin.getLogger().info("[AI聊天] 已加载黑名单文件: " + blFile.getName()); // 如果是已有文件变动:先从全局黑名单中移除该文件的旧关键词
if (blacklistFileKeywords.containsKey(fileName)) {
blacklist.removeAll(blacklistFileKeywords.get(fileName));
plugin.getLogger().info("[AI聊天] 黑名单文件已更新: " + fileName + "(" + newKeywords.size() + " 个关键词)");
} else {
plugin.getLogger().info("[AI聊天] 已加载黑名单文件: " + fileName + "(" + newKeywords.size() + " 个关键词)");
}
// 追加新关键词到全局黑名单并更新内存
blacklist.addAll(newKeywords);
blacklistFileKeywords.put(fileName, newKeywords);
blacklistFileHashes.put(fileName, newHash);
} catch (Exception e) { } catch (Exception e) {
plugin.getLogger().warning("[AI聊天] 加载黑名单文件失败: " + blFile.getName() + " - " + e.getMessage()); plugin.getLogger().warning("[AI聊天] 加载黑名单文件失败: " + fileName + " - " + e.getMessage());
}
}
// 处理被删除的文件:移除其关键词
for (String oldFile : blacklistFileKeywords.keySet()) {
if (!currentFileNames.contains(oldFile)) {
blacklist.removeAll(blacklistFileKeywords.get(oldFile));
blacklistFileKeywords.remove(oldFile);
blacklistFileHashes.remove(oldFile);
plugin.getLogger().info("[AI聊天] 黑名单文件已移除: " + oldFile);
} }
} }
} }
@@ -1574,6 +1611,13 @@ public class AIChatManager {
return true; return true;
} }
/** ★ 第73批:计算字符串的 CRC32 指纹(十六进制字符串),用于黑名单文件增量重载 */
private String crc32(String content) {
java.util.zip.CRC32 crc = new java.util.zip.CRC32();
crc.update(content.getBytes(java.nio.charset.StandardCharsets.UTF_8));
return Long.toHexString(crc.getValue());
}
/** /**
* ★ 第73批:从相对路径加载黑名单。 * ★ 第73批:从相对路径加载黑名单。
* <p> * <p>
+26 -4
View File
@@ -1,7 +1,29 @@
[ [
"404", "400",
"401",
"403", "403",
"200", "404",
"201", "405",
"202" "408",
"409",
"410",
"418",
"422",
"429",
"500",
"501",
"502",
"503",
"504",
"505",
"507",
"508",
"509",
"510",
"511",
"HTTP",
"http",
"HTTPS",
"https",
"nighx"
] ]