aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Users/UserService.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/slskd/Users/UserService.cs')
-rw-r--r--src/slskd/Users/UserService.cs179
1 files changed, 141 insertions, 38 deletions
diff --git a/src/slskd/Users/UserService.cs b/src/slskd/Users/UserService.cs
index 2939fd17..3ce827c8 100644
--- a/src/slskd/Users/UserService.cs
+++ b/src/slskd/Users/UserService.cs
@@ -1,29 +1,48 @@
-// <copyright file="UserService.cs" company="slskd Team">
-// Copyright (c) slskd Team. All rights reserved.
-//
-// This program is free software: you can redistribute it and/or modify
-// it under the terms of the GNU Affero General Public License as published
-// by the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU Affero General Public License for more details.
-//
-// You should have received a copy of the GNU Affero General Public License
-// along with this program. If not, see https://www.gnu.org/licenses/.
+// <copyright file="UserService.cs" company="JP Dillingham">
+// ▄▄▄▄ ▄▄▄▄ ▄▄▄▄
+// ▄▄▄▄▄▄█ █▄▄▄▄▄█ █▄▄▄▄▄█ █
+// █__ --█ █__ --█ ◄█ - █
+// █▄▄▄▄▄█▄▄█▄▄▄▄▄█▄▄█▄▄█▄▄▄▄▄█
+// ┍━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━ ━ ━┉ ┉ ┉
+// │ Copyright (c) JP Dillingham.
+// │
+// │ This program is free software: you can redistribute it and/or modify
+// │ it under the terms of the GNU Affero General Public License as published
+// │ by the Free Software Foundation, version 3.
+// │
+// │ This program is distributed in the hope that it will be useful,
+// │ but WITHOUT ANY WARRANTY; without even the implied warranty of
+// │ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// │ GNU Affero General Public License for more details.
+// │
+// │ You should have received a copy of the GNU Affero General Public License
+// │ along with this program. If not, see https://www.gnu.org/licenses/.
+// │
+// │ This program is distributed with Additional Terms pursuant to Section 7
+// │ of the AGPLv3. See the LICENSE file in the root directory of this
+// │ project for the complete terms and conditions.
+// │
+// │ https://slskd.org
+// │
+// ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ╌ ╌╌╌╌ ╌
+// │ SPDX-FileCopyrightText: JP Dillingham
+// │ SPDX-License-Identifier: AGPL-3.0-only
+// ╰───────────────────────────────────────────╶──── ─ ─── ─ ── ──┈ ┈
// </copyright>
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 Microsoft.Extensions.Internal;
using NetTools;
using Serilog;
using Soulseek;
@@ -70,15 +89,20 @@ namespace slskd.Users
/// </summary>
/// <param name="soulseekClient"></param>
/// <param name="optionsMonitor"></param>
+ /// <param name="systemClock"></param>
public UserService(
ISoulseekClient soulseekClient,
- IOptionsMonitor<Options> optionsMonitor)
+ IOptionsMonitor<Options> optionsMonitor,
+ ISystemClock systemClock = null)
{
Client = soulseekClient;
OptionsMonitor = optionsMonitor;
OptionsMonitor.OnChange(options => Configure(options));
+ InjectedClock = systemClock ?? new SystemClock();
+ BlacklistDecisionCache = new MemoryCache(new MemoryCacheOptions { Clock = InjectedClock, ExpirationScanFrequency = TimeSpan.FromMinutes(1), SizeLimit = 1000 });
+
// updates may be sent unsolicited from the server, so update when we get them. binding these events will cause
// multiple redundant round trips when initially watching a user or when explicitly requesting via
// GetStatus/GetStatistics. this is wasteful, but there's no functional side effect.
@@ -122,6 +146,9 @@ namespace slskd.Users
private ILogger Log { get; set; } = Serilog.Log.ForContext<UserService>();
private IOptionsMonitor<Options> OptionsMonitor { get; }
private Blacklist Blacklist { get; } = new Blacklist();
+ private IReadOnlyCollection<Regex> CompiledBlacklistPatterns { get; set; } = [];
+ private ISystemClock InjectedClock { get; }
+ private MemoryCache BlacklistDecisionCache { get; }
/// <summary>
/// Gets or sets the internal cache of User data.
@@ -147,31 +174,45 @@ namespace slskd.Users
private ConcurrentDictionary<string, bool> WatchedUsernamesDictionary { get; set; } = new ConcurrentDictionary<string, bool>();
/// <summary>
- /// Gets the name of the group for the specified <paramref name="username"/>.
+ /// Gets the name of the group for the specified <paramref name="username"/> from cached data.
/// </summary>
- /// <remarks>The group name is fetched from cached data, and lookups should always be fast.</remarks>
+ /// <remarks>
+ /// <para>
+ /// Excludes the 'blacklisted' group for performance; use <see cref="IsBlacklisted"/> to guard network operations instead.
+ /// </para>
+ /// <para>
+ /// This is an extremely hot path, and the value returned may be approximated or incorrect in the case of a
+ /// cache miss in order to provide low latency values.
+ /// </para>
+ /// </remarks>
/// <param name="username">The username of the peer.</param>
/// <returns>The group for the specified username.</returns>
public string GetGroup(string username)
{
- // note: this is an extremely hot path; keep the work done to an absolute minimum.
+ // users end up in this dictionary for a few reasons, including but maybe not limited to:
+ // * they show up in the user defined group list somewhere
+ // * the server told us they have privileges when we logged in
+ // * we 'watched' them
+ // * we fetched statistics or status for them
+ // if we handle any of their data they'll probably wind up here. if they show up in the config,
+ // their Group property will be set. otherwise we must resolve their group dynamically.
if (UserDictionary.TryGetValue(username ?? string.Empty, out var user))
{
- if (IsBlacklisted(user.Username))
- {
- return Application.BlacklistedGroup;
- }
-
+ // privileged users trump everything, and we resolve their group accordingly regardless of all other factors
if (user.Status?.IsPrivileged ?? false)
{
return Application.PrivilegedGroup;
}
+ // if the user isn't privileged and the user has put their username in any user defined group, that's
+ // their group. they aren't subjected to leecher checks.
if (user.Group != null)
{
return user.Group;
}
+ // check to see if they are a leecher. they may be in the dictionary because we checked their stats.
+ // if we don't have their stats we don't have enough info to make the call, group is inconclusive so fall through
var thresholds = OptionsMonitor.CurrentValue.Transfers.Groups.Leechers.Thresholds;
if (user.Statistics?.FileCount < thresholds.Files || user.Statistics?.DirectoryCount < thresholds.Directories)
@@ -271,33 +312,79 @@ namespace slskd.Users
/// <summary>
/// Gets a value indicating whether the specified <paramref name="username"/> and/or <paramref name="ipAddress"/> are blacklisted.
/// </summary>
+ /// <remarks>
+ /// This method may be taxing if the user has defined a blacklist file or large number of patterns. Call it to 'guard'
+ /// actions at the network level (with the exception of search results where latency is sensitive), but don't
+ /// use it to check once the user is 'in' the system (e.g. don't call this in the upload governor or queue logic).
+ /// </remarks>
/// <param name="username">The username to check.</param>
/// <param name="ipAddress">The IPAddress to check, if available.</param>
+ /// <param name="bypassCache">A value indicating whether to compute blacklisted status on a cache miss.</param>
/// <returns>A value indicating whether the specified user and/or IP are blacklisted.</returns>
- public bool IsBlacklisted(string username, IPAddress ipAddress = null)
+ public bool IsBlacklisted(string username, IPAddress ipAddress = null, bool bypassCache = true)
{
- var blacklist = OptionsMonitor.CurrentValue.Transfers.Groups.Blacklisted;
+ ArgumentNullException.ThrowIfNull(username);
- if (blacklist.Members.Contains(username))
+ if (BlacklistDecisionCache.TryGetValue<bool>(username, out var cached))
{
- return true;
+ return cached;
}
- // 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)))
+ // if we have a cache miss and the caller has specified bypassCache = false, assume that a cache miss
+ // means that the user is not blacklisted. we will only specify bypassCache = false when we're checking
+ // in a hot path, which at the time of this writing is only when determining whether to search shares for a
+ // request. we will check again before sending the results with bypassCache = true, and we should always
+ // follow this pattern when using bypassCache = false (specifically, check again before sending something)
+ if (!bypassCache)
{
- return true;
+ return false;
}
- // check the managed blacklist loaded from a third party blacklist file
- if (ipAddress is not null && Blacklist.Contains(ipAddress))
+ var blacklisted = IsBlacklistedInternal(username, ipAddress);
+
+ BlacklistDecisionCache.Set(
+ key: username,
+ value: blacklisted,
+ options: new MemoryCacheEntryOptions
+ {
+ AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(10),
+ Size = 1,
+ });
+
+ return blacklisted;
+
+ bool IsBlacklistedInternal(string username, IPAddress iPAddress = null)
{
- return true;
- }
+ // first, check to see if the name appears explicitly in the config
+ var blacklist = OptionsMonitor.CurrentValue.Transfers.Groups.Blacklisted;
- return false;
+ if (blacklist.Members.Contains(username))
+ {
+ return true;
+ }
+
+ // check to see if the username matches any patterns
+ if (CompiledBlacklistPatterns.Any(pattern => pattern.IsMatch(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 the managed blacklist loaded from a third party blacklist file
+ if (ipAddress is not null && Blacklist.Contains(ipAddress))
+ {
+ return true;
+ }
+
+ return false;
+ }
}
/// <summary>
@@ -404,6 +491,19 @@ namespace slskd.Users
LastOptionsHash = optionsHash;
}
+ // recompile blacklist patterns, regardless of whether they changed (this cheap to do)
+ var regexOptions = RegexOptions.Compiled;
+
+ 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.
@@ -449,6 +549,9 @@ namespace slskd.Users
LastBlacklistOptionsHash = blacklistOptionsHash;
}
+
+ // invalidate cached blacklist decisions
+ BlacklistDecisionCache.Clear();
}
private void Reset()