From fdaaec8c516f0b3bc2cf219e1b68f9849c6330db Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Fri, 4 Aug 2023 20:51:05 +0200 Subject: [PATCH] Code cleanup --- FraudCapturer/Configuration/BlockConfig.cs | 2 +- FraudCapturer/Configuration/Configurator.cs | 12 ++++++------ FraudCapturer/GlobalSuppressions.cs | 8 ++++++++ FraudCapturer/Helpers/DomainHelper.cs | 12 ++++-------- FraudCapturer/Helpers/FirewallHelper.cs | 4 ++-- FraudCapturer/Helpers/IpHelper.cs | 18 ++++++++---------- FraudCapturer/Helpers/PackageHelper.cs | 14 +++++++------- FraudCapturer/Program.cs | 16 ++++++++-------- 8 files changed, 44 insertions(+), 42 deletions(-) create mode 100644 FraudCapturer/GlobalSuppressions.cs diff --git a/FraudCapturer/Configuration/BlockConfig.cs b/FraudCapturer/Configuration/BlockConfig.cs index 1255ee9..791e6ea 100644 --- a/FraudCapturer/Configuration/BlockConfig.cs +++ b/FraudCapturer/Configuration/BlockConfig.cs @@ -22,7 +22,7 @@ internal class BlockConfig } } - public bool CheckIfBlockSet(IpInfo ipInfo, BlockConfigSet blockConfigSet) + public static bool CheckIfBlockSet(IpInfo ipInfo, BlockConfigSet blockConfigSet) { if (ipInfo.Type == "VPN" && blockConfigSet.BlockIfVpn) { diff --git a/FraudCapturer/Configuration/Configurator.cs b/FraudCapturer/Configuration/Configurator.cs index dd81117..49de39f 100644 --- a/FraudCapturer/Configuration/Configurator.cs +++ b/FraudCapturer/Configuration/Configurator.cs @@ -1,8 +1,8 @@ namespace FraudCapturer.Configuration; -internal class Configurator +internal static class Configurator { - public BlockConfig GetConfig() + public static BlockConfig GetConfig() { BlockConfig blockConfig = new BlockConfig(); @@ -24,7 +24,7 @@ internal class Configurator return blockConfig; } - private BlockConfigSet GetBlockConfigSetFromConsole(bool ifNotProxyDefault, bool ifproxyDefault, bool ifVpnDeault) + private static BlockConfigSet GetBlockConfigSetFromConsole(bool ifNotProxyDefault, bool ifproxyDefault, bool ifVpnDeault) { BlockConfigSet blockConfigSet = new BlockConfigSet(); Console.WriteLine($"Block if no Proxy detected {GetDefaultHintString(ifNotProxyDefault)}:"); @@ -39,7 +39,7 @@ internal class Configurator return blockConfigSet; } - private bool GetBoolValueFromConsole(bool defaultValue) + private static bool GetBoolValueFromConsole(bool defaultValue) { string input = Console.ReadLine() ?? string.Empty; @@ -48,7 +48,7 @@ internal class Configurator return defaultValue; } - while (input.ToLower() != "y" && input.ToLower() != "n") + while (input.ToLower() is not "y" and not "n") { input = Console.ReadLine() ?? string.Empty; } @@ -56,7 +56,7 @@ internal class Configurator return input.ToLower() == "y"; } - private string GetDefaultHintString(bool defaultValue) + private static string GetDefaultHintString(bool defaultValue) { return defaultValue ? "[Y/n]" : "[y/N]"; } diff --git a/FraudCapturer/GlobalSuppressions.cs b/FraudCapturer/GlobalSuppressions.cs new file mode 100644 index 0000000..b9294d3 --- /dev/null +++ b/FraudCapturer/GlobalSuppressions.cs @@ -0,0 +1,8 @@ +// This file is used by Code Analysis to maintain SuppressMessage +// attributes that are applied to this project. +// Project-level suppressions either have no target or are given +// a specific target and scoped to a namespace, type, member, etc. + +using System.Diagnostics.CodeAnalysis; + +[assembly: SuppressMessage("Minor Code Smell", "S1075:URIs should not be hardcoded", Justification = "", Scope = "member", Target = "~F:FraudCapturer.Program.AppUrl")] diff --git a/FraudCapturer/Helpers/DomainHelper.cs b/FraudCapturer/Helpers/DomainHelper.cs index 1bb8bc7..a03fff7 100644 --- a/FraudCapturer/Helpers/DomainHelper.cs +++ b/FraudCapturer/Helpers/DomainHelper.cs @@ -15,12 +15,8 @@ internal class DomainHelper { public static string[] GetDomainsFromDnsReqest(TransportPacket transportPacket) { - List domains = new(); MatchCollection matchCollection = Regex.Matches(transportPacket.GetPayloadAsString().ToLower(), @"(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]"); - foreach (Match match in matchCollection) - { - domains.Add(match.Value); - } + List domains = matchCollection.Select(match => match.Value).ToList(); return domains.Distinct().ToArray(); } @@ -79,13 +75,13 @@ internal class DomainHelper } } - private class AntiFishReqestBody + private sealed class AntiFishReqestBody { [JsonPropertyName("message")] public string? Message { get; set; } } - private class AntiFishResult + private sealed class AntiFishResult { [JsonPropertyName("followed")] public bool Followed { get; set; } @@ -103,7 +99,7 @@ internal class DomainHelper public double TrustRating { get; set; } } - private class AntiFishResultBody + private sealed class AntiFishResultBody { [JsonPropertyName("match")] public bool Match { get; set; } diff --git a/FraudCapturer/Helpers/FirewallHelper.cs b/FraudCapturer/Helpers/FirewallHelper.cs index 7113eb6..70f9eda 100644 --- a/FraudCapturer/Helpers/FirewallHelper.cs +++ b/FraudCapturer/Helpers/FirewallHelper.cs @@ -2,7 +2,7 @@ namespace FraudCapturer.Helpers; -internal class FirewallHelper +internal static class FirewallHelper { public static void BlockIp(IPAddress? ipAddress) { @@ -41,7 +41,7 @@ internal class FirewallHelper lock (Program.IpStorePath) { List iPs = File.ReadAllLines(Program.IpStorePath).ToList(); - iPs.Remove(ipAddress.ToString()); + _ = iPs.Remove(ipAddress.ToString()); File.WriteAllLines(Program.IpStorePath, iPs); diff --git a/FraudCapturer/Helpers/IpHelper.cs b/FraudCapturer/Helpers/IpHelper.cs index 3ee1465..db92ea7 100644 --- a/FraudCapturer/Helpers/IpHelper.cs +++ b/FraudCapturer/Helpers/IpHelper.cs @@ -5,7 +5,7 @@ using System.Text.Json; namespace FraudCapturer.Helpers; -internal class IpHelper +internal static class IpHelper { public static string? ProxycheckApiKey { get; set; } @@ -101,7 +101,7 @@ internal class IpHelper return ip[0] switch { 10 or 127 => true, - 172 => ip[1] >= 16 && ip[1] < 32, + 172 => ip[1] is >= 16 and < 32, 192 => ip[1] == 168, _ => false, }; @@ -124,16 +124,14 @@ internal class IpHelper return true; } - foreach (IPAddress localIP in localIPs) - { - if (hostIP.Equals(localIP)) - { - return true; - } - } + return localIPs.Any(i => i.Equals(hostIP)); } } - catch { } + catch + { + return false; + } + return false; } } \ No newline at end of file diff --git a/FraudCapturer/Helpers/PackageHelper.cs b/FraudCapturer/Helpers/PackageHelper.cs index a87572d..bb6563d 100644 --- a/FraudCapturer/Helpers/PackageHelper.cs +++ b/FraudCapturer/Helpers/PackageHelper.cs @@ -9,24 +9,24 @@ internal static class PackageHelper public static string GetPayloadAsString(this TransportPacket transportPacket) { byte[] data = transportPacket.PayloadData; - string bytes = ""; - string ascii = ""; + StringBuilder bytes = new StringBuilder(); + StringBuilder ascii = new StringBuilder(); for (int i = 1; i <= data.Length; i++) { // add the current byte to the bytes hex string - bytes += data[i - 1].ToString("x").PadLeft(2, '0') + " "; + _ = bytes.Append(data[i - 1].ToString("x").PadLeft(2, '0') + " "); // add the current byte to the asciiBytes array for later processing - if (data[i - 1] < 0x21 || data[i - 1] > 0x7e) + if (data[i - 1] is < 0x21 or > 0x7e) { - ascii += "."; + _ = ascii.Append('.'); } else { - ascii += Encoding.ASCII.GetString(new[] { data[i - 1] }); + _ = ascii.Append(Encoding.ASCII.GetString(new[] { data[i - 1] })); } } - return ascii.Trim('.'); + return ascii.ToString().Trim('.'); } } \ No newline at end of file diff --git a/FraudCapturer/Program.cs b/FraudCapturer/Program.cs index 3389f2b..cff365a 100644 --- a/FraudCapturer/Program.cs +++ b/FraudCapturer/Program.cs @@ -13,7 +13,7 @@ using System.Text.Json; namespace FraudCapturer; -public class Program +public static class Program { public const string AppName = "FraudCapturer"; public const string AppUrl = "https://github.com/Stone-Red-Code/FraudCapturer"; @@ -43,7 +43,7 @@ public class Program if (args.FirstOrDefault() == "config") { - blockConfig = new Configurator().GetConfig(); + blockConfig = Configurator.GetConfig(); string jsonConfig = JsonSerializer.Serialize(blockConfig); File.WriteAllText(ConfigStorePath, jsonConfig); @@ -119,10 +119,10 @@ public class Program private static void Device_OnPacketArrival(object sender, PacketCapture e) { RawCapture rawPacket = e.GetPacket(); - ProcessRawPacket(rawPacket); + _ = ProcessRawPacket(rawPacket); } - private static async void ProcessRawPacket(RawCapture rawPacket) + private static async Task ProcessRawPacket(RawCapture rawPacket) { Packet packet = Packet.ParsePacket(rawPacket.LinkLayerType, rawPacket.Data); if (packet is EthernetPacket) @@ -191,19 +191,19 @@ public class Program bool block = false; ConsoleColor consoleColor; - if (ipInfo.Risk >= 67 && blockConfig.CheckIfBlockSet(ipInfo, blockConfig.HighRiskSet)) + if (ipInfo.Risk >= 67 && BlockConfig.CheckIfBlockSet(ipInfo, blockConfig.HighRiskSet)) { FirewallHelper.BlockIp(remoteIpAddress); consoleColor = ConsoleColor.Red; block = true; } - else if (ipInfo.Risk <= 33 && blockConfig.CheckIfBlockSet(ipInfo, blockConfig.LowRiskSet)) + else if (ipInfo.Risk <= 33 && BlockConfig.CheckIfBlockSet(ipInfo, blockConfig.LowRiskSet)) { FirewallHelper.BlockIp(remoteIpAddress); consoleColor = ConsoleColor.Yellow; block = true; } - else if (blockConfig.CheckIfBlockSet(ipInfo, blockConfig.MeduimRiskSet)) + else if (BlockConfig.CheckIfBlockSet(ipInfo, blockConfig.MeduimRiskSet)) { FirewallHelper.BlockIp(remoteIpAddress); consoleColor = ConsoleColor.DarkYellow; @@ -255,7 +255,7 @@ public class Program continue; } - if (domainInfo.IsMatch == false) + if (!domainInfo.IsMatch) { if (lastDomain != domain) {