aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorGraham Sutherland <gsuberland@users.noreply.github.com>2024-01-26 22:25:14 +0000
committerGitHub <noreply@github.com>2024-01-26 22:25:14 +0000
commitb39b8606219bd3957b28310b2a4b42f2f8ce3f21 (patch)
tree5a551fc2f0a8de399e2aa77edb988ec371313d00 /src
parent75543ab667cd8e62c38a5d3530a6783b3408fc15 (diff)
downloadslskd-b39b8606219bd3957b28310b2a4b42f2f8ce3f21.tar.gz
Added blacklist file support to the options.
Add a new option that allows the user to specify a blacklist file containing CIDRs, e.g. from a blacklist publisher. Introduce a cache for the IPAddressRange objects representing blacklisted CIDRs, to avoid re-parsing them on every call, since the blacklist files may be large.
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Core/Options.cs50
1 files changed, 48 insertions, 2 deletions
diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs
index 3727b89d..49603872 100644
--- a/src/slskd/Core/Options.cs
+++ b/src/slskd/Core/Options.cs
@@ -928,11 +928,30 @@ namespace slskd
public string[] Members { get; init; } = Array.Empty<string>();
/// <summary>
+ /// A pre-parsed cache of IPAddressRange objects describing all blacklisted ranges.
+ /// </summary>
+ private List<IPAddressRange> BlacklistedCIDRs = new List<IPAddressRange>();
+
+ /// <summary>
/// Gets the list of group CIDRs.
/// </summary>
public string[] Cidrs { get; init; } = Array.Empty<string>();
/// <summary>
+ /// Gets the CIDR blacklist file path.
+ /// </summary>
+ public string BlacklistFile { get; init; } = string.Empty;
+
+ /// <summary>
+ /// Gets address ranges for all blacklisted CIDRs defined by the configuration file and the blacklist file.
+ /// </summary>
+ /// <returns>An IEnumerable of IPAddressRange objects describing the blacklisted CIDRs.</returns>
+ public IEnumerable<IPAddressRange> GetBlacklistedCIDRs()
+ {
+ return BlacklistedCIDRs;
+ }
+
+ /// <summary>
/// Extended validation.
/// </summary>
/// <param name="validationContext"></param>
@@ -940,12 +959,13 @@ namespace slskd
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
var results = new List<ValidationResult>();
+ var cache = new List<IPAddressRange>();
foreach (var cidr in Cidrs)
{
try
{
- _ = IPAddressRange.Parse(cidr);
+ cache.Add(IPAddressRange.Parse(cidr));
}
catch (Exception ex)
{
@@ -953,6 +973,32 @@ namespace slskd
}
}
+ // if a blacklist file is specified, process it too.
+ if (!string.IsNullOrEmpty(BlacklistFile))
+ {
+ try
+ {
+ var lines = System.IO.File.ReadAllLines(BlacklistFile).Where(line => !string.IsNullOrWhiteSpace(line) && !line.StartsWith("#"));
+ foreach (var cidr in lines)
+ {
+ try
+ {
+ cache.Add(IPAddressRange.Parse(cidr));
+ }
+ catch (Exception ex)
+ {
+ results.Add(new ValidationResult($"CIDR {cidr} in blacklist file is invalid: {ex.Message}"));
+ }
+ }
+ }
+ catch (Exception ex)
+ {
+ results.Add(new ValidationResult($"CIDR blacklist file {BlacklistFile} could not be loaded: {ex.Message}"));
+ }
+ }
+
+ BlacklistedCIDRs = cache.Distinct().ToList();
+
return results;
}
}
@@ -2066,4 +2112,4 @@ namespace slskd
}
}
}
-} \ No newline at end of file
+}