Skip to content

Commit

Permalink
#115 chore: added very simple mechanism for saving interfaces and cli…
Browse files Browse the repository at this point in the history
…ents
  • Loading branch information
joseantmazonsb committed Feb 27, 2022
1 parent c8876fd commit f2e728a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
6 changes: 5 additions & 1 deletion Linguard/Core/Models/Wireguard/Client.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Linguard.Core.Models.Wireguard;

public class Client : WireguardPeerBase {
public class Client : WireguardPeerBase, ICloneable {
public ICollection<IPAddressCidr> AllowedIPs { get; set; } = new List<IPAddressCidr>();
public bool Nat { get; set; }
public Uri PrimaryDns { get; set; }
Expand Down Expand Up @@ -42,4 +42,8 @@ public override string ToString() {
$"Public key: {PublicKey}{Environment.NewLine}" +
$"AllowedIPs: {string.Join(", ", AllowedIPs.Select(ip => ip.ToString()))}";
}

public object Clone() {
return MemberwiseClone();
}
}
8 changes: 6 additions & 2 deletions Linguard/Core/Models/Wireguard/Interface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Linguard.Core.Models.Wireguard;

public class Interface : WireguardPeerBase {
public class Interface : WireguardPeerBase, ICloneable {
public NetworkInterface Gateway { get; set; }
public int Port { get; set; }
public bool Auto { get; set; }
Expand Down Expand Up @@ -53,7 +53,11 @@ public override string ToString() {
$"Primary DNS: {PrimaryDns}{Environment.NewLine}" +
$"Secondary DNS: {SecondaryDns}";
}


public object Clone() {
return MemberwiseClone();
}

public override string Brief() {
return $"Name: {Name}, " +
$"Port: {Port}, " +
Expand Down
13 changes: 10 additions & 3 deletions Linguard/Web/Pages/EditClient.razor
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
@using Linguard.Core.Models.Wireguard
@using Linguard.Core.Services
@using Linguard.Core.Configuration
@using FluentValidation

@inject IConfigurationManager _configurationManager
@inject IWireguardService _wireguardService
@inject NotificationService _notificationService
@inject DialogService _dialogService
@inject NavigationManager _navigationManager
@inject IJSRuntime _js

@inject AbstractValidator<Client> _validator

@code {
const string Title = "Client";
Expand All @@ -21,7 +22,7 @@
IConfiguration Configuration => _configurationManager.Configuration;
Client? Client => Configuration.Wireguard.Interfaces
.SelectMany(i => i.Clients)
.SingleOrDefault(i => i.Id == Id);
.SingleOrDefault(i => i.Id == Id)?.Clone() as Client;
}

<PageTitle>@($"{AssemblyInfo.Product} | {Title}")</PageTitle>
Expand Down Expand Up @@ -56,6 +57,12 @@

@code {
private void Submit(Client args) {
throw new NotImplementedException();
var result = _validator.Validate(args);
if (result.IsValid) {
_configurationManager.Save();
_notificationService.Notify(NotificationSeverity.Success, "Client saved!");
return;
}
_notificationService.Notify(NotificationSeverity.Error, "Unable to save client.");
}
}
5 changes: 3 additions & 2 deletions Linguard/Web/Pages/EditInterface.razor
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
public Guid Id { get; set; }

IConfiguration Configuration => _configurationManager.Configuration;
Interface? Iface => Configuration.Wireguard.Interfaces.SingleOrDefault(i => i.Id == Id);
Interface? Iface => Configuration.Wireguard.Interfaces.SingleOrDefault(i => i.Id == Id)?.Clone() as Interface;

}
<PageTitle>@($"{AssemblyInfo.Product} | {Title}")</PageTitle>
Expand Down Expand Up @@ -117,9 +117,10 @@
var result = _validator.Validate(args);
if (result.IsValid) {
_configurationManager.Save();
_navigationManager.NavigateTo("wireguard");
_notificationService.Notify(NotificationSeverity.Success, "Interface saved!");
return;
}
_notificationService.Notify(NotificationSeverity.Error, "Unable to save interface.");
}

void EditWireguardClient(Client client) {
Expand Down

0 comments on commit f2e728a

Please sign in to comment.