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/slskd/Core/Options.cs') 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 fcde0fb58aa3a4e8aa7fc268c286842de39a9bb5 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 17 Mar 2024 17:34:05 -0500 Subject: ensure blacklist file exists and can be read --- src/slskd/Core/Options.cs | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) (limited to 'src/slskd/Core/Options.cs') diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index add3953a..e0ee1aa4 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -994,16 +994,8 @@ namespace slskd /// /// 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; - } + [FileExists(FileAccess.Read)] + public string File { get; init; } /// /// Extended validation. -- cgit v1.2.3 From 46db12ccfec443341d0a64888e7601667d0b6f0d Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 17 Mar 2024 17:34:27 -0500 Subject: remove i/o --- src/slskd/Core/Options.cs | 34 ++-------------------------------- 1 file changed, 2 insertions(+), 32 deletions(-) (limited to 'src/slskd/Core/Options.cs') diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index e0ee1aa4..c38cf85e 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -24,6 +24,7 @@ namespace slskd using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Diagnostics; + using System.IO; using System.Linq; using System.Text.Json.Serialization; using System.Text.RegularExpressions; @@ -981,11 +982,6 @@ 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. /// @@ -1019,32 +1015,6 @@ 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; } } @@ -1883,7 +1853,7 @@ namespace slskd [Argument(default, "https-cert-pfx")] [EnvironmentVariable("HTTPS_CERT_PFX")] [Description("path to X509 certificate .pfx")] - [FileExists] + [FileExists(FileAccess.Read)] [RequiresRestart] public string Pfx { get; init; } -- cgit v1.2.3 From fabfad8e17222514c14ca540c3b0a9911e816f29 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 21 Apr 2024 18:14:15 -0500 Subject: add Blacklist options --- src/slskd/Core/Options.cs | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) (limited to 'src/slskd/Core/Options.cs') diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index c38cf85e..e99eb85f 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -234,6 +234,12 @@ namespace slskd [Validate] public GroupsOptions Groups { get; init; } = new GroupsOptions(); + /// + /// Gets blacklist options. + /// + [Validate] + public BlacklistOptions Blacklist { get; init; } = new BlacklistOptions(); + /// /// Gets filter options. /// @@ -987,12 +993,6 @@ namespace slskd /// public string[] Cidrs { get; init; } = Array.Empty(); - /// - /// Gets the CIDR blacklist file path. - /// - [FileExists(FileAccess.Read)] - public string File { get; init; } - /// /// Extended validation. /// @@ -1106,6 +1106,27 @@ namespace slskd public bool Swagger { get; init; } = false; } + public class BlacklistOptions + { + /// + /// Gets a value indicating whether blacklist file support should be enabled. + /// + [Argument(default, "enable-blacklist")] + [EnvironmentVariable("BLACKLIST")] + [Description("enable blacklist file support")] + [RequiresRestart] + public bool Enabled { get; init; } + + /// + /// Gets the path to the blacklist file. + /// + [Argument(default, "blacklist-file")] + [EnvironmentVariable("BLACKLIST_FILE")] + [Description("path to blacklist file")] + [FileExists(FileAccess.Read)] + public string File { get; init; } + } + /// /// Filter options. /// -- cgit v1.2.3 From 6126fd7508c7ebb503ec29db81eb9e7e17170b71 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 27 Apr 2024 18:44:04 -0500 Subject: add validation for blacklist format --- src/slskd/Core/Options.cs | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) (limited to 'src/slskd/Core/Options.cs') diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index e99eb85f..afc9b07f 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -1106,7 +1106,10 @@ namespace slskd public bool Swagger { get; init; } = false; } - public class BlacklistOptions + /// + /// Blacklist options. + /// + public class BlacklistOptions : IValidatableObject { /// /// Gets a value indicating whether blacklist file support should be enabled. @@ -1125,6 +1128,36 @@ namespace slskd [Description("path to blacklist file")] [FileExists(FileAccess.Read)] public string File { get; init; } + + /// + /// Extended validation. + /// + /// + /// + public IEnumerable Validate(ValidationContext validationContext) + { + var results = new List(); + + if (!Enabled) + { + return results; + } + + // loading/validating the entire list will be costly on low spec systems + // just make sure that we can detect a valid format and leave it at that + // if there's a problem with any of the entries, the load will fail and + // kill the application + try + { + _ = slskd.Blacklist.DetectFormat(File); + } + catch (FormatException ex) + { + results.Add(new ValidationResult(ex.Message)); + } + + return results; + } } /// -- cgit v1.2.3 From 28372896b8dae9fd20b48a2a6bc8e077e91e18ea Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 28 Apr 2024 17:25:20 -0500 Subject: revert cidr cache --- src/slskd/Core/Options.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/slskd/Core/Options.cs') diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index afc9b07f..0ede272d 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -1001,13 +1001,12 @@ namespace slskd public IEnumerable Validate(ValidationContext validationContext) { var results = new List(); - var cache = new List(); foreach (var cidr in Cidrs ?? Array.Empty()) { try { - cache.Add(IPAddressRange.Parse(cidr)); + _ = IPAddressRange.Parse(cidr); } catch (Exception ex) { -- cgit v1.2.3 From 0f79d9f8fd07690e5e3757c1be6f23e1c205dd86 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 28 Apr 2024 18:02:15 -0500 Subject: make validation work --- src/slskd/Core/Blacklist.cs | 2 +- src/slskd/Core/Options.cs | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) (limited to 'src/slskd/Core/Options.cs') diff --git a/src/slskd/Core/Blacklist.cs b/src/slskd/Core/Blacklist.cs index 56414ad6..76f0b37f 100644 --- a/src/slskd/Core/Blacklist.cs +++ b/src/slskd/Core/Blacklist.cs @@ -151,7 +151,7 @@ public class Blacklist } } - throw new FormatException($"Failed to detect blacklist format. Only CIDR, P2P and DAT formats are supported"); + throw new FormatException("Failed to detect blacklist format. Only CIDR, P2P and DAT formats are supported"); } /// diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 0ede272d..81dc09f6 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -1148,11 +1148,12 @@ namespace slskd // kill the application try { - _ = slskd.Blacklist.DetectFormat(File); + // async not supported here, .Result is all we have + _ = slskd.Blacklist.DetectFormat(File).Result; } - catch (FormatException ex) + catch { - results.Add(new ValidationResult(ex.Message)); + results.Add(new ValidationResult("Failed to detect blacklist format. Only CIDR, P2P and DAT formats are supported")); } return results; -- cgit v1.2.3