diff options
| author | JP Dillingham <jp@dillingham.me> | 2023-10-21 20:10:08 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-10-21 20:10:08 -0500 |
| commit | 76af9740010154588cd538bbabe2073e14b43a74 (patch) | |
| tree | 1ba839465fee28e822baace9668cac3d13d309aa | |
| parent | d84bc1c82252928c13dbaa2aecaea5c4be0285a4 (diff) | |
| parent | 8ed2785f815d7dfbe1de0d7c0922f987e946c94a (diff) | |
| download | slskd-76af9740010154588cd538bbabe2073e14b43a74.tar.gz | |
Merge pull request #981 from slskd/blacklist
Add the ability to blacklist by IP address (CIDR)
| -rw-r--r-- | config/slskd.example.yml | 2 | ||||
| -rw-r--r-- | src/slskd/Application.cs | 28 | ||||
| -rw-r--r-- | src/slskd/Core/Options.cs | 32 |
3 files changed, 57 insertions, 5 deletions
diff --git a/config/slskd.example.yml b/config/slskd.example.yml index cc308e8d..138a5cb5 100644 --- a/config/slskd.example.yml +++ b/config/slskd.example.yml @@ -70,6 +70,8 @@ # blacklisted: # members: # - <username to blacklist> +# cidrs: +# - <CIDR to blacklist, e.g. 255.255.255.255/32> # user_defined: # my_buddies: # upload: diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 89ff6b27..aca75418 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -36,6 +36,7 @@ namespace slskd using Microsoft.AspNetCore.SignalR; using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Hosting; + using NetTools; using Serilog; using Serilog.Events; using slskd.Configuration; @@ -457,8 +458,9 @@ namespace slskd private Task EnqueueDownload(string username, IPEndPoint endpoint, string filename) { - if (Options.Groups.Blacklisted.Members.Contains(username)) + if (IsBlacklisted(username, endpoint.Address)) { + Log.Information("Rejected enqueue request for blacklisted user {Username} ({IP})", username, endpoint.Address); return Task.FromException(new DownloadEnqueueException($"File not shared.")); } @@ -475,8 +477,9 @@ namespace slskd { Metrics.Browse.RequestsReceived.Inc(1); - if (Options.Groups.Blacklisted.Members.Contains(username)) + if (IsBlacklisted(username, endpoint.Address)) { + Log.Information("Returned empty browse listing for blacklisted user {Username} ({IP})", username, endpoint.Address); return new BrowseResponse(); } @@ -899,8 +902,9 @@ namespace slskd /// <returns>A Task resolving an instance of Soulseek.Directory containing the contents of the requested directory.</returns> private async Task<Soulseek.Directory> DirectoryContentsResponseResolver(string username, IPEndPoint endpoint, int token, string directory) { - if (Options.Groups.Blacklisted.Members.Contains(username)) + if (IsBlacklisted(username, endpoint.Address)) { + Log.Information("Returned empty directory listing for blacklisted user {Username} ({IP})", username, endpoint.Address); return new Soulseek.Directory(directory); } @@ -1094,8 +1098,9 @@ namespace slskd { Metrics.Search.RequestsReceived.Inc(1); - if (Options.Groups.Blacklisted.Members.Contains(username)) + if (IsBlacklisted(username)) { + Log.Information("Returned empty search response for blacklisted user {Username}", username); return new SearchResponse(username, token, hasFreeUploadSlot: false, uploadSpeed: 0, queueLength: int.MaxValue, fileList: Enumerable.Empty<Soulseek.File>()); } @@ -1316,5 +1321,20 @@ namespace slskd throw; } } + + private bool IsBlacklisted(string username, IPAddress ipAddress = null) + { + if (Options.Groups.Blacklisted.Members.Contains(username)) + { + return true; + } + + if (ipAddress is not null && Options.Groups.Blacklisted.Cidrs.Select(c => IPAddressRange.Parse(c)).Any(range => range.Contains(ipAddress))) + { + return true; + } + + return false; + } } } diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 2b9c3455..85cf133e 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -850,6 +850,7 @@ namespace slskd /// <summary> /// Gets options for the blacklisted user group. /// </summary> + [Validate] public BlacklistedOptions Blacklisted { get; init; } = new BlacklistedOptions(); /// <summary> @@ -886,12 +887,41 @@ namespace slskd /// <summary> /// Built in blacklisted group options. /// </summary> - public class BlacklistedOptions + public class BlacklistedOptions : IValidatableObject { /// <summary> /// Gets the list of group member usernames. /// </summary> public string[] Members { get; init; } = Array.Empty<string>(); + + /// <summary> + /// Gets the list of group CIDRs. + /// </summary> + public string[] Cidrs { get; init; } = Array.Empty<string>(); + + /// <summary> + /// Extended validation. + /// </summary> + /// <param name="validationContext"></param> + /// <returns></returns> + public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) + { + var results = new List<ValidationResult>(); + + foreach (var cidr in Cidrs) + { + try + { + _ = IPAddressRange.Parse(cidr); + } + catch (Exception ex) + { + results.Add(new ValidationResult($"CIDR {cidr} is invalid: {ex.Message}")); + } + } + + return results; + } } /// <summary> |
