Skip to content

Commit

Permalink
#115 chore: better authentication, register page and refactor of conf…
Browse files Browse the repository at this point in the history
…iguration

+ configuration is now fully module based, allowing for an easy way to extend it
  • Loading branch information
joseantmazonsb committed Mar 20, 2022
1 parent 98e07a3 commit f27c815
Show file tree
Hide file tree
Showing 97 changed files with 1,140 additions and 451 deletions.
15 changes: 8 additions & 7 deletions Linguard/Cli.Test/AddClientCommandShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Castle.Core.Internal;
using FluentAssertions;
using Linguard.Cli.Commands;
using Linguard.Core.Configuration;
using Linguard.Core.Managers;
using Linguard.Core.Models.Wireguard;
using Linguard.Core.OS;
Expand All @@ -24,7 +25,7 @@ public async Task CreateRandomClient() {
var commandName = command.GetAttribute<CommandAttribute>().Name!;
var app = Utils.BuildTestApp(command);
var iface = GenerateInterface(app.ConfigurationManager);
app.ConfigurationManager.Configuration.Wireguard.Interfaces.Add(iface);
app.ConfigurationManager.Configuration.GetModule<IWireguardConfiguration>()!.Interfaces.Add(iface);

var commandLine = $"{commandName} --interface {iface.Name}";

Expand All @@ -40,7 +41,7 @@ public async Task CreateClientWithName() {
var commandName = command.GetAttribute<CommandAttribute>().Name!;
var app = Utils.BuildTestApp(command);
var iface = GenerateInterface(app.ConfigurationManager);
app.ConfigurationManager.Configuration.Wireguard.Interfaces.Add(iface);
app.ConfigurationManager.Configuration.GetModule<IWireguardConfiguration>()!.Interfaces.Add(iface);

const string name = "bob";
var commandLine = $"{commandName} --interface {iface.Name} --name {name}";
Expand All @@ -58,7 +59,7 @@ public async Task CreateClientWithEndpoint() {
var commandName = command.GetAttribute<CommandAttribute>().Name!;
var app = Utils.BuildTestApp(command);
var iface = GenerateInterface(app.ConfigurationManager);
app.ConfigurationManager.Configuration.Wireguard.Interfaces.Add(iface);
app.ConfigurationManager.Configuration.GetModule<IWireguardConfiguration>()!.Interfaces.Add(iface);

const string endpoint = "vpn.example2.com";
var commandLine = $"{commandName} --interface {iface.Name} --endpoint {endpoint}";
Expand All @@ -76,7 +77,7 @@ public async Task CreateClientWithIPs() {
var commandName = command.GetAttribute<CommandAttribute>().Name!;
var app = Utils.BuildTestApp(command);
var iface = GenerateInterface(app.ConfigurationManager);
app.ConfigurationManager.Configuration.Wireguard.Interfaces.Add(iface);
app.ConfigurationManager.Configuration.GetModule<IWireguardConfiguration>()!.Interfaces.Add(iface);

var faker = new Faker();
var ipv4 = $"{faker.Internet.Ip()}/24";
Expand All @@ -98,7 +99,7 @@ public async Task CreateClientWithDns() {
var commandName = command.GetAttribute<CommandAttribute>().Name!;
var app = Utils.BuildTestApp(command);
var iface = GenerateInterface(app.ConfigurationManager);
app.ConfigurationManager.Configuration.Wireguard.Interfaces.Add(iface);
app.ConfigurationManager.Configuration.GetModule<IWireguardConfiguration>()!.Interfaces.Add(iface);

var dns = "my.dns.com";
var commandLine = $"{commandName} --interface {iface.Name} --dns1 {dns}";
Expand All @@ -118,7 +119,7 @@ public async Task CreateClientWithTwoDns() {
var commandName = command.GetAttribute<CommandAttribute>().Name!;
var app = Utils.BuildTestApp(command);
var iface = GenerateInterface(app.ConfigurationManager);
app.ConfigurationManager.Configuration.Wireguard.Interfaces.Add(iface);
app.ConfigurationManager.Configuration.GetModule<IWireguardConfiguration>()!.Interfaces.Add(iface);

var dns1 = "my.dns.com";
var dns2 = "8.8.8.8";
Expand All @@ -139,7 +140,7 @@ public async Task CreateClientWithAllowedIPs() {
var commandName = command.GetAttribute<CommandAttribute>().Name!;
var app = Utils.BuildTestApp(command);
var iface = GenerateInterface(app.ConfigurationManager);
app.ConfigurationManager.Configuration.Wireguard.Interfaces.Add(iface);
app.ConfigurationManager.Configuration.GetModule<IWireguardConfiguration>()!.Interfaces.Add(iface);

const string allowedIPs = "10.7.1.2/24 10.7.1.3/24 10.8.1.3/24";
var commandLine = $"{commandName} --interface {iface.Name} --allowedIps {allowedIPs}";
Expand Down
9 changes: 5 additions & 4 deletions Linguard/Cli.Test/AddInterfaceCommandShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Core.Test.Mocks;
using FluentAssertions;
using Linguard.Cli.Commands;
using Linguard.Core.Configuration;
using Typin.Attributes;
using Xunit;

Expand All @@ -21,7 +22,7 @@ public async Task CreateInterfaceWithNoArguments() {
await app.App.RunAsync(commandLine);
var errors = app.Error.GetString();
errors.Should().BeEmpty();
app.ConfigurationManager.Configuration.Wireguard.Interfaces.Should().NotBeEmpty();
app.ConfigurationManager.Configuration.GetModule<IWireguardConfiguration>()!.Interfaces.Should().NotBeEmpty();
}

[Fact]
Expand All @@ -36,7 +37,7 @@ public async Task CreateTwoInterfacesWithNoArguments() {
await app.App.RunAsync(commandLine);
var errors = app.Error.GetString();
errors.Should().BeEmpty();
app.ConfigurationManager.Configuration.Wireguard.Interfaces.Should().HaveCount(2);
app.ConfigurationManager.Configuration.GetModule<IWireguardConfiguration>()!.Interfaces.Should().HaveCount(2);
}

[Fact]
Expand All @@ -50,7 +51,7 @@ public async Task CreateInterfaceWithDefinedName() {
await app.App.RunAsync(commandLine);
var errors = app.Error.GetString();
errors.Should().BeEmpty();
app.ConfigurationManager.Configuration.Wireguard.Interfaces.Should().NotBeEmpty();
app.ConfigurationManager.Configuration.GetModule<IWireguardConfiguration>()!.Interfaces.Should().NotBeEmpty();
}

[Fact]
Expand All @@ -65,6 +66,6 @@ public async Task CreateInterfaceWithDefinedGateway() {
await app.App.RunAsync(commandLine);
var errors = app.Error.GetString();
errors.Should().BeEmpty();
app.ConfigurationManager.Configuration.Wireguard.Interfaces.Should().NotBeEmpty();
app.ConfigurationManager.Configuration.GetModule<IWireguardConfiguration>()!.Interfaces.Should().NotBeEmpty();
}
}
3 changes: 2 additions & 1 deletion Linguard/Cli.Test/AddInterfaceCommandShouldNot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using FluentAssertions;
using Linguard.Cli.Commands;
using Linguard.Core;
using Linguard.Core.Configuration;
using Linguard.Core.Models.Wireguard.Validators;
using Typin.Attributes;
using Xunit;
Expand Down Expand Up @@ -37,7 +38,7 @@ public async Task AddTwoInterfacesWithTheSameName() {
await app.App.RunAsync(commandLine);
var errors = app.Error.GetString();
errors.Should().Contain(Validation.InterfaceNameAlreadyInUse);
app.ConfigurationManager.Configuration.Wireguard.Interfaces.Should().HaveCount(1);
app.ConfigurationManager.Configuration.GetModule<IWireguardConfiguration>()!.Interfaces.Should().HaveCount(1);
}

[Fact]
Expand Down
7 changes: 4 additions & 3 deletions Linguard/Cli.Test/ListClientsCommandShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Castle.Core.Internal;
using FluentAssertions;
using Linguard.Cli.Commands;
using Linguard.Core.Configuration;
using Linguard.Core.Managers;
using Linguard.Core.Models.Wireguard;
using Linguard.Core.OS;
Expand All @@ -23,7 +24,7 @@ public async Task ListOnePeer() {
var commandName = command.GetAttribute<CommandAttribute>().Name!;
var app = Utils.BuildTestApp(command);
var iface = GenerateInterface(app.ConfigurationManager);
app.ConfigurationManager.Configuration.Wireguard.Interfaces.Add(iface);
app.ConfigurationManager.Configuration.GetModule<IWireguardConfiguration>()!.Interfaces.Add(iface);
var peer = GeneratePeer(app.ConfigurationManager, iface);
iface.Clients.Add(peer);

Expand All @@ -44,9 +45,9 @@ public async Task ListPeersForSpecificInterface() {
var app = Utils.BuildTestApp(command);

var iface1 = GenerateInterface(app.ConfigurationManager);
app.ConfigurationManager.Configuration.Wireguard.Interfaces.Add(iface1);
app.ConfigurationManager.Configuration.GetModule<IWireguardConfiguration>()!.Interfaces.Add(iface1);
var iface2 = GenerateInterface(app.ConfigurationManager);
app.ConfigurationManager.Configuration.Wireguard.Interfaces.Add(iface2);
app.ConfigurationManager.Configuration.GetModule<IWireguardConfiguration>()!.Interfaces.Add(iface2);
var peer = GeneratePeer(app.ConfigurationManager, iface1);
iface1.Clients.Add(peer);
iface2.Clients.Add(GeneratePeer(app.ConfigurationManager, iface2));
Expand Down
3 changes: 2 additions & 1 deletion Linguard/Cli.Test/ListInterfacesCommandShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Castle.Core.Internal;
using FluentAssertions;
using Linguard.Cli.Commands;
using Linguard.Core.Configuration;
using Linguard.Core.Managers;
using Linguard.Core.Models.Wireguard;
using Linguard.Core.OS;
Expand All @@ -23,7 +24,7 @@ public async Task ListOneInterface() {
var commandName = command.GetAttribute<CommandAttribute>().Name!;
var app = Utils.BuildTestApp(command);
var iface = GenerateInterface(app.ConfigurationManager);
app.ConfigurationManager.Configuration.Wireguard.Interfaces.Add(iface);
app.ConfigurationManager.Configuration.GetModule<IWireguardConfiguration>()!.Interfaces.Add(iface);

var commandLine = $"{commandName}";

Expand Down
3 changes: 2 additions & 1 deletion Linguard/Cli.Test/ShowInterfaceCommandShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Castle.Core.Internal;
using FluentAssertions;
using Linguard.Cli.Commands;
using Linguard.Core.Configuration;
using Linguard.Core.Managers;
using Linguard.Core.Models.Wireguard;
using Linguard.Core.OS;
Expand All @@ -23,7 +24,7 @@ public async Task ShowInterface() {
var commandName = command.GetAttribute<CommandAttribute>().Name!;
var app = Utils.BuildTestApp(command);
var iface = GenerateInterface(app.ConfigurationManager);
app.ConfigurationManager.Configuration.Wireguard.Interfaces.Add(iface);
app.ConfigurationManager.Configuration.GetModule<IWireguardConfiguration>()!.Interfaces.Add(iface);

var commandLine = $"{commandName} --name {iface.Name}";

Expand Down
1 change: 1 addition & 0 deletions Linguard/Cli/Cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

<ItemGroup>
<ProjectReference Include="..\Core\Core.csproj" />
<ProjectReference Include="..\Yaml\Yaml.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
7 changes: 4 additions & 3 deletions Linguard/Cli/CliStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Linguard.Core.OS;
using Linguard.Core.Services;
using Linguard.Log;
using Linguard.Yaml.Serialization;
using Microsoft.Extensions.DependencyInjection;
using Typin;
using Typin.Directives;
Expand All @@ -18,10 +19,10 @@ namespace Linguard.Cli;
public class CliStartup : ICliStartup {

public void ConfigureServices(IServiceCollection services) {
services.AddSingleton<IConfigurationManager, YamlConfigurationManager>();
services.AddTransient<IConfiguration, Configuration>();
services.AddSingleton<IConfigurationManager, Yaml.YamlConfigurationManager<IConfiguration>>();
services.AddTransient<IConfiguration, ConfigurationBase>();
services.AddTransient<IWorkingDirectory, WorkingDirectory>();
services.AddSingleton<IConfigurationSerializer>(DefaultYamlConfigurationSerializer.Instance);
services.AddSingleton(DefaultYamlConfigurationSerializer.Instance);
services.AddTransient<ISystemWrapper, SystemWrapper>();
services.AddTransient<IWireguardService, WireguardService>();
services.AddTransient<IInterfaceGenerator, DefaultInterfaceGenerator>();
Expand Down
3 changes: 2 additions & 1 deletion Linguard/Cli/Commands/AddClientCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public AddClientCommand(IConfigurationManager configurationManager, IClientGener
private readonly ILogger _logger;
private readonly IClientGenerator _generator;
private readonly IConfigurationManager _configurationManager;
private IWireguardConfiguration Configuration => _configurationManager.Configuration.Wireguard;
private IWireguardConfiguration Configuration
=> _configurationManager.Configuration.GetModule<IWireguardConfiguration>()!;

[CommandOption("name", Description = "Name of the peer.")]
public string? Name { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion Linguard/Cli/Commands/AddInterfaceCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public virtual ValueTask ExecuteAsync(IConsole console) {
if (!Validate(iface, console)) {
return ValueTask.CompletedTask;
}
Configuration.Wireguard.Interfaces.Add(iface);
Configuration.GetModule<IWireguardConfiguration>()!.Interfaces.Add(iface);
ConfigurationManager.Save();
var msg = $"Added interface '{iface.Name}'.";
Logger.LogInformation(msg);
Expand Down
3 changes: 2 additions & 1 deletion Linguard/Cli/Commands/EditClientCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public EditClientCommand(IConfigurationManager configurationManager,
private readonly AbstractValidator<Client> _validator;
private readonly ILogger _logger;
private readonly IConfigurationManager _configurationManager;
private IWireguardConfiguration Configuration => _configurationManager.Configuration.Wireguard;
private IWireguardConfiguration Configuration
=> _configurationManager.Configuration.GetModule<IWireguardConfiguration>()!;

[CommandOption("name", Description = "Current name of the client.", IsRequired = true)]
public string Name { get; set; }
Expand Down
4 changes: 3 additions & 1 deletion Linguard/Cli/Commands/EditInterfaceCommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using FluentValidation;
using Linguard.Core;
using Linguard.Core.Configuration;
using Linguard.Core.Managers;
using Linguard.Core.Models.Wireguard;
using Linguard.Core.Services;
Expand All @@ -17,7 +18,8 @@ public EditInterfaceCommand(IConfigurationManager configurationManager, ILogger
}

public override ValueTask ExecuteAsync(IConsole console) {
var iface = Configuration.Wireguard.Interfaces.SingleOrDefault(i => i.Name.Equals(Name));
var iface = Configuration.GetModule<IWireguardConfiguration>()!
.Interfaces.SingleOrDefault(i => i.Name.Equals(Name));
if (iface == default) {
Logger.LogError($"No interface named '{Name}' was found.");
console.Error.WriteLine(Validation.InterfaceNotFound);
Expand Down
3 changes: 2 additions & 1 deletion Linguard/Cli/Commands/ListClientsCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public ListClientsCommand(IConfigurationManager configurationManager) {
}

private readonly IConfigurationManager _configurationManager;
private IWireguardConfiguration Configuration => _configurationManager.Configuration.Wireguard;
private IWireguardConfiguration Configuration
=> _configurationManager.Configuration.GetModule<IWireguardConfiguration>()!;

[CommandOption("interface", Description = "Name of the client's interface.")]
public string? Interface { get; set; } = default;
Expand Down
3 changes: 2 additions & 1 deletion Linguard/Cli/Commands/ListInterfacesCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public ListInterfacesCommand(IConfigurationManager configurationManager) {
}

private readonly IConfigurationManager _configurationManager;
private IWireguardConfiguration Configuration => _configurationManager.Configuration.Wireguard;
private IWireguardConfiguration Configuration
=> _configurationManager.Configuration.GetModule<IWireguardConfiguration>()!;

public ValueTask ExecuteAsync(IConsole console) {
var interfaces = Configuration.Interfaces;
Expand Down
2 changes: 1 addition & 1 deletion Linguard/Cli/Commands/ShowClientCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public ShowClientCommand(IConfigurationManager configurationManager) {
}

public ValueTask ExecuteAsync(IConsole console) {
var peer = Configuration.Wireguard.Interfaces
var peer = Configuration.GetModule<IWireguardConfiguration>()!.Interfaces
.SingleOrDefault(i => i.Name.Equals(Interface))
?.Clients
.SingleOrDefault(c => c.Name.Equals(Name));
Expand Down
3 changes: 2 additions & 1 deletion Linguard/Cli/Commands/ShowInterfaceCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public ShowInterfacesCommand(IConfigurationManager configurationManager) {
}

public ValueTask ExecuteAsync(IConsole console) {
var iface = Configuration.Wireguard.Interfaces.SingleOrDefault(i => i.Name.Equals(Name));
var iface = Configuration.GetModule<IWireguardConfiguration>()!
.Interfaces.SingleOrDefault(i => i.Name.Equals(Name));
if (iface == default) {
console.Error.WriteLine(Validation.InterfaceNotFound);
return ValueTask.CompletedTask;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
using Linguard.Core.Drivers.TrafficStorage;
using Linguard.Core;
using Linguard.Core.Configuration;
using Linguard.Core.Configuration.Serialization;
using Linguard.Core.Drivers.TrafficStorage;
using Linguard.Core.Models.Wireguard;
using Linguard.Yaml.Serialization;
using YamlDotNet.Serialization.NamingConventions;
using UriTypeConverter = Linguard.Yaml.Serialization.UriTypeConverter;

namespace Linguard.Core.Configuration.Serialization;
namespace Linguard.Cli;

public static class DefaultYamlConfigurationSerializer {

public static YamlConfigurationSerializer Instance => new YamlConfigurationSerializerBuilder()
public static IConfigurationSerializer Instance => new YamlConfigurationSerializerBuilder()
.WithNamingConvention(PascalCaseNamingConvention.Instance)
.WithTypeConverter<IPAddressCidrTypeConverter>()
.WithTypeConverter<NetworkInterfaceTypeConverter>()
.WithTypeConverter<UriTypeConverter>()
.WithTypeMapping<IConfiguration, Configuration>()
.WithTypeMapping<IConfiguration, ConfigurationBase>()
.WithTypeMapping<IWireguardConfiguration, WireguardConfiguration>()
.WithTypeMapping<ILoggingConfiguration, LoggingConfiguration>()
.WithTypeMapping<IWebConfiguration, WebConfiguration>()
.WithTypeMapping<ITrafficConfiguration, TrafficConfiguration>()
.WithTypeMapping<ITrafficStorageDriver, JsonTrafficStorageDriver>()
.WithTypeMapping<ISet<Interface>, HashSet<Interface>>()
Expand Down
1 change: 1 addition & 0 deletions Linguard/Core.Test/Core.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

<ItemGroup>
<ProjectReference Include="..\Core\Core.csproj" />
<ProjectReference Include="..\Yaml\Yaml.csproj" />
</ItemGroup>

</Project>
19 changes: 11 additions & 8 deletions Linguard/Core.Test/Mocks/DefaultConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@ namespace Core.Test.Mocks;
public sealed class DefaultConfiguration : Mock<IConfiguration> {

public DefaultConfiguration() {
SetupProperty(c => c.Wireguard, GetWireguardConfigurationMock().Object);
SetupProperty(c => c.Logging, GetLoggingConfigurationMock().Object);
SetupProperty(c => c.Traffic, GetTrafficConfigurationMock().Object);
SetupProperty(c => c.Web, GetWebConfigurationMock().Object);
SetupProperty(c => c.Modules, new HashSet<IConfigurationModule> {
GetWireguardConfigurationMock().Object,
GetLoggingConfigurationMock().Object,
GetTrafficConfigurationMock().Object
});
Setup(o => o.Clone()).Returns(Object);
Setup(o => o.GetModule<IConfigurationModule>())
.Returns(new InvocationFunc(invocation => {
var type = invocation.Method.GetGenericArguments()[0];
return Object.Modules.SingleOrDefault(m
=> m.GetType() == type || m.GetType().GetInterface(type.Name) != default);
}));
}

private Mock<IWireguardConfiguration> GetWireguardConfigurationMock() {
Expand All @@ -41,10 +48,6 @@ private Mock<IWireguardConfiguration> GetWireguardConfigurationMock() {
return wireguardConfiguration;
}

private Mock<IWebConfiguration> GetWebConfigurationMock() {
return new Mock<IWebConfiguration>();
}

private Mock<ILoggingConfiguration> GetLoggingConfigurationMock() {
return new Mock<ILoggingConfiguration>();;
}
Expand Down
3 changes: 2 additions & 1 deletion Linguard/Core.Test/Mocks/WireguardServiceMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using Bogus;
using ByteSizeLib;
using Linguard.Core.Configuration;
using Linguard.Core.Managers;
using Linguard.Core.Models;
using Linguard.Core.Models.Wireguard;
Expand Down Expand Up @@ -43,7 +44,7 @@ public WireguardServiceMock(IConfigurationManager manager, ISystemWrapper system
Setup(o => o.GetTrafficData())
.Returns(() => {
var data = new List<TrafficData>();
foreach (var iface in manager.Configuration.Wireguard.Interfaces) {
foreach (var iface in manager.Configuration.GetModule<IWireguardConfiguration>()!.Interfaces) {
data.AddRange(Object.GetTrafficData(iface));
}
return data;
Expand Down
Loading

0 comments on commit f27c815

Please sign in to comment.