-
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: improved configuration hierarchy
+ Generalized clone method for Configuration
- Loading branch information
1 parent
01e3ec1
commit 8af502d
Showing
5 changed files
with
118 additions
and
81 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,21 @@ | ||
namespace Linguard.Core.Configuration; | ||
|
||
public class Configuration : IConfiguration { | ||
|
||
public IWireguardConfiguration Wireguard { get; set; } = new WireguardConfiguration(); | ||
public ILoggingConfiguration Logging { get; set; } = new LoggingConfiguration(); | ||
public IWebConfiguration Web { get; set; } = new WebConfiguration(); | ||
public ITrafficConfiguration Traffic { get; set; } = new TrafficConfiguration(); | ||
|
||
public object Clone() { | ||
var clone = (IConfiguration) MemberwiseClone(); | ||
clone.Wireguard = (IWireguardConfiguration) Wireguard.Clone(); | ||
clone.Logging = (ILoggingConfiguration) Logging.Clone(); | ||
clone.Web = (IWebConfiguration) Web.Clone(); | ||
clone.Traffic = (ITrafficConfiguration) Traffic.Clone(); | ||
var cloneableProperties = GetType().GetProperties() | ||
.Where(p => p.PropertyType.GetInterfaces().Contains(typeof(ICloneable))); | ||
foreach (var property in cloneableProperties) { | ||
var propertyValue = property.GetValue(this); | ||
var propertyClone = propertyValue?.GetType().GetMethod(nameof(Clone)) | ||
!.Invoke(propertyValue, Array.Empty<object>()); | ||
property.SetValue(clone, propertyClone); | ||
} | ||
return clone; | ||
} | ||
} |
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,65 @@ | ||
using Linguard.Core.Configuration; | ||
using Linguard.Core.Drivers.TrafficStorage; | ||
using Linguard.Core.Models; | ||
using Linguard.Core.Models.Wireguard; | ||
using Linguard.Core.OS; | ||
using Linguard.Core.Utils; | ||
using Linguard.Log; | ||
|
||
namespace Linguard.Core.Managers; | ||
|
||
public abstract class ConfigurationManagerBase : IConfigurationManager { | ||
|
||
private readonly ICommandRunner _commandRunner; | ||
|
||
protected ConfigurationManagerBase(IConfiguration configuration, IWorkingDirectory workingDirectory, | ||
ICommandRunner commandRunner) { | ||
Configuration = configuration; | ||
WorkingDirectory = workingDirectory; | ||
_commandRunner = commandRunner; | ||
} | ||
|
||
public IConfiguration Configuration { get; set; } | ||
public IWorkingDirectory WorkingDirectory { get; set; } | ||
|
||
public void LoadDefaults() { | ||
LoadWebDefaults(); | ||
LoadLoggingDefaults(); | ||
LoadTrafficDefaults(); | ||
LoadWireguardDefaults(); | ||
} | ||
|
||
private void LoadWebDefaults() { | ||
Configuration.Web.Style = Style.Default; | ||
Configuration.Web.LoginAttempts = 10; | ||
Configuration.Web.SecretKey = ""; | ||
} | ||
private void LoadLoggingDefaults() { | ||
Configuration.Logging.Level = LogLevel.Info; | ||
Configuration.Logging.Overwrite = false; | ||
} | ||
private void LoadTrafficDefaults() { | ||
Configuration.Traffic.Enabled = true; | ||
Configuration.Traffic.StorageDriver = new JsonTrafficStorageDriver(); | ||
} | ||
private void LoadWireguardDefaults() { | ||
Configuration.Wireguard.Interfaces = new HashSet<Interface>(); | ||
Configuration.Wireguard.IptablesBin = _commandRunner | ||
.Run("whereis iptables | tr ' ' '\n' | grep bin").Stdout; | ||
Configuration.Wireguard.WireguardBin = _commandRunner | ||
.Run("whereis wg | tr ' ' '\n' | grep bin").Stdout; | ||
Configuration.Wireguard.WireguardQuickBin = _commandRunner | ||
.Run("whereis wg-quick | tr ' ' '\n' | grep bin").Stdout; | ||
Configuration.Wireguard.Interfaces = new(); | ||
Configuration.Wireguard.PrimaryDns = new("8.8.8.8", UriKind.RelativeOrAbsolute); | ||
Configuration.Wireguard.SecondaryDns = new("8.8.4.4", UriKind.RelativeOrAbsolute); | ||
var publicIp = Network.GetPublicIPAddress(); | ||
Configuration.Wireguard.Endpoint = publicIp == default | ||
? default | ||
: new(publicIp.ToString(), UriKind.RelativeOrAbsolute); | ||
} | ||
|
||
public abstract void Load(); | ||
public abstract void Save(); | ||
public abstract string Export(); | ||
} |
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,30 @@ | ||
using Linguard.Core.Configuration; | ||
using Linguard.Core.Configuration.Serialization; | ||
using Linguard.Core.OS; | ||
using Linguard.Core.Utils; | ||
|
||
namespace Linguard.Core.Managers; | ||
|
||
public abstract class DefaultFileConfigurationManager : FileConfigurationManager { | ||
|
||
protected DefaultFileConfigurationManager(IConfiguration configuration, IWorkingDirectory workingDirectory, | ||
ICommandRunner commandRunner, IConfigurationSerializer serializer) | ||
: base(configuration, workingDirectory, commandRunner, serializer) { | ||
} | ||
|
||
private FileInfo? _configurationFile; | ||
|
||
protected sealed override FileInfo ConfigurationFile { | ||
get { | ||
if (_configurationFile != default) return _configurationFile; | ||
var filename = Path.Combine(WorkingDirectory.BaseDirectory.FullName, AssemblyInfo.Product.ToLower()); | ||
var tries = 0; | ||
while (tries < SupportedExtensions.Length && _configurationFile is not { Exists: true }) { | ||
var filepath = Path.ChangeExtension(filename, SupportedExtensions[tries]); | ||
_configurationFile = new FileInfo(filepath); | ||
tries++; | ||
} | ||
return _configurationFile!; | ||
} | ||
} | ||
} |
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,75 +1,17 @@ | ||
using System.Net; | ||
using Linguard.Core.Configuration; | ||
using Linguard.Core.Configuration; | ||
using Linguard.Core.Configuration.Serialization; | ||
using Linguard.Core.Drivers.TrafficStorage; | ||
using Linguard.Core.Models; | ||
using Linguard.Core.Models.Wireguard; | ||
using Linguard.Core.OS; | ||
using Linguard.Core.Utils; | ||
using Linguard.Log; | ||
|
||
namespace Linguard.Core.Managers; | ||
|
||
public class YamlConfigurationManager : FileConfigurationManager { | ||
private static readonly string[] SupportedExtensions = {"yaml", "yml"}; | ||
public class YamlConfigurationManager : DefaultFileConfigurationManager { | ||
|
||
private FileInfo? _configurationFile; | ||
private readonly ICommandRunner _commandRunner; | ||
|
||
protected sealed override FileInfo ConfigurationFile { | ||
get { | ||
if (_configurationFile != default) return _configurationFile; | ||
var filename = Path.Combine(WorkingDirectory.BaseDirectory.FullName, AssemblyInfo.Product.ToLower()); | ||
var tries = 0; | ||
while (tries < SupportedExtensions.Length && _configurationFile is not { Exists: true }) { | ||
var filepath = Path.ChangeExtension(filename, SupportedExtensions[tries]); | ||
_configurationFile = new FileInfo(filepath); | ||
tries++; | ||
} | ||
return _configurationFile!; | ||
} | ||
} | ||
|
||
public override void LoadDefaults() { | ||
LoadWebDefaults(); | ||
LoadLoggingDefaults(); | ||
LoadTrafficDefaults(); | ||
LoadWireguardDefaults(); | ||
} | ||
|
||
private void LoadWebDefaults() { | ||
Configuration.Web.Style = Style.Default; | ||
Configuration.Web.LoginAttempts = 10; | ||
Configuration.Web.SecretKey = ""; | ||
} | ||
private void LoadLoggingDefaults() { | ||
Configuration.Logging.Level = LogLevel.Info; | ||
Configuration.Logging.Overwrite = false; | ||
} | ||
private void LoadTrafficDefaults() { | ||
Configuration.Traffic.Enabled = true; | ||
Configuration.Traffic.StorageDriver = new JsonTrafficStorageDriver(); | ||
} | ||
private void LoadWireguardDefaults() { | ||
Configuration.Wireguard.Interfaces = new HashSet<Interface>(); | ||
Configuration.Wireguard.IptablesBin = _commandRunner | ||
.Run("whereis iptables | tr ' ' '\n' | grep bin").Stdout; | ||
Configuration.Wireguard.WireguardBin = _commandRunner | ||
.Run("whereis wg | tr ' ' '\n' | grep bin").Stdout; | ||
Configuration.Wireguard.WireguardQuickBin = _commandRunner | ||
.Run("whereis wg-quick | tr ' ' '\n' | grep bin").Stdout; | ||
Configuration.Wireguard.Interfaces = new(); | ||
Configuration.Wireguard.PrimaryDns = new("8.8.8.8", UriKind.RelativeOrAbsolute); | ||
Configuration.Wireguard.SecondaryDns = new("8.8.4.4", UriKind.RelativeOrAbsolute); | ||
var publicIp = Network.GetPublicIPAddress(); | ||
Configuration.Wireguard.Endpoint = publicIp == default | ||
? default | ||
: new(publicIp.ToString(), UriKind.RelativeOrAbsolute); | ||
} | ||
public override string[] SupportedExtensions => new [] { | ||
"yaml", "yml" | ||
}; | ||
|
||
public YamlConfigurationManager(IConfiguration configuration, IWorkingDirectory workingDirectory, | ||
IConfigurationSerializer serializer, ICommandRunner commandRunner) | ||
: base(configuration, workingDirectory, serializer) { | ||
_commandRunner = commandRunner; | ||
ICommandRunner commandRunner, IConfigurationSerializer serializer) | ||
: base(configuration, workingDirectory, commandRunner, serializer) { | ||
} | ||
} |