Skip to content

Commit 0db7d37

Browse files
authored
Merge pull request #23 from zuckerthoben/master
Fixed unresponsive Programstart (async support)
2 parents c3d0166 + 70391fd commit 0db7d37

File tree

2 files changed

+29
-41
lines changed

2 files changed

+29
-41
lines changed
Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,41 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
42
using System.Net;
5-
using System.Text;
3+
using System.Net.Http;
64
using System.Threading.Tasks;
75

86
namespace CodeSwine_Solo_Public_Lobby.Helpers
97
{
108
public class IPTool
119
{
12-
private string _ipAddress;
10+
private HttpClient httpClient;
1311

14-
public string IpAddress
15-
{
16-
get
17-
{
18-
if (_ipAddress == null) _ipAddress = GrabInternetAddress();
19-
return _ipAddress;
20-
}
12+
public IPTool()
13+
{
14+
this.httpClient = new HttpClient();
2115
}
2216

2317
/// <summary>
2418
/// Gets the hosts IP Address.
2519
/// </summary>
2620
/// <returns>String value of IP.</returns>
27-
private string GrabInternetAddress()
21+
public async Task<string> GrabInternetAddressAsync()
2822
{
2923
// Still needs check to see if we could retrieve the IP.
3024
// Try for ipv6 first, but if that fails get ipv4
31-
string ip = "";
25+
string ip;
3226
try
3327
{
34-
ip = new WebClient().DownloadString("https://ipv6.icanhazip.com");
28+
var response = await this.httpClient.GetAsync(new Uri("https://ipv6.icanhazip.com"));
29+
ip = await response.Content.ReadAsStringAsync();
3530
}
3631
catch (Exception e)
3732
{
3833
ErrorLogger.LogException(e);
3934

4035
try
4136
{
42-
ip = new WebClient().DownloadString("https://ipv4.icanhazip.com");
37+
var response = await this.httpClient.GetAsync(new Uri("https://ipv4.icanhazip.com"));
38+
ip = await response.Content.ReadAsStringAsync();
4339
}
4440
catch (Exception e2)
4541
{
@@ -52,7 +48,7 @@ private string GrabInternetAddress()
5248

5349
public static bool ValidateIP(string ipString)
5450
{
55-
if (String.IsNullOrWhiteSpace(ipString))
51+
if (string.IsNullOrWhiteSpace(ipString))
5652
{
5753
return false;
5854
}
@@ -63,8 +59,9 @@ public static bool ValidateIP(string ipString)
6359
switch (address.AddressFamily)
6460
{
6561
case System.Net.Sockets.AddressFamily.InterNetwork:
66-
// we have IPv4
62+
// we have IPv4
6763
return true;
64+
6865
case System.Net.Sockets.AddressFamily.InterNetworkV6:
6966
// we have IPv6
7067
return true;
@@ -73,4 +70,4 @@ public static bool ValidateIP(string ipString)
7370
return false;
7471
}
7572
}
76-
}
73+
}

CodeSwine-Solo_Public_Lobby/CodeSwine-Solo_Public_Lobby/MainWindow.xaml.cs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,16 @@
66
using System.Linq;
77
using System.Net;
88
using System.Runtime.InteropServices;
9-
using System.Text;
109
using System.Threading.Tasks;
1110
using System.Windows;
12-
using System.Windows.Controls;
13-
using System.Windows.Data;
14-
using System.Windows.Documents;
15-
using System.Windows.Input;
1611
using System.Windows.Interop;
17-
using System.Windows.Media;
1812
using System.Windows.Media.Imaging;
19-
using System.Windows.Navigation;
20-
using System.Windows.Shapes;
2113

2214
namespace CodeSwine_Solo_Public_Lobby
2315
{
2416
public partial class MainWindow : Window
2517
{
2618
private IPTool iPTool = new IPTool();
27-
private DaWhitelist whiteList = new DaWhitelist();
2819
private List<IPAddress> addresses = new List<IPAddress>();
2920
private MWhitelist mWhitelist = new MWhitelist();
3021

@@ -37,15 +28,16 @@ public MainWindow()
3728
Loaded += MainWindow_Loaded;
3829
}
3930

40-
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
31+
private async void MainWindow_Loaded(object sender, RoutedEventArgs e)
4132
{
4233
FirewallRule.lblAdmin = lblAdmin;
43-
Init();
34+
await InitAsync();
4435
}
4536

46-
void Init()
37+
private async Task InitAsync()
4738
{
48-
lblYourIPAddress.Content += " " + iPTool.IpAddress + ".";
39+
var ipAddress = await iPTool.GrabInternetAddressAsync();
40+
lblYourIPAddress.Content += " " + ipAddress + ".";
4941
addresses = DaWhitelist.ReadIPsFromJSON();
5042
lsbAddresses.ItemsSource = addresses;
5143
foreach (IPAddress ip in addresses)
@@ -57,9 +49,9 @@ void Init()
5749

5850
private void btnAdd_Click(object sender, RoutedEventArgs e)
5951
{
60-
if(IPTool.ValidateIP(txbIpToAdd.Text))
52+
if (IPTool.ValidateIP(txbIpToAdd.Text))
6153
{
62-
if(!addresses.Contains(IPAddress.Parse(txbIpToAdd.Text)))
54+
if (!addresses.Contains(IPAddress.Parse(txbIpToAdd.Text)))
6355
{
6456
addresses.Add(IPAddress.Parse(txbIpToAdd.Text));
6557
lsbAddresses.Items.Refresh();
@@ -75,7 +67,7 @@ private void btnAdd_Click(object sender, RoutedEventArgs e)
7567

7668
private void btnDelete_Click(object sender, RoutedEventArgs e)
7769
{
78-
if(lsbAddresses.SelectedIndex != -1)
70+
if (lsbAddresses.SelectedIndex != -1)
7971
{
8072
mWhitelist.Ips.Remove(lsbAddresses.SelectedItem.ToString());
8173
addresses.Remove(IPAddress.Parse(lsbAddresses.SelectedItem.ToString()));
@@ -98,7 +90,7 @@ private void btnEnableDisable_Click(object sender, RoutedEventArgs e)
9890
SetRules();
9991
}
10092

101-
void SetRules()
93+
private void SetRules()
10294
{
10395
string remoteAddresses = RangeCalculator.GetRemoteAddresses(addresses);
10496

@@ -124,7 +116,7 @@ void SetRules()
124116
}
125117

126118
// If they are active and set.
127-
if(active && set)
119+
if (active && set)
128120
{
129121
FirewallRule.CreateInbound(remoteAddresses, false, true);
130122
FirewallRule.CreateOutbound(remoteAddresses, false, true);
@@ -133,22 +125,22 @@ void SetRules()
133125
}
134126
}
135127

136-
void UpdateNotActive()
128+
private void UpdateNotActive()
137129
{
138130
btnEnableDisable.Background = ColorBrush.Red;
139131
image4.Source = new BitmapImage(new Uri("/CodeSwine-Solo_Public_Lobby;component/ImageResources/unlocked.png", UriKind.Relative));
140132
lblLock.Content = "Rules not active." + Environment.NewLine + "Click to activate!";
141133
}
142134

143-
void UpdateActive()
135+
private void UpdateActive()
144136
{
145137
btnEnableDisable.Background = ColorBrush.Green;
146138
image4.Source = new BitmapImage(new Uri("/CodeSwine-Solo_Public_Lobby;component/ImageResources/locked.png", UriKind.Relative));
147139
lblLock.Content = "Rules active." + Environment.NewLine + "Click to deactivate!";
148140
}
149141

150142
[DllImport("User32.dll")]
151-
private static extern bool RegisterHotKey(
143+
private static extern bool RegisterHotKey(
152144
[In] IntPtr hWnd,
153145
[In] int id,
154146
[In] uint fsModifiers,
@@ -187,7 +179,6 @@ private void RegisterHotKey()
187179
const uint MOD_CTRL = 0x0002;
188180
if (!RegisterHotKey(helper.Handle, HOTKEY_ID, MOD_CTRL, VK_F10))
189181
{
190-
191182
}
192183
}
193184

@@ -221,4 +212,4 @@ private void OnHotKeyPressed()
221212
System.Media.SystemSounds.Hand.Play();
222213
}
223214
}
224-
}
215+
}

0 commit comments

Comments
 (0)