aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Core
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-01-30 18:15:12 -0600
committerGitHub <noreply@github.com>2026-01-30 18:15:12 -0600
commit09eabbce7d9bd4d42ffa9c30f97faa909a9ff8b0 (patch)
tree19e3a3da819dcd0568a10377fe8a6eacf3998902 /src/slskd/Core
parent92b49b7a4035822240ba51ffdf3383eb9a30b164 (diff)
parentd2bf89cc204e89c04f66b778d9f94e325deb5a3d (diff)
downloadslskd-09eabbce7d9bd4d42ffa9c30f97faa909a9ff8b0.tar.gz
Merge pull request #1623 from slskd/runtime-config
Add the ability to apply an options 'overlay' at run-time; enabling update of listen IP and port
Diffstat (limited to 'src/slskd/Core')
-rw-r--r--src/slskd/Core/API/Controllers/OptionsController.cs51
-rw-r--r--src/slskd/Core/Extensions.cs15
-rw-r--r--src/slskd/Core/OptionsOverlay.cs66
3 files changed, 127 insertions, 5 deletions
diff --git a/src/slskd/Core/API/Controllers/OptionsController.cs b/src/slskd/Core/API/Controllers/OptionsController.cs
index 25e9095a..f5c1a5b6 100644
--- a/src/slskd/Core/API/Controllers/OptionsController.cs
+++ b/src/slskd/Core/API/Controllers/OptionsController.cs
@@ -53,7 +53,7 @@ namespace slskd.Core.API
private IOptionsSnapshot<Options> OptionsSnapshot { get; }
private OptionsAtStartup OptionsAtStartup { get; }
private IStateMutator<State> StateMutator { get; }
- private ILogger Logger { get; } = Log.ForContext(typeof(OptionsController));
+ private ILogger Log { get; } = Serilog.Log.ForContext<OptionsController>();
/// <summary>
/// Gets the current application options.
@@ -68,6 +68,45 @@ namespace slskd.Core.API
}
/// <summary>
+ /// Overlay the application options with new values.
+ /// </summary>
+ /// <param name="overlay">The overlay to apply.</param>
+ /// <returns></returns>
+ [HttpPatch]
+ [Authorize(Policy = AuthPolicy.Any, Roles = AuthRole.AdministratorOnly)]
+ [ProducesResponseType(typeof(OptionsOverlay), 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();
+ }
+
+ if (!overlay.TryValidate(out var result))
+ {
+ Log.Warning("Options patch validation failed: {Message}", result.GetResultString());
+ return BadRequest(result.GetResultString());
+ }
+
+ try
+ {
+ Program.ApplyConfigurationOverlay(overlay);
+ return Ok(Program.ConfigurationOverlay?.Redact());
+ }
+ catch (Exception ex)
+ {
+ Log.Error(ex, "Failed to apply options patch: {Message}", ex.Message);
+ return StatusCode(500, $"Failed to apply options patch: {ex.Message}");
+ }
+ }
+
+ /// <summary>
/// Gets the application options provided at startup.
/// </summary>
/// <returns></returns>
@@ -130,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)
@@ -142,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 });
}
@@ -203,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;
diff --git a/src/slskd/Core/Extensions.cs b/src/slskd/Core/Extensions.cs
index db75cc9c..0789fa38 100644
--- a/src/slskd/Core/Extensions.cs
+++ b/src/slskd/Core/Extensions.cs
@@ -42,6 +42,21 @@ namespace slskd
}
/// <summary>
+ /// Redacts this instance of OptionsOverlay, replacing properties marked with <see cref="SecretAttribute"/> with '*****'.
+ /// </summary>
+ /// <remarks>
+ /// Creates a deep clone before redacting.
+ /// </remarks>
+ /// <param name="options">The OptionsOverlay instance to redact.</param>
+ /// <returns>A redacted instance.</returns>
+ public static OptionsOverlay Redact(this OptionsOverlay options)
+ {
+ var redacted = options.ToJson().FromJson<OptionsOverlay>();
+ Redactor.Redact(redacted, redactWith: "*****");
+ return redacted;
+ }
+
+ /// <summary>
/// Creates a copy of this instance with the specified parameters changed.
/// </summary>
/// <param name="o">The options instance to copy.</param>
diff --git a/src/slskd/Core/OptionsOverlay.cs b/src/slskd/Core/OptionsOverlay.cs
new file mode 100644
index 00000000..11167da5
--- /dev/null
+++ b/src/slskd/Core/OptionsOverlay.cs
@@ -0,0 +1,66 @@
+// <copyright file="OptionsOverlay.cs" company="slskd Team">
+// 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/.
+// </copyright>
+namespace slskd
+{
+ using System;
+ using System.ComponentModel.DataAnnotations;
+ using slskd.Validation;
+
+ /// <summary>
+ /// Volatile run-time overlay for application <see cref="Options"/>.
+ /// </summary>
+ /// <remarks>
+ /// <para>
+ /// 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.
+ /// </para>
+ /// <para>
+ /// Only options that can be applied while the application is running can be overlaid, given the nature of how
+ /// an overlay is applied.
+ /// </para>
+ /// <para>
+ /// Every property in this class must be nullable, and must have a null default value; the application
+ /// selectively applies the patch overlay only to information that is explicitly supplied.
+ /// </para>
+ /// </remarks>
+ public class OptionsOverlay
+ {
+ /// <summary>
+ /// Gets options for the Soulseek client.
+ /// </summary>
+ [Validate]
+ public SoulseekOptionsPatch Soulseek { get; init; } = null;
+
+ /// <summary>
+ /// Soulseek client options.
+ /// </summary>
+ public class SoulseekOptionsPatch
+ {
+ /// <summary>
+ /// Gets the local IP address on which to listen for incoming connections.
+ /// </summary>
+ [IPAddress]
+ public string ListenIpAddress { get; init; } = null;
+
+ /// <summary>
+ /// Gets the port on which to listen for incoming connections.
+ /// </summary>
+ [Range(1024, 65535)]
+ public int? ListenPort { get; init; } = null;
+ }
+ }
+} \ No newline at end of file