From b904f3bf845b136377dfef417c7857720d8d8d43 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 15 Oct 2023 17:32:07 -0500 Subject: add CIDR list to blacklisted group --- src/slskd/Core/Options.cs | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'src') 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 /// /// Gets options for the blacklisted user group. /// + [Validate] public BlacklistedOptions Blacklisted { get; init; } = new BlacklistedOptions(); /// @@ -886,12 +887,41 @@ namespace slskd /// /// Built in blacklisted group options. /// - public class BlacklistedOptions + public class BlacklistedOptions : IValidatableObject { /// /// Gets the list of group member usernames. /// public string[] Members { get; init; } = Array.Empty(); + + /// + /// Gets the list of group CIDRs. + /// + public string[] Cidrs { get; init; } = Array.Empty(); + + /// + /// Extended validation. + /// + /// + /// + public IEnumerable Validate(ValidationContext validationContext) + { + var results = new List(); + + foreach (var cidr in Cidrs) + { + try + { + _ = IPAddressRange.Parse(cidr); + } + catch (Exception ex) + { + results.Add(new ValidationResult($"CIDR {cidr} is invalid: {ex.Message}")); + } + } + + return results; + } } /// -- cgit v1.2.3 From 89528aa6447ec4889c103a1ce26f650e35d4781c Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 15 Oct 2023 17:33:22 -0500 Subject: check endpoints against blacklisted cidrs --- src/slskd/Application.cs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 89ff6b27..fc9f71c6 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,7 +458,7 @@ namespace slskd private Task EnqueueDownload(string username, IPEndPoint endpoint, string filename) { - if (Options.Groups.Blacklisted.Members.Contains(username)) + if (IsBlacklisted(username, endpoint.Address)) { return Task.FromException(new DownloadEnqueueException($"File not shared.")); } @@ -475,7 +476,7 @@ namespace slskd { Metrics.Browse.RequestsReceived.Inc(1); - if (Options.Groups.Blacklisted.Members.Contains(username)) + if (IsBlacklisted(username, endpoint.Address)) { return new BrowseResponse(); } @@ -899,7 +900,7 @@ namespace slskd /// A Task resolving an instance of Soulseek.Directory containing the contents of the requested directory. private async Task DirectoryContentsResponseResolver(string username, IPEndPoint endpoint, int token, string directory) { - if (Options.Groups.Blacklisted.Members.Contains(username)) + if (IsBlacklisted(username, endpoint.Address)) { return new Soulseek.Directory(directory); } @@ -1094,7 +1095,7 @@ namespace slskd { Metrics.Search.RequestsReceived.Inc(1); - if (Options.Groups.Blacklisted.Members.Contains(username)) + if (IsBlacklisted(username)) { return new SearchResponse(username, token, hasFreeUploadSlot: false, uploadSpeed: 0, queueLength: int.MaxValue, fileList: Enumerable.Empty()); } @@ -1316,5 +1317,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; + } } } -- cgit v1.2.3 From a7012184f27c511e49ee677be4513b6b7c586c62 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 21 Oct 2023 20:06:31 -0500 Subject: log when preventing blacklisted action --- src/slskd/Application.cs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index fc9f71c6..aca75418 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -460,6 +460,7 @@ namespace slskd { 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.")); } @@ -478,6 +479,7 @@ namespace slskd if (IsBlacklisted(username, endpoint.Address)) { + Log.Information("Returned empty browse listing for blacklisted user {Username} ({IP})", username, endpoint.Address); return new BrowseResponse(); } @@ -902,6 +904,7 @@ namespace slskd { if (IsBlacklisted(username, endpoint.Address)) { + Log.Information("Returned empty directory listing for blacklisted user {Username} ({IP})", username, endpoint.Address); return new Soulseek.Directory(directory); } @@ -1097,6 +1100,7 @@ namespace slskd 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()); } -- cgit v1.2.3