-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#115 refactor: added interface service to manage more specific interf…
…ace stuff the WireguardService was doing until now + Added guid to generators + Fixed some issues with mocks + Removed unused imports
- Loading branch information
1 parent
1b278c6
commit c8876fd
Showing
23 changed files
with
170 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,17 @@ | ||
using System.Net.NetworkInformation; | ||
using Bogus; | ||
using Moq; | ||
|
||
namespace Core.Test.Mocks; | ||
|
||
public class NetworkInterfaceMock : Mock<NetworkInterface> { | ||
public NetworkInterfaceMock(string name) { | ||
public NetworkInterfaceMock(string name, OperationalStatus status = OperationalStatus.Unknown) { | ||
SetupGet(i => i.Name).Returns(name); | ||
SetupGet(i => i.OperationalStatus).Returns(status); | ||
var properties = new Mock<IPInterfaceProperties>(); | ||
properties.SetupGet(o => o.UnicastAddresses) | ||
.Returns(new Mock<UnicastIPAddressInformationCollection>().Object); | ||
Setup(o => o.GetIPProperties()).Returns(properties.Object); | ||
Setup(o => o.GetPhysicalAddress()).Returns(new PhysicalAddress(new Faker().Random.Bytes(6))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 0 additions & 2 deletions
2
Linguard/Core/Configuration/Serialization/DefaultYamlConfigurationSerializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.Net.NetworkInformation; | ||
using Linguard.Core.Models.Wireguard; | ||
|
||
namespace Linguard.Core.Services; | ||
|
||
public interface IInterfaceService { | ||
IEnumerable<NetworkInterface> NetworkInterfaces { get; } | ||
Interface? GetInterface(Client client); | ||
bool IsInterfaceUp(Interface iface); | ||
bool IsInterfaceDown(Interface iface); | ||
void StartInterface(Interface @interface); | ||
void StopInterface(Interface @interface); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System.Net.NetworkInformation; | ||
using Linguard.Core.Configuration; | ||
using Linguard.Core.Managers; | ||
using Linguard.Core.Models.Wireguard; | ||
using Linguard.Core.OS; | ||
using Linguard.Core.Services.Exceptions; | ||
|
||
namespace Linguard.Core.Services; | ||
|
||
public class InterfaceService : IInterfaceService { | ||
|
||
private readonly IConfigurationManager _configurationManager; | ||
private readonly ICommandRunner _commandRunner; | ||
|
||
public InterfaceService(IConfigurationManager configurationManager, ICommandRunner commandRunner) { | ||
_configurationManager = configurationManager; | ||
_commandRunner = commandRunner; | ||
} | ||
|
||
private IWireguardConfiguration Configuration => _configurationManager.Configuration.Wireguard; | ||
|
||
public IEnumerable<NetworkInterface> NetworkInterfaces => NetworkInterface.GetAllNetworkInterfaces(); | ||
|
||
public Interface? GetInterface(Client client) => Configuration.Interfaces | ||
.SingleOrDefault(i => i.Clients.Contains(client)); | ||
|
||
public bool IsInterfaceUp(Interface iface) { | ||
return NetworkInterfaces | ||
.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 WireguardException(result.Stderr); | ||
} | ||
|
||
public void StopInterface(Interface @interface) { | ||
var result = _commandRunner | ||
.Run($"sudo {Configuration.WireguardQuickBin} down {@interface.Name}"); | ||
if (!result.Success) throw new WireguardException(result.Stderr); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.