健壮性优化

This commit is contained in:
龙腾猫跃 committed 2026-06-28 23:57:37 +08:00
1 parent fe9fc04c20
commit 0a3b21505a
2 files changed
+11 -3

No files matched your search

+6 -2
View File
@@ -44,13 +44,17 @@ public class JsonConfigProvider : IConfigProvider {
if (value is null) {
// 直接在 JSON 中表示为 null
json.Value[key] = JValue.CreateNull();
Logger.Trace($"配置已修改:{key} = null({filePath})");
} else if (encrypted) {
// 需要加密,在 JSON 中保存密文字符串
throw new NotImplementedException("加密需要改为使用识别码,现在尚未实现。");
Logger.Trace($"配置已修改:{key} = 已加密({filePath})");
json.Value[key] = CryptographyUtils.AesEncrypt(value is string str ? str : JsonConvert.SerializeObject(value));
} else {
// 用 JToken 保留原始结构
json.Value[key] = JToken.FromObject(value);
JToken token = JToken.FromObject(value);
json.Value[key] = token;
Logger.Trace($"配置已修改:{key} = {token.ToString(Formatting.None)}({filePath})");
}
}
MakeDirty();
@@ -125,7 +129,7 @@ public static class ConfigUtils {
/// 从 secret.json 中读取特定密钥,如果未找到对应密钥则抛出异常。
/// </summary>
public static string GetSecret(string key)
=> secret.Read<string>(key, null, false) ?? throw new KeyNotFoundException($"Secret not found: {key}");
=> secret.Read<string>(key, null, false) ?? throw new KeyNotFoundException($"未找到密钥:{key}");
private static readonly JsonConfigProvider secret = new(Path.Combine(Paths.AppData, "secret.json"));
}