Skip to content

Commit 99a09a4

Browse files
committed
Added rules hotkey toggle, cleaned code and enabled windows minimize.
1 parent 0239575 commit 99a09a4

File tree

60 files changed

+337
-95
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+337
-95
lines changed
Binary file not shown.
Binary file not shown.

CodeSwine-Solo_Public_Lobby/CodeSwine-Solo_Public_Lobby/CodeSwine-Private_Public_Lobby.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
</Compile>
9999
<Compile Include="DataAccess\DaWhitelist.cs" />
100100
<Compile Include="Helpers\ColorBrush.cs" />
101+
<Compile Include="Helpers\ErrorLogger.cs" />
101102
<Compile Include="Helpers\FirewallRule.cs" />
102103
<Compile Include="Helpers\IPTool.cs" />
103104
<Compile Include="Helpers\RangeCalculator.cs" />

CodeSwine-Solo_Public_Lobby/CodeSwine-Solo_Public_Lobby/DataAccess/DaWhitelist.cs

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,11 @@ namespace CodeSwine_Solo_Public_Lobby.DataAccess
1010
{
1111
public class DaWhitelist
1212
{
13-
public IPTool iPTool = new IPTool();
14-
private bool useWhitelist;
15-
private string iPCount;
13+
private static string path = AppDomain.CurrentDomain.BaseDirectory + "settings.json";
1614

17-
public DaWhitelist()
18-
{
19-
ReadIPsFromJSON();
20-
}
21-
22-
public List<IPAddress> IpAddressess {
23-
get { return ReadIPsFromJSON(); }
24-
}
25-
26-
public string IPCount {
27-
get { return iPCount; }
28-
}
29-
30-
public bool UseWhitelist {
31-
get { return useWhitelist; }
32-
set { useWhitelist = value; }
33-
}
34-
35-
List<IPAddress> ReadIPsFromJSON()
15+
public static List<IPAddress> ReadIPsFromJSON()
3616
{
3717
List<IPAddress> addresses = new List<IPAddress>();
38-
39-
string path = Environment.ExpandEnvironmentVariables(AppDomain.CurrentDomain.BaseDirectory + "settings.json");
40-
41-
string json = "";
4218

4319
if(!File.Exists(path))
4420
{
@@ -47,24 +23,20 @@ List<IPAddress> ReadIPsFromJSON()
4723

4824
using (StreamReader r = new StreamReader(path))
4925
{
50-
json = r.ReadToEnd();
26+
string json = r.ReadToEnd();
27+
MWhitelist whitelist = JsonConvert.DeserializeObject<MWhitelist>(json);
28+
foreach (string address in whitelist.Ips)
29+
{
30+
if (IPTool.ValidateIPv4(address.ToString())) addresses.Add(IPAddress.Parse(address));
31+
}
5132
}
52-
53-
MWhitelist whitelist = JsonConvert.DeserializeObject<MWhitelist>(json);
54-
55-
foreach (string address in whitelist.Ips)
56-
{
57-
if(iPTool.ValidateIPv4(address.ToString())) addresses.Add(IPAddress.Parse(address));
58-
}
59-
60-
iPCount = addresses.Count.ToString();
6133
return addresses;
6234
}
6335

6436
public static void SaveToJson(MWhitelist whitelist)
6537
{
6638
string json = JsonConvert.SerializeObject(whitelist);
67-
File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + "settings.json", json);
39+
File.WriteAllText(path, json);
6840
}
6941
}
7042
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.IO;
3+
4+
namespace CodeSwine_Solo_Public_Lobby.Helpers
5+
{
6+
public class ErrorLogger
7+
{
8+
public static void LogException(Exception e)
9+
{
10+
string path = AppDomain.CurrentDomain.BaseDirectory + "error.log";
11+
12+
if (!File.Exists(path))
13+
{
14+
using (StreamWriter sw = File.CreateText(path))
15+
{
16+
sw.WriteLine("Error log generated at " + DateTime.Now.ToShortDateString());
17+
}
18+
}
19+
20+
using (StreamWriter sw = File.AppendText(path))
21+
{
22+
sw.WriteLine(e.Message.ToString());
23+
}
24+
}
25+
}
26+
}

CodeSwine-Solo_Public_Lobby/CodeSwine-Solo_Public_Lobby/Helpers/FirewallRule.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ public static void CreateOutbound(string addresses, bool enabled, bool toggle)
3838
firewallPolicy.Rules.Add(firewallRule);
3939
}
4040

