diff options
| author | JP Dillingham <jp@dillingham.me> | 2024-03-11 21:42:13 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-11 21:42:13 -0500 |
| commit | d242a9bfeee2a87a9c982516edbb61192669fb02 (patch) | |
| tree | b3a2687d2f96f79e38bd4e1e343fc4b5afdb436f | |
| parent | e3fc5c61afb1c17018e6ea1474a84c7c2e500162 (diff) | |
| parent | 01eb8baea79339ac7683d1a8b0078427e3be5c75 (diff) | |
| download | slskd-d242a9bfeee2a87a9c982516edbb61192669fb02.tar.gz | |
Merge pull request #1062 from gsuberland/blacklist-file
Add support for CIDR blacklist files
| -rw-r--r-- | config/slskd.example.yml | 1 | ||||
| -rw-r--r-- | docs/config.md | 3 | ||||
| -rw-r--r-- | src/slskd/Core/Options.cs | 50 | ||||
| -rw-r--r-- | src/slskd/Users/UserService.cs | 4 |
4 files changed, 53 insertions, 5 deletions
diff --git a/config/slskd.example.yml b/config/slskd.example.yml index 4e34098b..34e1f7fb 100644 --- a/config/slskd.example.yml +++ b/config/slskd.example.yml @@ -108,6 +108,7 @@ # - <username to blacklist> # cidrs: # - <CIDR to blacklist, e.g. 255.255.255.255/32> +# blacklistfile: <path to file containing CIDRs to blacklist> # user_defined: # my_buddies: # upload: diff --git a/docs/config.md b/docs/config.md index 924e4de9..265b3fd2 100644 --- a/docs/config.md +++ b/docs/config.md @@ -425,7 +425,7 @@ Blacklisted users are prevented from: - Retrieving directory contents - Enqueueing downloads -Users can be blacklisted by adding their username to the `members` list. Additionally, users can be blacklisted by IP address, or range of addresses by adding a CIDR entry to the `cidrs` list. +Users can be blacklisted by adding their username to the `members` list. Users can also be blacklisted by IP address, or range of addresses by adding a CIDR entry to the `cidrs` list. In addition, an external blacklist file containing CIDRs can be specified using `blacklistfile`, with each CIDR on its own line. Empty lines and lines starting with `#` are ignored. Users added to the blacklist will be blocked from enqueueing any new files. Any existing active or queued transfers will need to be cancelled manually. @@ -437,6 +437,7 @@ groups: - <username to blacklist> cidrs: - <CIDR to blacklist, e.g. 255.255.255.255/32> + blacklistfile: 'D:\blacklist.txt' ``` ## User Defined Groups diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index cfe78eb1..add3953a 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -982,11 +982,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> @@ -994,12 +1013,13 @@ namespace slskd public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) { var results = new List<ValidationResult>(); + var cache = new List<IPAddressRange>(); foreach (var cidr in Cidrs ?? Array.Empty<string>()) { try { - _ = IPAddressRange.Parse(cidr); + cache.Add(IPAddressRange.Parse(cidr)); } catch (Exception ex) { @@ -1007,6 +1027,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; } } @@ -2078,4 +2124,4 @@ namespace slskd } } } -}
\ No newline at end of file +} 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 +} |
