Settings.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System.Security.Cryptography;
  2. using System.Text;
  3. using System.Text.Json;
  4. using System.Text.Json.Serialization;
  5. namespace AutoPatcherAdmin
  6. {
  7. public static class Settings
  8. {
  9. private static readonly string JsonFileName = Path.Combine(AppContext.BaseDirectory, "PatchAdmin.json");
  10. private static readonly string LegacyIniFileName = Path.Combine(AppContext.BaseDirectory, "PatchAdmin.ini");
  11. private static readonly JsonSerializerOptions JsonOptions = new()
  12. {
  13. WriteIndented = true
  14. };
  15. public static string Client = @"S:\Patch\";
  16. public static string Host = @"ftp://127.0.0.1/";
  17. public static string Login = string.Empty;
  18. public static string Password = string.Empty;
  19. public static string Protocol = "Ftp";
  20. public static int Port = 0;
  21. public static bool AllowCleanUp = true;
  22. public static bool CompressFiles = false;
  23. public static void Load()
  24. {
  25. if (File.Exists(JsonFileName))
  26. {
  27. LoadJson();
  28. return;
  29. }
  30. if (File.Exists(LegacyIniFileName))
  31. {
  32. ImportLegacyIni();
  33. Save();
  34. return;
  35. }
  36. Save();
  37. }
  38. public static void Save()
  39. {
  40. var data = new AutoPatcherSettingsData
  41. {
  42. Client = Client,
  43. Host = Host,
  44. Login = Login,
  45. EncryptedPassword = EncryptPassword(Password),
  46. Protocol = Protocol,
  47. Port = Port,
  48. AllowCleanUp = AllowCleanUp,
  49. CompressFiles = CompressFiles,
  50. };
  51. File.WriteAllText(JsonFileName, JsonSerializer.Serialize(data, JsonOptions));
  52. }
  53. private static string EncryptPassword(string plaintext)
  54. {
  55. if (string.IsNullOrEmpty(plaintext)) return string.Empty;
  56. try
  57. {
  58. byte[] bytes = Encoding.UTF8.GetBytes(plaintext);
  59. byte[] encrypted = ProtectedData.Protect(bytes, null, DataProtectionScope.CurrentUser);
  60. return Convert.ToBase64String(encrypted);
  61. }
  62. catch
  63. {
  64. return string.Empty;
  65. }
  66. }
  67. private static string DecryptPassword(string encryptedBase64)
  68. {
  69. if (string.IsNullOrEmpty(encryptedBase64)) return string.Empty;
  70. try
  71. {
  72. byte[] bytes = Convert.FromBase64String(encryptedBase64);
  73. byte[] decrypted = ProtectedData.Unprotect(bytes, null, DataProtectionScope.CurrentUser);
  74. return Encoding.UTF8.GetString(decrypted);
  75. }
  76. catch
  77. {
  78. return string.Empty;
  79. }
  80. }
  81. private static void LoadJson()
  82. {
  83. try
  84. {
  85. string json = File.ReadAllText(JsonFileName);
  86. var data = JsonSerializer.Deserialize<AutoPatcherSettingsData>(json, JsonOptions);
  87. if (data == null) return;
  88. Client = data.Client ?? Client;
  89. Host = data.Host ?? Host;
  90. Login = data.Login ?? Login;
  91. Protocol = data.Protocol ?? Protocol;
  92. Port = data.Port;
  93. AllowCleanUp = data.AllowCleanUp;
  94. CompressFiles = data.CompressFiles;
  95. Password = !string.IsNullOrEmpty(data.EncryptedPassword)
  96. ? DecryptPassword(data.EncryptedPassword)
  97. : data.Password ?? string.Empty;
  98. }
  99. catch
  100. {
  101. Save();
  102. }
  103. }
  104. private static void ImportLegacyIni()
  105. {
  106. Dictionary<string, string> values = ReadLegacyIniSection("AutoPatcher");
  107. Client = ReadString(values, "Client", Client);
  108. Host = ReadString(values, "Host", Host);
  109. Login = ReadString(values, "Login", Login);
  110. Password = ReadString(values, "Password", Password);
  111. Protocol = ReadString(values, "Protocol", Protocol);
  112. Port = ReadInt(values, "Port", Port);
  113. AllowCleanUp = ReadBool(values, "AllowCleanUp", AllowCleanUp);
  114. CompressFiles = ReadBool(values, "CompressFiles", CompressFiles);
  115. }
  116. private static Dictionary<string, string> ReadLegacyIniSection(string section)
  117. {
  118. var values = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  119. if (!File.Exists(LegacyIniFileName)) return values;
  120. bool inSection = false;
  121. foreach (string rawLine in File.ReadAllLines(LegacyIniFileName))
  122. {
  123. string line = rawLine.Trim();
  124. if (line.Length == 0 || line.StartsWith(";") || line.StartsWith("#")) continue;
  125. if (line.StartsWith("[") && line.EndsWith("]"))
  126. {
  127. inSection = string.Equals(line.Trim('[', ']'), section, StringComparison.OrdinalIgnoreCase);
  128. continue;
  129. }
  130. if (!inSection) continue;
  131. int separator = line.IndexOf('=');
  132. if (separator <= 0) continue;
  133. string key = line.Substring(0, separator).Trim();
  134. string value = line.Substring(separator + 1).Trim();
  135. values[key] = value;
  136. }
  137. return values;
  138. }
  139. private static string ReadString(Dictionary<string, string> values, string key, string fallback)
  140. {
  141. return values.TryGetValue(key, out string? value) && !string.IsNullOrWhiteSpace(value)
  142. ? value
  143. : fallback;
  144. }
  145. private static int ReadInt(Dictionary<string, string> values, string key, int fallback)
  146. {
  147. return values.TryGetValue(key, out string? value) && int.TryParse(value, out int result)
  148. ? result
  149. : fallback;
  150. }
  151. private static bool ReadBool(Dictionary<string, string> values, string key, bool fallback)
  152. {
  153. return values.TryGetValue(key, out string? value) && bool.TryParse(value, out bool result)
  154. ? result
  155. : fallback;
  156. }
  157. private sealed class AutoPatcherSettingsData
  158. {
  159. public string? Client { get; set; }
  160. public string? Host { get; set; }
  161. public string? Login { get; set; }
  162. [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
  163. public string? Password { get; set; } // legacy plaintext — read only for migration
  164. public string? EncryptedPassword { get; set; }
  165. public string? Protocol { get; set; }
  166. public int Port { get; set; }
  167. public bool AllowCleanUp { get; set; } = true;
  168. public bool CompressFiles { get; set; }
  169. }
  170. }
  171. }