aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2023-10-21 20:10:08 -0500
committerGitHub <noreply@github.com>2023-10-21 20:10:08 -0500
commit76af9740010154588cd538bbabe2073e14b43a74 (patch)
tree1ba839465fee28e822baace9668cac3d13d309aa /src
parentd84bc1c82252928c13dbaa2aecaea5c4be0285a4 (diff)
parent8ed2785f815d7dfbe1de0d7c0922f987e946c94a (diff)
downloadslskd-76af9740010154588cd538bbabe2073e14b43a74.tar.gz
Merge pull request #981 from slskd/blacklist
Add the ability to blacklist by IP address (CIDR)
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Application.cs28
-rw-r--r--src/slskd/Core/Options.cs32
2 files changed, 55 insertions, 5 deletions
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>