diff options
| author | JP Dillingham <jp@dillingham.me> | 2022-03-02 21:40:24 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-03-02 21:40:24 -0600 |
| commit | 6ef002ac938be75bff76ad3bb2fbfee8133aad7d (patch) | |
| tree | cd9a0628696e6b29cffb9ee694580e375fb21498 | |
| parent | 539d44d4e3812d8ec7b969cc59c1634f00b147f4 (diff) | |
| parent | 244747ab85bf80424c4d376401a8f56db8536a24 (diff) | |
| download | slskd-6ef002ac938be75bff76ad3bb2fbfee8133aad7d.tar.gz | |
Merge pull request #375 from slskd/leechers
Identify leechers
| -rw-r--r-- | config/slskd.example.yml | 3 | ||||
| -rw-r--r-- | docs/config.md | 5 | ||||
| -rw-r--r-- | src/slskd/Options.cs | 38 | ||||
| -rw-r--r-- | src/slskd/Users/UserService.cs | 37 | ||||
| -rw-r--r-- | tests/slskd.Tests.Unit/Transfers/Uploads/UploadQueueTests.cs | 3 |
5 files changed, 69 insertions, 17 deletions
diff --git a/config/slskd.example.yml b/config/slskd.example.yml index 2bf0863b..21c95076 100644 --- a/config/slskd.example.yml +++ b/config/slskd.example.yml @@ -25,6 +25,9 @@ # strategy: roundrobin # slots: 10 # leechers: +# thresholds: +# files: 1 +# directories: 1 # upload: # priority: 999 # strategy: roundrobin diff --git a/docs/config.md b/docs/config.md index 4f56e49f..813c036b 100644 --- a/docs/config.md +++ b/docs/config.md @@ -195,7 +195,7 @@ upload: The `default` built-in group contains all users that have not been explicitly added to a user defined group, are not privileged, and that haven't been identified as leechers. -The `leechers` built-in group contains users that have been identified as leechers. +The `leechers` built-in group contains users that have not been explicitly added to a user defined group, are not privileged, and that have shared file and/or directory counts that are less than the configured `thresholds` for the group. By default, users must share at least 1 directory with 1 file to avoid being identified as leechers. The `privileged` built-in is used to prioritize users who have purchased privileges on the Soulseek network. This groups is not configurable, has a priority of 0 (the highest), a strategy of `FirstInFirstOut`, and can use any number of slots, up to the global limit. @@ -211,6 +211,9 @@ groups: slots: 10 speed_limit: 50000 leechers: + thresholds: + files: 1 + directories: 1 upload: priority: 99 strategy: roundrobin diff --git a/src/slskd/Options.cs b/src/slskd/Options.cs index 5bebeb68..64ca494f 100644 --- a/src/slskd/Options.cs +++ b/src/slskd/Options.cs @@ -480,7 +480,7 @@ namespace slskd /// These options apply to users that have been identified as leechers, and have not been added as a member of any group. /// </remarks> [Validate] - public BuiltInOptions Leechers { get; init; } = new BuiltInOptions(); + public LeecherOptions Leechers { get; init; } = new LeecherOptions(); /// <summary> /// Gets user defined groups and options. @@ -514,6 +514,42 @@ namespace slskd } /// <summary> + /// Built in leecher group options. + /// </summary> + public class LeecherOptions + { + /// <summary> + /// Gets leecher threshold options. + /// </summary> + [Validate] + public ThresholdOptions Thresholds { get; init; } = new ThresholdOptions(); + + /// <summary> + /// Gets upload options. + /// </summary> + [Validate] + public UploadOptions Upload { get; init; } = new UploadOptions(); + } + + /// <summary> + /// Leecher threshold options. + /// </summary> + public class ThresholdOptions + { + /// <summary> + /// Gets the minimum number of shared files required to avoid being classified as a leecher. + /// </summary> + [Range(1, int.MaxValue)] + public int Files { get; init; } = 1; + + /// <summary> + /// Gets the minimum number of shared directories required to avoid being classified as a leecher. + /// </summary> + [Range(1, int.MaxValue)] + public int Directories { get; init; } = 1; + } + + /// <summary> /// User defined user group options. /// </summary> public class UserDefinedOptions diff --git a/src/slskd/Users/UserService.cs b/src/slskd/Users/UserService.cs index 5adc7c71..1854944f 100644 --- a/src/slskd/Users/UserService.cs +++ b/src/slskd/Users/UserService.cs @@ -110,7 +110,17 @@ namespace slskd.Users return Application.PrivilegedGroup; } - return user.Group ?? Application.DefaultGroup; + 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; @@ -225,21 +235,16 @@ namespace slskd.Users /// <returns>The operation context.</returns> public async Task WatchAsync(string username) { - // note that this isn't strictly synchronized, and this may be executed more than once per user. this is acceptable, - // and has no side effects other than increased round trips to the server. - if (!WatchedUsernamesDictionary.ContainsKey(username)) - { - await Client.AddUserAsync(username); - WatchedUsernamesDictionary.TryAdd(username, true); + await Client.AddUserAsync(username); + WatchedUsernamesDictionary.TryAdd(username, true); - Log.Information("Added user {Username} to watch list", 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); - } + // 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); } private void UpdateStatistics(string username, Statistics statistics) @@ -279,10 +284,14 @@ namespace slskd.Users 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( diff --git a/tests/slskd.Tests.Unit/Transfers/Uploads/UploadQueueTests.cs b/tests/slskd.Tests.Unit/Transfers/Uploads/UploadQueueTests.cs index 4722906f..ba059046 100644 --- a/tests/slskd.Tests.Unit/Transfers/Uploads/UploadQueueTests.cs +++ b/tests/slskd.Tests.Unit/Transfers/Uploads/UploadQueueTests.cs @@ -81,8 +81,9 @@ { Groups = new Options.GroupsOptions() { - Leechers = new Options.GroupsOptions.BuiltInOptions() + Leechers = new Options.GroupsOptions.LeecherOptions() { + Thresholds = new Options.GroupsOptions.ThresholdOptions(), Upload = new Options.GroupsOptions.UploadOptions() { Priority = priority, |
