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 | |
| 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
| -rw-r--r-- | config/slskd.example.yml | 2 | ||||
| -rw-r--r-- | docs/config.md | 36 | ||||
| -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 |
5 files changed, 122 insertions, 21 deletions
diff --git a/config/slskd.example.yml b/config/slskd.example.yml index a0ca4f3f..66379626 100644 --- a/config/slskd.example.yml +++ b/config/slskd.example.yml @@ -137,10 +137,12 @@ # - ^.{1,2}$ # web: # port: 5030 +# ip_address: 0.0.0.0,[::] # socket: ~ # https: # disabled: false # port: 5031 +# ip_address: 0.0.0.0,[::] # force: false # certificate: # pfx: ~ diff --git a/docs/config.md b/docs/config.md index 2646dd94..36f57e89 100644 --- a/docs/config.md +++ b/docs/config.md @@ -798,6 +798,8 @@ soulseek: The default HTTP listen port is 5030, but can be anything between 1 and 65535. +The default HTTP IP address is `[::]` which corresponds to any IPv4 or IPv6 IP address; all network interfaces on the host. Specifying an IP address will cause the application to listen only on the specified network interface. Multiple IP addresses can be supplied in a comma-separated list, and both IPv4 and IPv6 formats can be supplied. + A [unix domain socket](https://en.wikipedia.org/wiki/Unix_domain_socket) can be used on both unix-like operating systems and Windows. The specified socket file path must be absolute, and must point to a file that does not exist (slskd will manage it automatically). The URL base option allows the application to operate behind a reverse proxy. Setting a base of "slskd" would make the web UI accessible at `http://<host>:<port>/slskd`. @@ -806,18 +808,20 @@ The content path can be used to force the application to serve static web conten Logging of HTTP requests is disabled by default. -| Command-Line | Environment Variable | Description | -| ----------------- | -------------------------- | ------------------------------------------------- | -| `-l\|--http-port` | `SLSKD_HTTP_PORT` | The HTTP listen port | -| `--http-socket` | `SLSKD_HTTP_SOCKET` | The HTTP listen unix domain socket (UDS) path | -| `--url-base` | `SLSKD_URL_BASE` | The base url for web requests | -| `--content-path` | `SLSKD_CONTENT_PATH` | The path to static web content | -| `--http-logging` | `SLSKD_HTTP_LOGGING` | Determines whether HTTP requests are to be logged | +| Command-Line | Environment Variable | Description | +| -------------------------- | ------------------------------ | --------------------------------------------------- | +| `-l\|--http-port` | `SLSKD_HTTP_PORT` | The HTTP listen port | +| `--http-listen-ip-address` | `SLSKD_HTTP_IP_ADDRESS` | The IP address on which to listen for HTTP requests | +| `--http-socket` | `SLSKD_HTTP_SOCKET` | The HTTP listen unix domain socket (UDS) path | +| `--url-base` | `SLSKD_URL_BASE` | The base url for web requests | +| `--content-path` | `SLSKD_CONTENT_PATH` | The path to static web content | +| `--http-logging` | `SLSKD_HTTP_LOGGING` | Determines whether HTTP requests are to be logged | #### **YAML** ```yaml web: port: 5030 + ip_address: "[::]" // any IPv4 or IPv6 address socket: ~ url_base: / content_path: wwwroot @@ -828,17 +832,20 @@ web: The default HTTPS port is 5031, but can be anything between 1 and 65535. +The default HTTPS IP address is `[::]` which corresponds to any IPv4 or IPv6 IP address; all network interfaces on the host. Specifying an IP address will cause the application to listen only on the specified network interface. Multiple IP addresses can be supplied in a comma-separated list, and both IPv4 and IPv6 formats can be supplied. + By default, the application generates a new, self-signed X509 certificate at each startup. If, for whatever reason, a self-signed certificate isn't sufficient, or if the certificate needs to be shared among systems or applications, a certificate `.pfx` and password can be defined. The application can produce a self-signed `.pfx` file and random password using the `--generate-cert` command. -| Command-Line | Environment Variable | Description | -| ----------------------- | --------------------------- | -------------------------------------------------------------- | -| `-L\|--https-port` | `SLSKD_HTTPS_PORT` | The HTTPS listen port | -| `-f\|--force-https` | `SLSKD_HTTPS_FORCE` | Determines whether HTTP requests are to be redirected to HTTPS | -| `--https-cert-pfx` | `SLSKD_HTTPS_CERT_PFX` | The path to the X509 certificate .pfx file | -| `--https-cert-password` | `SLSKD_HTTPS_CERT_PASSWORD` | The password for the X509 certificate | -| `--no-https` | `SLSKD_NO_HTTPS` | Determines whether HTTPS is to be disabled | +| Command-Line | Environment Variable | Description | +| --------------------------- | ------------------------------- | -------------------------------------------------------------- | +| `-L\|--https-port` | `SLSKD_HTTPS_PORT` | The HTTPS listen port | +| `--https-ip-address` | `SLSKD_HTTPS_IP_ADDRESS` | The IP address on which to listen for HTTPS requests | +| `-f\|--force-https` | `SLSKD_HTTPS_FORCE` | Determines whether HTTP requests are to be redirected to HTTPS | +| `--https-cert-pfx` | `SLSKD_HTTPS_CERT_PFX` | The path to the X509 certificate .pfx file | +| `--https-cert-password` | `SLSKD_HTTPS_CERT_PASSWORD` | The password for the X509 certificate | +| `--no-https` | `SLSKD_NO_HTTPS` | Determines whether HTTPS is to be disabled | #### **YAML** ```yaml @@ -846,6 +853,7 @@ web: https: disabled: false port: 5031 + ip_address: "[::]" // any IPv4 or IPv6 address force: false certificate: pfx: ~ 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 |
