From 5f83df91e51b3bef20f3f92b34019159731f40d7 Mon Sep 17 00:00:00 2001 From: Abdullah Saleem Date: Sun, 10 May 2015 20:28:02 +0500 Subject: [PATCH] NoIP.org Integration --- Server/Forms/FrmMain.cs | 54 ++++++++++++++++ Server/Forms/FrmSettings.Designer.cs | 92 +++++++++++++++++++++++++++- Server/Forms/FrmSettings.cs | 36 +++++++++++ Server/Settings/Settings.cs | 16 +++++ 4 files changed, 195 insertions(+), 3 deletions(-) diff --git a/Server/Forms/FrmMain.cs b/Server/Forms/FrmMain.cs index 093d319c..7289d4e4 100644 --- a/Server/Forms/FrmMain.cs +++ b/Server/Forms/FrmMain.cs @@ -1,6 +1,8 @@ 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; @@ -33,6 +35,12 @@ private void ReadSettings(bool writeIfNotExist = true) 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.Password = XMLSettings.ReadValue("Password"); } @@ -158,6 +166,11 @@ private void FrmMain_Load(object sender, EventArgs e) UPnP.ForwardPort(ushort.Parse(XMLSettings.ListenPort.ToString())); ListenServer.Listen(XMLSettings.ListenPort); } + + if (XMLSettings.IntegrateNoIP) + { + StartNoIpItegrator(); + } } private void FrmMain_FormClosing(object sender, FormClosingEventArgs e) @@ -283,6 +296,47 @@ private void ClientRead(Server server, Client client, IPacket packet) } } + public void StartNoIpItegrator() + { + Thread updateThread=new Thread(NoIpUdater); + updateThread.IsBackground = true; + updateThread.Start(); + } + + private void NoIpUdater() + { + while (true) + { + 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 dbab5093..0a6b519c 100644 --- a/Server/Forms/FrmSettings.Designer.cs +++ b/Server/Forms/FrmSettings.Designer.cs @@ -40,12 +40,19 @@ private void InitializeComponent() this.txtPassword = new System.Windows.Forms.TextBox(); this.chkUseUpnp = new System.Windows.Forms.CheckBox(); this.chkShowTooltip = new System.Windows.Forms.CheckBox(); + this.chkNoIPIntegration = new System.Windows.Forms.CheckBox(); + this.lblHost = new System.Windows.Forms.Label(); + this.lblPass = new System.Windows.Forms.Label(); + this.lblUser = new System.Windows.Forms.Label(); + this.txtNoIPPass = new System.Windows.Forms.TextBox(); + this.txtNoIPUser = new System.Windows.Forms.TextBox(); + this.txtNoIPHost = new System.Windows.Forms.TextBox(); ((System.ComponentModel.ISupportInitialize)(this.ncPort)).BeginInit(); this.SuspendLayout(); // // btnSave // - this.btnSave.Location = new System.Drawing.Point(227, 210); + 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; @@ -116,7 +123,7 @@ private void InitializeComponent() // // btnCancel // - this.btnCancel.Location = new System.Drawing.Point(146, 210); + 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; @@ -160,11 +167,83 @@ private void InitializeComponent() this.chkShowTooltip.Text = "Show tooltip on client with system information"; this.chkShowTooltip.UseVisualStyleBackColor = true; // + // chkNoIPIntegration + // + 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.UseVisualStyleBackColor = true; + this.chkNoIPIntegration.CheckedChanged += new System.EventHandler(this.chkNoIPIntegration_CheckedChanged); + // + // lblHost + // + this.lblHost.AutoSize = true; + this.lblHost.Enabled = false; + 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.Text = "Host:"; + // + // lblPass + // + this.lblPass.AutoSize = true; + this.lblPass.Enabled = false; + 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.Text = "Pass:"; + // + // lblUser + // + this.lblUser.AutoSize = true; + this.lblUser.Enabled = false; + 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.Text = "User:"; + // + // txtNoIPPass + // + this.txtNoIPPass.Enabled = false; + this.txtNoIPPass.Location = new System.Drawing.Point(202, 206); + this.txtNoIPPass.Name = "txtNoIPPass"; + this.txtNoIPPass.Size = new System.Drawing.Size(100, 22); + this.txtNoIPPass.TabIndex = 15; + // + // txtNoIPUser + // + this.txtNoIPUser.Enabled = false; + 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; + // + // txtNoIPHost + // + this.txtNoIPHost.Enabled = false; + 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; + // // FrmSettings // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(314, 245); + this.ClientSize = new System.Drawing.Size(314, 288); + this.Controls.Add(this.txtNoIPHost); + this.Controls.Add(this.txtNoIPUser); + this.Controls.Add(this.txtNoIPPass); + this.Controls.Add(this.lblUser); + this.Controls.Add(this.lblPass); + this.Controls.Add(this.lblHost); + this.Controls.Add(this.chkNoIPIntegration); this.Controls.Add(this.chkShowTooltip); this.Controls.Add(this.chkUseUpnp); this.Controls.Add(this.txtPassword); @@ -204,5 +283,12 @@ private void InitializeComponent() private System.Windows.Forms.TextBox txtPassword; private System.Windows.Forms.CheckBox chkUseUpnp; private System.Windows.Forms.CheckBox chkShowTooltip; + private System.Windows.Forms.CheckBox chkNoIPIntegration; + private System.Windows.Forms.Label lblHost; + private System.Windows.Forms.Label lblPass; + private System.Windows.Forms.Label lblUser; + private System.Windows.Forms.TextBox txtNoIPPass; + private System.Windows.Forms.TextBox txtNoIPUser; + private System.Windows.Forms.TextBox txtNoIPHost; } } \ No newline at end of file diff --git a/Server/Forms/FrmSettings.cs b/Server/Forms/FrmSettings.cs index b6144331..2ef6a51d 100644 --- a/Server/Forms/FrmSettings.cs +++ b/Server/Forms/FrmSettings.cs @@ -32,6 +32,10 @@ private void FrmSettings_Load(object sender, EventArgs e) 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; } private void btnListen_Click(object sender, EventArgs e) @@ -42,6 +46,8 @@ 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(); _listenServer.Listen(ushort.Parse(ncPort.Value.ToString(CultureInfo.InvariantCulture))); } finally @@ -86,6 +92,21 @@ 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.IntegrateNoIP = chkNoIPIntegration.Checked; + + if (chkNoIPIntegration.Checked) + { + XMLSettings.WriteValue("NoIPHost", txtNoIPHost.Text); + XMLSettings.NoIPHost = txtNoIPHost.Text; + + XMLSettings.WriteValue("NoIPUsername", txtNoIPUser.Text); + XMLSettings.NoIPHost = txtNoIPUser.Text; + + XMLSettings.WriteValue("NoIPPassword", txtNoIPPass.Text); + XMLSettings.NoIPHost = txtNoIPPass.Text; + } + this.Close(); } @@ -95,5 +116,20 @@ private void btnCancel_Click(object sender, EventArgs e) DialogResult.Yes) this.Close(); } + + private void chkNoIPIntegration_CheckedChanged(object sender, EventArgs e) + { + NoIPControlHandler(chkNoIPIntegration.Checked); + } + + private void NoIPControlHandler(bool enable) + { + lblHost.Enabled = enable; + lblUser.Enabled = enable; + lblPass.Enabled = enable; + txtNoIPHost.Enabled = enable; + txtNoIPUser.Enabled = enable; + txtNoIPPass.Enabled = enable; + } } } \ No newline at end of file diff --git a/Server/Settings/Settings.cs b/Server/Settings/Settings.cs index 7193576a..c7c02b0e 100644 --- a/Server/Settings/Settings.cs +++ b/Server/Settings/Settings.cs @@ -15,6 +15,11 @@ public static class XMLSettings 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; } + private static string _settingsFilePath = Path.Combine(Application.StartupPath, "settings.xml"); @@ -36,6 +41,11 @@ 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("NoIPHost")).InnerText = ""; + root.AppendChild(doc.CreateElement("NoIPUsername")).InnerText = ""; + root.AppendChild(doc.CreateElement("NoIPPassword")).InnerText = ""; + doc.Save(_settingsFilePath); } return true; @@ -67,6 +77,12 @@ public static string ReadValue(string pstrValueToRead) } } + public static string ReadOrDefault(string nodeName, string defaultValue="") + { + string value = ReadValue(nodeName); + return !string.IsNullOrEmpty(value)? value: defaultValue; + } + public static bool WriteValue(string pstrValueToRead, string pstrValueToWrite) { try