From 11032266c9cb3b1c538b6a4a5fe63fdc385a3b31 Mon Sep 17 00:00:00 2001 From: MaxXor Date: Sun, 10 May 2015 18:11:22 +0200 Subject: [PATCH] Improved NoIP.org Integration --- Client/Core/Commands/CommandHandler.cs | 3 -- Server/Core/Misc/NoIpUpdater.cs | 56 +++++++++++++++++++++++++ Server/Forms/FrmMain.cs | 58 +++----------------------- Server/Forms/FrmSettings.Designer.cs | 20 ++++----- Server/Forms/FrmSettings.cs | 5 ++- Server/Server.csproj | 1 + Server/Settings/Settings.cs | 8 ++-- 7 files changed, 80 insertions(+), 71 deletions(-) create mode 100644 Server/Core/Misc/NoIpUpdater.cs diff --git a/Client/Core/Commands/CommandHandler.cs b/Client/Core/Commands/CommandHandler.cs index 4b09987b..9415020d 100644 --- a/Client/Core/Commands/CommandHandler.cs +++ b/Client/Core/Commands/CommandHandler.cs @@ -582,9 +582,6 @@ public static void HandleVisitWebsite(Packets.ServerPackets.VisitWebsite command using (HttpWebResponse response = (HttpWebResponse) request.GetResponse()) { - using (Stream dataStream = response.GetResponseStream()) - { - } } } catch diff --git a/Server/Core/Misc/NoIpUpdater.cs b/Server/Core/Misc/NoIpUpdater.cs new file mode 100644 index 00000000..991b7837 --- /dev/null +++ b/Server/Core/Misc/NoIpUpdater.cs @@ -0,0 +1,56 @@ +using System; +using System.Net; +using System.Text; +using System.Threading; +using xServer.Settings; + +namespace xServer.Core.Misc +{ + public static class NoIpUpdater + { + private static bool _running; + + public static void Start() + { + if (_running) return; + Thread updateThread = new Thread(BackgroundUpdater) {IsBackground = true}; + updateThread.Start(); + } + + private static void BackgroundUpdater() + { + _running = true; + while (XMLSettings.IntegrateNoIP) + { + try + { + string wanIp = string.Empty; + using (WebClient wc = new WebClient()) + { + wanIp = wc.DownloadString("http://icanhazip.com/"); + } + + if (!string.IsNullOrEmpty(wanIp)) + { + HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(string.Format("http://dynupdate.no-ip.com/nic/update?hostname={0}&myip={1}", XMLSettings.NoIPHost, wanIp)); + request.UserAgent = string.Format("X IP Automation Tool/3 {0}", XMLSettings.NoIPUsername); + request.Timeout = 20000; + request.Headers.Add(HttpRequestHeader.Authorization, string.Format("Basic {0}", Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", XMLSettings.NoIPUsername, XMLSettings.NoIPPassword))))); + request.Method = "GET"; + + using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) + { + } + } + + } + catch + { + } + + Thread.Sleep(TimeSpan.FromMinutes(10)); + } + _running = false; + } + } +} diff --git a/Server/Forms/FrmMain.cs b/Server/Forms/FrmMain.cs index c08bec1d..8b5bea34 100644 --- a/Server/Forms/FrmMain.cs +++ b/Server/Forms/FrmMain.cs @@ -1,8 +1,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Net; -using System.Text; using System.Threading; using System.Windows.Forms; using xServer.Core; @@ -31,15 +29,12 @@ private void ReadSettings(bool writeIfNotExist = true) XMLSettings.AutoListen = bool.Parse(XMLSettings.ReadValue("AutoListen")); XMLSettings.ShowPopup = bool.Parse(XMLSettings.ReadValue("ShowPopup")); XMLSettings.UseUPnP = bool.Parse(XMLSettings.ReadValue("UseUPnP")); - XMLSettings.ShowToolTip = - bool.Parse(!string.IsNullOrEmpty(XMLSettings.ReadValue("ShowToolTip")) - ? XMLSettings.ReadValue("ShowToolTip") - : "False"); //fallback - XMLSettings.IntegrateNoIP = bool.Parse(XMLSettings.ReadOrDefault("IntegrateNoIP","False")); - XMLSettings.NoIPHost = XMLSettings.ReadOrDefault("NoIPHost"); - XMLSettings.NoIPUsername = XMLSettings.ReadOrDefault("NoIPUsername"); - XMLSettings.NoIPPassword =XMLSettings.ReadOrDefault("NoIPPassword"); + XMLSettings.ShowToolTip = bool.Parse(XMLSettings.ReadValueSafe("ShowToolTip", "False")); + XMLSettings.IntegrateNoIP = bool.Parse(XMLSettings.ReadValueSafe("EnableNoIPUpdater", "False")); + XMLSettings.NoIPHost = XMLSettings.ReadValueSafe("NoIPHost"); + XMLSettings.NoIPUsername = XMLSettings.ReadValueSafe("NoIPUsername"); + XMLSettings.NoIPPassword = XMLSettings.ReadValueSafe("NoIPPassword"); XMLSettings.Password = XMLSettings.ReadValue("Password"); } @@ -169,7 +164,7 @@ private void FrmMain_Load(object sender, EventArgs e) if (XMLSettings.IntegrateNoIP) { - StartNoIpItegrator(); + NoIpUpdater.Start(); } } @@ -296,47 +291,6 @@ private void ClientRead(Server server, Client client, IPacket packet) } } - public void StartNoIpItegrator() - { - Thread updateThread=new Thread(NoIpUdater); - updateThread.IsBackground = true; - updateThread.Start(); - } - - public void NoIpUdater() - { - while (XMLSettings.IntegrateNoIP) - { - try - { - string myIp = ""; - using (WebClient wc = new WebClient()) - { - myIp = wc.DownloadString("http://icanhazip.com/"); - } - - if (!string.IsNullOrEmpty(myIp)) - { - string link = string.Format("http://dynupdate.no-ip.com/nic/update?hostname={0}&myip={1}", XMLSettings.NoIPHost, myIp); - HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(link); - request.Method = "GET"; - request.UserAgent = string.Format("X IP Automation Tool/3 {0}", XMLSettings.NoIPUsername); - request.Headers.Add(HttpRequestHeader.Authorization, string.Format("Basic {0}", Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", XMLSettings.NoIPUsername, XMLSettings.NoIPPassword))))); - - HttpWebResponse response = (HttpWebResponse)request.GetResponse(); - response.Close(); - } - - } - catch (Exception) - { - //Ignore - } - - Thread.Sleep(TimeSpan.FromMinutes(5)); - } - } - private void lstClients_ColumnClick(object sender, ColumnClickEventArgs e) { // Determine if clicked column is already the column that is being sorted. diff --git a/Server/Forms/FrmSettings.Designer.cs b/Server/Forms/FrmSettings.Designer.cs index 0a6b519c..117b82c2 100644 --- a/Server/Forms/FrmSettings.Designer.cs +++ b/Server/Forms/FrmSettings.Designer.cs @@ -55,7 +55,7 @@ private void InitializeComponent() this.btnSave.Location = new System.Drawing.Point(227, 253); this.btnSave.Name = "btnSave"; this.btnSave.Size = new System.Drawing.Size(75, 23); - this.btnSave.TabIndex = 10; + this.btnSave.TabIndex = 17; this.btnSave.Text = "&Save"; this.btnSave.UseVisualStyleBackColor = true; this.btnSave.Click += new System.EventHandler(this.btnSave_Click); @@ -126,7 +126,7 @@ private void InitializeComponent() this.btnCancel.Location = new System.Drawing.Point(146, 253); this.btnCancel.Name = "btnCancel"; this.btnCancel.Size = new System.Drawing.Size(75, 23); - this.btnCancel.TabIndex = 9; + this.btnCancel.TabIndex = 16; this.btnCancel.Text = "&Cancel"; this.btnCancel.UseVisualStyleBackColor = true; this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click); @@ -172,9 +172,9 @@ private void InitializeComponent() this.chkNoIPIntegration.AutoSize = true; this.chkNoIPIntegration.Location = new System.Drawing.Point(15, 155); this.chkNoIPIntegration.Name = "chkNoIPIntegration"; - this.chkNoIPIntegration.Size = new System.Drawing.Size(191, 17); - this.chkNoIPIntegration.TabIndex = 11; - this.chkNoIPIntegration.Text = "Integrate NoIP.org DNS Updater"; + this.chkNoIPIntegration.Size = new System.Drawing.Size(184, 17); + this.chkNoIPIntegration.TabIndex = 9; + this.chkNoIPIntegration.Text = "Activate NoIP.org DNS Updater"; this.chkNoIPIntegration.UseVisualStyleBackColor = true; this.chkNoIPIntegration.CheckedChanged += new System.EventHandler(this.chkNoIPIntegration_CheckedChanged); // @@ -185,7 +185,7 @@ private void InitializeComponent() this.lblHost.Location = new System.Drawing.Point(33, 181); this.lblHost.Name = "lblHost"; this.lblHost.Size = new System.Drawing.Size(34, 13); - this.lblHost.TabIndex = 12; + this.lblHost.TabIndex = 10; this.lblHost.Text = "Host:"; // // lblPass @@ -195,7 +195,7 @@ private void InitializeComponent() this.lblPass.Location = new System.Drawing.Point(170, 209); this.lblPass.Name = "lblPass"; this.lblPass.Size = new System.Drawing.Size(32, 13); - this.lblPass.TabIndex = 13; + this.lblPass.TabIndex = 14; this.lblPass.Text = "Pass:"; // // lblUser @@ -205,7 +205,7 @@ private void InitializeComponent() this.lblUser.Location = new System.Drawing.Point(33, 209); this.lblUser.Name = "lblUser"; this.lblUser.Size = new System.Drawing.Size(33, 13); - this.lblUser.TabIndex = 14; + this.lblUser.TabIndex = 12; this.lblUser.Text = "User:"; // // txtNoIPPass @@ -222,7 +222,7 @@ private void InitializeComponent() this.txtNoIPUser.Location = new System.Drawing.Point(73, 206); this.txtNoIPUser.Name = "txtNoIPUser"; this.txtNoIPUser.Size = new System.Drawing.Size(91, 22); - this.txtNoIPUser.TabIndex = 16; + this.txtNoIPUser.TabIndex = 13; // // txtNoIPHost // @@ -230,7 +230,7 @@ private void InitializeComponent() this.txtNoIPHost.Location = new System.Drawing.Point(73, 178); this.txtNoIPHost.Name = "txtNoIPHost"; this.txtNoIPHost.Size = new System.Drawing.Size(229, 22); - this.txtNoIPHost.TabIndex = 17; + this.txtNoIPHost.TabIndex = 11; // // FrmSettings // diff --git a/Server/Forms/FrmSettings.cs b/Server/Forms/FrmSettings.cs index 996fd5ed..606ed42e 100644 --- a/Server/Forms/FrmSettings.cs +++ b/Server/Forms/FrmSettings.cs @@ -2,6 +2,7 @@ using System.Globalization; using System.Windows.Forms; using xServer.Core; +using xServer.Core.Misc; using xServer.Settings; namespace xServer.Forms @@ -47,7 +48,7 @@ private void btnListen_Click(object sender, EventArgs e) if (chkUseUpnp.Checked) Core.Helper.UPnP.ForwardPort(ushort.Parse(ncPort.Value.ToString(CultureInfo.InvariantCulture))); if(chkNoIPIntegration.Checked) - FrmMain.Instance.StartNoIpItegrator(); + NoIpUpdater.Start(); _listenServer.Listen(ushort.Parse(ncPort.Value.ToString(CultureInfo.InvariantCulture))); } finally @@ -92,7 +93,7 @@ private void btnSave_Click(object sender, EventArgs e) XMLSettings.WriteValue("ShowToolTip", chkShowTooltip.Checked.ToString()); XMLSettings.ShowToolTip = chkShowTooltip.Checked; - XMLSettings.WriteValue("IntegrateNoIP", chkNoIPIntegration.Checked.ToString()); + XMLSettings.WriteValue("EnableNoIPUpdater", chkNoIPIntegration.Checked.ToString()); XMLSettings.IntegrateNoIP = chkNoIPIntegration.Checked; if (chkNoIPIntegration.Checked) diff --git a/Server/Server.csproj b/Server/Server.csproj index b0967cf1..edf98d48 100644 --- a/Server/Server.csproj +++ b/Server/Server.csproj @@ -74,6 +74,7 @@ + diff --git a/Server/Settings/Settings.cs b/Server/Settings/Settings.cs index c7c02b0e..19baf23a 100644 --- a/Server/Settings/Settings.cs +++ b/Server/Settings/Settings.cs @@ -41,7 +41,7 @@ public static bool WriteDefaultSettings() root.AppendChild(doc.CreateElement("UseUPnP")).InnerText = "False"; root.AppendChild(doc.CreateElement("ShowToolTip")).InnerText = "False"; - root.AppendChild(doc.CreateElement("IntegrateNoIP")).InnerText = "False"; + root.AppendChild(doc.CreateElement("EnableNoIPUpdater")).InnerText = "False"; root.AppendChild(doc.CreateElement("NoIPHost")).InnerText = ""; root.AppendChild(doc.CreateElement("NoIPUsername")).InnerText = ""; root.AppendChild(doc.CreateElement("NoIPPassword")).InnerText = ""; @@ -77,10 +77,10 @@ public static string ReadValue(string pstrValueToRead) } } - public static string ReadOrDefault(string nodeName, string defaultValue="") + public static string ReadValueSafe(string pstrValueToRead, string defaultValue = "") { - string value = ReadValue(nodeName); - return !string.IsNullOrEmpty(value)? value: defaultValue; + string value = ReadValue(pstrValueToRead); + return (!string.IsNullOrEmpty(value)) ? value: defaultValue; } public static bool WriteValue(string pstrValueToRead, string pstrValueToWrite)