Skip to content

Commit

Permalink
#115 refactor: getinterface method is now part of the wireguard config
Browse files Browse the repository at this point in the history
+ Also added calls to AddClient and RemoveClient where needed
  • Loading branch information
joseantmazonsb committed Feb 28, 2022
1 parent ed56e90 commit 42d1eb3
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 27 deletions.
16 changes: 13 additions & 3 deletions Linguard/Core.Test/Mocks/DefaultConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using Linguard.Core.Configuration;
using Linguard.Core.Drivers.TrafficStorage;
using Linguard.Core.Models.Wireguard;
Expand All @@ -9,14 +10,23 @@ namespace Core.Test.Mocks;
public sealed class DefaultConfiguration : Mock<IConfiguration> {

public DefaultConfiguration() {
SetupProperty(c => c.Wireguard, new Mock<IWireguardConfiguration>().
SetupProperty(c => c.Interfaces, new HashSet<Interface>())
.Object);
SetupProperty(c => c.Wireguard, GetWireguardConfigurationMock().Object);
SetupProperty(c => c.Logging, new Mock<ILoggingConfiguration>().Object);
SetupProperty(c => c.Traffic, new Mock<ITrafficConfiguration>()
.SetupProperty(c => c.StorageDriver, new Mock<ITrafficStorageDriver>().Object)
.Object);
SetupProperty(c => c.Web, new Mock<IWebConfiguration>().Object);
Setup(o => o.Clone()).Returns(Object);
}

private Mock<IWireguardConfiguration> GetWireguardConfigurationMock() {
var wireguardConfiguration = new Mock<IWireguardConfiguration>();
var interfaces = new HashSet<Interface>();
wireguardConfiguration.Setup(o => o.GetInterface(It.IsAny<Client>()))
.Returns<Client>(c =>
interfaces.SingleOrDefault(i => i.Clients.Contains(c))
);
wireguardConfiguration.SetupProperty(c => c.Interfaces, interfaces);
return wireguardConfiguration;
}
}
6 changes: 6 additions & 0 deletions Linguard/Core/Configuration/IWireguardConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ public interface IWireguardConfiguration : ICloneable {
/// Default endpoint for all peers if none specified at interface level.
/// </summary>
public Uri? Endpoint { get; set; }
/// <summary>
/// Get the interface associated to a given client or <c>default</c> if none.
/// </summary>
/// <param name="client"></param>
/// <returns></returns>
Interface? GetInterface(Client client);
}
4 changes: 3 additions & 1 deletion Linguard/Core/Configuration/WireguardConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ public class WireguardConfiguration : IWireguardConfiguration {
public Uri? PrimaryDns { get; set; }
public Uri? SecondaryDns { get; set; }
public Uri? Endpoint { get; set; }

public Interface? GetInterface(Client client) => Interfaces
.SingleOrDefault(i => i.Clients.Contains(client));

public object Clone() {
return MemberwiseClone();
}
Expand Down
6 changes: 4 additions & 2 deletions Linguard/Core/Services/IWireguardService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

namespace Linguard.Core.Services;

/// <summary>
/// Provides an abstraction layer to interact with Wireguard.
/// </summary>
public interface IWireguardService {
Interface? GetInterface(Client client);
void StartInterface(Interface iface);
void StopInterface(Interface iface);
void AddClient(Interface iface, Client client);
void RemoveClient(Interface iface, Client client);
void RemoveClient(Client client);
string? GenerateWireguardPrivateKey();
string? GenerateWireguardPublicKey(string privateKey);
DateTime GetLastHandshake(Client client);
Expand Down
10 changes: 4 additions & 6 deletions Linguard/Core/Services/WireguardService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ public WireguardService(IConfigurationManager configurationManager, ISystemWrapp

private IWireguardConfiguration Configuration => _configurationManager.Configuration.Wireguard;

public Interface? GetInterface(Client client) => Configuration.Interfaces
.SingleOrDefault(i => i.Clients.Contains(client));

public void StartInterface(Interface @interface) {
var result = _systemWrapper
.RunCommand($"sudo {Configuration.WireguardQuickBin} up {@interface.Name}");
Expand All @@ -41,7 +38,8 @@ public void AddClient(Interface iface, Client client) {
if (!result.Success) throw new WireguardException(result.Stderr);
}

public void RemoveClient(Interface iface, Client client) {
public void RemoveClient(Client client) {
var iface = Configuration.GetInterface(client);
var cmd = $"sudo {Configuration.WireguardQuickBin} set {iface.Name} peer {client.PublicKey} remove";
var result = _systemWrapper.RunCommand(cmd);
if (!result.Success) throw new WireguardException(result.Stderr);
Expand All @@ -63,7 +61,7 @@ public string GenerateWireguardPublicKey(string privateKey) {

public DateTime GetLastHandshake(Client client) {
var rawData = _systemWrapper
.RunCommand($"{Configuration.WireguardBin} show {GetInterface(client).Name} dump")
.RunCommand($"{Configuration.WireguardBin} show {Configuration.GetInterface(client).Name} dump")
.Stdout;
try {
return WireguardDumpParser.GetLastHandshake(rawData, client);
Expand All @@ -82,7 +80,7 @@ public IEnumerable<TrafficData> GetTrafficData() {
}

public TrafficData? GetTrafficData(Client client) {
var data = GetTrafficData(GetInterface(client));
var data = GetTrafficData(Configuration.GetInterface(client));
return data.SingleOrDefault(e => e.Peer.Equals(client));
}

Expand Down
1 change: 1 addition & 0 deletions Linguard/Web/Pages/AddClient.razor
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
private void Submit(Client client) {
var result = _validator.Validate(client);
if (result.IsValid) {
_wireguardService.AddClient(Interface, client);
Interface!.Clients.Add(client);
_configurationManager.Save();
_navigationManager.NavigateTo("wireguard");
Expand Down
4 changes: 2 additions & 2 deletions Linguard/Web/Pages/Wireguard.razor
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@
<RadzenDataGridColumn TItem="Client" Property="IPv6Address" Title="IPv6"/>
<RadzenDataGridColumn TItem="Client" Title="Interface">
<Template Context="data">
<a href="/interface/@(_wireguardService.GetInterface(data)?.Id)">
@(_wireguardService.GetInterface(data)?.Name)
<a href="/interface/@(Configuration.Wireguard.GetInterface(data)?.Id)">
@(Configuration.Wireguard.GetInterface(data)?.Name)
</a>
</Template>
</RadzenDataGridColumn>
Expand Down
6 changes: 3 additions & 3 deletions Linguard/Web/Services/WebService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public void RemoveWireguardModel(IWireguardPeer peer) {
RemoveInterface(iface);
break;
}
ConfigurationManager.Save();
}

public byte[] GetQrCode(IWireguardPeer peer) {
Expand All @@ -59,12 +60,11 @@ public byte[] GetQrCode(IWireguardPeer peer) {
}

private void RemoveClient(Client client) {
WireguardService.GetInterface(client)?.Clients.Remove(client);
ConfigurationManager.Save();
WireguardService.RemoveClient(client);
Configuration.GetInterface(client)?.Clients.Remove(client);
}

private void RemoveInterface(Interface iface) {
Configuration.Interfaces.Remove(iface);
ConfigurationManager.Save();
}
}
2 changes: 1 addition & 1 deletion Linguard/Web/Shared/ClientConfigurationForm.razor
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@
protected override void OnInitialized() {
base.OnInitialized();
if (Interface == default) {
Interface = _wireguardService.GetInterface(Client);
Interface = Configuration.Wireguard.GetInterface(Client);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Linguard/WebMock/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

var manager = new DefaultConfigurationManager().Object;
var systemMock = new SystemMock().Object;
var wireguardServiceMock = new WireguardServiceMock(manager, systemMock).Object;
var wireguardServiceMock = new WireguardServiceMock(systemMock).Object;
builder.Services.AddSingleton(manager);
builder.Services.AddSingleton(systemMock);
builder.Services.AddSingleton(wireguardServiceMock);
Expand Down
10 changes: 2 additions & 8 deletions Linguard/WebMock/WireguardServiceMock.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
using Linguard.Core.Managers;
using Linguard.Core.Models.Wireguard;
using Linguard.Core.Models.Wireguard;
using Linguard.Core.OS;
using Linguard.Core.Services;
using Moq;

namespace WebMock;

public class WireguardServiceMock : Mock<IWireguardService> {
public WireguardServiceMock(IConfigurationManager configurationManager, ISystemWrapper systemWrapper) {
public WireguardServiceMock(ISystemWrapper systemWrapper) {
Setup(o => o.StartInterface(It.IsAny<Interface>()))
.Callback<Interface>(systemWrapper.AddNetworkInterface);
Setup(o => o.StopInterface(It.IsAny<Interface>()))
.Callback<Interface>(systemWrapper.RemoveNetworkInterface);
Setup(o => o.GetInterface(It.IsAny<Client>()))
.Returns<Client>(c =>
configurationManager.Configuration.Wireguard.Interfaces
.SingleOrDefault(i => i.Clients.Contains(c))
);
}
}

0 comments on commit 42d1eb3

Please sign in to comment.