diff options
| author | JP Dillingham <jp@dillingham.me> | 2025-12-13 17:15:33 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-12-13 17:15:33 -0600 |
| commit | 0b939fe41b22e0151d2b0e106310520f19d0c670 (patch) | |
| tree | fd7023550e6e65cddccedfe4e3073d686329e02c /src/slskd/Common/Validation/IPAddressAttribute.cs | |
| parent | e08479ee8d91952fee13b009f861912a92a2dbae (diff) | |
| parent | 8c842ff28f507e886ebf7ca6217e11ae2650dff9 (diff) | |
| download | slskd-0b939fe41b22e0151d2b0e106310520f19d0c670.tar.gz | |
Merge branch 'master' into copilot/gracefully-handle-mutex-creation
Diffstat (limited to 'src/slskd/Common/Validation/IPAddressAttribute.cs')
| -rw-r--r-- | src/slskd/Common/Validation/IPAddressAttribute.cs | 32 |
1 files changed, 30 insertions, 2 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."); + } + } } } |
