From ee041dc6c810982393745df7502c1630876bca6b Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 21 Apr 2026 20:59:03 -0500 Subject: clarify purpose of group --- src/slskd/Users/Types/User.cs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src') diff --git a/src/slskd/Users/Types/User.cs b/src/slskd/Users/Types/User.cs index 7297d747..473bedfb 100644 --- a/src/slskd/Users/Types/User.cs +++ b/src/slskd/Users/Types/User.cs @@ -42,6 +42,9 @@ namespace slskd.Users /// /// Gets the user's configured group. /// + /// + /// This is the group under which the username appears in config, *not* the group derived at runtime. + /// public string Group { get; init; } /// -- cgit v1.2.3 From d49b11b2e74f872ef79a3966bac22601191ba773 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 21 Apr 2026 22:17:23 -0500 Subject: add a memory cache for blacklist decisions, recompile patterns and invalidate cache on config change --- src/slskd/Users/UserService.cs | 97 ++++++++++++++++++++++++++---------------- 1 file changed, 60 insertions(+), 37 deletions(-) (limited to 'src') diff --git a/src/slskd/Users/UserService.cs b/src/slskd/Users/UserService.cs index 73bf1118..9a29e567 100644 --- a/src/slskd/Users/UserService.cs +++ b/src/slskd/Users/UserService.cs @@ -34,12 +34,14 @@ using Microsoft.Extensions.Options; namespace slskd.Users { + using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text.RegularExpressions; using System.Threading.Tasks; + using Microsoft.Extensions.Caching.Memory; using NetTools; using Serilog; using Soulseek; @@ -139,6 +141,8 @@ namespace slskd.Users private IOptionsMonitor OptionsMonitor { get; } private Blacklist Blacklist { get; } = new Blacklist(); private IReadOnlyCollection CompiledBlacklistPatterns { get; set; } = []; + private MemoryCache BlacklistDecisionCache { get; } = + new MemoryCache(new MemoryCacheOptions { ExpirationScanFrequency = TimeSpan.FromMinutes(1), SizeLimit = 1000 }); /// /// Gets or sets the internal cache of User data. @@ -293,44 +297,59 @@ namespace slskd.Users /// A value indicating whether the specified user and/or IP are blacklisted. public bool IsBlacklisted(string username, IPAddress ipAddress = null) { - if (username is null) return false; + ArgumentNullException.ThrowIfNull(username); - var blacklist = OptionsMonitor.CurrentValue.Transfers.Groups.Blacklisted; - - if (blacklist.Members.Contains(username)) + if (BlacklistDecisionCache.TryGetValue(username, out var u)) { - return true; + return u; } - // check username patterns (regex). respects the same case sensitivity flag used by other - // user-defined regular expressions throughout the application. - if (blacklist.Patterns.Length > 0) + var b = IsBlacklistedInternal(username, ipAddress); + + BlacklistDecisionCache.Set(username, b, new MemoryCacheEntryOptions { SlidingExpiration = TimeSpan.FromMinutes(10) }); + + return b; + + bool IsBlacklistedInternal(string username, IPAddress iPAddress = null) { - var regexOptions = OptionsMonitor.CurrentValue.Flags.CaseSensitiveRegEx - ? RegexOptions.None - : RegexOptions.IgnoreCase; + // first, check to see if the name appears explicitly in the config + var blacklist = OptionsMonitor.CurrentValue.Transfers.Groups.Blacklisted; - if (blacklist.Patterns.Any(p => Regex.IsMatch(username, p, regexOptions))) + if (blacklist.Members.Contains(username)) { return true; } - } - // check the user-curated list of blacklisted CIDRs that exists along with the list of - // blacklisted usernames. these CIDRs should be one-offs and would not be expected to appear in a - // blacklist supplied by a third party (but might?) - if (ipAddress is not null && blacklist.Cidrs.Select(c => IPAddressRange.Parse(c)).Any(range => range.Contains(ipAddress))) - { - return true; - } + // check username patterns (regex). respects the same case sensitivity flag used by other + // user-defined regular expressions throughout the application. + if (blacklist.Patterns.Length > 0) + { + var regexOptions = OptionsMonitor.CurrentValue.Flags.CaseSensitiveRegEx + ? RegexOptions.None + : RegexOptions.IgnoreCase; - // check the managed blacklist loaded from a third party blacklist file - if (ipAddress is not null && Blacklist.Contains(ipAddress)) - { - return true; - } + if (blacklist.Patterns.Any(p => Regex.IsMatch(username, p, regexOptions))) + { + return true; + } + } - return false; + // check the user-curated list of blacklisted CIDRs that exists along with the list of + // blacklisted usernames. these CIDRs should be one-offs and would not be expected to appear in a + // blacklist supplied by a third party (but might?) + if (ipAddress is not null && blacklist.Cidrs.Select(c => IPAddressRange.Parse(c)).Any(range => range.Contains(ipAddress))) + { + return true; + } + + // check the managed blacklist loaded from a third party blacklist file + if (ipAddress is not null && Blacklist.Contains(ipAddress)) + { + return true; + } + + return false; + } } /// @@ -434,21 +453,22 @@ namespace slskd.Users } } - var regexOptions = RegexOptions.Compiled; - - if (!OptionsMonitor.CurrentValue.Flags.CaseSensitiveRegEx) - { - regexOptions |= RegexOptions.IgnoreCase; - } + LastOptionsHash = optionsHash; + } - CompiledBlacklistPatterns = OptionsMonitor.CurrentValue.Transfers.Groups.Blacklisted.Patterns - .Select(f => new Regex(f, regexOptions)) - .ToList() - .AsReadOnly(); + // recompile blacklist patterns, regardless of whether they changed (this cheap to do) + var regexOptions = RegexOptions.Compiled; - LastOptionsHash = optionsHash; + if (!OptionsMonitor.CurrentValue.Flags.CaseSensitiveRegEx) + { + regexOptions |= RegexOptions.IgnoreCase; } + CompiledBlacklistPatterns = OptionsMonitor.CurrentValue.Transfers.Groups.Blacklisted.Patterns + .Select(f => new Regex(f, regexOptions)) + .ToList() + .AsReadOnly(); + var blacklistOptionsHash = Compute.Sha1Hash(options.Blacklist.ToJson()); // there's no forced re-config of the blacklist; either the config changed or it didn't. @@ -494,6 +514,9 @@ namespace slskd.Users LastBlacklistOptionsHash = blacklistOptionsHash; } + + // invalidate cached blacklist decisions + BlacklistDecisionCache.Clear(); } private void Reset() -- cgit v1.2.3