Skip to content

Commit

Permalink
#115 chore: added wireguard exceptions and auxiliary methods to check…
Browse files Browse the repository at this point in the history
… if an interface is up or down
  • Loading branch information
joseantmazonsb committed Feb 26, 2022
1 parent 0332685 commit 49a441b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 26 deletions.
7 changes: 7 additions & 0 deletions Linguard/Core/Services/Exceptions/WireguardException.cs
Original file line number Diff line number Diff line change
@@ -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) {}
}
2 changes: 2 additions & 0 deletions Linguard/Core/Services/IWireguardService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
42 changes: 26 additions & 16 deletions Linguard/Core/Services/WireguardService.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -103,10 +114,9 @@ public IEnumerable<TrafficData> GetTrafficData(Interface iface) {
var rawData = _commandRunner
.Run($"{Configuration.WireguardBin} show {iface.Name} dump")
.Stdout;
if (string.IsNullOrEmpty(rawData)) {
return Enumerable.Empty<TrafficData>();
}
return WireguardDumpParser.GetTrafficData(rawData, iface);
return string.IsNullOrEmpty(rawData)
? Enumerable.Empty<TrafficData>()
: WireguardDumpParser.GetTrafficData(rawData, iface);
}

private string GenerateWireguardConfiguration(Interface @interface) {
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion Linguard/Web/Pages/EditInterface.razor
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<div class="col">
<h1 class="mb-3">
@Title
@if (InterfaceNames.Contains(Iface.Name)) {
@if (_wireguardService.IsInterfaceUp(Iface)) {
<RadzenBadge class="float-end" IsPill="true" Text="up" BadgeStyle="BadgeStyle.Success" />
}
else {
Expand Down
11 changes: 2 additions & 9 deletions Linguard/Web/Pages/Wireguard.razor
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<RadzenDataGridColumn TItem="Interface" Title="Actions" Resizable="false"
Filterable="false" Sortable="false">
<Template Context="data">
@if (_interfaces.SingleOrDefault(i => i.Name.Equals(data.Name)) == default) {
@if (_wireguardService.IsInterfaceUp(data)) {
<RadzenButton Icon="play_arrow" ButtonStyle="ButtonStyle.Success" title="Start"
Click="() => StartInterface(data)" />
}
Expand Down Expand Up @@ -118,12 +118,11 @@
IConfiguration Configuration => _configurationManager.Configuration;
ICollection<Interface> _wireguardInterfaces;
ICollection<Client> _clients;
ICollection<NetworkInterface> _interfaces;
bool _startingAll;
bool _stoppingAll;

protected override void OnInitialized() {
_interfaces = NetworkInterface.GetAllNetworkInterfaces();
NetworkInterface.GetAllNetworkInterfaces();
_wireguardInterfaces = Configuration.Wireguard.Interfaces
.OrderBy(i => i.Name)
.ToList();
Expand All @@ -132,14 +131,9 @@
.ToList();
}

void RefreshNetworkInterfaces() {
_interfaces = NetworkInterface.GetAllNetworkInterfaces();
}

void StartInterface(Interface iface) {
try {
_wireguardService.StartInterface(iface);
RefreshNetworkInterfaces();
_interfacesGrid.Reload();
}
catch (Exception e) {
Expand All @@ -154,7 +148,6 @@
void StopInterface(Interface @interface) {
try {
_wireguardService.StopInterface(@interface);
RefreshNetworkInterfaces();
_interfacesGrid.Reload();
}
catch (Exception e) {
Expand Down

0 comments on commit 49a441b

Please sign in to comment.