Improved Settings

- Full fallback support for old versions of settings files
- Improved usage when writing and reading from settings
This commit is contained in:
MaxXor 2015-08-15 20:13:52 +02:00
parent 8f4b63f6a2
commit 08589c4d03
11 changed files with 198 additions and 106 deletions

View File

@ -50,11 +50,11 @@ namespace xServer.Core.Commands
FrmMain.Instance.AddClientToListview(lvi); FrmMain.Instance.AddClientToListview(lvi);
if (XMLSettings.ShowPopup) if (Settings.ShowPopup)
FrmMain.Instance.ShowPopup(client); FrmMain.Instance.ShowPopup(client);
client.Value.IsAuthenticated = true; client.Value.IsAuthenticated = true;
if (XMLSettings.ShowToolTip) if (Settings.ShowToolTip)
new Packets.ServerPackets.GetSystemInfo().Execute(client); new Packets.ServerPackets.GetSystemInfo().Execute(client);
} }
catch catch

View File

@ -112,7 +112,7 @@ namespace xServer.Core.Commands
if (packet.SystemInfos == null) if (packet.SystemInfos == null)
return; return;
if (XMLSettings.ShowToolTip) if (Settings.ShowToolTip)
{ {
var builder = new StringBuilder(); var builder = new StringBuilder();
for (int i = 0; i < packet.SystemInfos.Length; i += 2) for (int i = 0; i < packet.SystemInfos.Length; i += 2)

View File

@ -5,45 +5,161 @@ using System.Xml.XPath;
namespace xServer.Core.Data namespace xServer.Core.Data
{ {
public static class XMLSettings public static class Settings
{ {
private static readonly string SettingsPath = Path.Combine(Application.StartupPath, "settings.xml");
public const string VERSION = "RELEASE4.1"; public const string VERSION = "RELEASE4.1";
public static ushort ListenPort { get; set; }
public static bool ShowToU { get; set; }
public static bool AutoListen { get; set; }
public static bool ShowPopup { get; set; }
public static bool UseUPnP { get; set; }
public static bool ShowToolTip { get; set; }
public static string Password { get; set; }
public static bool IntegrateNoIP { get; set; }
public static string NoIPHost { get; set; }
public static string NoIPUsername { get; set; }
public static string NoIPPassword { get; set; }
public static string SaveFormat { get; set; }
private static readonly string _settingsPath = Path.Combine(Application.StartupPath, "settings.xml"); public static ushort ListenPort
public static void ReadSettings()
{ {
ListenPort = ushort.Parse(ReadValueSafe("ListenPort", "4782")); get
ShowToU = bool.Parse(ReadValueSafe("ShowToU", "True")); {
AutoListen = bool.Parse(ReadValueSafe("AutoListen", "False")); return ushort.Parse(ReadValueSafe("ListenPort", "4782"));
ShowPopup = bool.Parse(ReadValueSafe("ShowPopup", "False")); }
UseUPnP = bool.Parse(ReadValueSafe("UseUPnP", "False")); set
SaveFormat = ReadValueSafe("SaveFormat", "APP - URL - USER:PASS"); {
ShowToolTip = bool.Parse(ReadValueSafe("ShowToolTip", "False")); WriteValue("ListenPort", value.ToString());
IntegrateNoIP = bool.Parse(ReadValueSafe("EnableNoIPUpdater", "False")); }
NoIPHost = ReadValueSafe("NoIPHost"); }
NoIPUsername = ReadValueSafe("NoIPUsername");
NoIPPassword = ReadValueSafe("NoIPPassword"); public static bool ShowToU
Password = ReadValueSafe("Password", "1234"); {
get
{
return bool.Parse(ReadValueSafe("ShowToU", "True"));
}
set
{
WriteValue("ShowToU", value.ToString());
}
}
public static bool AutoListen
{
get
{
return bool.Parse(ReadValueSafe("AutoListen", "False"));
}
set
{
WriteValue("AutoListen", value.ToString());
}
}
public static bool ShowPopup
{
get
{
return bool.Parse(ReadValueSafe("ShowPopup", "False"));
}
set
{
WriteValue("ShowPopup", value.ToString());
}
}
public static bool UseUPnP
{
get
{
return bool.Parse(ReadValueSafe("UseUPnP", "False"));
}
set
{
WriteValue("UseUPnP", value.ToString());
}
}
public static bool ShowToolTip
{
get
{
return bool.Parse(ReadValueSafe("ShowToolTip", "False"));
}
set
{
WriteValue("ShowToolTip", value.ToString());
}
}
public static string Password
{
get
{
return ReadValueSafe("Password", "1234");
}
set
{
WriteValue("Password", value);
}
}
public static bool EnableNoIPUpdater
{
get
{
return bool.Parse(ReadValueSafe("EnableNoIPUpdater", "False"));
}
set
{
WriteValue("EnableNoIPUpdater", value.ToString());
}
}
public static string NoIPHost
{
get
{
return ReadValueSafe("NoIPHost");
}
set
{
WriteValue("NoIPHost", value);
}
}
public static string NoIPUsername
{
get
{
return ReadValueSafe("NoIPUsername");
}
set
{
WriteValue("NoIPUsername", value);
}
}
public static string NoIPPassword
{
get
{
return ReadValueSafe("NoIPPassword");
}
set
{
WriteValue("NoIPPassword", value);
}
}
public static string SaveFormat
{
get
{
return ReadValueSafe("SaveFormat", "APP - URL - USER:PASS");
}
set
{
WriteValue("SaveFormat", value);
}
} }
public static string ReadValue(string pstrValueToRead) public static string ReadValue(string pstrValueToRead)
{ {
try try
{ {
XPathDocument doc = new XPathDocument(_settingsPath); XPathDocument doc = new XPathDocument(SettingsPath);
XPathNavigator nav = doc.CreateNavigator(); XPathNavigator nav = doc.CreateNavigator();
XPathExpression expr = nav.Compile(@"/settings/" + pstrValueToRead); XPathExpression expr = nav.Compile(@"/settings/" + pstrValueToRead);
XPathNodeIterator iterator = nav.Select(expr); XPathNodeIterator iterator = nav.Select(expr);
@ -60,28 +176,28 @@ namespace xServer.Core.Data
} }
} }
public static string ReadValueSafe(string pstrValueToRead, string defaultValue = "") private static string ReadValueSafe(string pstrValueToRead, string defaultValue = "")
{ {
string value = ReadValue(pstrValueToRead); string value = ReadValue(pstrValueToRead);
return (!string.IsNullOrEmpty(value)) ? value: defaultValue; return (!string.IsNullOrEmpty(value)) ? value: defaultValue;
} }
public static bool WriteValue(string pstrValueToRead, string pstrValueToWrite) private static void WriteValue(string pstrValueToRead, string pstrValueToWrite)
{ {
try try
{ {
XmlDocument doc = new XmlDocument(); XmlDocument doc = new XmlDocument();
if (File.Exists(_settingsPath)) if (File.Exists(SettingsPath))
{ {
using (var reader = new XmlTextReader(_settingsPath)) using (var reader = new XmlTextReader(SettingsPath))
{ {
doc.Load(reader); doc.Load(reader);
} }
} }
else else
{ {
var dir = Path.GetDirectoryName(_settingsPath); var dir = Path.GetDirectoryName(SettingsPath);
if (!Directory.Exists(dir)) if (!Directory.Exists(dir))
{ {
Directory.CreateDirectory(dir); Directory.CreateDirectory(dir);
@ -95,16 +211,14 @@ namespace xServer.Core.Data
{ {
oldNode = doc.SelectSingleNode("settings"); oldNode = doc.SelectSingleNode("settings");
oldNode.AppendChild(doc.CreateElement(pstrValueToRead)).InnerText = pstrValueToWrite; oldNode.AppendChild(doc.CreateElement(pstrValueToRead)).InnerText = pstrValueToWrite;
doc.Save(_settingsPath); doc.Save(SettingsPath);
return true; return;
} }
oldNode.InnerText = pstrValueToWrite; oldNode.InnerText = pstrValueToWrite;
doc.Save(_settingsPath); doc.Save(SettingsPath);
return true;
} }
catch catch
{ {
return false;
} }
} }
} }

View File

@ -20,15 +20,15 @@ namespace xServer.Core.Utilities
private static void BackgroundUpdater() private static void BackgroundUpdater()
{ {
_running = true; _running = true;
while (XMLSettings.IntegrateNoIP) while (Settings.EnableNoIPUpdater)
{ {
try try
{ {
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(string.Format("http://dynupdate.no-ip.com/nic/update?hostname={0}", XMLSettings.NoIPHost)); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(string.Format("http://dynupdate.no-ip.com/nic/update?hostname={0}", Settings.NoIPHost));
request.Proxy = null; request.Proxy = null;
request.UserAgent = string.Format("xRAT No-Ip Updater/2.0 {0}", XMLSettings.NoIPUsername); request.UserAgent = string.Format("xRAT No-Ip Updater/2.0 {0}", Settings.NoIPUsername);
request.Timeout = 10000; request.Timeout = 10000;
request.Headers.Add(HttpRequestHeader.Authorization, string.Format("Basic {0}", Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", XMLSettings.NoIPUsername, XMLSettings.NoIPPassword))))); request.Headers.Add(HttpRequestHeader.Authorization, string.Format("Basic {0}", Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", Settings.NoIPUsername, Settings.NoIPPassword)))));
request.Method = "GET"; request.Method = "GET";
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())

View File

@ -11,7 +11,7 @@ namespace xServer.Forms
{ {
InitializeComponent(); InitializeComponent();
lblVersion.Text = XMLSettings.VERSION; lblVersion.Text = Settings.VERSION;
rtxtContent.Text = Properties.Resources.TermsOfUse; rtxtContent.Text = Properties.Resources.TermsOfUse;
lnkGithubPage.Links.Add(new LinkLabel.Link { LinkData = "https://github.com/MaxXor/xRAT" }); lnkGithubPage.Links.Add(new LinkLabel.Link { LinkData = "https://github.com/MaxXor/xRAT" });

View File

@ -30,7 +30,7 @@ namespace xServer.Forms
_hosts.Add(host); _hosts.Add(host);
lstHosts.DataSource = new BindingSource(_hosts, null); lstHosts.DataSource = new BindingSource(_hosts, null);
txtTag.Text = profile.ReadValueSafe("Tag", "Office04"); txtTag.Text = profile.ReadValueSafe("Tag", "Office04");
txtPassword.Text = profile.ReadValueSafe("Password", XMLSettings.Password); txtPassword.Text = profile.ReadValueSafe("Password", Settings.Password);
txtDelay.Text = profile.ReadValueSafe("Delay", "5000"); txtDelay.Text = profile.ReadValueSafe("Delay", "5000");
txtMutex.Text = profile.ReadValueSafe("Mutex", FormatHelper.GenerateMutex()); txtMutex.Text = profile.ReadValueSafe("Mutex", FormatHelper.GenerateMutex());
chkInstall.Checked = bool.Parse(profile.ReadValueSafe("InstallClient", "False")); chkInstall.Checked = bool.Parse(profile.ReadValueSafe("InstallClient", "False"));
@ -86,7 +86,7 @@ namespace xServer.Forms
{ {
LoadProfile("Default"); LoadProfile("Default");
txtPort.Text = XMLSettings.ListenPort.ToString(); txtPort.Text = Settings.ListenPort.ToString();
UpdateControlStates(); UpdateControlStates();

View File

@ -40,10 +40,10 @@ namespace xServer.Forms
{ {
Instance = this; Instance = this;
XMLSettings.ReadSettings(); AES.PreHashKey(Settings.Password);
AES.PreHashKey(XMLSettings.Password);
#if !DEBUG #if !DEBUG
if (XMLSettings.ShowToU) if (Settings.ShowToU)
ShowTermsOfService(); ShowTermsOfService();
#endif #endif
@ -86,22 +86,22 @@ namespace xServer.Forms
private void AutostartListeningP() private void AutostartListeningP()
{ {
if (XMLSettings.AutoListen && XMLSettings.UseUPnP) if (Settings.AutoListen && Settings.UseUPnP)
{ {
UPnP.Initialize(XMLSettings.ListenPort); UPnP.Initialize(Settings.ListenPort);
ConServer.Listen(XMLSettings.ListenPort); ConServer.Listen(Settings.ListenPort);
} }
else if (XMLSettings.AutoListen) else if (Settings.AutoListen)
{ {
UPnP.Initialize(); UPnP.Initialize();
ConServer.Listen(XMLSettings.ListenPort); ConServer.Listen(Settings.ListenPort);
} }
else else
{ {
UPnP.Initialize(); UPnP.Initialize();
} }
if (XMLSettings.IntegrateNoIP) if (Settings.EnableNoIPUpdater)
{ {
NoIpUpdater.Start(); NoIpUpdater.Start();
} }
@ -116,7 +116,7 @@ namespace xServer.Forms
private void FrmMain_FormClosing(object sender, FormClosingEventArgs e) private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
{ {
ConServer.Disconnect(); ConServer.Disconnect();
UPnP.DeletePortMap(XMLSettings.ListenPort); UPnP.DeletePortMap(Settings.ListenPort);
nIcon.Visible = false; nIcon.Visible = false;
nIcon.Dispose(); nIcon.Dispose();
Instance = null; Instance = null;

View File

@ -27,7 +27,7 @@ namespace xServer.Forms
InitializeComponent(); InitializeComponent();
this.Text = WindowHelper.GetWindowTitle("Password Recovery", _clients.Length); this.Text = WindowHelper.GetWindowTitle("Password Recovery", _clients.Length);
txtFormat.Text = XMLSettings.SaveFormat; txtFormat.Text = Settings.SaveFormat;
} }
private void FrmPasswordRecovery_Load(object sender, EventArgs e) private void FrmPasswordRecovery_Load(object sender, EventArgs e)
@ -144,8 +144,7 @@ namespace xServer.Forms
private void txtFormat_TextChanged(object sender, EventArgs e) private void txtFormat_TextChanged(object sender, EventArgs e)
{ {
XMLSettings.WriteValue("SaveFormat", txtFormat.Text); Settings.SaveFormat = txtFormat.Text;
XMLSettings.SaveFormat = txtFormat.Text;
} }
#endregion #endregion

View File

@ -31,16 +31,16 @@ namespace xServer.Forms
private void FrmSettings_Load(object sender, EventArgs e) private void FrmSettings_Load(object sender, EventArgs e)
{ {
ncPort.Value = XMLSettings.ListenPort; ncPort.Value = Settings.ListenPort;
chkAutoListen.Checked = XMLSettings.AutoListen; chkAutoListen.Checked = Settings.AutoListen;
chkPopup.Checked = XMLSettings.ShowPopup; chkPopup.Checked = Settings.ShowPopup;
txtPassword.Text = XMLSettings.Password; txtPassword.Text = Settings.Password;
chkUseUpnp.Checked = XMLSettings.UseUPnP; chkUseUpnp.Checked = Settings.UseUPnP;
chkShowTooltip.Checked = XMLSettings.ShowToolTip; chkShowTooltip.Checked = Settings.ShowToolTip;
chkNoIPIntegration.Checked = XMLSettings.IntegrateNoIP; chkNoIPIntegration.Checked = Settings.EnableNoIPUpdater;
txtNoIPHost.Text = XMLSettings.NoIPHost; txtNoIPHost.Text = Settings.NoIPHost;
txtNoIPUser.Text = XMLSettings.NoIPUsername; txtNoIPUser.Text = Settings.NoIPUsername;
txtNoIPPass.Text = XMLSettings.NoIPPassword; txtNoIPPass.Text = Settings.NoIPPassword;
} }
private ushort GetPortSafe() private ushort GetPortSafe()
@ -121,39 +121,19 @@ namespace xServer.Forms
return; return;
} }
XMLSettings.WriteValue("ListenPort", port.ToString()); Settings.ListenPort = port;
XMLSettings.ListenPort = port; Settings.AutoListen = chkAutoListen.Checked;
Settings.ShowPopup = chkPopup.Checked;
XMLSettings.WriteValue("AutoListen", chkAutoListen.Checked.ToString());
XMLSettings.AutoListen = chkAutoListen.Checked;
XMLSettings.WriteValue("ShowPopup", chkPopup.Checked.ToString());
XMLSettings.ShowPopup = chkPopup.Checked;
var newPassword = txtPassword.Text; var newPassword = txtPassword.Text;
if (newPassword != XMLSettings.Password) if (newPassword != Settings.Password)
AES.PreHashKey(newPassword); AES.PreHashKey(newPassword);
XMLSettings.WriteValue("Password", newPassword); Settings.Password = newPassword;
XMLSettings.Password = newPassword; Settings.UseUPnP = chkUseUpnp.Checked;
Settings.ShowToolTip = chkShowTooltip.Checked;
XMLSettings.WriteValue("UseUPnP", chkUseUpnp.Checked.ToString()); Settings.EnableNoIPUpdater = chkNoIPIntegration.Checked;
XMLSettings.UseUPnP = chkUseUpnp.Checked; Settings.NoIPHost = txtNoIPHost.Text;
Settings.NoIPUsername = txtNoIPUser.Text;
XMLSettings.WriteValue("ShowToolTip", chkShowTooltip.Checked.ToString()); Settings.NoIPPassword = txtNoIPPass.Text;
XMLSettings.ShowToolTip = chkShowTooltip.Checked;
XMLSettings.WriteValue("EnableNoIPUpdater", chkNoIPIntegration.Checked.ToString());
XMLSettings.IntegrateNoIP = chkNoIPIntegration.Checked;
XMLSettings.WriteValue("NoIPHost", txtNoIPHost.Text);
XMLSettings.NoIPHost = txtNoIPHost.Text;
XMLSettings.WriteValue("NoIPUsername", txtNoIPUser.Text);
XMLSettings.NoIPUsername = txtNoIPUser.Text;
XMLSettings.WriteValue("NoIPPassword", txtNoIPPass.Text);
XMLSettings.NoIPPassword = txtNoIPPass.Text;
this.Close(); this.Close();
} }

View File

@ -31,8 +31,7 @@ namespace xServer.Forms
private void btnAccept_Click(object sender, EventArgs e) private void btnAccept_Click(object sender, EventArgs e)
{ {
var showToU = !chkDontShowAgain.Checked; var showToU = !chkDontShowAgain.Checked;
XMLSettings.WriteValue("ShowToU", showToU.ToString()); Settings.ShowToU = showToU;
XMLSettings.ShowToU = showToU;
_exit = false; _exit = false;
this.Close(); this.Close();
} }

View File

@ -33,4 +33,4 @@ using xServer.Core.Data;
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.0.0.0")] [assembly: AssemblyVersion("2.0.0.0")]
[assembly: AssemblyFileVersion("2.0.0.0")] [assembly: AssemblyFileVersion("2.0.0.0")]
[assembly: AssemblyInformationalVersion("2.0.0.0 " + XMLSettings.VERSION)] [assembly: AssemblyInformationalVersion("2.0.0.0 " + Settings.VERSION)]