Skip to content

Commit

Permalink
Add support for setting a custom API to use for fetching the users IP
Browse files Browse the repository at this point in the history
  • Loading branch information
rbclark committed Dec 22, 2016
1 parent 89cd9c2 commit 3373d18
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 20 deletions.
6 changes: 6 additions & 0 deletions SpeedTest/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@
<setting name="SpeedUnits" serializeAs="String">
<value>False</value>
</setting>
<setting name="AutoIPDetect" serializeAs="String">
<value>True</value>
</setting>
<setting name="IPDetectURL" serializeAs="String">
<value />
</setting>
</SpeedTest.Properties.Settings>
</userSettings>
</configuration>
21 changes: 18 additions & 3 deletions SpeedTest/FormMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.IO;
using System.Net;
using System.Net.NetworkInformation;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using SpeedTest.Properties;
using Microsoft.Win32;
Expand Down Expand Up @@ -532,11 +533,16 @@ private void webClient_DownloadStringCompleted(object sender, DownloadStringComp
string status = "-";
StatusCode newStatus = StatusCode.Online;
int imageIndex = 0;
// Regex to match IP address
Regex regex = new Regex(@"(?:[0-9]{1,3}\.){3}[0-9]{1,3}");

if (e.Error == null) // String downloaded successfully
if (e.Error == null && regex.IsMatch(e.Result)) // String downloaded successfully and contains IP address
{
// Get the first instance of an IP in the result using regex
Match match = regex.Match(e.Result);

// Remove line breaks from the IP address
ip = e.Result.Replace("\r", "").Replace("\n", "");
ip = match.Value;

StatusMain.Text = "Testing speed at: " + Global.ConvertTime(DateTime.Now);

Expand Down Expand Up @@ -649,12 +655,21 @@ public void GetRemoteIP()

if (Global.IsOnline)
{
string api = string.Empty;
if (Settings.Default.AutoIPDetect)
{
api = "http://icanhazip.com";
}
else
{
api = Settings.Default.IPDetectURL;
}
// Create a new WebClient object and download the string
CustomWebClient webClient = new CustomWebClient();
webClient.DownloadDataCompleted += webClient_DownloadDataCompleted;
webClient.DownloadProgressChanged += webClient_DownloadProgressChanged;
webClient.DownloadStringCompleted += webClient_DownloadStringCompleted;
webClient.DownloadStringAsync(new Uri("http://icanhazip.com/"));
webClient.DownloadStringAsync(new Uri(api));
}
else
{
Expand Down
92 changes: 77 additions & 15 deletions SpeedTest/FormSettings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions SpeedTest/FormSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public FormSettings()
RadioCustomDownload.Checked = !Settings.Default.AutoDownload;
TextDownloadURL.Text = Settings.Default.DownloadURL;

RadioAutoDetectIP.Checked = Settings.Default.AutoIPDetect;
RadioCustomIPDetect.Checked = !Settings.Default.AutoIPDetect;
TextIPDetectURL.Text = Settings.Default.IPDetectURL;

RadioISOFormat.Checked = Settings.Default.TimeFormat == 0;
RadioEUFormat.Checked = Settings.Default.TimeFormat == 1;
RadioUSFormat.Checked = Settings.Default.TimeFormat == 2;
Expand All @@ -38,6 +42,7 @@ public FormSettings()
CheckAutoStart.Checked = Settings.Default.AutoStart;
CheckMinimize.Checked = Settings.Default.Minimize;

TextIPDetectURL.Enabled = !RadioAutoDetectIP.Checked;
TextDownloadURL.Enabled = !RadioAutoDownload.Checked;
}
#endregion
Expand All @@ -49,6 +54,9 @@ protected override void OnFormClosing(FormClosingEventArgs e)
Settings.Default.AutoDownload = RadioAutoDownload.Checked;
Settings.Default.DownloadURL = TextDownloadURL.Text;

Settings.Default.AutoIPDetect = RadioAutoDetectIP.Checked;
Settings.Default.IPDetectURL = TextIPDetectURL.Text;

if (RadioISOFormat.Checked) Settings.Default.TimeFormat = 0;
else if (RadioEUFormat.Checked) Settings.Default.TimeFormat = 1;
else if (RadioUSFormat.Checked) Settings.Default.TimeFormat = 2;
Expand All @@ -70,6 +78,12 @@ private void RadioAutoDownload_CheckedChanged(object sender, EventArgs e)
TextDownloadURL.Enabled = !RadioAutoDownload.Checked;
}

/// <summary>Occurs when the IP API radio button Checked property changes.</summary>
private void RadioAutoDetectIP_CheckedChanged(object sender, EventArgs e)
{
TextIPDetectURL.Enabled = !RadioAutoDetectIP.Checked;
}

/// <summary>Occurs when the Log File button is clicked.</summary>
private void ButtonLogFile_Click(object sender, EventArgs e)
{
Expand Down
1 change: 1 addition & 0 deletions SpeedTest/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ static void Main(string[] args)

// Initialize settings
if (string.IsNullOrEmpty(Settings.Default.DownloadURL)) Settings.Default.DownloadURL = "http://loggger.com/bigfile.zip";
if (string.IsNullOrEmpty(Settings.Default.IPDetectURL)) Settings.Default.IPDetectURL = "http://icanhazip.com";
if (string.IsNullOrEmpty(Settings.Default.LogFile)) Settings.Default.LogFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + Path.DirectorySeparatorChar + "SpeedTest" + Path.DirectorySeparatorChar + "SpeedTestLog.csv";
if (Settings.Default.LogFile.ToLower().StartsWith(Application.StartupPath.ToLower())) Settings.Default.LogFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + Path.DirectorySeparatorChar + "SpeedTest" + Path.DirectorySeparatorChar + "ConnectionLog.csv";

Expand Down
Loading

0 comments on commit 3373d18

Please sign in to comment.