Skip to content

Commit

Permalink
Added rules hotkey toggle, cleaned code and enabled windows minimize.
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeSwine committed Aug 23, 2017
1 parent 0239575 commit 99a09a4
Show file tree
Hide file tree
Showing 60 changed files with 337 additions and 95 deletions.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
</Compile>
<Compile Include="DataAccess\DaWhitelist.cs" />
<Compile Include="Helpers\ColorBrush.cs" />
<Compile Include="Helpers\ErrorLogger.cs" />
<Compile Include="Helpers\FirewallRule.cs" />
<Compile Include="Helpers\IPTool.cs" />
<Compile Include="Helpers\RangeCalculator.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,11 @@ namespace CodeSwine_Solo_Public_Lobby.DataAccess
{
public class DaWhitelist
{
public IPTool iPTool = new IPTool();
private bool useWhitelist;
private string iPCount;
private static string path = AppDomain.CurrentDomain.BaseDirectory + "settings.json";

public DaWhitelist()
{
ReadIPsFromJSON();
}

public List<IPAddress> IpAddressess {
get { return ReadIPsFromJSON(); }
}

public string IPCount {
get { return iPCount; }
}

public bool UseWhitelist {
get { return useWhitelist; }
set { useWhitelist = value; }
}

List<IPAddress> ReadIPsFromJSON()
public static List<IPAddress> ReadIPsFromJSON()
{
List<IPAddress> addresses = new List<IPAddress>();

string path = Environment.ExpandEnvironmentVariables(AppDomain.CurrentDomain.BaseDirectory + "settings.json");

string json = "";

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

using (StreamReader r = new StreamReader(path))
{
json = r.ReadToEnd();
string json = r.ReadToEnd();
MWhitelist whitelist = JsonConvert.DeserializeObject<MWhitelist>(json);
foreach (string address in whitelist.Ips)
{
if (IPTool.ValidateIPv4(address.ToString())) addresses.Add(IPAddress.Parse(address));
}
}

MWhitelist whitelist = JsonConvert.DeserializeObject<MWhitelist>(json);

foreach (string address in whitelist.Ips)
{
if(iPTool.ValidateIPv4(address.ToString())) addresses.Add(IPAddress.Parse(address));
}

iPCount = addresses.Count.ToString();
return addresses;
}

public static void SaveToJson(MWhitelist whitelist)
{
string json = JsonConvert.SerializeObject(whitelist);
File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + "settings.json", json);
File.WriteAllText(path, json);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.IO;

namespace CodeSwine_Solo_Public_Lobby.Helpers
{
public class ErrorLogger
{
public static void LogException(Exception e)
{
string path = AppDomain.CurrentDomain.BaseDirectory + "error.log";

if (!File.Exists(path))
{
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("Error log generated at " + DateTime.Now.ToShortDateString());
}
}

using (StreamWriter sw = File.AppendText(path))
{
sw.WriteLine(e.Message.ToString());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ public static void CreateOutbound(string addresses, bool enabled, bool toggle)
firewallPolicy.Rules.Add(firewallRule);
}

} catch(Exception e)
} catch (Exception e)
{
ErrorLogger.LogException(e);
MessageBox.Show("Please start this program as administrator!");
}
}
Expand Down Expand Up @@ -84,7 +85,7 @@ public static void CreateInbound(string addresses, bool enabled, bool toggle)
}
catch (Exception e)
{

ErrorLogger.LogException(e);
}
}

