diff options
Diffstat (limited to 'src/slskd/Core/Options.cs')
| -rw-r--r-- | src/slskd/Core/Options.cs | 112 |
1 files changed, 108 insertions, 4 deletions
diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 254adcab..20b53f6c 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -115,7 +115,7 @@ namespace slskd /// <summary> /// Gets a value indicating whether to display a list of configuration environment variables. /// </summary> - [Argument('e', "envars")] + [Argument(default, "envars")] [Description("display environment variables")] [Obsolete("Used only for documentation; see Program for actual implementation")] [JsonIgnore] @@ -125,7 +125,7 @@ namespace slskd /// <summary> /// Gets a value indicating whether to generate an X509 certificate and password. /// </summary> - [Argument('g', "generate-cert")] + [Argument(default, "generate-cert")] [Description("generate X509 certificate and password for HTTPS")] [Obsolete("Used only for documentation; see Program for actual implementation")] [JsonIgnore] @@ -135,7 +135,7 @@ namespace slskd /// <summary> /// Gets a value indicating whether to generate a random secret. /// </summary> - [Argument('k', "generate-secret")] + [Argument(default, "generate-secret")] [Description("generate random secret of the specified length")] [Obsolete("Used only for documentation; see Program for actual implementation")] [JsonIgnore] @@ -1252,6 +1252,12 @@ namespace slskd return results; } + if (string.IsNullOrWhiteSpace(File)) + { + results.Add(new ValidationResult("The Enabled field is true, but no File has been specified.")); + return results; + } + // loading/validating the entire list will be costly on low spec systems // just make sure that we can detect a valid format and leave it at that // if there's a problem with any of the entries, the load will fail and @@ -1833,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")] @@ -1887,7 +1903,7 @@ namespace slskd /// <summary> /// Authentication options. /// </summary> - public class WebAuthenticationOptions + public class WebAuthenticationOptions : IValidatableObject { /// <summary> /// Gets a value indicating whether authentication should be disabled. @@ -1925,11 +1941,89 @@ namespace slskd public JwtOptions Jwt { get; init; } = new JwtOptions(); /// <summary> + /// Gets the primary API key. Defaults to 'Administrator' role and CIDR '0.0.0.0/0,::/0'. + /// role and CIDR can be supplied by prefixing the key with 'role=[role];cidr=[cidrs];[key]'. + /// </summary> + [Argument('k', "api-key")] + [EnvironmentVariable("API_KEY")] + [Description("primary API key value")] + [Secret] + [RequiresRestart] + public string ApiKey { get; init; } + + /// <summary> /// Gets API keys. /// </summary> [Validate] public Dictionary<string, ApiKeyOptions> ApiKeys { get; init; } = new Dictionary<string, ApiKeyOptions>(); + public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) + { + var results = new List<ValidationResult>(); + + if (string.IsNullOrWhiteSpace(ApiKey)) + { + return results; + } + + var tuples = ApiKey.Split(';'); + + // if only one tuple is supplied, it's just the key, no role or list of CIDRs + if (tuples.Length == 1) + { + var key = tuples.FirstOrDefault(); + + if (key?.Length < 16 || key?.Length > 255) + { + results.Add(new ValidationResult("API key must be between 16 and 255 characters")); + } + + return results; + } + + /* + if multiple tuples are supplied, verify that: + + * if a tuple starts with 'role=', it's defining a role and it must match one of the Role enum values + * if a tuple starts with 'cidr=', it's defining a list of CIDRs which need to be valid + * if a tuple doesn't start with a prefix, it's the API key itself and it must be between 16 and 255 characters + */ + foreach (var tuple in tuples) + { + if (tuple.StartsWith("role=", StringComparison.OrdinalIgnoreCase)) + { + var role = tuple.Split('=').LastOrDefault(); + + if (!Enum.TryParse(typeof(Role), value: role, ignoreCase: true, out _)) + { + results.Add(new ValidationResult($"API key role must be one of: {string.Join(", ", Enum.GetNames(typeof(Role)))}")); + } + } + else if (tuple.StartsWith("cidr=", StringComparison.OrdinalIgnoreCase)) + { + var cidrs = tuple.Split('=').LastOrDefault(); + + foreach (var cidr in cidrs.Split(',')) + { + try + { + _ = IPAddressRange.Parse(cidr); + } + catch (Exception ex) + { + results.Add(new ValidationResult($"API key CIDR {cidr} is invalid: {ex.Message}")); + } + } + } + else if (tuple?.Length < 16 || tuple?.Length > 255) + { + results.Add(new ValidationResult("API key must be between 16 and 255 characters")); + } + } + + return results; + } + /// <summary> /// JWT options. /// </summary> @@ -2034,6 +2128,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")] |
