From 5d0d9eff5b21c30563c125d54a1be88ca57b2398 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 24 Jan 2026 19:25:11 -0600 Subject: stub OptionsPatch --- src/slskd/Core/OptionsPatch.cs | 45 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/slskd/Core/OptionsPatch.cs diff --git a/src/slskd/Core/OptionsPatch.cs b/src/slskd/Core/OptionsPatch.cs new file mode 100644 index 00000000..636264a6 --- /dev/null +++ b/src/slskd/Core/OptionsPatch.cs @@ -0,0 +1,45 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// +namespace slskd +{ + using System; + using System.ComponentModel.DataAnnotations; + using slskd.Validation; + + public class OptionsPatch + { + public SoulseekOptionsPatch Soulseek { get; init; } = null; + + /// + /// Soulseek client options. + /// + public class SoulseekOptionsPatch + { + /// + /// Gets the local IP address on which to listen for incoming connections. + /// + [IPAddress] + public string? ListenIpAddress { get; init; } = "0.0.0.0"; + + /// + /// Gets the port on which to listen for incoming connections. + /// + [Range(1024, 65535)] + public int? ListenPort { get; init; } = 50300; + } + } +} \ No newline at end of file -- cgit v1.2.3 From 3834633fffc784bb06d4e69459c8c4748f0b6ae9 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 25 Jan 2026 18:46:13 -0600 Subject: document patch, add extension to validate --- src/slskd/Common/Validation/Extensions.cs | 20 ++++++++++++++++++++ src/slskd/Core/OptionsPatch.cs | 28 ++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/slskd/Common/Validation/Extensions.cs b/src/slskd/Common/Validation/Extensions.cs index b7e396a3..6233d53b 100644 --- a/src/slskd/Common/Validation/Extensions.cs +++ b/src/slskd/Common/Validation/Extensions.cs @@ -44,5 +44,25 @@ namespace slskd.Validation return true; } + + /// + /// Validates options. + /// + /// The options patch instance to validate. + /// The result of the validation, if invalid. + /// A value indicating whether the instance is valid. + public static bool TryValidate(this OptionsPatch patch, out CompositeValidationResult result) + { + result = null; + var results = new List(); + + if (!Validator.TryValidateObject(patch, new ValidationContext(patch), results, true)) + { + result = new CompositeValidationResult("Invalid configuration", results); + return false; + } + + return true; + } } } \ No newline at end of file diff --git a/src/slskd/Core/OptionsPatch.cs b/src/slskd/Core/OptionsPatch.cs index 636264a6..6eee75e3 100644 --- a/src/slskd/Core/OptionsPatch.cs +++ b/src/slskd/Core/OptionsPatch.cs @@ -20,10 +20,32 @@ namespace slskd using System.ComponentModel.DataAnnotations; using slskd.Validation; + /// + /// Run-time patch for application . + /// + /// + /// + /// The values specified in this patch are applied at run-time only, are volatile (lost when the application restarts), + /// and take precedence over all other options, regardless of which method was used to define them. + /// + /// + /// Only options that can be applied while the application is running can be patched, given the nature of how + /// a patch is applied. + /// + /// + /// Every property in this class must be nullable, and must have a null default value; the application + /// selectively applies the patch using only information explicitly supplied. + /// + /// public class OptionsPatch { + public static OptionsPatch Current { get; private set; } = new(); + + [Validate] public SoulseekOptionsPatch Soulseek { get; init; } = null; + public static void SetCurrent(OptionsPatch patch) => Current = patch; + /// /// Soulseek client options. /// @@ -33,13 +55,15 @@ namespace slskd /// Gets the local IP address on which to listen for incoming connections. /// [IPAddress] - public string? ListenIpAddress { get; init; } = "0.0.0.0"; +#pragma warning disable CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. + public string? ListenIpAddress { get; init; } = null; +#pragma warning restore CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. /// /// Gets the port on which to listen for incoming connections. /// [Range(1024, 65535)] - public int? ListenPort { get; init; } = 50300; + public int? ListenPort { get; init; } = null; } } } \ No newline at end of file -- cgit v1.2.3 From 385b54334209d06e2761a0c0f3c0b782065ce79c Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 25 Jan 2026 18:46:35 -0600 Subject: add patch endpoint for options --- .../Core/API/Controllers/OptionsController.cs | 41 +++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/slskd/Core/API/Controllers/OptionsController.cs b/src/slskd/Core/API/Controllers/OptionsController.cs index 25e9095a..f14acc39 100644 --- a/src/slskd/Core/API/Controllers/OptionsController.cs +++ b/src/slskd/Core/API/Controllers/OptionsController.cs @@ -22,6 +22,7 @@ namespace slskd.Core.API using System; using System.IO; using System.Reflection; + using System.Text.RegularExpressions; using Asp.Versioning; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -41,19 +42,22 @@ namespace slskd.Core.API public class OptionsController : ControllerBase { public OptionsController( + IApplication application, OptionsAtStartup optionsAtStartup, IOptionsSnapshot optionsSnapshot, IStateMutator stateMutator) { + Application = application; OptionsAtStartup = optionsAtStartup; OptionsSnapshot = optionsSnapshot; StateMutator = stateMutator; } + private IApplication Application { get; } private IOptionsSnapshot OptionsSnapshot { get; } private OptionsAtStartup OptionsAtStartup { get; } private IStateMutator StateMutator { get; } - private ILogger Logger { get; } = Log.ForContext(typeof(OptionsController)); + private ILogger Logger { get; } = Log.ForContext(); /// /// Gets the current application options. @@ -67,6 +71,41 @@ namespace slskd.Core.API return Ok(OptionsSnapshot.Value.Redact()); } + /// + /// Patch the application options with new values. + /// + /// The patch to apply. + /// + [HttpPatch] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(Options), 200)] + public IActionResult Patch([FromBody] OptionsPatch patch) + { + if (patch is null) + { + return NoContent(); + } + + if (!patch.TryValidate(out var result)) + { + var error = Regex.Replace(result.GetResultView().ToString(), @"\s+", " ").Trim(); + Logger.Warning("Options patch validation failed: {Message}", error); + return BadRequest(error); + } + + try + { + Application.ApplyOptionsPatchAsync(patch); + } + catch (Exception ex) + { + Logger.Error(ex, "Failed to apply options patch: {Message}", ex.Message); + return StatusCode(500, $"Failed to apply options patch: {ex.Message}"); + } + + return Ok(OptionsSnapshot.Value.Redact()); + } + /// /// Gets the application options provided at startup. /// -- cgit v1.2.3 From 39e0600425bd7d6205dd12e610c5149738a2d3f9 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 25 Jan 2026 18:47:02 -0600 Subject: stub patch logic --- src/slskd/Application.cs | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index bed8aab2..3f4ae37f 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -56,6 +56,7 @@ namespace slskd { public Task CheckVersionAsync(); public void CollectGarbage(); + public Task ApplyOptionsPatchAsync(OptionsPatch patch); } public sealed class Application : IApplication @@ -228,6 +229,7 @@ namespace slskd private ConcurrentDictionary Loggers { get; } = new ConcurrentDictionary(); private Options Options => OptionsMonitor.CurrentValue; private OptionsAtStartup OptionsAtStartup { get; set; } + private OptionsPatch OptionsPatch { get; set; } private IOptionsMonitor OptionsMonitor { get; set; } private SemaphoreSlim OptionsSyncRoot { get; } = new SemaphoreSlim(1, 1); private SemaphoreSlim GlobalEnqueueSemaphore { get; } = new SemaphoreSlim(10, 10); @@ -315,6 +317,8 @@ namespace slskd } } + public Task ApplyOptionsPatchAsync(OptionsPatch patch) => PatchOptions(patch); + async Task IHostedService.StartAsync(CancellationToken cancellationToken) { Log.Information("Application started"); @@ -1391,7 +1395,26 @@ namespace slskd } } - private async Task OptionsMonitor_OnChange(Options newOptions) + private Task OptionsMonitor_OnChange(Options newOptions) + { + return UpdateOptions(newOptions); + } + + private async Task PatchOptions(OptionsPatch patch) + { + await OptionsSyncRoot.WaitAsync(); + + try + { + OptionsPatch = patch; + } + finally + { + OptionsSyncRoot.Release(); + } + } + + private async Task UpdateOptions(Options newOptions) { // this code is known to fire more than once per update. i'm not sure whether these might be executed concurrently. // lock to be safe, because we need to accurately track the last value of Options for diffing purposes. threading @@ -1520,8 +1543,29 @@ namespace slskd inactivityTimeout: connection.Timeout.Transfer); } + /* + apply options from the run-time options patch + + if options are present (are not null), they take precedent over any other options, regardless of + how they were defined (command line, envar, yaml) + + this needs to be done for every single property in OptionsPatch, or the next time options are updated + the value may be overwritten + */ + IPAddress listenIPAddress = !string.IsNullOrEmpty(OptionsPatch?.Soulseek?.ListenIpAddress) + ? IPAddress.Parse(OptionsPatch.Soulseek.ListenIpAddress) + : old.ListenIpAddress == update.ListenIpAddress + ? null + : IPAddress.Parse(update.ListenIpAddress); + + int? listenPort = (OptionsPatch?.Soulseek?.ListenPort.HasValue ?? false) + ? OptionsPatch.Soulseek.ListenPort.Value + : old.ListenPort == update.ListenPort + ? null + : update.ListenPort; + var patch = new SoulseekClientOptionsPatch( - listenIPAddress: old.ListenIpAddress == update.ListenIpAddress ? null : IPAddress.Parse(update.ListenIpAddress), + listenIPAddress: listenIPAddress, listenPort: old.ListenPort == update.ListenPort ? null : update.ListenPort, enableDistributedNetwork: old.DistributedNetwork.Disabled == update.DistributedNetwork.Disabled ? null : !update.DistributedNetwork.Disabled, distributedChildLimit: old.DistributedNetwork.ChildLimit == update.DistributedNetwork.ChildLimit ? null : update.DistributedNetwork.ChildLimit, -- cgit v1.2.3 From b66ccf8ffa201c7acaaba5f4e512de3e1ddc694a Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 25 Jan 2026 18:53:00 -0600 Subject: stub a configuration source --- .../RuntimePatchConfigurationSource.cs | 152 +++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 src/slskd/Common/Configuration/RuntimePatchConfigurationSource.cs diff --git a/src/slskd/Common/Configuration/RuntimePatchConfigurationSource.cs b/src/slskd/Common/Configuration/RuntimePatchConfigurationSource.cs new file mode 100644 index 00000000..84dd8b35 --- /dev/null +++ b/src/slskd/Common/Configuration/RuntimePatchConfigurationSource.cs @@ -0,0 +1,152 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +namespace slskd.Configuration +{ + using System; + using System.Collections.Generic; + using System.Reflection; + using Microsoft.Extensions.Configuration; + + /// + /// Extension methods for adding . + /// + public static class RuntimePatchConfigurationExtensions + { + /// + /// Adds a runtime patch configuration source to . + /// + /// The to which to add. + /// Configures the source. + /// The updated . + public static IConfigurationBuilder AddRuntimePatch( + this IConfigurationBuilder builder, + Action configureSource) + => builder.Add(configureSource); + } + + /// + /// A runtime patch . + /// + public class RuntimePatchConfigurationProvider : ConfigurationProvider + { + /// + /// Initializes a new instance of the class. + /// + /// The source settings. + public RuntimePatchConfigurationProvider(RuntimePatchConfigurationSource source) + { + Source = source; + source.Provider = this; + } + + private RuntimePatchConfigurationSource Source { get; } + + /// + /// Loads patch values from the current and maps them to the corresponding keys. + /// + public override void Load() + { + var data = new Dictionary(StringComparer.OrdinalIgnoreCase); + + if (Source.Patch != null) + { + Flatten(Source.Patch, "slskd", data); + } + + Data = data; + } + + /// + /// Applies the specified and triggers a configuration reload. + /// + /// The patch to apply. + public void ApplyPatch(OptionsPatch patch) + { + Source.Patch = patch; + Load(); + OnReload(); + } + + private void Flatten(object obj, string path, Dictionary data) + { + if (obj == null) + { + return; + } + + var type = obj.GetType(); + var props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); + + foreach (var property in props) + { + var value = property.GetValue(obj); + + if (value == null) + { + continue; + } + + var key = ConfigurationPath.Combine(path, property.Name.ToLowerInvariant()); + + if (property.PropertyType.IsClass && property.PropertyType != typeof(string)) + { + Flatten(value, key, data); + } + else + { + data[key] = value.ToString(); + } + } + } + } + + /// + /// Represents a runtime patch as an . + /// + public class RuntimePatchConfigurationSource : IConfigurationSource + { + /// + /// Gets or sets the provider instance. + /// + internal RuntimePatchConfigurationProvider Provider { get; set; } + + /// + /// Gets or sets the current patch. + /// + internal OptionsPatch Patch { get; set; } + + /// + /// Builds the for this source. + /// + /// The . + /// A . + public IConfigurationProvider Build(IConfigurationBuilder builder) + { + return new RuntimePatchConfigurationProvider(this); + } + + /// + /// Applies the specified and triggers a configuration reload. + /// + /// The patch to apply. + public void ApplyPatch(OptionsPatch patch) + { + Provider?.ApplyPatch(patch); + } + } +} -- cgit v1.2.3 From dab94cbdfe2b9db21a82f447c49ba2cc46800bbb Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 27 Jan 2026 19:47:57 -0600 Subject: revert application changes --- src/slskd/Application.cs | 48 ++---------------------------------------------- 1 file changed, 2 insertions(+), 46 deletions(-) diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 3f4ae37f..bed8aab2 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -56,7 +56,6 @@ namespace slskd { public Task CheckVersionAsync(); public void CollectGarbage(); - public Task ApplyOptionsPatchAsync(OptionsPatch patch); } public sealed class Application : IApplication @@ -229,7 +228,6 @@ namespace slskd private ConcurrentDictionary Loggers { get; } = new ConcurrentDictionary(); private Options Options => OptionsMonitor.CurrentValue; private OptionsAtStartup OptionsAtStartup { get; set; } - private OptionsPatch OptionsPatch { get; set; } private IOptionsMonitor OptionsMonitor { get; set; } private SemaphoreSlim OptionsSyncRoot { get; } = new SemaphoreSlim(1, 1); private SemaphoreSlim GlobalEnqueueSemaphore { get; } = new SemaphoreSlim(10, 10); @@ -317,8 +315,6 @@ namespace slskd } } - public Task ApplyOptionsPatchAsync(OptionsPatch patch) => PatchOptions(patch); - async Task IHostedService.StartAsync(CancellationToken cancellationToken) { Log.Information("Application started"); @@ -1395,26 +1391,7 @@ namespace slskd } } - private Task OptionsMonitor_OnChange(Options newOptions) - { - return UpdateOptions(newOptions); - } - - private async Task PatchOptions(OptionsPatch patch) - { - await OptionsSyncRoot.WaitAsync(); - - try - { - OptionsPatch = patch; - } - finally - { - OptionsSyncRoot.Release(); - } - } - - private async Task UpdateOptions(Options newOptions) + private async Task OptionsMonitor_OnChange(Options newOptions) { // this code is known to fire more than once per update. i'm not sure whether these might be executed concurrently. // lock to be safe, because we need to accurately track the last value of Options for diffing purposes. threading @@ -1543,29 +1520,8 @@ namespace slskd inactivityTimeout: connection.Timeout.Transfer); } - /* - apply options from the run-time options patch - - if options are present (are not null), they take precedent over any other options, regardless of - how they were defined (command line, envar, yaml) - - this needs to be done for every single property in OptionsPatch, or the next time options are updated - the value may be overwritten - */ - IPAddress listenIPAddress = !string.IsNullOrEmpty(OptionsPatch?.Soulseek?.ListenIpAddress) - ? IPAddress.Parse(OptionsPatch.Soulseek.ListenIpAddress) - : old.ListenIpAddress == update.ListenIpAddress - ? null - : IPAddress.Parse(update.ListenIpAddress); - - int? listenPort = (OptionsPatch?.Soulseek?.ListenPort.HasValue ?? false) - ? OptionsPatch.Soulseek.ListenPort.Value - : old.ListenPort == update.ListenPort - ? null - : update.ListenPort; - var patch = new SoulseekClientOptionsPatch( - listenIPAddress: listenIPAddress, + listenIPAddress: old.ListenIpAddress == update.ListenIpAddress ? null : IPAddress.Parse(update.ListenIpAddress), listenPort: old.ListenPort == update.ListenPort ? null : update.ListenPort, enableDistributedNetwork: old.DistributedNetwork.Disabled == update.DistributedNetwork.Disabled ? null : !update.DistributedNetwork.Disabled, distributedChildLimit: old.DistributedNetwork.ChildLimit == update.DistributedNetwork.ChildLimit ? null : update.DistributedNetwork.ChildLimit, -- cgit v1.2.3 From 8859b9a6c82b1490349f7ffb4f6699b502384f8a Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 27 Jan 2026 19:48:25 -0600 Subject: call it an overlay --- src/slskd/Common/Validation/Extensions.cs | 6 +- .../Core/API/Controllers/OptionsController.cs | 4 +- src/slskd/Core/OptionsOverlay.cs | 66 +++++++++++++++++++++ src/slskd/Core/OptionsPatch.cs | 69 ---------------------- 4 files changed, 71 insertions(+), 74 deletions(-) create mode 100644 src/slskd/Core/OptionsOverlay.cs delete mode 100644 src/slskd/Core/OptionsPatch.cs diff --git a/src/slskd/Common/Validation/Extensions.cs b/src/slskd/Common/Validation/Extensions.cs index 6233d53b..87d83965 100644 --- a/src/slskd/Common/Validation/Extensions.cs +++ b/src/slskd/Common/Validation/Extensions.cs @@ -48,15 +48,15 @@ namespace slskd.Validation /// /// Validates options. /// - /// The options patch instance to validate. + /// The options overlay instance to validate. /// The result of the validation, if invalid. /// A value indicating whether the instance is valid. - public static bool TryValidate(this OptionsPatch patch, out CompositeValidationResult result) + public static bool TryValidate(this OptionsOverlay overlay, out CompositeValidationResult result) { result = null; var results = new List(); - if (!Validator.TryValidateObject(patch, new ValidationContext(patch), results, true)) + if (!Validator.TryValidateObject(overlay, new ValidationContext(overlay), results, true)) { result = new CompositeValidationResult("Invalid configuration", results); return false; diff --git a/src/slskd/Core/API/Controllers/OptionsController.cs b/src/slskd/Core/API/Controllers/OptionsController.cs index f14acc39..291ae561 100644 --- a/src/slskd/Core/API/Controllers/OptionsController.cs +++ b/src/slskd/Core/API/Controllers/OptionsController.cs @@ -79,7 +79,7 @@ namespace slskd.Core.API [HttpPatch] [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(Options), 200)] - public IActionResult Patch([FromBody] OptionsPatch patch) + public IActionResult Patch([FromBody] OptionsOverlay patch) { if (patch is null) { @@ -95,7 +95,7 @@ namespace slskd.Core.API try { - Application.ApplyOptionsPatchAsync(patch); + Program.ApplyConfigurationOverlay(patch); } catch (Exception ex) { diff --git a/src/slskd/Core/OptionsOverlay.cs b/src/slskd/Core/OptionsOverlay.cs new file mode 100644 index 00000000..faea4ea7 --- /dev/null +++ b/src/slskd/Core/OptionsOverlay.cs @@ -0,0 +1,66 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// +namespace slskd +{ + using System; + using System.ComponentModel.DataAnnotations; + using slskd.Validation; + + /// + /// Volatile run-time overlay for application . + /// + /// + /// + /// The values specified in this overlay are applied at run-time only, are volatile (lost when the application restarts), + /// and take precedence over all other options, regardless of which method was used to define them. + /// + /// + /// Only options that can be applied while the application is running can be overlaid, given the nature of how + /// an overlay is applied. + /// + /// + /// Every property in this class must be nullable, and must have a null default value; the application + /// selectively applies the patch overlay only information explicitly supplied. + /// + /// + public class OptionsOverlay + { + /// + /// Gets options for the Soulseek client. + /// + [Validate] + public SoulseekOptionsPatch Soulseek { get; init; } = null; + + /// + /// Soulseek client options. + /// + public class SoulseekOptionsPatch + { + /// + /// Gets the local IP address on which to listen for incoming connections. + /// + [IPAddress] + public string ListenIpAddress { get; init; } = null; + + /// + /// Gets the port on which to listen for incoming connections. + /// + [Range(1024, 65535)] + public int? ListenPort { get; init; } = null; + } + } +} \ No newline at end of file diff --git a/src/slskd/Core/OptionsPatch.cs b/src/slskd/Core/OptionsPatch.cs deleted file mode 100644 index 6eee75e3..00000000 --- a/src/slskd/Core/OptionsPatch.cs +++ /dev/null @@ -1,69 +0,0 @@ -// -// Copyright (c) slskd Team. All rights reserved. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published -// by the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see https://www.gnu.org/licenses/. -// -namespace slskd -{ - using System; - using System.ComponentModel.DataAnnotations; - using slskd.Validation; - - /// - /// Run-time patch for application . - /// - /// - /// - /// The values specified in this patch are applied at run-time only, are volatile (lost when the application restarts), - /// and take precedence over all other options, regardless of which method was used to define them. - /// - /// - /// Only options that can be applied while the application is running can be patched, given the nature of how - /// a patch is applied. - /// - /// - /// Every property in this class must be nullable, and must have a null default value; the application - /// selectively applies the patch using only information explicitly supplied. - /// - /// - public class OptionsPatch - { - public static OptionsPatch Current { get; private set; } = new(); - - [Validate] - public SoulseekOptionsPatch Soulseek { get; init; } = null; - - public static void SetCurrent(OptionsPatch patch) => Current = patch; - - /// - /// Soulseek client options. - /// - public class SoulseekOptionsPatch - { - /// - /// Gets the local IP address on which to listen for incoming connections. - /// - [IPAddress] -#pragma warning disable CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. - public string? ListenIpAddress { get; init; } = null; -#pragma warning restore CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. - - /// - /// Gets the port on which to listen for incoming connections. - /// - [Range(1024, 65535)] - public int? ListenPort { get; init; } = null; - } - } -} \ No newline at end of file -- cgit v1.2.3 From e8e3a4aa80fbcca9cf8c841c94ff73396c662352 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 27 Jan 2026 19:48:55 -0600 Subject: wip --- .../RuntimePatchConfigurationSource.cs | 152 --------------------- .../VolatileOverlayConfigurationSource.cs | 127 +++++++++++++++++ src/slskd/Program.cs | 6 +- 3 files changed, 132 insertions(+), 153 deletions(-) delete mode 100644 src/slskd/Common/Configuration/RuntimePatchConfigurationSource.cs create mode 100644 src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs diff --git a/src/slskd/Common/Configuration/RuntimePatchConfigurationSource.cs b/src/slskd/Common/Configuration/RuntimePatchConfigurationSource.cs deleted file mode 100644 index 84dd8b35..00000000 --- a/src/slskd/Common/Configuration/RuntimePatchConfigurationSource.cs +++ /dev/null @@ -1,152 +0,0 @@ -// -// Copyright (c) slskd Team. All rights reserved. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published -// by the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see https://www.gnu.org/licenses/. -// - -namespace slskd.Configuration -{ - using System; - using System.Collections.Generic; - using System.Reflection; - using Microsoft.Extensions.Configuration; - - /// - /// Extension methods for adding . - /// - public static class RuntimePatchConfigurationExtensions - { - /// - /// Adds a runtime patch configuration source to . - /// - /// The to which to add. - /// Configures the source. - /// The updated . - public static IConfigurationBuilder AddRuntimePatch( - this IConfigurationBuilder builder, - Action configureSource) - => builder.Add(configureSource); - } - - /// - /// A runtime patch . - /// - public class RuntimePatchConfigurationProvider : ConfigurationProvider - { - /// - /// Initializes a new instance of the class. - /// - /// The source settings. - public RuntimePatchConfigurationProvider(RuntimePatchConfigurationSource source) - { - Source = source; - source.Provider = this; - } - - private RuntimePatchConfigurationSource Source { get; } - - /// - /// Loads patch values from the current and maps them to the corresponding keys. - /// - public override void Load() - { - var data = new Dictionary(StringComparer.OrdinalIgnoreCase); - - if (Source.Patch != null) - { - Flatten(Source.Patch, "slskd", data); - } - - Data = data; - } - - /// - /// Applies the specified and triggers a configuration reload. - /// - /// The patch to apply. - public void ApplyPatch(OptionsPatch patch) - { - Source.Patch = patch; - Load(); - OnReload(); - } - - private void Flatten(object obj, string path, Dictionary data) - { - if (obj == null) - { - return; - } - - var type = obj.GetType(); - var props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); - - foreach (var property in props) - { - var value = property.GetValue(obj); - - if (value == null) - { - continue; - } - - var key = ConfigurationPath.Combine(path, property.Name.ToLowerInvariant()); - - if (property.PropertyType.IsClass && property.PropertyType != typeof(string)) - { - Flatten(value, key, data); - } - else - { - data[key] = value.ToString(); - } - } - } - } - - /// - /// Represents a runtime patch as an . - /// - public class RuntimePatchConfigurationSource : IConfigurationSource - { - /// - /// Gets or sets the provider instance. - /// - internal RuntimePatchConfigurationProvider Provider { get; set; } - - /// - /// Gets or sets the current patch. - /// - internal OptionsPatch Patch { get; set; } - - /// - /// Builds the for this source. - /// - /// The . - /// A . - public IConfigurationProvider Build(IConfigurationBuilder builder) - { - return new RuntimePatchConfigurationProvider(this); - } - - /// - /// Applies the specified and triggers a configuration reload. - /// - /// The patch to apply. - public void ApplyPatch(OptionsPatch patch) - { - Provider?.ApplyPatch(patch); - } - } -} diff --git a/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs b/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs new file mode 100644 index 00000000..30328292 --- /dev/null +++ b/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs @@ -0,0 +1,127 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +namespace slskd.Configuration +{ + using System; + using System.Linq; + using System.Reflection; + using System.Text.Json; + using Microsoft.Extensions.Configuration; + + /// + /// A runtime patch . + /// + public class VolatileOverlayConfigurationProvider : ConfigurationProvider + { + // /// + // /// Initializes a new instance of the class. + // /// + // /// The source settings. + // public VolatileOverlayConfigurationProvider(VolatileOverlayConfigurationSource source) + // { + // TargetType = source.TargetType; + // Namespace = TargetType.Namespace.Split('.').First(); + // } + public VolatileOverlayConfigurationProvider() + { + TargetType = typeof(T); + Namespace = TargetType.Namespace.Split('.').First(); + } + + private T Current { get; set; } + private string Namespace { get; set; } + private Type TargetType { get; set; } + + public override void Load() + { + if (Current is null) + { + return; + } + + void Map(Type type, string path) + { + var props = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); + + foreach (PropertyInfo property in props) + { + var key = ConfigurationPath.Combine(path, property.Name.ToLowerInvariant()); + + if (property.PropertyType.Namespace.StartsWith(Namespace)) + { + Map(property.PropertyType, key); + } + else + { + // don't add array values to the configuration; these are additive across providers + // and the default value from the class is "stuck", so adding them again results in duplicates. + if (!property.PropertyType.IsArray) + { + var value = property.GetValue(Current); + + if (value != null) + { + Data[key] = value.ToString(); + } + } + else + { + // serialize array defaults and stick them on the parent key + // (not indexed by array position). this value is "stuck", and + // we want to show that in the config debug view. this isn't really + // functional, just illustrative. + Data[key] = JsonSerializer.Serialize(property.GetValue(Current)); + } + } + } + } + + Map(TargetType, Namespace); + + Console.WriteLine("load called!"); + // Data["slskd:soulseek:listenport"] = Current.Soulseek.ListenPort.ToString(); + + // todo: take the current patch value and stuff the options into Data[key] + Console.WriteLine(Data.ToJson()); + } + + public void Apply(T overlay) + { + Current = overlay; + Load(); + OnReload(); + } + } + + public class VolatileOverlayConfigurationSource : IConfigurationSource + { + public VolatileOverlayConfigurationSource() + { + Provider = new VolatileOverlayConfigurationProvider(); + } + + private VolatileOverlayConfigurationProvider Provider { get; set; } + + public void Apply(T overlay) => Provider.Apply(overlay); + + public IConfigurationProvider Build(IConfigurationBuilder builder) + { + return Provider; + } + } +} diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index d6873dc0..16cfe6c9 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -244,6 +244,7 @@ namespace slskd private static ILogger Log { get; set; } = new ConsoleWriteLineLogger(); private static Mutex Mutex { get; set; } private static IDisposable DotNetRuntimeStats { get; set; } + private static VolatileOverlayConfigurationSource VolatileOverlayConfigurationSource { get; set; } = new VolatileOverlayConfigurationSource(); [Argument('g', "generate-cert", "generate X509 certificate and password for HTTPs")] private static bool GenerateCertificate { get; set; } @@ -269,6 +270,8 @@ namespace slskd /// An optional exit code. public static void Exit(int code = 1) => Environment.Exit(code); + public static void ApplyConfigurationOverlay(OptionsOverlay overlay) => VolatileOverlayConfigurationSource.Apply(overlay); + /// /// Entrypoint. /// @@ -1281,7 +1284,8 @@ namespace slskd .AddCommandLine( targetType: typeof(Options), multiValuedArguments, - commandLine: Environment.CommandLine); + commandLine: Environment.CommandLine) + .Add(VolatileOverlayConfigurationSource); } private static IServiceCollection AddDbContext(this IServiceCollection services, string connectionString) -- cgit v1.2.3 From 95197676a8712df1e30edece4f20ec39726d1257 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 27 Jan 2026 20:07:43 -0600 Subject: more wip --- .../VolatileOverlayConfigurationSource.cs | 25 +++++----------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs b/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs index 30328292..9ae9c9ef 100644 --- a/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs +++ b/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs @@ -28,15 +28,6 @@ namespace slskd.Configuration /// public class VolatileOverlayConfigurationProvider : ConfigurationProvider { - // /// - // /// Initializes a new instance of the class. - // /// - // /// The source settings. - // public VolatileOverlayConfigurationProvider(VolatileOverlayConfigurationSource source) - // { - // TargetType = source.TargetType; - // Namespace = TargetType.Namespace.Split('.').First(); - // } public VolatileOverlayConfigurationProvider() { TargetType = typeof(T); @@ -54,7 +45,7 @@ namespace slskd.Configuration return; } - void Map(Type type, string path) + void Map(Type type, string path, object instance) { var props = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); @@ -64,7 +55,7 @@ namespace slskd.Configuration if (property.PropertyType.Namespace.StartsWith(Namespace)) { - Map(property.PropertyType, key); + Map(property.PropertyType, key, property.GetValue(instance)); } else { @@ -72,7 +63,7 @@ namespace slskd.Configuration // and the default value from the class is "stuck", so adding them again results in duplicates. if (!property.PropertyType.IsArray) { - var value = property.GetValue(Current); + var value = property.GetValue(instance); if (value != null) { @@ -85,19 +76,13 @@ namespace slskd.Configuration // (not indexed by array position). this value is "stuck", and // we want to show that in the config debug view. this isn't really // functional, just illustrative. - Data[key] = JsonSerializer.Serialize(property.GetValue(Current)); + Data[key] = JsonSerializer.Serialize(property.GetValue(instance)); } } } } - Map(TargetType, Namespace); - - Console.WriteLine("load called!"); - // Data["slskd:soulseek:listenport"] = Current.Soulseek.ListenPort.ToString(); - - // todo: take the current patch value and stuff the options into Data[key] - Console.WriteLine(Data.ToJson()); + Map(TargetType, Namespace, Current); } public void Apply(T overlay) -- cgit v1.2.3 From 5c1cdfd24f4344bbc011a69b69979bfd5695e9aa Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 29 Jan 2026 08:03:01 -0600 Subject: add redact for overlay --- src/slskd/Core/Extensions.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/slskd/Core/Extensions.cs b/src/slskd/Core/Extensions.cs index db75cc9c..707daa88 100644 --- a/src/slskd/Core/Extensions.cs +++ b/src/slskd/Core/Extensions.cs @@ -41,6 +41,21 @@ namespace slskd return redacted; } + /// + /// Redacts this instance of Options, replacing properties marked with with '*****'. + /// + /// + /// Creates a deep clone before redacting. + /// + /// The Options instance to redact. + /// A redacted instance. + public static OptionsOverlay Redact(this OptionsOverlay options) + { + var redacted = options.ToJson().FromJson(); + Redactor.Redact(redacted, redactWith: "*****"); + return redacted; + } + /// /// Creates a copy of this instance with the specified parameters changed. /// -- cgit v1.2.3 From 894458eaa695a44eed02e5c50190326428337cda Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 29 Jan 2026 08:03:27 -0600 Subject: docs and stuff --- .../DefaultValueConfigurationSource.cs | 1 + .../VolatileOverlayConfigurationSource.cs | 56 ++++++++++++++++++++-- 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs b/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs index 8cf62e91..f125b1fc 100644 --- a/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs +++ b/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs @@ -85,6 +85,7 @@ namespace slskd.Configuration /// public override void Load() { + // note: pretty much the same as VolatileOverlayConfigurationProvider; the two should be updated in lockstep void Map(Type type, string path) { var defaults = Activator.CreateInstance(type); diff --git a/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs b/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs index 9ae9c9ef..8707fe69 100644 --- a/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs +++ b/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs @@ -24,27 +24,44 @@ namespace slskd.Configuration using Microsoft.Extensions.Configuration; /// - /// A runtime patch . + /// A that provides a volatile, run-time overlay that supersedes other configuration + /// sources. /// + /// + /// To use this provider as intended, the application must instantiate and store an instance of either + /// or , and then invoke with the desired overlay object. + /// + /// The Type of the overlay. public class VolatileOverlayConfigurationProvider : ConfigurationProvider { + /// + /// Initializes a new instance of the class. + /// public VolatileOverlayConfigurationProvider() { TargetType = typeof(T); Namespace = TargetType.Namespace.Split('.').First(); } - private T Current { get; set; } + /// + /// Gets the current overlay value. + /// + public T CurrentValue { get; private set; } + private string Namespace { get; set; } private Type TargetType { get; set; } + /// + /// Loads values from the overlay. + /// public override void Load() { - if (Current is null) + if (CurrentValue is null) { return; } + // note: pretty much the same as DefaultValueConfigurationProvider; the two should be updated in lockstep void Map(Type type, string path, object instance) { var props = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); @@ -82,28 +99,57 @@ namespace slskd.Configuration } } - Map(TargetType, Namespace, Current); + Map(TargetType, Namespace, CurrentValue); } + /// + /// Applies the goven . + /// + /// An object containing the values to overlay. public void Apply(T overlay) { - Current = overlay; + CurrentValue = overlay; Load(); OnReload(); } } + /// + /// Represents values that are provided at run-time as an . + /// + /// + /// To use this provider as intended, the application must instantiate and store an instance of either + /// or , and then invoke with the desired overlay object. + /// + /// The Type of the overlay. public class VolatileOverlayConfigurationSource : IConfigurationSource { + /// + /// Initializes a new instance of the class. + /// public VolatileOverlayConfigurationSource() { Provider = new VolatileOverlayConfigurationProvider(); } + /// + /// Gets the current overlay value. + /// + public T CurrentValue => Provider.CurrentValue; + private VolatileOverlayConfigurationProvider Provider { get; set; } + /// + /// Applies the given to the underlying . + /// + /// An object containing the values to overlay. public void Apply(T overlay) => Provider.Apply(overlay); + /// + /// Builds the for this source. + /// + /// The . + /// A . public IConfigurationProvider Build(IConfigurationBuilder builder) { return Provider; -- cgit v1.2.3 From 94c6943c23e6b3c6cb6efb8a8ca45055b9153d58 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 29 Jan 2026 08:03:42 -0600 Subject: more docs --- src/slskd/Program.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 16cfe6c9..143cd460 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -270,6 +270,10 @@ namespace slskd /// An optional exit code. public static void Exit(int code = 1) => Environment.Exit(code); + /// + /// Apply an instance of on top of the existing application configuration. + /// + /// The overlay containing the property values to be overlaid. public static void ApplyConfigurationOverlay(OptionsOverlay overlay) => VolatileOverlayConfigurationSource.Apply(overlay); /// @@ -1285,7 +1289,7 @@ namespace slskd targetType: typeof(Options), multiValuedArguments, commandLine: Environment.CommandLine) - .Add(VolatileOverlayConfigurationSource); + .Add(VolatileOverlayConfigurationSource); // this must come last in order to supersede all other sources } private static IServiceCollection AddDbContext(this IServiceCollection services, string connectionString) -- cgit v1.2.3 From a631b5fbeaaf4e21c0a0a3c266d94d710eb4ffd7 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 29 Jan 2026 18:31:09 -0600 Subject: use /overlay endpoints --- .../Core/API/Controllers/OptionsController.cs | 33 ++++++++++++++-------- src/slskd/Program.cs | 5 ++++ 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/slskd/Core/API/Controllers/OptionsController.cs b/src/slskd/Core/API/Controllers/OptionsController.cs index 291ae561..bfbb2dd7 100644 --- a/src/slskd/Core/API/Controllers/OptionsController.cs +++ b/src/slskd/Core/API/Controllers/OptionsController.cs @@ -42,18 +42,15 @@ namespace slskd.Core.API public class OptionsController : ControllerBase { public OptionsController( - IApplication application, OptionsAtStartup optionsAtStartup, IOptionsSnapshot optionsSnapshot, IStateMutator stateMutator) { - Application = application; OptionsAtStartup = optionsAtStartup; OptionsSnapshot = optionsSnapshot; StateMutator = stateMutator; } - private IApplication Application { get; } private IOptionsSnapshot OptionsSnapshot { get; } private OptionsAtStartup OptionsAtStartup { get; } private IStateMutator StateMutator { get; } @@ -72,21 +69,21 @@ namespace slskd.Core.API } /// - /// Patch the application options with new values. + /// Overlay the application options with new values. /// - /// The patch to apply. + /// The overlay to apply. /// - [HttpPatch] + [HttpPut("overlay")] [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(Options), 200)] - public IActionResult Patch([FromBody] OptionsOverlay patch) + public IActionResult ApplyOverlay([FromBody] OptionsOverlay overlay) { - if (patch is null) + if (overlay is null) { return NoContent(); } - if (!patch.TryValidate(out var result)) + if (!overlay.TryValidate(out var result)) { var error = Regex.Replace(result.GetResultView().ToString(), @"\s+", " ").Trim(); Logger.Warning("Options patch validation failed: {Message}", error); @@ -95,15 +92,14 @@ namespace slskd.Core.API try { - Program.ApplyConfigurationOverlay(patch); + Program.ApplyConfigurationOverlay(overlay); + return Ok(OptionsSnapshot.Value.Redact()); } catch (Exception ex) { Logger.Error(ex, "Failed to apply options patch: {Message}", ex.Message); return StatusCode(500, $"Failed to apply options patch: {ex.Message}"); } - - return Ok(OptionsSnapshot.Value.Redact()); } /// @@ -119,6 +115,19 @@ namespace slskd.Core.API return Ok(OptionsAtStartup.Redact()); } + /// + /// Gets the current application option overlay, if one has been applied. + /// + /// + [HttpGet] + [Route("overlay")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(OptionsOverlay), 200)] + public IActionResult GetOverlay() + { + return Ok(Program.ConfigurationOverlay?.Redact()); + } + /// /// Gets the debug view of the current application options. /// diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 143cd460..3b4cdb73 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -189,6 +189,11 @@ namespace slskd [EnvironmentVariable("CONFIG")] public static string ConfigurationFile { get; private set; } = null; + /// + /// Gets the current configuration overlay, if one has been applied. + /// + public static OptionsOverlay ConfigurationOverlay { get; private set; } = VolatileOverlayConfigurationSource?.CurrentValue; + /// /// Gets the path where persistent data is saved. /// -- cgit v1.2.3 From 3989f893139b078227caec8677de095435191a61 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 29 Jan 2026 19:37:13 -0600 Subject: oops --- src/slskd/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 3b4cdb73..75c51355 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -192,7 +192,7 @@ namespace slskd /// /// Gets the current configuration overlay, if one has been applied. /// - public static OptionsOverlay ConfigurationOverlay { get; private set; } = VolatileOverlayConfigurationSource?.CurrentValue; + public static OptionsOverlay ConfigurationOverlay => VolatileOverlayConfigurationSource?.CurrentValue; /// /// Gets the path where persistent data is saved. -- cgit v1.2.3 From 501c0415d3091d92b79569353b98c96d05a78909 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 29 Jan 2026 19:37:23 -0600 Subject: add GetResultString --- src/slskd/Common/Validation/ValidationExtensions.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/slskd/Common/Validation/ValidationExtensions.cs b/src/slskd/Common/Validation/ValidationExtensions.cs index 7a85a995..a29667a2 100644 --- a/src/slskd/Common/Validation/ValidationExtensions.cs +++ b/src/slskd/Common/Validation/ValidationExtensions.cs @@ -26,6 +26,9 @@ namespace slskd.Validation /// public static class ValidationExtensions { + public static string GetResultString(this CompositeValidationResult result) + => string.Join(' ', result.GetResultView(0).Select(s => s.Trim())); + public static string GetResultView(this CompositeValidationResult result) => string.Join("\n", result.GetResultView(0)); -- cgit v1.2.3 From 74ca21dc0566b081f9fa372d6895b12bc0aaa569 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 29 Jan 2026 19:37:48 -0600 Subject: tweak endpoints, create backup before updating --- .../Core/API/Controllers/OptionsController.cs | 45 +++++++++------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/src/slskd/Core/API/Controllers/OptionsController.cs b/src/slskd/Core/API/Controllers/OptionsController.cs index bfbb2dd7..e6222791 100644 --- a/src/slskd/Core/API/Controllers/OptionsController.cs +++ b/src/slskd/Core/API/Controllers/OptionsController.cs @@ -22,7 +22,6 @@ namespace slskd.Core.API using System; using System.IO; using System.Reflection; - using System.Text.RegularExpressions; using Asp.Versioning; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -54,7 +53,7 @@ namespace slskd.Core.API private IOptionsSnapshot OptionsSnapshot { get; } private OptionsAtStartup OptionsAtStartup { get; } private IStateMutator StateMutator { get; } - private ILogger Logger { get; } = Log.ForContext(); + private ILogger Log { get; } = Serilog.Log.ForContext(); /// /// Gets the current application options. @@ -73,11 +72,17 @@ namespace slskd.Core.API /// /// The overlay to apply. /// - [HttpPut("overlay")] - [Authorize(Policy = AuthPolicy.Any)] + [HttpPatch] + [Authorize(Policy = AuthPolicy.Any, Roles = AuthRole.AdministratorOnly)] [ProducesResponseType(typeof(Options), 200)] public IActionResult ApplyOverlay([FromBody] OptionsOverlay overlay) { + if (!OptionsSnapshot.Value.RemoteConfiguration) + { + Log.Warning("Blocked options overlay application; remote configuration is disabled"); + return Forbid(); + } + if (overlay is null) { return NoContent(); @@ -85,19 +90,18 @@ namespace slskd.Core.API if (!overlay.TryValidate(out var result)) { - var error = Regex.Replace(result.GetResultView().ToString(), @"\s+", " ").Trim(); - Logger.Warning("Options patch validation failed: {Message}", error); - return BadRequest(error); + Log.Warning("Options patch validation failed: {Message}", result.GetResultString()); + return BadRequest(result.GetResultString()); } try { Program.ApplyConfigurationOverlay(overlay); - return Ok(OptionsSnapshot.Value.Redact()); + return Ok(Program.ConfigurationOverlay?.Redact()); } catch (Exception ex) { - Logger.Error(ex, "Failed to apply options patch: {Message}", ex.Message); + Log.Error(ex, "Failed to apply options patch: {Message}", ex.Message); return StatusCode(500, $"Failed to apply options patch: {ex.Message}"); } } @@ -115,19 +119,6 @@ namespace slskd.Core.API return Ok(OptionsAtStartup.Redact()); } - /// - /// Gets the current application option overlay, if one has been applied. - /// - /// - [HttpGet] - [Route("overlay")] - [Authorize(Policy = AuthPolicy.Any)] - [ProducesResponseType(typeof(OptionsOverlay), 200)] - public IActionResult GetOverlay() - { - return Ok(Program.ConfigurationOverlay?.Redact()); - } - /// /// Gets the debug view of the current application options. /// @@ -178,7 +169,7 @@ namespace slskd.Core.API return Ok(yaml); } - [HttpPost] + [HttpPut] [Authorize(Policy = AuthPolicy.JwtOnly, Roles = AuthRole.AdministratorOnly)] [Route("yaml")] public IActionResult UpdateYamlFile([FromBody] string yaml) @@ -190,17 +181,19 @@ namespace slskd.Core.API if (!TryValidateYaml(yaml, out var error)) { - Logger.Error(error, "Failed to validate YAML configuration"); + Log.Error(error, "Failed to validate YAML configuration"); return BadRequest(error); } try { + IOFile.Copy(Program.ConfigurationFile, $"{Program.ConfigurationFile}.bak", overwrite: true); + IOFile.WriteAllText(Program.ConfigurationFile, yaml); if (OptionsSnapshot.Value.Flags.NoConfigWatch) { - Logger.Information("Configuration watch is disabled; restart required for changes to take effect"); + Log.Information("Configuration watch is disabled; restart required for changes to take effect"); StateMutator.SetValue(state => state with { PendingRestart = true }); } @@ -251,7 +244,7 @@ namespace slskd.Core.API } catch (Exception ex) { - Logger.Warning(ex, "Configuration validation failed"); + Log.Warning(ex, "Configuration validation failed"); error = ex.Message; -- cgit v1.2.3 From 61dd929e46dff037c62c1aef96b22d1fc3d12d9f Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 30 Jan 2026 08:33:26 -0600 Subject: use PUT to update yaml --- src/web/src/lib/options.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/web/src/lib/options.js b/src/web/src/lib/options.js index 620be946..1c3c55c5 100644 --- a/src/web/src/lib/options.js +++ b/src/web/src/lib/options.js @@ -21,5 +21,5 @@ export const validateYaml = async ({ yaml }) => { }; export const updateYaml = async ({ yaml }) => { - return (await api.post('/options/yaml', yaml)).data; + return (await api.put('/options/yaml', yaml)).data; }; -- cgit v1.2.3 From 183315d6d9696a79ef0b371507d573d16b02a261 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 30 Jan 2026 09:44:38 -0600 Subject: Update src/slskd/Core/Extensions.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/slskd/Core/Extensions.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/slskd/Core/Extensions.cs b/src/slskd/Core/Extensions.cs index 707daa88..0789fa38 100644 --- a/src/slskd/Core/Extensions.cs +++ b/src/slskd/Core/Extensions.cs @@ -42,12 +42,12 @@ namespace slskd } /// - /// Redacts this instance of Options, replacing properties marked with with '*****'. + /// Redacts this instance of OptionsOverlay, replacing properties marked with with '*****'. /// /// /// Creates a deep clone before redacting. /// - /// The Options instance to redact. + /// The OptionsOverlay instance to redact. /// A redacted instance. public static OptionsOverlay Redact(this OptionsOverlay options) { -- cgit v1.2.3 From 5c30a775b4f4850e21f54a53b490857b379aa070 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 30 Jan 2026 09:44:54 -0600 Subject: Update src/slskd/Core/API/Controllers/OptionsController.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/slskd/Core/API/Controllers/OptionsController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/slskd/Core/API/Controllers/OptionsController.cs b/src/slskd/Core/API/Controllers/OptionsController.cs index e6222791..f5c1a5b6 100644 --- a/src/slskd/Core/API/Controllers/OptionsController.cs +++ b/src/slskd/Core/API/Controllers/OptionsController.cs @@ -74,7 +74,7 @@ namespace slskd.Core.API /// [HttpPatch] [Authorize(Policy = AuthPolicy.Any, Roles = AuthRole.AdministratorOnly)] - [ProducesResponseType(typeof(Options), 200)] + [ProducesResponseType(typeof(OptionsOverlay), 200)] public IActionResult ApplyOverlay([FromBody] OptionsOverlay overlay) { if (!OptionsSnapshot.Value.RemoteConfiguration) -- cgit v1.2.3 From d70aa1b4427be1a4fed47409720f2035034bf41e Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 30 Jan 2026 09:45:06 -0600 Subject: Update src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs b/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs index 8707fe69..318742e7 100644 --- a/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs +++ b/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs @@ -103,7 +103,7 @@ namespace slskd.Configuration } /// - /// Applies the goven . + /// Applies the given . /// /// An object containing the values to overlay. public void Apply(T overlay) -- cgit v1.2.3 From effb7b01ef1d8410a5456a4374200abdac44f437 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 30 Jan 2026 09:45:23 -0600 Subject: Update src/slskd/Core/OptionsOverlay.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/slskd/Core/OptionsOverlay.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/slskd/Core/OptionsOverlay.cs b/src/slskd/Core/OptionsOverlay.cs index faea4ea7..11167da5 100644 --- a/src/slskd/Core/OptionsOverlay.cs +++ b/src/slskd/Core/OptionsOverlay.cs @@ -34,7 +34,7 @@ namespace slskd /// /// /// Every property in this class must be nullable, and must have a null default value; the application - /// selectively applies the patch overlay only information explicitly supplied. + /// selectively applies the patch overlay only to information that is explicitly supplied. /// /// public class OptionsOverlay -- cgit v1.2.3 From bf6c8f8974239bf0fcf6d6b141ec674e66c137c6 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 30 Jan 2026 17:57:02 -0600 Subject: avoid null refs --- src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs b/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs index 318742e7..0d6debec 100644 --- a/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs +++ b/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs @@ -64,6 +64,11 @@ namespace slskd.Configuration // note: pretty much the same as DefaultValueConfigurationProvider; the two should be updated in lockstep void Map(Type type, string path, object instance) { + if (instance is null) + { + return; + } + var props = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo property in props) -- cgit v1.2.3 From fafb8a5f15024a4a369d98376ddef4b3b20cf30c Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 30 Jan 2026 18:07:29 -0600 Subject: document overlay --- docs/config.md | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/docs/config.md b/docs/config.md index 9836a063..226d523b 100644 --- a/docs/config.md +++ b/docs/config.md @@ -13,7 +13,7 @@ The application supports several different configuration sources to make it easy The configuration options used by the application derive from the values specified by each of the sources, with sources higher in the hierarchy overwriting or expanding the configuration set earlier. The hierarchy is: ``` -Default Values < Environment Variables < YAML Configuration File < Command Line Arguments +Default Values < Environment Variables < YAML Configuration File < Command Line Arguments < Run-Time Overlay ``` Some options can be specified as lists. In these cases, when combining configurations from multiple sources, care should be taken. .NET framework not only appends but also overwrites values. If a list of `one;two;three` is defined in an environment variable for an option, and the YAML configuration specifies a list of `foo` and `bar`, the resulting configuration will be a list containing `foo, bar, three`. @@ -58,6 +58,26 @@ Some options are backed by arrays or lists and allow multiple options to be set --some-option 1 --some-option 2 --some-option 3 ``` +## Run-Time Overlay + +A small subset of options can be set at run-time via an API call. These options take precedence over all other sources and the values persist through configuration changes; they are 'sticky' but also volatile, meaning they are lost when the application is restarted. + +Details about the API endpoint (including the full set of available options) can be found in the OpenAPI specification that can be generated by enabling the 'swagger' feature. + +The following is a complete example (currently only `listenPort` and `listenIpAddress` are supported): + +```bash +curl --location --request PATCH 'http://localhost:5030/api/v0/options' \ +--header 'X-API-Key: ' \ +--header 'Content-Type: application/json' \ +--data '{ + "soulseek": { + "listenPort": 52342, + "listenIpAddress": "0.0.0.0" + } +}' +``` + # Remote Configuration The application contains APIs for retrieving and updating the YAML configuration file. By default, this option is disabled. Applications run within untrusted networks, especially those where the application is internet-facing, should consider the risks of enabling this option. -- cgit v1.2.3 From d2bf89cc204e89c04f66b778d9f94e325deb5a3d Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 30 Jan 2026 18:10:13 -0600 Subject: note --- docs/config.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/config.md b/docs/config.md index 226d523b..d1ad2dd1 100644 --- a/docs/config.md +++ b/docs/config.md @@ -78,6 +78,8 @@ curl --location --request PATCH 'http://localhost:5030/api/v0/options' \ }' ``` +Note: the Soulseek server is updated to reflect changes in `listenPort` automatically; there is no need to reconnect. + # Remote Configuration The application contains APIs for retrieving and updating the YAML configuration file. By default, this option is disabled. Applications run within untrusted networks, especially those where the application is internet-facing, should consider the risks of enabling this option. -- cgit v1.2.3