diff options
| author | JP Dillingham <jp@dillingham.me> | 2025-12-09 19:33:24 -0600 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2025-12-09 19:33:24 -0600 |
| commit | b90b5a96654727c7ded4a5b926bc315aa99014ee (patch) | |
| tree | d1d6fc475b0387e10102ee589ec6c15b754df450 /src | |
| parent | c026b58c435372b7042d5a1ddf911dd4429ba993 (diff) | |
| download | slskd-b90b5a96654727c7ded4a5b926bc315aa99014ee.tar.gz | |
add support for commaa separated lists of IPs
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Common/Validation/IPAddressAttribute.cs | 31 |
1 files changed, 29 insertions, 2 deletions
diff --git a/src/slskd/Common/Validation/IPAddressAttribute.cs b/src/slskd/Common/Validation/IPAddressAttribute.cs index 6e53df57..17ae5f08 100644 --- a/src/slskd/Common/Validation/IPAddressAttribute.cs +++ b/src/slskd/Common/Validation/IPAddressAttribute.cs @@ -25,6 +25,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 +43,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(','); + + 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."); + } + } } } |
