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);
if (XMLSettings.ShowPopup)
if (Settings.ShowPopup)
FrmMain.Instance.ShowPopup(client);
client.Value.IsAuthenticated = true;
if (XMLSettings.ShowToolTip)
if (Settings.ShowToolTip)
new Packets.ServerPackets.GetSystemInfo().Execute(client);
}
catch

View File

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

View File

@ -5,45 +5,161 @@ using System.Xml.XPath;
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 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 void ReadSettings()
public static ushort ListenPort
{
ListenPort = ushort.Parse(ReadValueSafe("ListenPort", "4782"));
ShowToU = bool.Parse(ReadValueSafe("ShowToU", "True"));
AutoListen = bool.Parse(ReadValueSafe("AutoListen", "False"));
ShowPopup = bool.Parse(ReadValueSafe("ShowPopup", "False"));
UseUPnP = bool.Parse(ReadValueSafe("UseUPnP", "False"));
SaveFormat = ReadValueSafe("SaveFormat", "APP - URL - USER:PASS");
ShowToolTip = bool.Parse(ReadValueSafe("ShowToolTip", "False"));
IntegrateNoIP = bool.Parse(ReadValueSafe("EnableNoIPUpdater", "False"));
NoIPHost = ReadValueSafe("NoIPHost");
NoIPUsername = ReadValueSafe("NoIPUsername");
NoIPPassword = ReadValueSafe("NoIPPassword");
Password = ReadValueSafe("Password", "1234");
get
{
return ushort.Parse(ReadValueSafe("ListenPort", "4782"));
}
set
{
WriteValue("ListenPort", value.ToString());
}
}
public static bool ShowToU
{
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)
{
try
{
XPathDocument doc = new XPathDocument(_settingsPath);
XPathDocument doc = new XPathDocument(SettingsPath);
XPathNavigator nav = doc.CreateNavigator();
XPathExpression expr = nav.Compile(@"/settings/" + pstrValueToRead);
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);
return (!string.IsNullOrEmpty(value)) ? value: defaultValue;
}
public static bool WriteValue(string pstrValueToRead, string pstrValueToWrite)
private static void WriteValue(string pstrValueToRead, string pstrValueToWrite)
{
try
{
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);
}
}
else
{
var dir = Path.GetDirectoryName(_settingsPath);
var dir = Path.GetDirectoryName(SettingsPath);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
@ -95,16 +211,14 @@ namespace xServer.Core.Data
{
oldNode = doc.SelectSingleNode("settings");
oldNode.AppendChild(doc.CreateElement(pstrValueToRead)).InnerText = pstrValueToWrite;
doc.Save(_settingsPath);
return true;
doc.Save(SettingsPath);
return;
}
oldNode.InnerText = pstrValueToWrite;
doc.Save(_settingsPath);
return true;
doc.Save(SettingsPath);
}
catch
{
return false;
}
}
}

View File

@ -20,15 +20,15 @@ namespace xServer.Core.Utilities
private static void BackgroundUpdater()
{
_running = true;
while (XMLSettings.IntegrateNoIP)
while (Settings.EnableNoIPUpdater)
{
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.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.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";
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())

View File

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

View File

@ -30,7 +30,7 @@ namespace xServer.Forms
_hosts.Add(host);
lstHosts.DataSource = new BindingSource(_hosts, null);
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");
txtMutex.Text = profile.ReadValueSafe("Mutex", FormatHelper.GenerateMutex());
chkInstall.Checked = bool.Parse(profile.ReadValueSafe("InstallClient", "False"));
@ -86,7 +86,7 @@ namespace xServer.Forms
{
LoadProfile("Default");
txtPort.Text = XMLSettings.ListenPort.ToString();
txtPort.Text = Settings.ListenPort.ToString();
UpdateControlStates();

View File

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

View File

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

View File

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

View File

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

View File

@ -33,4 +33,4 @@ using xServer.Core.Data;
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("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)]