From 49a441bde19315c2dbab9848705017b360b19a48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Maz=C3=B3n=20San=20Bartolom=C3=A9?= Date: Sat, 26 Feb 2022 13:25:53 +0100 Subject: [PATCH] #115 chore: added wireguard exceptions and auxiliary methods to check if an interface is up or down --- .../Services/Exceptions/WireguardException.cs | 7 ++++ Linguard/Core/Services/IWireguardService.cs | 2 + Linguard/Core/Services/WireguardService.cs | 42 ++++++++++++------- Linguard/Web/Pages/EditInterface.razor | 2 +- Linguard/Web/Pages/Wireguard.razor | 11 +---- 5 files changed, 38 insertions(+), 26 deletions(-) create mode 100644 Linguard/Core/Services/Exceptions/WireguardException.cs diff --git a/Linguard/Core/Services/Exceptions/WireguardException.cs b/Linguard/Core/Services/Exceptions/WireguardException.cs new file mode 100644 index 0000000..08ce476 --- /dev/null +++ b/Linguard/Core/Services/Exceptions/WireguardException.cs @@ -0,0 +1,7 @@ +namespace Linguard.Core.Services.Exceptions; + +public class WireguardException : Exception { + public WireguardException() {} + public WireguardException(string message) : base(message) {} + public WireguardException(string message, Exception innerException) : base(message, innerException) {} +} \ No newline at end of file diff --git a/Linguard/Core/Services/IWireguardService.cs b/Linguard/Core/Services/IWireguardService.cs index 3000dce..f6b6481 100644 --- a/Linguard/Core/Services/IWireguardService.cs +++ b/Linguard/Core/Services/IWireguardService.cs @@ -6,6 +6,8 @@ namespace Linguard.Core.Services; public interface IWireguardService { Interface? GetInterface(Client client); + bool IsInterfaceUp(Interface iface); + bool IsInterfaceDown(Interface iface); void StartInterface(Interface @interface); void StopInterface(Interface @interface); string? GenerateWireguardPrivateKey(); diff --git a/Linguard/Core/Services/WireguardService.cs b/Linguard/Core/Services/WireguardService.cs index 60386dc..e3c3294 100644 --- a/Linguard/Core/Services/WireguardService.cs +++ b/Linguard/Core/Services/WireguardService.cs @@ -1,12 +1,11 @@ -using System.Linq.Expressions; -using System.Net.NetworkInformation; +using System.Net.NetworkInformation; using System.Text; -using System.Text.RegularExpressions; using Linguard.Core.Configuration; using Linguard.Core.Managers; using Linguard.Core.Models; using Linguard.Core.Models.Wireguard; using Linguard.Core.OS; +using Linguard.Core.Services.Exceptions; using Linguard.Core.Utils; namespace Linguard.Core.Services; @@ -25,26 +24,39 @@ public WireguardService(IConfigurationManager configurationManager, ICommandRunn public Interface? GetInterface(Client client) => Configuration.Interfaces .SingleOrDefault(i => i.Clients.Contains(client)); + public bool IsInterfaceUp(Interface iface) { + return NetworkInterface.GetAllNetworkInterfaces() + .Any(i => i.Name.Equals(iface.Name) && i.OperationalStatus == OperationalStatus.Up); + } + + public bool IsInterfaceDown(Interface iface) { + return !IsInterfaceUp(iface); + } + public void StartInterface(Interface @interface) { var result = _commandRunner .Run($"sudo {Configuration.WireguardQuickBin} up {@interface.Name}"); - if (!result.Success) throw new Exception(result.Stderr); + if (!result.Success) throw new WireguardException(result.Stderr); } public void StopInterface(Interface @interface) { var result = _commandRunner .Run($"sudo {Configuration.WireguardQuickBin} down {@interface.Name}"); - if (!result.Success) throw new Exception(result.Stderr); + if (!result.Success) throw new WireguardException(result.Stderr); } public string? GenerateWireguardPrivateKey() { - return _commandRunner - .Run($"sudo {Configuration.WireguardBin} genkey").Stdout; + var result = _commandRunner + .Run($"sudo {Configuration.WireguardBin} genkey"); + if (!result.Success) throw new WireguardException(result.Stderr); + return result.Stdout; } public string? GenerateWireguardPublicKey(string privateKey) { - return _commandRunner - .Run($"echo {privateKey} | sudo {Configuration.WireguardBin} pubkey").Stdout; + var result = _commandRunner + .Run($"echo {privateKey} | sudo {Configuration.WireguardBin} pubkey"); + if (!result.Success) throw new WireguardException(result.Stderr); + return result.Stdout; } public string[] GenerateOnUpRules(string interfaceName, NetworkInterface gateway) { @@ -81,8 +93,7 @@ public DateTime GetLastHandshake(Client client) { return WireguardDumpParser.GetLastHandshake(rawData, client); } catch (Exception e) { - // TODO: log - return default; + throw new WireguardException($"Unable to obtain last handshake for client {client.Name}", e); } } @@ -103,10 +114,9 @@ public IEnumerable GetTrafficData(Interface iface) { var rawData = _commandRunner .Run($"{Configuration.WireguardBin} show {iface.Name} dump") .Stdout; - if (string.IsNullOrEmpty(rawData)) { - return Enumerable.Empty(); - } - return WireguardDumpParser.GetTrafficData(rawData, iface); + return string.IsNullOrEmpty(rawData) + ? Enumerable.Empty() + : WireguardDumpParser.GetTrafficData(rawData, iface); } private string GenerateWireguardConfiguration(Interface @interface) { @@ -163,7 +173,7 @@ string GetWireguardDns() { } } - private string GetWireguardAddress(IWireguardPeer peer) { + private static string GetWireguardAddress(IWireguardPeer peer) { string address; if (peer.IPv4Address != default) { address = peer.IPv4Address.ToString(); diff --git a/Linguard/Web/Pages/EditInterface.razor b/Linguard/Web/Pages/EditInterface.razor index 7239673..f69a2ee 100644 --- a/Linguard/Web/Pages/EditInterface.razor +++ b/Linguard/Web/Pages/EditInterface.razor @@ -37,7 +37,7 @@

@Title - @if (InterfaceNames.Contains(Iface.Name)) { + @if (_wireguardService.IsInterfaceUp(Iface)) { } else { diff --git a/Linguard/Web/Pages/Wireguard.razor b/Linguard/Web/Pages/Wireguard.razor index 4bbdcce..c0ce9a9 100644 --- a/Linguard/Web/Pages/Wireguard.razor +++ b/Linguard/Web/Pages/Wireguard.razor @@ -39,7 +39,7 @@