aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-04-22 08:13:00 -0500
committerJP Dillingham <jp@dillingham.me>2026-04-22 08:13:00 -0500
commitaaf49f0a498e6597668dac2b812431ab28dfcb51 (patch)
treed83ba1f5a1551c36c7d9f69a1fb49e6aa7282769
parentd2a71a7340c5caf7923824910f112be5b169378d (diff)
parentd49b11b2e74f872ef79a3966bac22601191ba773 (diff)
downloadslskd-aaf49f0a498e6597668dac2b812431ab28dfcb51.tar.gz
Merge branch 'blacklist-regex' of github.com:slskd/slskd into blacklist-regex
-rw-r--r--src/slskd/Users/Types/User.cs3
-rw-r--r--src/slskd/Users/UserService.cs150
2 files changed, 78 insertions, 75 deletions
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
/// <summary>
/// Gets the user's configured group.
/// </summary>
+ /// <remarks>
+ /// This is the group under which the username appears in config, *not* the group derived at runtime.
+ /// </remarks>
public string Group { get; init; }
/// <summary>
diff --git a/src/slskd/Users/UserService.cs b/src/slskd/Users/UserService.cs
index 0eb541e0..469680cf 100644
--- a/src/slskd/Users/UserService.cs
+++ b/src/slskd/Users/UserService.cs
@@ -141,7 +141,8 @@ namespace slskd.Users
private IOptionsMonitor<Options> OptionsMonitor { get; }
private Blacklist Blacklist { get; } = new Blacklist();
private IReadOnlyCollection<Regex> CompiledBlacklistPatterns { get; set; } = [];
- private MemoryCache GetGroupCache { get; } = new MemoryCache(new MemoryCacheOptions() { });
+ private MemoryCache BlacklistDecisionCache { get; } =
+ new MemoryCache(new MemoryCacheOptions { ExpirationScanFrequency = TimeSpan.FromMinutes(1), SizeLimit = 1000 });
/// <summary>
/// Gets or sets the internal cache of User data.
@@ -174,49 +175,32 @@ namespace slskd.Users
/// <returns>The group for the specified username.</returns>
public string GetGroup(string username)
{
- if (GetGroupCache.TryGetValue<string>(username, out var cached))
+ if (UserDictionary.TryGetValue(username ?? string.Empty, out var user))
{
- return cached;
- }
- else
- {
- var group = GetGroupInternal(username);
-
- GetGroupCache.Set<string>(username, group, new MemoryCacheEntryOptions { SlidingExpiration = TimeSpan.FromMinutes(10) });
-
- return group;
- }
-
- // note: this is an extremely hot path; keep the work done to an absolute minimum.
- string GetGroupInternal(string username)
- {
- if (UserDictionary.TryGetValue(username ?? string.Empty, out var user))
+ if (IsBlacklisted(user.Username))
{
- if (IsBlacklisted(user.Username))
- {
- return Application.BlacklistedGroup;
- }
+ return Application.BlacklistedGroup;
+ }
- if (user.Status?.IsPrivileged ?? false)
- {
- return Application.PrivilegedGroup;
- }
+ if (user.Status?.IsPrivileged ?? false)
+ {
+ return Application.PrivilegedGroup;
+ }
- if (user.Group != null)
- {
- return user.Group;
- }
+ if (user.Group != null)
+ {
+ return user.Group;
+ }
- var thresholds = OptionsMonitor.CurrentValue.Transfers.Groups.Leechers.Thresholds;
+ var thresholds = OptionsMonitor.CurrentValue.Transfers.Groups.Leechers.Thresholds;
- if (user.Statistics?.FileCount < thresholds.Files || user.Statistics?.DirectoryCount < thresholds.Directories)
- {
- return Application.LeecherGroup;
- }
+ if (user.Statistics?.FileCount < thresholds.Files || user.Statistics?.DirectoryCount < thresholds.Directories)
+ {
+ return Application.LeecherGroup;
}
-
- return Application.DefaultGroup;
}
+
+ return Application.DefaultGroup;
}
/// <summary>
@@ -312,47 +296,59 @@ namespace slskd.Users
/// <returns>A value indicating whether the specified user and/or IP are blacklisted.</returns>
public bool IsBlacklisted(string username, IPAddress ipAddress = null)
{
- if (string.IsNullOrEmpty(username))
+ ArgumentNullException.ThrowIfNull(username);
+
+ if (BlacklistDecisionCache.TryGetValue<bool>(username, out var u))
{
- throw new ArgumentNullException(nameof(username));
+ return u;
}
- var blacklist = OptionsMonitor.CurrentValue.Transfers.Groups.Blacklisted;
+ var b = IsBlacklistedInternal(username, ipAddress);
- if (blacklist.Members.Contains(username))
- {
- return true;
- }
+ BlacklistDecisionCache.Set(username, b, new MemoryCacheEntryOptions { SlidingExpiration = TimeSpan.FromMinutes(10) });
+
+ return b;
- // 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)
+ 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;
+ }
}
/// <summary>
@@ -456,21 +452,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.
@@ -516,6 +513,9 @@ namespace slskd.Users
LastBlacklistOptionsHash = blacklistOptionsHash;
}
+
+ // invalidate cached blacklist decisions
+ BlacklistDecisionCache.Clear();
}
private void Reset()