Improved NoIP.org Integration

This commit is contained in:
MaxXor 2015-05-10 18:11:22 +02:00
parent 4f8bd1b431
commit 11032266c9
7 changed files with 80 additions and 71 deletions

View File

@ -582,9 +582,6 @@ public static void HandleVisitWebsite(Packets.ServerPackets.VisitWebsite command
using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
{
using (Stream dataStream = response.GetResponseStream())
{
}
}
}
catch

View File

@ -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;
}
}
}

View File

@ -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.

View File

@ -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
//

View File

@ -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)

View File

@ -74,6 +74,7 @@
<Compile Include="Core\Compression\JpgCompression.cs" />
<Compile Include="Core\Misc\ListViewColumnSorter.cs" />
<Compile Include="Core\Extensions\ListViewExtensions.cs" />
<Compile Include="Core\Misc\NoIpUpdater.cs" />
<Compile Include="Core\Misc\SavedVariables.cs" />
<Compile Include="Core\Packets\ClientPackets\DesktopResponse.cs" />
<Compile Include="Core\Packets\ClientPackets\DirectoryResponse.cs" />

View File

@ -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)