diff options
| author | JP Dillingham <jp@dillingham.me> | 2025-12-11 09:24:09 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-12-11 09:24:09 -0600 |
| commit | 5f00d9c8545aa7c025c65ace89086ba0c621911a (patch) | |
| tree | 4666052b0844b654133a040a57f47c55d323426e /src | |
| parent | a55adb37e3c5bc2d249a6a8cb23873c0391f4580 (diff) | |
| parent | dccf744efc462775957879bb8b5e1df8732a21ac (diff) | |
| download | slskd-5f00d9c8545aa7c025c65ace89086ba0c621911a.tar.gz | |
Merge pull request #1585 from slskd/ip
Add the ability to bind HTTP and HTTPS to specific IP addresses
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Common/Validation/IPAddressAttribute.cs | 32 | ||||
| -rw-r--r-- | src/slskd/Core/Options.cs | 20 | ||||
| -rw-r--r-- | src/slskd/Program.cs | 53 |
3 files changed, 98 insertions, 7 deletions
diff --git a/src/slskd/Common/Validation/IPAddressAttribute.cs b/src/slskd/Common/Validation/IPAddressAttribute.cs index 6e53df57..b9075808 100644 --- a/src/slskd/Common/Validation/IPAddressAttribute.cs +++ b/src/slskd/Common/Validation/IPAddressAttribute.cs @@ -18,6 +18,7 @@ namespace slskd.Validation { using System.ComponentModel.DataAnnotations; + using System.Linq; using System.Net; /// <summary> @@ -25,6 +26,13 @@ namespace slskd.Validation /// </summary> public class IPAddressAttribute : ValidationAttribute { + public IPAddressAttribute(bool allowCommaSeparatedValues = false) + { + AllowCommaSeparatedValues = allowCommaSeparatedValues; + } + + private bool AllowCommaSeparatedValues { get; set; } + protected override ValidationResult IsValid(object value, ValidationContext validationContext) { if (value != null) @@ -36,9 +44,29 @@ namespace slskd.Validation return fail; } - if (!IPAddress.TryParse(valueAsString, out _)) + if (!valueAsString.Contains(',')) { - return fail; + if (!IPAddress.TryParse(valueAsString, out _)) + { + return fail; + } + } + else + { + if (!AllowCommaSeparatedValues) + { + return new ValidationResult($"The {validationContext.DisplayName} field accepts a single value only (a comma separated list was specified)"); + } + + var values = valueAsString.Split(',').Select(v => v.Trim()); + + foreach (var currentValue in values) + { + if (!IPAddress.TryParse(currentValue, out _)) + { + return new ValidationResult($"The {validationContext.DisplayName} field contains one or more invalid IPv4 or IPv6 IP addresses."); + } + } } } diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 76dcd45c..1739f5fb 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -1839,6 +1839,16 @@ namespace slskd public int Port { get; init; } = 5030; /// <summary> + /// Gets the comma separated list of IPv4 or IPv6 IP addresses on which to listen for HTTP requests. + /// </summary> + [Argument(default, "http-ip-address")] + [EnvironmentVariable("HTTP_IP_ADDRESS")] + [Description("IP addresses on which to listen for HTTP requests")] + [IPAddress(allowCommaSeparatedValues: true)] + [RequiresRestart] + public string IpAddress { get; init; } + + /// <summary> /// Gets the HTTP listen unix domain socket (UDS) path. /// </summary> [Argument(default, "http-socket")] @@ -2040,6 +2050,16 @@ namespace slskd public int Port { get; init; } = 5031; /// <summary> + /// Gets the comma separated list of IPv4 or IPv6 IP addresses on which to listen for HTTPS requests. + /// </summary> + [Argument(default, "https-ip-address")] + [EnvironmentVariable("HTTPS_IP_ADDRESS")] + [Description("IP addresses on which to listen for HTTPS requests")] + [IPAddress(allowCommaSeparatedValues: true)] + [RequiresRestart] + public string IpAddress { get; init; } + + /// <summary> /// Gets a value indicating whether HTTP requests should be redirected to HTTPS. /// </summary> [Argument('f', "force-https")] diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index aa320961..288fa0e3 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -40,6 +40,7 @@ namespace slskd using Microsoft.AspNetCore.Diagnostics; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; + using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -478,19 +479,39 @@ namespace slskd .UseUrls() .UseKestrel(options => { - Log.Information($"Listening for HTTP requests at http://{IPAddress.Any}:{OptionsAtStartup.Web.Port}/"); - options.Listen(IPAddress.Any, OptionsAtStartup.Web.Port); + // configure HTTP, either by listening at any IP or by each of the IPs provided in the + // config (note: they've already been validated at this point!) + if (string.IsNullOrWhiteSpace(OptionsAtStartup.Web.IpAddress)) + { + Log.Information("Listening for HTTP requests at http://{IP}:{Port}/", IPAddress.IPv6Any, OptionsAtStartup.Web.Port); + options.Listen(IPAddress.IPv6Any, OptionsAtStartup.Web.Port); // [::]; any IPv4 or IPv6 address + } + else + { + var httpIps = OptionsAtStartup.Web.IpAddress + .Split(',') + .Select(ip => ip.Trim()) + .Select(ip => IPAddress.Parse(ip)); + + foreach (var ip in httpIps) + { + Log.Information("Listening for HTTP requests at http://{IP}:{Port}/", ip, OptionsAtStartup.Web.Port); + options.Listen(ip, OptionsAtStartup.Web.Port); + } + } + // configure UDS, if supplied if (OptionsAtStartup.Web.Socket != null) { Log.Information($"Listening for HTTP requests on unix domain socket (UDS) {OptionsAtStartup.Web.Socket}"); options.ListenUnixSocket(OptionsAtStartup.Web.Socket); } - if (!OptionsAtStartup.Web.Https.Disabled) + // configure HTTPS, again listening on any IP or on a list of supplied IPs + // use a local function because Microsoft can't get enough of the obtuse builder pattern + static void ListenHttps(KestrelServerOptions o, IPAddress ip) { - Log.Information($"Listening for HTTPS requests at https://{IPAddress.Any}:{OptionsAtStartup.Web.Https.Port}/"); - options.Listen(IPAddress.Any, OptionsAtStartup.Web.Https.Port, listenOptions => + o.Listen(ip, OptionsAtStartup.Web.Https.Port, listenOptions => { var cert = OptionsAtStartup.Web.Https.Certificate; @@ -506,6 +527,28 @@ namespace slskd } }); } + + if (!OptionsAtStartup.Web.Https.Disabled) + { + if (string.IsNullOrWhiteSpace(OptionsAtStartup.Web.Https.IpAddress)) + { + Log.Information("Listening for HTTPS requests at https://{IP}:{Port}/", IPAddress.IPv6Any, OptionsAtStartup.Web.Https.Port); + ListenHttps(options, IPAddress.IPv6Any); + } + else + { + var httpsIps = OptionsAtStartup.Web.Https.IpAddress + .Split(',') + .Select(ip => ip.Trim()) + .Select(ip => IPAddress.Parse(ip)); + + foreach (var ip in httpsIps) + { + Log.Information("Listening for HTTPS requests at https://{IP}:{Port}/", ip, OptionsAtStartup.Web.Https.Port); + ListenHttps(options, ip); + } + } + } }); builder.Services |
