From b39b8606219bd3957b28310b2a4b42f2f8ce3f21 Mon Sep 17 00:00:00 2001 From: Graham Sutherland Date: Fri, 26 Jan 2024 22:25:14 +0000 Subject: 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. --- src/slskd/Core/Options.cs | 50 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) (limited to 'src') 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 @@ -927,11 +927,30 @@ namespace slskd /// public string[] Members { get; init; } = Array.Empty(); + /// + /// A pre-parsed cache of IPAddressRange objects describing all blacklisted ranges. + /// + private List BlacklistedCIDRs = new List(); + /// /// Gets the list of group CIDRs. /// public string[] Cidrs { get; init; } = Array.Empty(); + /// + /// Gets the CIDR blacklist file path. + /// + public string BlacklistFile { get; init; } = string.Empty; + + /// + /// Gets address ranges for all blacklisted CIDRs defined by the configuration file and the blacklist file. + /// + /// An IEnumerable of IPAddressRange objects describing the blacklisted CIDRs. + public IEnumerable GetBlacklistedCIDRs() + { + return BlacklistedCIDRs; + } + /// /// Extended validation. /// @@ -940,12 +959,13 @@ namespace slskd public IEnumerable Validate(ValidationContext validationContext) { var results = new List(); + var cache = new List(); 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 +} -- cgit v1.2.3 From 0b194f1922f1eedf0e4cf7d9dce59b47e7f5389a Mon Sep 17 00:00:00 2001 From: Graham Sutherland Date: Fri, 26 Jan 2024 22:28:14 +0000 Subject: Update IsBlacklisted to use the new GetBlacklistedCIDRs function. This ensures that blacklisted CIDRs from both the config file and the blacklist file are considered. --- src/slskd/Users/UserService.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/slskd/Users/UserService.cs b/src/slskd/Users/UserService.cs index 5b15b671..324e3743 100644 --- a/src/slskd/Users/UserService.cs +++ b/src/slskd/Users/UserService.cs @@ -273,7 +273,7 @@ namespace slskd.Users return true; } - if (ipAddress is not null && blacklist.Cidrs.Select(c => IPAddressRange.Parse(c)).Any(range => range.Contains(ipAddress))) + if (ipAddress is not null && blacklist.GetBlacklistedCIDRs().Any(range => range.Contains(ipAddress))) { return true; } @@ -410,4 +410,4 @@ namespace slskd.Users updateValueFactory: (key, user) => user with { Username = username, Status = status }); } } -} \ No newline at end of file +} -- cgit v1.2.3