You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
6.3 KiB

  1. namespace SoldierData
  2. {
  3. using Newtonsoft.Json;
  4. using OT.COM.Encryption;
  5. using OT.COM.LogisticsUtil;
  6. using System;
  7. using System.Collections.Concurrent;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. public static class SecretData
  11. {
  12. private static string _SecretFilePath = null;
  13. private static readonly byte[] _Key = null;
  14. private static readonly byte[] _IV = null;
  15. private static readonly object _secretlock = new();
  16. private static ConcurrentDictionary<string, string> _dicSecret = null;
  17. private static FileSystemWatcher _fswWatchwatcher = null;
  18. public static void Watch()
  19. {
  20. if (_fswWatchwatcher == null)
  21. {
  22. _fswWatchwatcher = new FileSystemWatcher();
  23. string sSecretFilePath = SecretData.GetSecretFilePath();
  24. _fswWatchwatcher.Path = new FileInfo(sSecretFilePath).Directory.FullName;
  25. _fswWatchwatcher.NotifyFilter = NotifyFilters.LastWrite;
  26. _fswWatchwatcher.Filter = "*.dat";
  27. // Add event handlers.
  28. _fswWatchwatcher.Changed += new FileSystemEventHandler(OnChanged);
  29. // Begin watching.
  30. _fswWatchwatcher.EnableRaisingEvents = true;
  31. }
  32. }
  33. // Define the event handlers.
  34. private static void OnChanged(object source, FileSystemEventArgs e)
  35. {
  36. if (SecretData.GetSecretFilePath() == e.FullPath)
  37. {
  38. SecretData.Refresh();
  39. }
  40. }
  41. static SecretData()
  42. {
  43. if (Util.GetSettingString(out string sKeyValue, "KEY") != null)
  44. {
  45. _Key = System.Text.Encoding.Unicode.GetBytes(sKeyValue);
  46. }
  47. if (Util.GetSettingString(out string sIVValue, "IV") != null)
  48. {
  49. _IV = System.Text.Encoding.Unicode.GetBytes(sIVValue);
  50. }
  51. }
  52. public static string GetSecretFilePath()
  53. {
  54. if (_SecretFilePath == null)
  55. {
  56. Util.GetSettingString(out string sSecretFolderName, "SECRETFOLDER", "Origtek");
  57. string sFolder = System.IO.Path.Combine(
  58. Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
  59. sSecretFolderName);
  60. if(!Directory.Exists(sFolder))
  61. {
  62. Directory.CreateDirectory(sFolder);
  63. }
  64. _SecretFilePath = System.IO.Path.Combine(
  65. Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
  66. sSecretFolderName,
  67. "Secret.dat");
  68. }
  69. return _SecretFilePath;
  70. }
  71. public static string FetchData(out ConcurrentDictionary<string, string> o_cdicResult)
  72. {
  73. ConcurrentDictionary<string, string> dicRes = new();
  74. string sMsg = null;
  75. try
  76. {
  77. do
  78. {
  79. string sPath = GetSecretFilePath();
  80. if (!File.Exists(sPath))
  81. {
  82. sMsg = "SECRET FILE NOT EXIST";
  83. break;
  84. }
  85. string sContent = new AES().DecryptByteToString(File.ReadAllBytes(sPath),
  86. _Key,
  87. _IV);
  88. Dictionary<string, string> dicResTemp = JsonConvert.DeserializeObject<Dictionary<string, string>>(sContent);
  89. foreach (KeyValuePair<string, string> kvp in dicResTemp)
  90. {
  91. dicRes.TryAdd(kvp.Key, kvp.Value);
  92. }
  93. }
  94. while (false);
  95. }
  96. catch (Exception ex)
  97. {
  98. sMsg = Util.GetLastExceptionMsg(ex);
  99. }
  100. o_cdicResult = dicRes;
  101. return sMsg;
  102. }
  103. public static void Refresh()
  104. {
  105. lock (_secretlock)
  106. {
  107. _dicSecret = null;
  108. }
  109. }
  110. public static string GetData(out ConcurrentDictionary<string, string> o_dicData)
  111. {
  112. string sMsg = null;
  113. ConcurrentDictionary<string, string> dicRes = new();
  114. try
  115. {
  116. do
  117. {
  118. lock (_secretlock)
  119. {
  120. if (_fswWatchwatcher == null)
  121. {
  122. Watch();
  123. }
  124. if (_dicSecret == null)
  125. {
  126. sMsg = FetchData(out _dicSecret);
  127. }
  128. if(sMsg != null)
  129. {
  130. break;
  131. }
  132. if (_dicSecret != null)
  133. {
  134. foreach (KeyValuePair<string, string> kvp in _dicSecret)
  135. {
  136. dicRes.TryAdd(kvp.Key, kvp.Value);
  137. }
  138. }
  139. }
  140. }
  141. while (false);
  142. }
  143. catch (Exception ex)
  144. {
  145. sMsg = Util.GetLastExceptionMsg(ex);
  146. }
  147. o_dicData = dicRes;
  148. return sMsg;
  149. }
  150. public static string SetData(Dictionary<string, string> i_dicData)
  151. {
  152. string sMsg = null;
  153. try
  154. {
  155. do
  156. {
  157. string sPath = GetSecretFilePath();
  158. if (!File.Exists(sPath))
  159. {
  160. sMsg = "SECRET FILE NOT EXIST";
  161. break;
  162. }
  163. byte[] byData = new AES().EncryptStringToByte(JsonConvert.SerializeObject(i_dicData),
  164. _Key,
  165. _IV);
  166. File.WriteAllBytes(sPath, byData);
  167. }
  168. while (false);
  169. }
  170. catch (Exception ex)
  171. {
  172. sMsg = Util.GetLastExceptionMsg(ex);
  173. }
  174. return sMsg;
  175. }
  176. }
  177. }