tbf/EventViewer/LocalSettings.cs

213 lines
8.1 KiB
C#
Raw Normal View History

///
/// Copyright (c) 2020 Sensus Slovensko a.s.
///
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Xml.Serialization;
namespace EventViewer
{
/// <summary>
/// Class to be serialized to an XML file...
/// </summary>
[XmlRootAttribute("EventViewer")]
public class LocalSettings
{
static XmlSerializer serializer = XmlSerializer.FromTypes(new[] { typeof(LocalSettings) })[0];
///
/// Configuration file ecryption/decryption key and initialization vector
///
static byte[] key = new byte[] { 83, 254, 105, 64, 184, 201, 195, 127, 52, 99, 77, 45, 252, 132, 163, 156 };
static byte[] IV = new byte[] { 189, 23, 137, 56, 204, 241, 242, 118, 28, 89, 9, 198, 224, 111, 186, 125 };
/// Common
public string ConnectionString;
public bool NonAnonymousAccess;
public string UserName;
public string UsersDBConnString;
public int RecentTimeDays;
public string Language;
/// MainWnd
public int WndLeft;
public int WndTop;
public int WndWidth;
public int WndHeight;
public bool WndMaximized;
public int ColWid1;
public int ColWid2;
public int ColWid3;
public int ColWid4;
public int ColWid5;
public int ColWid6;
public LocalSettings()
{
}
public LocalSettings(bool createNew)
: this()
{
/// Default settings
/// Common
#if DEBUG
ConnectionString = "SERVER=localhost; DATABASE=test-e; UID=root; PASSWORD=kraken; CHARSET=utf8";
UsersDBConnString = "SERVER=localhost; DATABASE=test; UID=root; PASSWORD=kraken; CHARSET=utf8;";
#else
ConnectionString = "SERVER=10.42.128.16; DATABASE=st_wr_shared_events; UID=v9305784; PASSWORD=p89344390; CHARSET=utf8";
UsersDBConnString = "SERVER=10.42.128.16; DATABASE=st_wr_shared_config; UID=u9305784; PASSWORD=p89344390; CHARSET=utf8;";
#endif
RecentTimeDays = 7;
Language = "sk";
/// MainWnd
WndLeft = 20;
WndTop = 20;
WndWidth = 1250;
WndHeight = 750;
WndMaximized = false;
ColWid1 = 80;
ColWid2 = 50;
ColWid3 = 100;
ColWid4 = 60;
ColWid5 = 70;
ColWid6 = 500;
}
/// <summary>
/// Load public fields of this class from the XML file.
/// </summary>
/// <param name="fileName">Name of the file.</param>
/// <returns>
/// LocalSettings object or null when Load() fails
/// </returns>
public static LocalSettings Load(string fileName)
{
try
{
bool isEncrypted = true;
using (StreamReader reader = new StreamReader(fileName))
{
isEncrypted = !reader.ReadLine().StartsWith("<?xml version=");
reader.Close();
}
if (isEncrypted)
{
/// Write settings to a string
StringBuilder plain = new StringBuilder();
using (RijndaelManaged aes = new RijndaelManaged { Padding = PaddingMode.Zeros })
{
using (FileStream fsCrypt = new FileStream(fileName, FileMode.Open))
{
using (ICryptoTransform decryptor = aes.CreateDecryptor(key, IV))
{
using (CryptoStream cs = new CryptoStream(fsCrypt, decryptor, CryptoStreamMode.Read))
{
using (MemoryStream mStream = new MemoryStream())
{
using (var writer = new BinaryWriter(mStream))
{
int data;
while ((data = cs.ReadByte()) != -1)
{
if (data != 0) writer.Write((byte)data);
}
writer.Flush();
mStream.Position = 0;
using (var reader = new StreamReader(mStream))
{
LocalSettings ls = serializer.Deserialize(reader) as LocalSettings;
//log.WarnFormat("Succesfully loaded and decrypted from '{0}'", fileName);
return ls;
}
}
}
}
}
}
}
}
else
{
using (StreamReader reader = new StreamReader(fileName))
{
LocalSettings ls = serializer.Deserialize(reader) as LocalSettings;
//log.WarnFormat("Succesfully loaded from '{0}'", fileName);
return ls;
}
}
}
2021-04-07 19:32:33 +00:00
catch (Exception)
{
//log.ErrorFormat("Failed to load from '{0}': {1}", fileName, e.Message);
return null;
}
}
/// <summary>
/// Save public fields of this class to the XML file.
/// </summary>
public void Save()
{
try
{
#if false
/// Write settings to an unencrypted file
using (TextWriter writer = new StreamWriter(Program.LocalSettingsFileName))
{
serializer.Serialize(writer, this);
log.Warn("LocalSettings succesfully saved");
}
#else
/// Encrypt and write to file 'Program.LocalSettingsFileName'
using (RijndaelManaged aes = new RijndaelManaged { Padding = PaddingMode.Zeros })
{
using (FileStream fsCrypt = new FileStream(Program.LocalSettingsFileName, FileMode.Create))
{
using (ICryptoTransform encryptor = aes.CreateEncryptor(key, IV))
{
using (CryptoStream cs = new CryptoStream(fsCrypt, encryptor, CryptoStreamMode.Write))
{
using (MemoryStream mStream = new MemoryStream())
{
using (var writer = new StreamWriter(mStream))
{
/// Serialize and write settings to a memory stream
serializer.Serialize(writer, this);
writer.Flush();
mStream.Position = 0;
using (var reader = new StreamReader(mStream))
{
/// Encrypt and write to file 'Program.LocalSettingsFileName'
int data;
while ((data = mStream.ReadByte()) != -1) cs.WriteByte((byte)data);
//log.WarnFormat("Succesfully encrypted and saved to '{0}'", Program.LocalSettingsFileName);
}
}
}
}
}
}
}
#endif
}
2021-04-07 19:32:33 +00:00
catch (Exception)
{
//log.ErrorFormat("Failed to save to '{0}': {1}", Program.LocalSettingsFileName, e.Message);
}
}
}
}