AppConfigHelper.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.Configuration;
  3. namespace ivf_tl_Operate.Helpers
  4. {
  5. /// <summary>
  6. /// M5-01-2 / M5-02:统一配置读写封装(operate 单一数据源 App.config 的集中接管点)。
  7. /// - 强类型容错读取(缺键返回默认值而非 NPE,缓解 R8"改一处漏一处起不来")。
  8. /// - 加密项读写经 <see cref="CryptoHelper"/>(passWord / engineerPwd 等本机凭据)。
  9. /// - <see cref="MigratePlaintextCredentials"/>:启动一次性明文→密文迁移(幂等)。
  10. /// 分层标注([D8] 待确认):
  11. /// 本地层:urlIp/urlPort/mqttIp/mqttPort/kfkaIP/kfkaPort/cacheDisk/Language/outInter/tlNum/houseEnabled/userName/passWord(加密)/engineerPwd(加密)
  12. /// 数据库层(TLSetting,不在此封装):autoFocusTime/videoFps/cropNum/heapDate/keepDate/cleanSurfaceData
  13. /// 暂留本地、待 D8 定归属:csTime/gbTime/VentNum/... 换气/CCD 类业务键
  14. /// [M7] 容错与迁移随合并进程运行验证(本地不可构建/运行)。
  15. /// </summary>
  16. public static class AppConfigHelper
  17. {
  18. /// <summary>工程师口令的 config 键(M5-02-3 治理 tl13579)。</summary>
  19. public const string EngineerPwdKey = "engineerPwd";
  20. /// <summary>工程师口令缺省值(首次无值回退;迁移时加密回写)。</summary>
  21. public const string DefaultEngineerPwd = "tl13579";
  22. /// <summary>容错读取字符串,缺键/空返回默认值。</summary>
  23. public static string GetString(string key, string defaultValue = "")
  24. {
  25. try
  26. {
  27. var v = ConfigurationManager.AppSettings[key];
  28. return string.IsNullOrEmpty(v) ? defaultValue : v;
  29. }
  30. catch
  31. {
  32. return defaultValue;
  33. }
  34. }
  35. /// <summary>容错读取整数,缺键/解析失败返回默认值。</summary>
  36. public static int GetInt(string key, int defaultValue = 0)
  37. {
  38. return int.TryParse(GetString(key, null), out var r) ? r : defaultValue;
  39. }
  40. /// <summary>
  41. /// 读取加密项并解密(旧明文原样返回,由迁移逻辑负责回写)。缺键返回默认值。
  42. /// </summary>
  43. public static string GetDecrypted(string key, string defaultValue = "")
  44. {
  45. var raw = GetString(key, null);
  46. if (string.IsNullOrEmpty(raw)) return defaultValue;
  47. return CryptoHelper.Decrypt(raw);
  48. }
  49. /// <summary>配置收敛:operate↔control 共享的连接键(唯一数据源 tl-shared.config)。</summary>
  50. private static readonly System.Collections.Generic.HashSet<string> SharedKeys =
  51. new System.Collections.Generic.HashSet<string>(System.StringComparer.OrdinalIgnoreCase)
  52. { "urlIp", "urlPort", "mqttIp", "mqttPort", "kfkaIP", "kfkaPort", "outInter" };
  53. /// <summary>共享文件路径 = operate 输出根目录\tl-shared.config(control 经 ..\ 指此)。</summary>
  54. public static string SharedConfigPath =>
  55. System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "tl-shared.config");
  56. /// <summary>
  57. /// 写回普通键。配置收敛:共享键落唯一数据源 tl-shared.config(SharedConfigStore),
  58. /// 非共享键沿用 OpenExeConfiguration 落 operate 自己的 config。写后刷新本进程缓存。
  59. /// </summary>
  60. public static void Save(string key, string value)
  61. {
  62. try
  63. {
  64. if (SharedKeys.Contains(key))
  65. {
  66. SharedConfigStore.Write(SharedConfigPath, key, value);
  67. }
  68. else
  69. {
  70. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  71. if (config.AppSettings.Settings[key] == null)
  72. config.AppSettings.Settings.Add(key, value);
  73. else
  74. config.AppSettings.Settings[key].Value = value;
  75. config.Save(ConfigurationSaveMode.Modified);
  76. }
  77. ConfigurationManager.RefreshSection("appSettings");
  78. }
  79. catch
  80. {
  81. // 写失败不抛出(避免阻断 UI/启动);[M7] 运行环境核查落盘。
  82. }
  83. }
  84. /// <summary>加密后写回(用于 passWord / engineerPwd 等凭据)。</summary>
  85. public static void SaveEncrypted(string key, string plain)
  86. {
  87. Save(key, CryptoHelper.Encrypt(plain));
  88. }
  89. /// <summary>
  90. /// M5-02-5:启动一次性凭据迁移(幂等)。
  91. /// (a) passWord:明文(旧值,如 123456)→密文回写;已是密文则跳过。
  92. /// (b) engineerPwd:键缺失→以默认 tl13579 加密建键;明文→密文回写。
  93. /// 仅当存在需迁移的明文时才写盘,已全部加密则不触碰文件(幂等)。
  94. /// </summary>
  95. public static void MigratePlaintextCredentials()
  96. {
  97. try
  98. {
  99. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  100. bool changed = false;
  101. // (a) passWord 明文 → 密文
  102. var pw = config.AppSettings.Settings["passWord"];
  103. if (pw != null && !string.IsNullOrEmpty(pw.Value) && !CryptoHelper.IsEncrypted(pw.Value))
  104. {
  105. pw.Value = CryptoHelper.Encrypt(pw.Value);
  106. changed = true;
  107. }
  108. // (b) engineerPwd 缺键或明文 → 加密建键/回写(治理 tl13579)
  109. var ep = config.AppSettings.Settings[EngineerPwdKey];
  110. if (ep == null)
  111. {
  112. config.AppSettings.Settings.Add(EngineerPwdKey, CryptoHelper.Encrypt(DefaultEngineerPwd));
  113. changed = true;
  114. }
  115. else if (!string.IsNullOrEmpty(ep.Value) && !CryptoHelper.IsEncrypted(ep.Value))
  116. {
  117. ep.Value = CryptoHelper.Encrypt(ep.Value);
  118. changed = true;
  119. }
  120. if (changed)
  121. {
  122. config.Save(ConfigurationSaveMode.Modified);
  123. ConfigurationManager.RefreshSection("appSettings");
  124. }
  125. }
  126. catch
  127. {
  128. // 迁移失败不阻断启动;[M7] 运行环境核查首次迁移。
  129. }
  130. }
  131. /// <summary>读取工程师口令明文(解密;键缺失/解密失败回退默认 tl13579)。</summary>
  132. public static string GetEngineerPwd()
  133. {
  134. var v = GetDecrypted(EngineerPwdKey, DefaultEngineerPwd);
  135. return string.IsNullOrEmpty(v) ? DefaultEngineerPwd : v;
  136. }
  137. }
  138. }