41-
} catch(Exception e)
41+
} catch (Exception e)
4242
{
43+
ErrorLogger.LogException(e);
4344
MessageBox.Show("Please start this program as administrator!");
4445
}
4546
}
@@ -84,7 +85,7 @@ public static void CreateInbound(string addresses, bool enabled, bool toggle)
8485
}
8586
catch (Exception e)
8687
{
87-
88+
ErrorLogger.LogException(e);
8889
}
8990
}
9091

@@ -106,9 +107,9 @@ public static void DeleteRules()
106107
firewallPolicy.Rules.Remove(firewallRuleOutbound.Name);
107108
} catch (Exception e)
108109
{
110+
ErrorLogger.LogException(e);
109111
MessageBox.Show("Run this program as administrator!");
110112
}
111-
112113
}
113114
}
114115
}

CodeSwine-Solo_Public_Lobby/CodeSwine-Solo_Public_Lobby/Helpers/IPTool.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,43 @@ namespace CodeSwine_Solo_Public_Lobby.Helpers
99
{
1010
public class IPTool
1111
{
12+
private string _ipAddress;
13+
1214
public string IpAddress
1315
{
1416
get
1517
{
16-
return GrabInternetAddress();
18+
if (_ipAddress == null) _ipAddress = GrabInternetAddress();
19+
return _ipAddress;
1720
}
1821
}
1922

2023
/// <summary>
2124
/// Gets the hosts IP Address.
2225
/// </summary>
2326
/// <returns>String value of IP.</returns>
24-
public string GrabInternetAddress()
27+
private string GrabInternetAddress()
2528
{
26-
// Still needs check to see if we could retrieve the IP.
27-
return new WebClient().DownloadString("http://icanhazip.com");
29+
// Still needs check to see if we could retrieve the IP.
30+
string ip = "";
31+
try
32+
{
33+
ip = new WebClient().DownloadString("http://icanhazip.com");
34+
}
35+
catch (Exception e)
36+
{
37+
ErrorLogger.LogException(e);
38+
ip = "IP not found.";
39+
}
40+
return ip;
2841
}
2942

3043
/// <summary>
3144
/// Validates IP Address.
3245
/// </summary>
3346
/// <param name="ipString"></param>
3447
/// <returns>Bool True or False.</returns>
35-
public bool ValidateIPv4(string ipString)
48+
public static bool ValidateIPv4(string ipString)
3649
{
3750
if (String.IsNullOrWhiteSpace(ipString))
3851
{

CodeSwine-Solo_Public_Lobby/CodeSwine-Solo_Public_Lobby/MainWindow.xaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
66
xmlns:local="clr-namespace:CodeSwine_Solo_Public_Lobby"
77
mc:Ignorable="d"
8-
Title="GTA 5 Online: Private Public Lobby" Height="400" Width="923" ResizeMode="NoResize">
8+
Title="GTA 5 Online: Private Public Lobby" Height="400" Width="923" ResizeMode="CanMinimize">
99

1010
<Grid>
1111
<Grid.ColumnDefinitions>
@@ -94,5 +94,6 @@
9494
<Label x:Name="label2_Copy" Content="forum/member.php?u=4577569" Grid.Column="2" HorizontalAlignment="Left" Margin="55,58,0,0" Grid.Row="2" VerticalAlignment="Top" Width="226" Foreground="#FF707C80"/>
9595
<Image x:Name="image6_Copy" Grid.Column="2" HorizontalAlignment="Left" Height="42" Margin="14,8,0,0" Grid.Row="2" VerticalAlignment="Top" Width="42" Source="ImageResources/5mods.jpg"/>
9696
<Label x:Name="label2_Copy1" Content="gta5-mods.com/users/CodeSwine" Grid.Column="2" HorizontalAlignment="Left" Margin="56,16,0,0" Grid.Row="2" VerticalAlignment="Top" Width="226" Foreground="#FF707C80"/>
97+
<Label x:Name="label3" Content="Hotkey: CTRL+F10 to toggle." HorizontalAlignment="Left" Margin="10,0,0,0" Grid.Row="3" VerticalAlignment="Top" Height="28" Width="276" FontWeight="Bold" Foreground="#FF707C80"/>
9798
</Grid>
9899
</Window>

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

Lines changed: 96 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
using System.Collections.Generic;
66
using System.Linq;
77
using System.Net;
8+
using System.Runtime.InteropServices;
89
using System.Text;
910
using System.Threading.Tasks;
1011
using System.Windows;
1112
using System.Windows.Controls;
1213
using System.Windows.Data;
1314
using System.Windows.Documents;
1415
using System.Windows.Input;
16+
using System.Windows.Interop;
1517
using System.Windows.Media;
1618
using System.Windows.Media.Imaging;
1719
using System.Windows.Navigation;
@@ -23,34 +25,38 @@ public partial class MainWindow : Window
2325
{
2426
private IPTool iPTool = new IPTool();
2527
private DaWhitelist whiteList = new DaWhitelist();
26-
List<IPAddress> addresses = new List<IPAddress>();
27-
MWhitelist mWhitelist = new MWhitelist();
28+
private List<IPAddress> addresses = new List<IPAddress>();
29+
private MWhitelist mWhitelist = new MWhitelist();
2830

29-
bool set = false;
30-
bool active = false;
31+
private bool set = false;
32+
private bool active = false;
3133

3234
public MainWindow()
3335
{
3436
InitializeComponent();
37+
Loaded += MainWindow_Loaded;
38+
}
39+
40+
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
41+
{
3542
Init();
3643
}
3744

3845
void Init()
3946
{
40-
FirewallRule.DeleteRules();
41-
lblYourIPAddress.Content += " " + iPTool.GrabInternetAddress() + ".";
42-
addresses = whiteList.IpAddressess;
47+
lblYourIPAddress.Content += " " + iPTool.IpAddress + ".";
48+
addresses = DaWhitelist.ReadIPsFromJSON();
4349
lsbAddresses.ItemsSource = addresses;
4450
foreach (IPAddress ip in addresses)
4551
{
4652
mWhitelist.Ips.Add(ip.ToString());
4753
}
48-
lblAmountIPs.Content = whiteList.IpAddressess.Count() + " IPs whitelisted!";
54+
SetIpCount();
4955
}
5056

5157
private void btnAdd_Click(object sender, RoutedEventArgs e)
5258
{
53-
if(iPTool.ValidateIPv4(txbIpToAdd.Text))
59+
if(IPTool.ValidateIPv4(txbIpToAdd.Text))
5460
{
5561
if(!addresses.Contains(IPAddress.Parse(txbIpToAdd.Text)))
5662
{
@@ -60,7 +66,7 @@ private void btnAdd_Click(object sender, RoutedEventArgs e)
6066
DaWhitelist.SaveToJson(mWhitelist);
6167
set = false; active = false;
6268
FirewallRule.DeleteRules();
63-
lblAmountIPs.Content = whiteList.IpAddressess.Count() + " IPs whitelisted!";
69+
SetIpCount();
6470
UpdateNotActive();
6571
}
6672
}
@@ -76,11 +82,16 @@ private void btnDelete_Click(object sender, RoutedEventArgs e)
7682
DaWhitelist.SaveToJson(mWhitelist);
7783
set = false; active = false;
7884
FirewallRule.DeleteRules();
79-
lblAmountIPs.Content = whiteList.IpAddressess.Count() + " IPs whitelisted!";
85+
SetIpCount();
8086
UpdateNotActive();
8187
}
8288
}
8389

90+
private void SetIpCount()
91+
{
92+
lblAmountIPs.Content = addresses.Count() + " IPs whitelisted!";
93+
}
94+
8495
private void btnEnableDisable_Click(object sender, RoutedEventArgs e)
8596
{
8697
SetRules();
@@ -134,5 +145,79 @@ void UpdateActive()
134145
image4.Source = new BitmapImage(new Uri("/CodeSwine-Solo_Public_Lobby;component/ImageResources/locked.png", UriKind.Relative));
135146
lblLock.Content = "Rules active." + Environment.NewLine + "Click to deactivate!";
136147
}
148+
149+
[DllImport("User32.dll")]
150+
private static extern bool RegisterHotKey(
151+
[In] IntPtr hWnd,
152+
[In] int id,
153+
[In] uint fsModifiers,
154+
[In] uint vk);
155+
156+
[DllImport("User32.dll")]
157+
private static extern bool UnregisterHotKey(
158+
[In] IntPtr hWnd,
159+
[In] int id);
160+
161+
private HwndSource _source;
162+
private const int HOTKEY_ID = 9000;
163+
164+
protected override void OnSourceInitialized(EventArgs e)
165+
{
166+
base.OnSourceInitialized(e);
167+
var helper = new WindowInteropHelper(this);
168+
_source = HwndSource.FromHwnd(helper.Handle);
169+
_source.AddHook(HwndHook);
170+
RegisterHotKey();
171+
}
172+
173+
protected override void OnClosed(EventArgs e)
174+
{
175+
_source.RemoveHook(HwndHook);
176+
_source = null;
177+
UnregisterHotKey();
178+
FirewallRule.DeleteRules();
179+
base.OnClosed(e);
180+
}
181+
182+
private void RegisterHotKey()
183+
{
184+
var helper = new WindowInteropHelper(this);
185+
const uint VK_F10 = 0x79;
186+
const uint MOD_CTRL = 0x0002;
187+
if (!RegisterHotKey(helper.Handle, HOTKEY_ID, MOD_CTRL, VK_F10))
188+
{
189+
190+
}
191+
}
192+
193+
private void UnregisterHotKey()
194+
{
195+
var helper = new WindowInteropHelper(this);
196+
UnregisterHotKey(helper.Handle, HOTKEY_ID);
197+
}
198+
199+
private IntPtr HwndHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
200+
{
201+
const int WM_HOTKEY = 0x0312;
202+
switch (msg)
203+
{
204+
case WM_HOTKEY:
205+
switch (wParam.ToInt32())
206+
{
207+
case HOTKEY_ID:
208+
OnHotKeyPressed();
209+
handled = true;
210+
break;
211+
}
212+
break;
213+
}
214+
return IntPtr.Zero;
215+
}
216+
217+
private void OnHotKeyPressed()
218+
{
219+
SetRules();
220+
System.Media.SystemSounds.Hand.Play();
221+
}
137222
}
138223
}

CodeSwine-Solo_Public_Lobby/CodeSwine-Solo_Public_Lobby/bin/Debug/CodeSwine-Solo_Public_Lobby.application

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
1515
</dsig:Transforms>
1616
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" />
17-
<dsig:DigestValue>eAblOi7QTaYfd9f8Q3poleT1jRrrPvPCz0HkmeQIJ1k=</dsig:DigestValue>
17+
<dsig:DigestValue>VHzEx4ssOPYDOF++OyMy0ViJoxW3HExO/TgmzdgEDaw=</dsig:DigestValue>
1818
</hash>
1919
</dependentAssembly>
2020
</dependency>

0 commit comments

Comments
 (0)