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
212 lines
6.3 KiB
namespace SoldierData
|
|
{
|
|
using Newtonsoft.Json;
|
|
using OT.COM.Encryption;
|
|
using OT.COM.LogisticsUtil;
|
|
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
public static class SecretData
|
|
{
|
|
private static string _SecretFilePath = null;
|
|
private static readonly byte[] _Key = null;
|
|
private static readonly byte[] _IV = null;
|
|
private static readonly object _secretlock = new();
|
|
private static ConcurrentDictionary<string, string> _dicSecret = null;
|
|
private static FileSystemWatcher _fswWatchwatcher = null;
|
|
public static void Watch()
|
|
{
|
|
if (_fswWatchwatcher == null)
|
|
{
|
|
_fswWatchwatcher = new FileSystemWatcher();
|
|
string sSecretFilePath = SecretData.GetSecretFilePath();
|
|
_fswWatchwatcher.Path = new FileInfo(sSecretFilePath).Directory.FullName;
|
|
_fswWatchwatcher.NotifyFilter = NotifyFilters.LastWrite;
|
|
_fswWatchwatcher.Filter = "*.dat";
|
|
|
|
|
|
// Add event handlers.
|
|
_fswWatchwatcher.Changed += new FileSystemEventHandler(OnChanged);
|
|
// Begin watching.
|
|
_fswWatchwatcher.EnableRaisingEvents = true;
|
|
}
|
|
}
|
|
|
|
|
|
// Define the event handlers.
|
|
private static void OnChanged(object source, FileSystemEventArgs e)
|
|
{
|
|
if (SecretData.GetSecretFilePath() == e.FullPath)
|
|
{
|
|
SecretData.Refresh();
|
|
}
|
|
}
|
|
|
|
static SecretData()
|
|
{
|
|
if (Util.GetSettingString(out string sKeyValue, "KEY") != null)
|
|
{
|
|
_Key = System.Text.Encoding.Unicode.GetBytes(sKeyValue);
|
|
}
|
|
|
|
if (Util.GetSettingString(out string sIVValue, "IV") != null)
|
|
{
|
|
_IV = System.Text.Encoding.Unicode.GetBytes(sIVValue);
|
|
}
|
|
}
|
|
|
|
public static string GetSecretFilePath()
|
|
{
|
|
if (_SecretFilePath == null)
|
|
{
|
|
Util.GetSettingString(out string sSecretFolderName, "SECRETFOLDER", "Origtek");
|
|
|
|
string sFolder = System.IO.Path.Combine(
|
|
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
|
|
sSecretFolderName);
|
|
|
|
if(!Directory.Exists(sFolder))
|
|
{
|
|
Directory.CreateDirectory(sFolder);
|
|
}
|
|
|
|
_SecretFilePath = System.IO.Path.Combine(
|
|
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
|
|
sSecretFolderName,
|
|
"Secret.dat");
|
|
}
|
|
return _SecretFilePath;
|
|
}
|
|
|
|
public static string FetchData(out ConcurrentDictionary<string, string> o_cdicResult)
|
|
{
|
|
ConcurrentDictionary<string, string> dicRes = new();
|
|
string sMsg = null;
|
|
|
|
try
|
|
{
|
|
do
|
|
{
|
|
string sPath = GetSecretFilePath();
|
|
if (!File.Exists(sPath))
|
|
{
|
|
sMsg = "SECRET FILE NOT EXIST";
|
|
break;
|
|
}
|
|
|
|
string sContent = new AES().DecryptByteToString(File.ReadAllBytes(sPath),
|
|
_Key,
|
|
_IV);
|
|
|
|
Dictionary<string, string> dicResTemp = JsonConvert.DeserializeObject<Dictionary<string, string>>(sContent);
|
|
|
|
foreach (KeyValuePair<string, string> kvp in dicResTemp)
|
|
{
|
|
dicRes.TryAdd(kvp.Key, kvp.Value);
|
|
}
|
|
|
|
}
|
|
while (false);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
sMsg = Util.GetLastExceptionMsg(ex);
|
|
}
|
|
|
|
o_cdicResult = dicRes;
|
|
return sMsg;
|
|
}
|
|
|
|
public static void Refresh()
|
|
{
|
|
lock (_secretlock)
|
|
{
|
|
_dicSecret = null;
|
|
}
|
|
}
|
|
|
|
public static string GetData(out ConcurrentDictionary<string, string> o_dicData)
|
|
{
|
|
string sMsg = null;
|
|
ConcurrentDictionary<string, string> dicRes = new();
|
|
|
|
try
|
|
{
|
|
do
|
|
{
|
|
lock (_secretlock)
|
|
{
|
|
|
|
if (_fswWatchwatcher == null)
|
|
{
|
|
Watch();
|
|
}
|
|
|
|
|
|
if (_dicSecret == null)
|
|
{
|
|
sMsg = FetchData(out _dicSecret);
|
|
}
|
|
|
|
if(sMsg != null)
|
|
{
|
|
break;
|
|
}
|
|
|
|
if (_dicSecret != null)
|
|
{
|
|
foreach (KeyValuePair<string, string> kvp in _dicSecret)
|
|
{
|
|
dicRes.TryAdd(kvp.Key, kvp.Value);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
while (false);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
sMsg = Util.GetLastExceptionMsg(ex);
|
|
}
|
|
|
|
o_dicData = dicRes;
|
|
|
|
return sMsg;
|
|
}
|
|
|
|
public static string SetData(Dictionary<string, string> i_dicData)
|
|
{
|
|
string sMsg = null;
|
|
|
|
try
|
|
{
|
|
do
|
|
{
|
|
string sPath = GetSecretFilePath();
|
|
if (!File.Exists(sPath))
|
|
{
|
|
sMsg = "SECRET FILE NOT EXIST";
|
|
break;
|
|
}
|
|
|
|
|
|
byte[] byData = new AES().EncryptStringToByte(JsonConvert.SerializeObject(i_dicData),
|
|
_Key,
|
|
_IV);
|
|
|
|
File.WriteAllBytes(sPath, byData);
|
|
|
|
}
|
|
while (false);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
sMsg = Util.GetLastExceptionMsg(ex);
|
|
}
|
|
|
|
|
|
return sMsg;
|
|
}
|
|
}
|
|
}
|