aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Common/Validation/IPAddressAttribute.cs
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2025-12-13 16:59:51 -0600
committerGitHub <noreply@github.com>2025-12-13 16:59:51 -0600
commit1d27718843b0d62afa8a52847eadcbdbcbf44b98 (patch)
treebc6d2bd7fd959d0740a68b5b35a3baf8f1de31c0 /src/slskd/Common/Validation/IPAddressAttribute.cs
parent6dbedb038a152a22dd9ee4b04a41a8b3ffde5d1f (diff)
parent54584b8edd08f704427355d59323163086102207 (diff)
downloadslskd-1d27718843b0d62afa8a52847eadcbdbcbf44b98.tar.gz
Merge branch 'master' into copilot/add-sort-directories-files
Diffstat (limited to 'src/slskd/Common/Validation/IPAddressAttribute.cs')
-rw-r--r--src/slskd/Common/Validation/IPAddressAttribute.cs32
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.");
+ }
+ }
}
}