|
@@ -43,11 +43,20 @@ namespace InABox.Configuration
|
|
return GetFileName();
|
|
return GetFileName();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ protected virtual string SerializeConfiguration(Dictionary<string, T> config)
|
|
|
|
+ {
|
|
|
|
+ return Serialization.Serialize(config, true);
|
|
|
|
+ }
|
|
|
|
+ protected virtual Dictionary<string, T>? DeserializeConfiguration(string text)
|
|
|
|
+ {
|
|
|
|
+ return Serialization.Deserialize<Dictionary<string, T>>(text);
|
|
|
|
+ }
|
|
|
|
+
|
|
private Dictionary<string, T> LoadCurrentConfig(string filename)
|
|
private Dictionary<string, T> LoadCurrentConfig(string filename)
|
|
{
|
|
{
|
|
Dictionary<string, T>? config = null;
|
|
Dictionary<string, T>? config = null;
|
|
if (File.Exists(filename))
|
|
if (File.Exists(filename))
|
|
- config = Serialization.Deserialize<Dictionary<string, T>>(File.ReadAllText(filename));
|
|
|
|
|
|
+ config = DeserializeConfiguration(File.ReadAllText(filename));
|
|
return config ?? new Dictionary<string, T>();
|
|
return config ?? new Dictionary<string, T>();
|
|
}
|
|
}
|
|
|
|
|
|
@@ -69,8 +78,7 @@ namespace InABox.Configuration
|
|
{
|
|
{
|
|
//Logger.Send(LogType.Information, "", "Local Config: Deserializing Configuration");
|
|
//Logger.Send(LogType.Information, "", "Local Config: Deserializing Configuration");
|
|
|
|
|
|
- var config =
|
|
|
|
- Serialization.Deserialize<Dictionary<string, T>>(File.ReadAllText(filename)); //Toml.ReadString<T>(File.ReadAllText(FileName));
|
|
|
|
|
|
+ var config = DeserializeConfiguration(File.ReadAllText(filename)); //Toml.ReadString<T>(File.ReadAllText(FileName));
|
|
if (config != null && config.ContainsKey(Section))
|
|
if (config != null && config.ContainsKey(Section))
|
|
//Logger.Send(LogType.Information, "", "Local Config: Found Section ["+Section+"}");
|
|
//Logger.Send(LogType.Information, "", "Local Config: Found Section ["+Section+"}");
|
|
if (config[Section] != null)
|
|
if (config[Section] != null)
|
|
@@ -96,7 +104,7 @@ namespace InABox.Configuration
|
|
|
|
|
|
config[Section] = obj;
|
|
config[Section] = obj;
|
|
|
|
|
|
- var contents = Serialization.Serialize(config, true);
|
|
|
|
|
|
+ var contents = SerializeConfiguration(config);
|
|
|
|
|
|
File.WriteAllText(filename, contents);
|
|
File.WriteAllText(filename, contents);
|
|
|
|
|
|
@@ -113,7 +121,7 @@ namespace InABox.Configuration
|
|
config[section] = obj;
|
|
config[section] = obj;
|
|
}
|
|
}
|
|
|
|
|
|
- var contents = Serialization.Serialize(config, true);
|
|
|
|
|
|
+ var contents = SerializeConfiguration(config);
|
|
File.WriteAllText(filename, contents);
|
|
File.WriteAllText(filename, contents);
|
|
|
|
|
|
foreach(var (section, obj) in objs)
|
|
foreach(var (section, obj) in objs)
|
|
@@ -131,7 +139,7 @@ namespace InABox.Configuration
|
|
config.Remove(Section);
|
|
config.Remove(Section);
|
|
if (config.Any())
|
|
if (config.Any())
|
|
{
|
|
{
|
|
- var contents = Serialization.Serialize(config);
|
|
|
|
|
|
+ var contents = SerializeConfiguration(config);
|
|
|
|
|
|
File.WriteAllText(filename, contents);
|
|
File.WriteAllText(filename, contents);
|
|
}
|
|
}
|
|
@@ -156,12 +164,43 @@ namespace InABox.Configuration
|
|
if (!File.Exists(filename))
|
|
if (!File.Exists(filename))
|
|
return new[] { "" };
|
|
return new[] { "" };
|
|
|
|
|
|
- var config =
|
|
|
|
- Serialization.Deserialize<Dictionary<string, T>>(File.ReadAllText(filename)); //Toml.ReadString<T>(File.ReadAllText(FileName));
|
|
|
|
|
|
+ var config = DeserializeConfiguration(File.ReadAllText(filename));
|
|
if (config == null)
|
|
if (config == null)
|
|
return new[] { "" };
|
|
return new[] { "" };
|
|
|
|
|
|
return config.Keys.ToArray();
|
|
return config.Keys.ToArray();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ public class EncryptedLocalConfiguration<T> : LocalConfiguration<T>
|
|
|
|
+ where T : LocalConfigurationSettings, new()
|
|
|
|
+ {
|
|
|
|
+ private byte[] Key;
|
|
|
|
+
|
|
|
|
+ public EncryptedLocalConfiguration(byte[] key, string section = "") : base(section)
|
|
|
|
+ {
|
|
|
|
+ Key = key;
|
|
|
|
+ }
|
|
|
|
+ public EncryptedLocalConfiguration(byte[] key, string section, string folder) : base(section, folder)
|
|
|
|
+ {
|
|
|
|
+ Key = key;
|
|
|
|
+ }
|
|
|
|
+ public EncryptedLocalConfiguration(string key, string section = "") : this(ConvertKey(key), section) { }
|
|
|
|
+ public EncryptedLocalConfiguration(string key, string section, string folder) : this(ConvertKey(key), section, folder) { }
|
|
|
|
+
|
|
|
|
+ protected override string SerializeConfiguration(Dictionary<string, T> config)
|
|
|
|
+ {
|
|
|
|
+ return Encryption.EncryptV2(base.SerializeConfiguration(config), Key);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ protected override Dictionary<string, T>? DeserializeConfiguration(string text)
|
|
|
|
+ {
|
|
|
|
+ return base.DeserializeConfiguration(Encryption.DecryptV2(text, Key));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static byte[] ConvertKey(string key)
|
|
|
|
+ {
|
|
|
|
+ return Convert.FromBase64String(key);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|