Expand All @@ -106,9 +107,9 @@ public static void DeleteRules()
firewallPolicy.Rules.Remove(firewallRuleOutbound.Name);
} catch (Exception e)
{
ErrorLogger.LogException(e);
MessageBox.Show("Run this program as administrator!");
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,43 @@ namespace CodeSwine_Solo_Public_Lobby.Helpers
{
public class IPTool
{
private string _ipAddress;

public string IpAddress
{
get
{
return GrabInternetAddress();
if (_ipAddress == null) _ipAddress = GrabInternetAddress();
return _ipAddress;
}
}

/// <summary>
/// Gets the hosts IP Address.
/// </summary>
/// <returns>String value of IP.</returns>
public string GrabInternetAddress()
private string GrabInternetAddress()
{
// Still needs check to see if we could retrieve the IP.
return new WebClient().DownloadString("http://icanhazip.com");
// Still needs check to see if we could retrieve the IP.
string ip = "";
try
{
ip = new WebClient().DownloadString("http://icanhazip.com");
}
catch (Exception e)
{
ErrorLogger.LogException(e);
ip = "IP not found.";
}
return ip;
}

/// <summary>
/// Validates IP Address.
/// </summary>
/// <param name="ipString"></param>
/// <returns>Bool True or False.</returns>
public bool ValidateIPv4(string ipString)
public static bool ValidateIPv4(string ipString)
{
if (String.IsNullOrWhiteSpace(ipString))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:CodeSwine_Solo_Public_Lobby"
mc:Ignorable="d"
Title="GTA 5 Online: Private Public Lobby" Height="400" Width="923" ResizeMode="NoResize">
Title="GTA 5 Online: Private Public Lobby" Height="400" Width="923" ResizeMode="CanMinimize">

<Grid>
<Grid.ColumnDefinitions>
Expand Down Expand Up @@ -94,5 +94,6 @@
<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"/>
<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"/>
<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"/>
<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"/>
</Grid>
</Window>
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
Expand All @@ -23,34 +25,38 @@ public partial class MainWindow : Window
{
private IPTool iPTool = new IPTool();
private DaWhitelist whiteList = new DaWhitelist();
List<IPAddress> addresses = new List<IPAddress>();
MWhitelist mWhitelist = new MWhitelist();
private List<IPAddress> addresses = new List<IPAddress>();
private MWhitelist mWhitelist = new MWhitelist();

bool set = false;
bool active = false;
private bool set = false;
private bool active = false;

public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
}

private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
Init();
}

void Init()
{
FirewallRule.DeleteRules();
lblYourIPAddress.Content += " " + iPTool.GrabInternetAddress() + ".";
addresses = whiteList.IpAddressess;
lblYourIPAddress.Content += " " + iPTool.IpAddress + ".";
addresses = DaWhitelist.ReadIPsFromJSON();
lsbAddresses.ItemsSource = addresses;
foreach (IPAddress ip in addresses)
{
mWhitelist.Ips.Add(ip.ToString());
}
lblAmountIPs.Content = whiteList.IpAddressess.Count() + " IPs whitelisted!";
SetIpCount();
}

private void btnAdd_Click(object sender, RoutedEventArgs e)
{
if(iPTool.ValidateIPv4(txbIpToAdd.Text))
if(IPTool.ValidateIPv4(txbIpToAdd.Text))
{
if(!addresses.Contains(IPAddress.Parse(txbIpToAdd.Text)))
{
Expand All @@ -60,7 +66,7 @@ private void btnAdd_Click(object sender, RoutedEventArgs e)
DaWhitelist.SaveToJson(mWhitelist);
set = false; active = false;
FirewallRule.DeleteRules();
lblAmountIPs.Content = whiteList.IpAddressess.Count() + " IPs whitelisted!";
SetIpCount();
UpdateNotActive();
}
}
Expand All @@ -76,11 +82,16 @@ private void btnDelete_Click(object sender, RoutedEventArgs e)
DaWhitelist.SaveToJson(mWhitelist);
set = false; active = false;
FirewallRule.DeleteRules();
lblAmountIPs.Content = whiteList.IpAddressess.Count() + " IPs whitelisted!";
SetIpCount();
UpdateNotActive();
}
}

private void SetIpCount()
{
lblAmountIPs.Content = addresses.Count() + " IPs whitelisted!";
}

private void btnEnableDisable_Click(object sender, RoutedEventArgs e)
{
SetRules();
Expand Down Expand Up @@ -134,5 +145,79 @@ void UpdateActive()
image4.Source = new BitmapImage(new Uri("/CodeSwine-Solo_Public_Lobby;component/ImageResources/locked.png", UriKind.Relative));
lblLock.Content = "Rules active." + Environment.NewLine + "Click to deactivate!";
}

[DllImport("User32.dll")]
private static extern bool RegisterHotKey(
[In] IntPtr hWnd,
[In] int id,
[In] uint fsModifiers,
[In] uint vk);

[DllImport("User32.dll")]
private static extern bool UnregisterHotKey(
[In] IntPtr hWnd,
[In] int id);

private HwndSource _source;
private const int HOTKEY_ID = 9000;

protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
var helper = new WindowInteropHelper(this);
_source = HwndSource.FromHwnd(helper.Handle);
_source.AddHook(HwndHook);
RegisterHotKey();
}

protected override void OnClosed(EventArgs e)
{
_source.RemoveHook(HwndHook);
_source = null;
UnregisterHotKey();
FirewallRule.DeleteRules();
base.OnClosed(e);
}

private void RegisterHotKey()
{
var helper = new WindowInteropHelper(this);
const uint VK_F10 = 0x79;
const uint MOD_CTRL = 0x0002;
if (!RegisterHotKey(helper.Handle, HOTKEY_ID, MOD_CTRL, VK_F10))
{

}
}

private void UnregisterHotKey()
{
var helper = new WindowInteropHelper(this);
UnregisterHotKey(helper.Handle, HOTKEY_ID);
}

private IntPtr HwndHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
const int WM_HOTKEY = 0x0312;
switch (msg)
{
case WM_HOTKEY:
switch (wParam.ToInt32())
{
case HOTKEY_ID:
OnHotKeyPressed();
handled = true;
break;
}
break;
}
return IntPtr.Zero;
}

private void OnHotKeyPressed()
{
SetRules();
System.Media.SystemSounds.Hand.Play();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" />
<dsig:DigestValue>eAblOi7QTaYfd9f8Q3poleT1jRrrPvPCz0HkmeQIJ1k=</dsig:DigestValue>
<dsig:DigestValue>VHzEx4ssOPYDOF++OyMy0ViJoxW3HExO/TgmzdgEDaw=</dsig:DigestValue>
</hash>
</dependentAssembly>
</dependency>
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@
</dependentAssembly>
</dependency>
<dependency>
<dependentAssembly dependencyType="install" allowDelayedBinding="true" codebase="CodeSwine-Solo_Public_Lobby.exe" size="161776">
<dependentAssembly dependencyType="install" allowDelayedBinding="true" codebase="CodeSwine-Solo_Public_Lobby.exe" size="163312">
<assemblyIdentity name="CodeSwine-Solo_Public_Lobby" version="1.0.0.0" language="neutral" processorArchitecture="msil" />
<hash>
<dsig:Transforms>
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" />
<dsig:DigestValue>YfbrIiNIkSCY9oSJXhfajX9JWvxB04Z/yVq5/I5r1Gs=</dsig:DigestValue>
<dsig:DigestValue>DaQVVdUJTX8s3VMONV9ebyqPlN1GNPmV/89nNDBNf9Y=</dsig:DigestValue>
</hash>
</dependentAssembly>
</dependency>
Expand Down
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit 99a09a4

Please sign in to comment.