diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-11-20 16:24:28 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-20 16:24:28 -0600 |
| commit | 951f447d7b7412ea54f16dda9641b884594740ff (patch) | |
| tree | 1bb6c425a782b4e748e51cfecd1a6cdca367c1b8 /src/slskd | |
| parent | 56339e50cc78e963b9634143af0986919cad4d43 (diff) | |
| parent | c9934c3a232d42d4199f1f284ed00a614803059c (diff) | |
| download | slskd-951f447d7b7412ea54f16dda9641b884594740ff.tar.gz | |
Merge branch 'master' into bump
Diffstat (limited to 'src/slskd')
| -rw-r--r-- | src/slskd/Common/Extensions.cs | 13 | ||||
| -rw-r--r-- | src/slskd/Common/ManagedState.cs | 2 | ||||
| -rw-r--r-- | src/slskd/Core/API/Controllers/ConfigurationController.cs | 10 | ||||
| -rw-r--r-- | src/slskd/Core/API/Controllers/OptionsController.cs | 81 | ||||
| -rw-r--r-- | src/slskd/Options.cs | 8 | ||||
| -rw-r--r-- | src/slskd/Program.cs | 8 | ||||
| -rw-r--r-- | src/slskd/slskd.csproj | 2 |
7 files changed, 116 insertions, 8 deletions
diff --git a/src/slskd/Common/Extensions.cs b/src/slskd/Common/Extensions.cs index 79314389..4d463092 100644 --- a/src/slskd/Common/Extensions.cs +++ b/src/slskd/Common/Extensions.cs @@ -226,6 +226,17 @@ namespace slskd .Serialize(obj); /// <summary> + /// Deserializes this string from yaml to an object of type <typeparamref name="T"/>. + /// </summary> + /// <typeparam name="T">The type to which to deserialize the string.</typeparam> + /// <param name="str">The string to deserialize.</param> + /// <returns>The new object deserialzied from the string.</returns> + public static T FromYaml<T>(this string str) => new DeserializerBuilder() + .WithNamingConvention(UnderscoredNamingConvention.Instance) + .Build() + .Deserialize<T>(str); + + /// <summary> /// Converts a fully qualified remote filename to a local filename based in the provided /// <paramref name="baseDirectory"/>, swapping directory characters for those specific to the local OS, removing any /// characters that are invalid for the local OS, and making the path relative to the remote store (including the @@ -277,7 +288,7 @@ namespace slskd /// <typeparam name="T">The type to which to deserialize the string.</typeparam> /// <param name="str">The string to deserialize.</param> /// <returns>The new object deserialzied from the string.</returns> - public static T ToObject<T>(this string str) => JsonSerializer.Deserialize<T>(str, GetJsonSerializerOptions()); + public static T FromJson<T>(this string str) => JsonSerializer.Deserialize<T>(str, GetJsonSerializerOptions()); private static JsonSerializerOptions GetJsonSerializerOptions() { diff --git a/src/slskd/Common/ManagedState.cs b/src/slskd/Common/ManagedState.cs index 26fbcb2a..bf6565db 100644 --- a/src/slskd/Common/ManagedState.cs +++ b/src/slskd/Common/ManagedState.cs @@ -184,7 +184,7 @@ namespace slskd { lock (Lock) { - var previous = CurrentValue.ToJson().ToObject<T>(); + var previous = CurrentValue.ToJson().FromJson<T>(); CurrentValue = setter(CurrentValue); Changed?.Invoke((previous, CurrentValue)); diff --git a/src/slskd/Core/API/Controllers/ConfigurationController.cs b/src/slskd/Core/API/Controllers/ConfigurationController.cs index 63b30a64..a7925174 100644 --- a/src/slskd/Core/API/Controllers/ConfigurationController.cs +++ b/src/slskd/Core/API/Controllers/ConfigurationController.cs @@ -19,8 +19,11 @@ using Microsoft.Extensions.Options; namespace slskd.Core.API { + using System; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; + using Serilog; + using IOFile = System.IO.File; /// <summary> /// Configuration. @@ -38,20 +41,21 @@ namespace slskd.Core.API IStateMonitor<State> applicationStateMonitor) { Application = application; - OptionsShapshot = optionsShapshot; + OptionsSnapshot = optionsShapshot; ApplicationStateMonitor = applicationStateMonitor; } private IApplication Application { get; } - private IOptionsSnapshot<Options> OptionsShapshot { get; } + private IOptionsSnapshot<Options> OptionsSnapshot { get; } private IStateMonitor<State> ApplicationStateMonitor { get; } + private ILogger Logger { get; set; } = Log.ForContext(typeof(Program)); [HttpGet] [Route("")] [Authorize] public IActionResult GetOptions() { - return Ok(OptionsShapshot.Value); + return Ok(OptionsSnapshot.Value); } [HttpPut] diff --git a/src/slskd/Core/API/Controllers/OptionsController.cs b/src/slskd/Core/API/Controllers/OptionsController.cs index fb2057b9..b4626cb9 100644 --- a/src/slskd/Core/API/Controllers/OptionsController.cs +++ b/src/slskd/Core/API/Controllers/OptionsController.cs @@ -19,8 +19,12 @@ using Microsoft.Extensions.Options; namespace slskd.Core.API { + using System; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; + using Serilog; + using slskd.Validation; + using IOFile = System.IO.File; /// <summary> /// Options. @@ -42,6 +46,7 @@ namespace slskd.Core.API private IOptionsSnapshot<Options> OptionsSnapshot { get; } private OptionsAtStartup OptionsAtStartup { get; } + private ILogger Logger { get; } = Log.ForContext(typeof(OptionsController)); /// <summary> /// Gets the current application options. @@ -67,5 +72,81 @@ namespace slskd.Core.API { return Ok(OptionsAtStartup); } + + [HttpGet] + [Authorize] + [Route("yaml")] + public IActionResult GetYamlFile() + { + if (!OptionsSnapshot.Value.RemoteConfiguration) + { + return Forbid(); + } + + var yaml = IOFile.ReadAllText(Program.ConfigurationFile); + return Ok(yaml); + } + + [HttpPost] + [Authorize] + [Route("yaml")] + public IActionResult UpdateYamlFile([FromBody] string yaml) + { + if (!OptionsSnapshot.Value.RemoteConfiguration) + { + return Forbid(); + } + + if (!TryValidateYaml(yaml, out var error)) + { + Logger.Error(error, "Failed to validate YAML configuration"); + return BadRequest(error); + } + + IOFile.WriteAllText(Program.ConfigurationFile, yaml); + return Ok(); + } + + [HttpPost] + [Authorize] + [Route("yaml/validate")] + public IActionResult ValidateYamlFile([FromBody] string yaml) + { + if (!TryValidateYaml(yaml, out var error)) + { + return Ok(error); + } + + return Ok(); + } + + private bool TryValidateYaml(string yaml, out string error) + { + error = null; + + try + { + var options = yaml.FromYaml<Options>(); + + if (!options.TryValidate(out var result)) + { + error = result.GetResultView(); + return false; + } + } + catch (Exception ex) + { + error = ex.Message; + + if (ex.InnerException != null) + { + error += $": {ex.InnerException.Message}"; + } + + return false; + } + + return true; + } } } diff --git a/src/slskd/Options.cs b/src/slskd/Options.cs index 0d487cc2..84b160c0 100644 --- a/src/slskd/Options.cs +++ b/src/slskd/Options.cs @@ -178,6 +178,14 @@ namespace slskd public bool NoVersionCheck { get; private set; } = false; /// <summary> + /// Gets a value indicating whether remote configuration of options is allowed. + /// </summary> + [Argument(default, "remote-configuration")] + [EnvironmentVariable("REMOTE_CONFIGURATION")] + [Description("allow remote configuration")] + public bool RemoteConfiguration { get; private set; } = false; + + /// <summary> /// Gets the unique name for this instance. /// </summary> [Argument('i', "instance-name")] diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 2403dbbf..c70db953 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -122,6 +122,11 @@ namespace slskd public static string AppDirectory { get; private set; } = DefaultAppDirectory; /// <summary> + /// Gets the fully qualified path to the application configuration file. + /// </summary> + public static string ConfigurationFile => Path.Combine(AppDirectory, $"{AppName}.yml"); + + /// <summary> /// Gets the default downloads directory. /// </summary> public static string DefaultDownloadsDirectory => Path.Combine(AppDirectory, "downloads"); @@ -134,10 +139,9 @@ namespace slskd /// <summary> /// Gets a buffer containing the last few log events. /// </summary> - public static ConcurrentFixedSizeQueue<LogRecord> LogBuffer { get; } = new ConcurrentFixedSizeQueue<LogRecord>(size: 500); + public static ConcurrentFixedSizeQueue<LogRecord> LogBuffer { get; } = new ConcurrentFixedSizeQueue<LogRecord>(size: 100); private static IConfigurationRoot Configuration { get; set; } - private static string ConfigurationFile => Path.Combine(AppDirectory, $"{AppName}.yml"); private static OptionsAtStartup OptionsAtStartup { get; } = new OptionsAtStartup(); [Argument('g', "generate-cert", "generate X509 certificate and password for HTTPs")] diff --git a/src/slskd/slskd.csproj b/src/slskd/slskd.csproj index 65f6845c..5de36e78 100644 --- a/src/slskd/slskd.csproj +++ b/src/slskd/slskd.csproj @@ -68,7 +68,7 @@ <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> </PackageReference> - <PackageReference Include="Soulseek" Version="3.0.0-pre-991113210753" /> + <PackageReference Include="Soulseek" Version="3.0.0" /> <PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.354"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> |
