// // 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/. // using Microsoft.Extensions.Options; namespace slskd.Users { using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Net; using System.Threading.Tasks; using NetTools; using Serilog; using Soulseek; /// /// Provides information and operations for network peers. /// /// /// /// This class maintains a that acts as a non-expiring cache of information /// collected about a user. This includes their statistics (share counts, speed, etc), their status (privileged, etc) /// and, if they are a member of a user-defined group, their group. /// /// /// This class also maintains a to keep track of which usernames have been /// "watched" server side and for which we will therefore receive events when their status changes. /// /// /// If a user's information is in the , it's because we requested it at some point. If that user /// is also "watched", we can assume that the data in the dictionary is up to date and will be kept so. /// /// /// The data in the can continue to grow until -- unlikely -- it contains a record for every /// user on or that was on the network at any point since the last client connect. This is a calculated risk, roughly /// knowing the size of the network, the size of the data being stored, and balanced against the consequences of not having /// a user's data when it is needed (for queue positioning, speed limits, etc). /// /// /// The method acts on cached data _only_. This method should be called within hot paths, /// such as a transfer governor or from the upload queue. We care more that it is fast than if it is stale. If no data for the /// requested user is cached, that user is assumed to be in the default group. /// /// /// The method is similar to , except that if /// the requested user is not cached, it will fetch the user's data and cache it before returning. This method accepts an optional /// parameter that can be used to force a "refresh" of the requested user's data, useful for times when we want the latest data, /// and can afford to wait for it. /// /// public class UserService : IUserService { /// /// Initializes a new instance of the class. /// /// /// public UserService( ISoulseekClient soulseekClient, IOptionsMonitor optionsMonitor) { Client = soulseekClient; OptionsMonitor = optionsMonitor; OptionsMonitor.OnChange(options => Configure(options)); // 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. Client.UserStatisticsChanged += (_, userStatistics) => UpdateStatistics(userStatistics.Username, userStatistics.ToStatistics()); Client.UserStatusChanged += (sender, userStatus) => { UpdateStatus(userStatus.Username, userStatus.ToStatus()); // the server doesn't send statistics events by itself, so when a user status changes, fetch stats at the same time. _ = GetStatisticsAsync(userStatus.Username); }; Client.Connected += (_, _) => Reset(); Client.LoggedIn += (_, _) => Configure(OptionsMonitor.CurrentValue, force: true); Client.PrivilegedUserListReceived += (_, list) => Client_PrivilegedUserListReceived(list); Configure(OptionsMonitor.CurrentValue); } /// /// Gets the list of tracked users. /// public IReadOnlyList Users => UserDictionary.Values.ToList().AsReadOnly(); /// /// Gets the list of watched usernames. /// public IReadOnlyList WatchedUsernames => WatchedUsernamesDictionary.Keys.ToList().AsReadOnly(); private ISoulseekClient Client { get; } private string LastOptionsHash { get; set; } private ILogger Log { get; set; } = Serilog.Log.ForContext(); private IOptionsMonitor OptionsMonitor { get; } /// /// Gets or sets the internal cache of User data. /// /// /// /// This method retrieves the requested user's information from a dictionary, and is therefore a 'get'. This /// information is needed to control who can do what, who is subject to what limits, how to control the upload /// queue, and how to govern speeds. The logic in this method is the 'hottest' in the application and *must* /// remain very simple and _fast_. /// /// /// If this method is called for a user who has not previously been cached, it will return the default group, /// meaning leech, privilege, and user defined group discrimination will not work. For this reason it is critical /// that user information is retrieved and cached upon first interaction with that user. /// /// /// Uploads can be cached indefinitely, and for that reason the user data cache must be filled indefinitely; /// meaning there is no invalidation and that user data will accrue for the lifetime of the application (instance). /// /// private ConcurrentDictionary UserDictionary { get; set; } = new ConcurrentDictionary(); private ConcurrentDictionary WatchedUsernamesDictionary { get; set; } = new ConcurrentDictionary(); /// /// Gets the name of the group for the specified . /// /// The group name is fetched from cached data, and lookups should always be fast. /// The username of the peer. /// The group for the specified username. public string GetGroup(string username) { // note: this is an extremely hot path; keep the work done to an absolute minimum. if (UserDictionary.TryGetValue(username ?? string.Empty, out var user)) { if (IsBlacklisted(user.Username)) { return Application.BlacklistedGroup; } if (user.Status?.IsPrivileged ?? false) { return Application.PrivilegedGroup; } if (user.Group != null) { return user.Group; } var thresholds = OptionsMonitor.CurrentValue.Groups.Leechers.Thresholds; if (user.Statistics?.FileCount < thresholds.Files || user.Statistics?.DirectoryCount < thresholds.Directories) { return Application.LeecherGroup; } } return Application.DefaultGroup; } /// /// Retrieves peer . /// /// The username of the peer. /// The retrieved info. public async Task GetInfoAsync(string username) { var soulseekUserInfo = await Client.GetUserInfoAsync(username); return soulseekUserInfo.ToInfo(); } /// /// Retrieves a peer's IP endpoint, including their IP address and listen port. /// /// The username of the peer. /// The retrieved endpoint. public Task GetIPEndPointAsync(string username) { return Client.GetUserEndPointAsync(username); } /// /// Gets the name of the group for the specified , or, if the user's information isn't /// cached, fetches and caches the user's information from the server, then returns the group. /// /// The fetch of fresh data can be forced by specifying = true. /// The username of the peer. /// /// A value determining whether the user's information should be fetched from the server, regardless of local cache. /// /// The group for the specified username. public async Task GetOrFetchGroupAsync(string username, bool forceFetch = false) { if (!UserDictionary.ContainsKey(username) || forceFetch) { // ensure the record will exist UserDictionary.TryAdd(username, new User { Username = username }); // try to populate the user's info await GetStatisticsAsync(username); await GetStatusAsync(username); } return GetGroup(username); } /// /// Retrieves the current of a peer, and caches the result. /// /// The username of the peer. /// The retrieved statistics. public async Task GetStatisticsAsync(string username) { var soulseekStatistics = await Client.GetUserStatisticsAsync(username); var statistics = soulseekStatistics.ToStatistics(); UpdateStatistics(username, statistics); return statistics; } /// /// Retrieves the current of a peer, and caches the result. /// /// The username of the peer. /// The retrieved status. public async Task GetStatusAsync(string username) { var soulseekStatus = await Client.GetUserStatusAsync(username); var status = soulseekStatus.ToStatus(); UpdateStatus(username, status); return status; } /// /// Grants the specified peer the specified number of privilege days. /// /// The username of the peer. /// The number of days to grant. /// The operation context. public Task GrantPrivilegesAsync(string username, int days) => Client.GrantUserPrivilegesAsync(username, days); /// /// Gets a value indicating whether the specified and/or are blacklisted. /// /// The username to check. /// The IPAddress to check, if available. /// A value indicating whether the specified user and/or IP are blacklisted. public bool IsBlacklisted(string username, IPAddress ipAddress = null) { var blacklist = OptionsMonitor.CurrentValue.Groups.Blacklisted; if (blacklist.Members.Contains(username)) { return true; } if (ipAddress is not null && blacklist.Cidrs.Select(c => IPAddressRange.Parse(c)).Any(range => range.Contains(ipAddress))) { return true; } return false; } /// /// Retrieves a value indicating whether the specified peer is privileged. /// /// The username of the peer. /// A value indicating whether the specified peer is privileged. public async Task IsPrivilegedAsync(string username) { var status = await GetStatusAsync(username); return status.IsPrivileged; } /// /// Gets a value indicating whether the specified is watched. /// /// The username of the peer. /// A value indicating whether the username is watched. public bool IsWatched(string username) => WatchedUsernamesDictionary.ContainsKey(username); /// /// Adds the specified username to the server-side user list. /// /// /// Idempotent; if a user is already watched, subsequent calls will only update their status and statistics. /// /// Any user we want to track in any way needs to be watched so that their statistics and status are updated /// properly via server side events. This seems extreme and wasteful, but the only alternative is to periodically /// spam the server for the information instead of letting the server spam the client when things change. /// /// /// The username of the peer. /// The operation context. public async Task WatchAsync(string username) { // watch the user, server side await Client.AddUserAsync(username); UserDictionary.TryAdd(username, new User { Username = username }); Log.Information("Added user {Username} to watch list", username); // the server does not automatically send status and statistics information when watching a user initially. explicitly // fetch both, so that the user has been watched and all information populated when this method returns. await GetStatusAsync(username); await GetStatisticsAsync(username); // delay the add until last, so that IsWatched won't return true until stats and status are populated. this may result // in several unecessary calls to WatchAsync (if someone is checking IsWatched() and WatchAsync()ing if false), but we // can be sure that if IsWatched() is true, we have valid stats and status. if we can't ensure this, the application // will have non-deterministic behavior when it makes decisions about user groups, limits, governance etc. WatchedUsernamesDictionary.TryAdd(username, true); } private void Client_PrivilegedUserListReceived(IEnumerable list) { foreach (var username in list) { UserDictionary.AddOrUpdate( key: username, addValue: new User() { Status = new Status { IsPrivileged = true } }, updateValueFactory: (key, user) => user with { Username = username, Status = user.Status with { IsPrivileged = true } }); } } private void Configure(Options options, bool force = false) { var optionsHash = Compute.Sha1Hash(options.Groups.UserDefined.ToJson()); if (optionsHash == LastOptionsHash && !force) { return; } // get a list of tracked names that haven't been explicitly added to any group, including those that were previlously // configured but have now been removed var usernamesBeforeUpdate = UserDictionary.Keys.ToList(); var usernamesAfterUpdate = options.Groups.UserDefined.SelectMany(g => g.Value.Members); var usernamesRemoved = usernamesBeforeUpdate.Except(usernamesAfterUpdate); // clear the configured group for anyone that was removed from config, or that was added transiently foreach (var username in usernamesRemoved) { UserDictionary.AddOrUpdate( key: username, addValue: new User() { Username = username }, updateValueFactory: (key, user) => user with { Username = username, Group = null }); } // sort by priority, descending. this will cause the highest priority group for the user to be persisted when the // operation is complete. foreach (var group in options.Groups.UserDefined.OrderByDescending(kvp => kvp.Value.Upload.Priority)) { foreach (var username in group.Value.Members) { UserDictionary.AddOrUpdate( key: username, addValue: new User() { Username = username, Group = group.Key }, updateValueFactory: (key, user) => user with { Username = username, Group = group.Key }); if (Client.State.HasFlag(SoulseekClientStates.Connected) && Client.State.HasFlag(SoulseekClientStates.LoggedIn)) { _ = WatchAsync(username); } } } LastOptionsHash = optionsHash; } private void Reset() { WatchedUsernamesDictionary.Clear(); UserDictionary.Clear(); } private void UpdateStatistics(string username, Statistics statistics) { UserDictionary.AddOrUpdate( key: username, addValue: new User() { Statistics = statistics }, updateValueFactory: (key, user) => user with { Username = username, Statistics = statistics }); } private void UpdateStatus(string username, Status status) { UserDictionary.AddOrUpdate( key: username, addValue: new User() { Status = status }, updateValueFactory: (key, user) => user with { Username = username, Status = status }); } } }