From a5dfc10ed3cd1b8381537870626fa9a7fbdcd8a3 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 3 Feb 2024 17:57:40 -0600 Subject: allow -1 limit to indicate unlimited --- src/slskd/Core/Options.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 68f3c3dc..2aadf206 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -1060,19 +1060,19 @@ namespace slskd /// /// Gets the limit for number of files. /// - [Range(1, int.MaxValue)] + [Range(-1, int.MaxValue)] public int? Files { get; init; } = null; /// /// Gets the limit for number of megabytes. /// - [Range(1, int.MaxValue)] + [Range(-1, int.MaxValue)] public int? Megabytes { get; init; } = null; /// /// Gets the limit for number of failures. /// - [Range(1, int.MaxValue)] + [Range(-1, int.MaxValue)] public int? Failures { get; init; } = null; } } -- cgit v1.2.3 From c2a3adcff71c40f0a81565f26238c9b6c9b0695f Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 3 Feb 2024 18:22:33 -0600 Subject: apply defaults over config --- src/slskd/Application.cs | 62 +++++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index b105cb56..b647ba39 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -511,9 +511,9 @@ namespace slskd return; } - // resolve the limits for this user's group. there's no 'fallthrough' here, if limits for a group - // are unset, then that group is unlimited. + // resolve the limits for this user's group. Options.GroupsOptions.LimitsOptions limits; + var defaults = Options.Groups.Default.Limits; if (Options.Groups.UserDefined.TryGetValue(group, out var userDefinedOptions)) { @@ -523,25 +523,21 @@ namespace slskd { limits = group switch { - DefaultGroup => Options.Groups.Default.Limits, + DefaultGroup => defaults, LeecherGroup => Options.Groups.Leechers.Limits, _ => Options.Groups.Default.Limits, }; } - bool IsNull(Options.GroupsOptions.LimitsExtendedOptions l) => l is null || (l?.Files is null && l?.Megabytes is null); - - // if no limits are set, we're good to go; just enqueue the file. - if (limits is null || (IsNull(limits.Daily) && IsNull(limits.Weekly) && IsNull(limits.Queued))) - { - Log.Debug("Limits bypassed for {Username} and {File}; no limits are set", username, filename); - await Transfers.Uploads.EnqueueAsync(username, filename); - return; - } - var sw = new Stopwatch(); sw.Start(); + bool IsNull(Options.GroupsOptions.LimitsExtendedOptions lim, Options.GroupsOptions.LimitsExtendedOptions def) + => (lim is null && def is null) || ((lim?.Files ?? def?.Files ?? lim?.Megabytes ?? def?.Megabytes ?? lim.Failures ?? def.Failures) is null); + + bool IsUnlimited(Options.GroupsOptions.LimitsExtendedOptions lim, Options.GroupsOptions.LimitsExtendedOptions def) + => IsNull(lim, def) || (lim?.Files == -1 && def?.Files == -1 && lim?.Megabytes == -1 && def.Megabytes == -1 && lim?.Failures == -1 && def?.Failures == -1); + /* * we have limits set, so now we have to fetch the data and compare to see if any would be hit if we allow this transfer to be enqueued. * the strategy here is to summarize all uploads: @@ -549,46 +545,62 @@ namespace slskd * 2) that were started within the time period * 3) that did not end due to an error (state includes errored, exception column is set) */ - (bool Files, bool Megabytes) OverLimits((int Files, long Bytes) stats, Options.GroupsOptions.LimitsExtendedOptions options, long size) + (bool Files, bool Megabytes, bool Failures) OverLimits( + (int Files, long Bytes, int Failures) stats, + Options.GroupsOptions.LimitsExtendedOptions options, + Options.GroupsOptions.LimitsExtendedOptions defaults, + long size) { var files = false; var megabytes = false; - var byteLimit = (options?.Megabytes ?? 0) * 1000 * 1000; + var failures = false; - if (options?.Megabytes is not null && (stats.Bytes + size) > byteLimit) + var byteLimit = options?.Megabytes ?? defaults?.Megabytes; + + if (byteLimit is not null && byteLimit >= 0 && (stats.Bytes + size) > (byteLimit * 1000 * 1000)) { - Log.Debug("Projected bytes {Bytes} exceeds limit {Limit}", stats.Bytes + size, byteLimit); + Log.Debug("Projected bytes {Bytes} exceeds limit {Limit}", stats.Bytes + size, byteLimit * 1000 * 1000); megabytes = true; } - if (options?.Files is not null && (stats.Files + 1) > options.Files) + var fileLimit = options?.Files ?? defaults?.Files; + + if (fileLimit is not null && fileLimit >= 0 && (stats.Files + 1) > fileLimit) { - Log.Debug("Projected file count {Files} exceeds limit {Limit}", stats.Files + 1, options.Files); + Log.Debug("Projected file count {Files} exceeds limit {Limit}", stats.Files + 1, fileLimit); files = true; } - return (files, megabytes); + var failureLimit = options?.Failures ?? defaults?.Failures; + + if (failureLimit is not null && failureLimit >= 0 && stats.Failures > failureLimit) + { + Log.Debug("Number of failed transfers exceeds limit of {Limit}", failureLimit); + failures = true; + } + + return (files, megabytes, failures); } // start with the queue, since that should contain the fewest files and should be the least expensive to check // "queued" includes both queued and in progress; records with a null EndedAt property, which is guaranteed to be set // for terminal transfers. - if (!IsNull(limits.Queued)) + if (!IsNull(limits.Queued, defaults.Queued) && !IsUnlimited(limits.Queued, defaults.Queued)) { var queued = Transfers.Uploads.Summarize( expression: t => t.Username == username && t.EndedAt == null); Log.Debug("Fetched queue stats: files: {Files}, bytes: {Bytes} ({Time}ms)", queued.Files, queued.Bytes, sw.ElapsedMilliseconds); - var over = OverLimits(queued, limits!.Queued, resolved.Size); + var over = OverLimits(queued, limits?.Queued, defaults?.Queued, resolved.Size); - if (over.Files || over.Megabytes) + if (over.Files || over.Megabytes || over.Failures) { Log.Information("Rejected enqueue request for user {Username}: Queued limits exceeded", username); // note: return exactly 'Too many files' or 'Too many megabytes' to ensure interop with other clients. // these messages are retryable, while anything else is not - throw new DownloadEnqueueException($"Too many {(over.Files ? "files" : "megabytes")}"); + throw new DownloadEnqueueException($"Too many {(over.Megabytes ? "megabytes" : "files")}"); } } @@ -597,7 +609,7 @@ namespace slskd // * started within the last week // * which have or have not ended (assuming queued files will complete) // * that were not errored - if (!IsNull(limits.Weekly)) + if (!IsNull(limits.Weekly, defaults.Weekly) && !IsUnlimited(limits.Weekly, defaults.Weekly)) { var erroredState = TransferStates.Completed | TransferStates.Errored; var cutoffDateTime = DateTime.UtcNow.AddDays(-7); -- cgit v1.2.3 From 6fb9c9cca24260ad33fa121fc935e293f5c98f66 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 3 Feb 2024 19:00:37 -0600 Subject: undo failure handing --- src/slskd/Application.cs | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index b647ba39..facea568 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) slskd Team. All rights reserved. // // This program is free software: you can redistribute it and/or modify @@ -545,16 +545,14 @@ namespace slskd * 2) that were started within the time period * 3) that did not end due to an error (state includes errored, exception column is set) */ - (bool Files, bool Megabytes, bool Failures) OverLimits( - (int Files, long Bytes, int Failures) stats, + (bool Files, bool Megabytes) OverLimits( + (int Files, long Bytes) stats, Options.GroupsOptions.LimitsExtendedOptions options, Options.GroupsOptions.LimitsExtendedOptions defaults, long size) { var files = false; var megabytes = false; - var failures = false; - var byteLimit = options?.Megabytes ?? defaults?.Megabytes; if (byteLimit is not null && byteLimit >= 0 && (stats.Bytes + size) > (byteLimit * 1000 * 1000)) @@ -571,15 +569,7 @@ namespace slskd files = true; } - var failureLimit = options?.Failures ?? defaults?.Failures; - - if (failureLimit is not null && failureLimit >= 0 && stats.Failures > failureLimit) - { - Log.Debug("Number of failed transfers exceeds limit of {Limit}", failureLimit); - failures = true; - } - - return (files, megabytes, failures); + return (files, megabytes); } // start with the queue, since that should contain the fewest files and should be the least expensive to check -- cgit v1.2.3 From 5dd649857610eb1e8160757f1dadeef077cefc13 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 3 Feb 2024 19:00:55 -0600 Subject: don't check for failed when looking at queued --- src/slskd/Application.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index facea568..c171a918 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -584,7 +584,7 @@ namespace slskd var over = OverLimits(queued, limits?.Queued, defaults?.Queued, resolved.Size); - if (over.Files || over.Megabytes || over.Failures) + if (over.Files || over.Megabytes) { Log.Information("Rejected enqueue request for user {Username}: Queued limits exceeded", username); -- cgit v1.2.3 From f07c2cab4c4226e40be989d1a40aea0296a1756b Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 3 Feb 2024 19:21:33 -0600 Subject: coalesce remaining nulls --- src/slskd/Application.cs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index c171a918..f7e58e2f 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) slskd Team. All rights reserved. // // This program is free software: you can redistribute it and/or modify @@ -575,7 +575,7 @@ namespace slskd // start with the queue, since that should contain the fewest files and should be the least expensive to check // "queued" includes both queued and in progress; records with a null EndedAt property, which is guaranteed to be set // for terminal transfers. - if (!IsNull(limits.Queued, defaults.Queued) && !IsUnlimited(limits.Queued, defaults.Queued)) + if (!IsNull(limits?.Queued, defaults?.Queued) && !IsUnlimited(limits?.Queued, defaults?.Queued)) { var queued = Transfers.Uploads.Summarize( expression: t => t.Username == username && t.EndedAt == null); @@ -599,7 +599,7 @@ namespace slskd // * started within the last week // * which have or have not ended (assuming queued files will complete) // * that were not errored - if (!IsNull(limits.Weekly, defaults.Weekly) && !IsUnlimited(limits.Weekly, defaults.Weekly)) + if (!IsNull(limits?.Weekly, defaults?.Weekly) && !IsUnlimited(limits?.Weekly, defaults?.Weekly)) { var erroredState = TransferStates.Completed | TransferStates.Errored; var cutoffDateTime = DateTime.UtcNow.AddDays(-7); @@ -612,7 +612,9 @@ namespace slskd Log.Debug("Fetched weekly failures: {Failures} ({Time}ms)", failures.Files, sw.ElapsedMilliseconds); - if (failures.Files >= limits.Weekly.Failures) + var failureLimit = limits?.Weekly?.Failures ?? defaults?.Weekly?.Failures; + + if (failureLimit is not null && failures.Files >= failureLimit) { Log.Information("Rejected enqueue request for user {Username}: Weekly failure limit met or exceeded", username); throw new DownloadEnqueueException("Too many failed transfers this week"); @@ -627,7 +629,7 @@ namespace slskd Log.Debug("Fetched weekly stats: files: {Files}, bytes: {Bytes} ({Time}ms)", weekly.Files, weekly.Bytes, sw.ElapsedMilliseconds); - var over = OverLimits(weekly, limits!.Weekly, resolved.Size); + var over = OverLimits(weekly, limits?.Weekly, defaults?.Weekly, resolved.Size); if (over.Files || over.Megabytes) { @@ -638,7 +640,7 @@ namespace slskd // lastly, check daily limits. the criteria for this is the same as weekly, just looking over the previous day instead // of the previous 7. - if (!IsNull(limits.Daily)) + if (!IsNull(limits?.Daily, defaults?.Daily) && !IsUnlimited(limits?.Daily, defaults?.Daily)) { var erroredState = TransferStates.Completed | TransferStates.Errored; var cutoffDateTime = DateTime.UtcNow.AddDays(-1); @@ -651,7 +653,9 @@ namespace slskd Log.Debug("Fetched daily failures: {Failures} ({Time}ms)", failures.Files, sw.ElapsedMilliseconds); - if (failures.Files >= limits.Daily.Failures) + var failureLimit = limits?.Daily?.Failures ?? defaults?.Daily?.Failures; + + if (failureLimit is not null && failures.Files >= failureLimit) { Log.Information("Rejected enqueue request for user {Username}: Daily failure limit met or exceeded", username); throw new DownloadEnqueueException("Too many failed transfers today"); @@ -666,7 +670,7 @@ namespace slskd Log.Debug("Fetched daily stats: files: {Files}, bytes: {Bytes} ({Time}ms)", daily.Files, daily.Bytes, sw.ElapsedMilliseconds); - var over = OverLimits(daily, limits!.Daily, resolved.Size); + var over = OverLimits(daily, limits?.Daily, defaults?.Daily, resolved.Size); if (over.Files || over.Megabytes) { -- cgit v1.2.3 From e49ce88e3487f424b2fc824d13b08086f00f633e Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 4 Feb 2024 16:00:49 -0600 Subject: add global limits --- src/slskd/Core/Options.cs | 102 ++++++++++++++++++++++++---------------------- 1 file changed, 54 insertions(+), 48 deletions(-) (limited to 'src') diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 2aadf206..992541bc 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -786,6 +786,12 @@ namespace slskd [Validate] public GlobalUploadOptions Upload { get; init; } = new GlobalUploadOptions(); + /// + /// Gets global limits. + /// + [Validate] + public LimitsOptions Limits { get; init; } = new LimitsOptions(); + /// /// Gets global download options. /// @@ -843,6 +849,54 @@ namespace slskd } } + /// + /// Limit options. + /// + public class LimitsOptions + { + /// + /// Gets limits for queued transfers. + /// + [Validate] + public Limits Queued { get; init; } = new Limits(); + + /// + /// Gets daily limits for transfers. + /// + [Validate] + public Limits Daily { get; init; } = new Limits(); + + /// + /// Gets weekly limits for transfers. + /// + [Validate] + public Limits Weekly { get; init; } = new Limits(); + + /// + /// Limits. + /// + public class Limits + { + /// + /// Gets the limit for number of files. + /// + [Range(1, int.MaxValue)] + public int? Files { get; init; } = null; + + /// + /// Gets the limit for number of megabytes. + /// + [Range(1, int.MaxValue)] + public int? Megabytes { get; init; } = null; + + /// + /// Gets the limit for number of failures. + /// + [Range(1, int.MaxValue)] + public int? Failures { get; init; } = null; + } + } + /// /// User groups. /// @@ -1027,54 +1081,6 @@ namespace slskd [Range(1, int.MaxValue)] public int SpeedLimit { get; init; } = int.MaxValue; } - - /// - /// Upload limit options. - /// - public class LimitsOptions - { - /// - /// Gets limits for queued transfers. - /// - [Validate] - public LimitsExtendedOptions Queued { get; init; } = new LimitsExtendedOptions(); - - /// - /// Gets daily limits for transfers. - /// - [Validate] - public LimitsExtendedOptions Daily { get; init; } = new LimitsExtendedOptions(); - - /// - /// Gets weekly limits for transfers. - /// - [Validate] - public LimitsExtendedOptions Weekly { get; init; } = new LimitsExtendedOptions(); - } - - /// - /// Extended limit options. - /// - public class LimitsExtendedOptions - { - /// - /// Gets the limit for number of files. - /// - [Range(-1, int.MaxValue)] - public int? Files { get; init; } = null; - - /// - /// Gets the limit for number of megabytes. - /// - [Range(-1, int.MaxValue)] - public int? Megabytes { get; init; } = null; - - /// - /// Gets the limit for number of failures. - /// - [Range(-1, int.MaxValue)] - public int? Failures { get; init; } = null; - } } /// -- cgit v1.2.3 From 7675366cd9578056e6321b80e9bcd5a408721ced Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 4 Feb 2024 16:01:20 -0600 Subject: fall back to global limits instead of defaults --- src/slskd/Application.cs | 47 +++++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index f7e58e2f..aa6af2bc 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -511,9 +511,11 @@ namespace slskd return; } + // we'll fall back to global limits for any limit that isn't set at the group level + var global = Options.Global.Limits; + // resolve the limits for this user's group. - Options.GroupsOptions.LimitsOptions limits; - var defaults = Options.Groups.Default.Limits; + Options.LimitsOptions limits; if (Options.Groups.UserDefined.TryGetValue(group, out var userDefinedOptions)) { @@ -523,20 +525,14 @@ namespace slskd { limits = group switch { - DefaultGroup => defaults, + DefaultGroup => Options.Groups.Default.Limits, LeecherGroup => Options.Groups.Leechers.Limits, - _ => Options.Groups.Default.Limits, + _ => Options.Groups.Default.Limits, // that's weird! we'll just go with defaults.. }; } - var sw = new Stopwatch(); - sw.Start(); - - bool IsNull(Options.GroupsOptions.LimitsExtendedOptions lim, Options.GroupsOptions.LimitsExtendedOptions def) - => (lim is null && def is null) || ((lim?.Files ?? def?.Files ?? lim?.Megabytes ?? def?.Megabytes ?? lim.Failures ?? def.Failures) is null); - - bool IsUnlimited(Options.GroupsOptions.LimitsExtendedOptions lim, Options.GroupsOptions.LimitsExtendedOptions def) - => IsNull(lim, def) || (lim?.Files == -1 && def?.Files == -1 && lim?.Megabytes == -1 && def.Megabytes == -1 && lim?.Failures == -1 && def?.Failures == -1); + bool IsNull(Options.LimitsOptions.Limits lim, Options.LimitsOptions.Limits global) + => (lim is null && global is null) || ((lim?.Files ?? global?.Files ?? lim?.Megabytes ?? global?.Megabytes ?? lim.Failures ?? global.Failures) is null); /* * we have limits set, so now we have to fetch the data and compare to see if any would be hit if we allow this transfer to be enqueued. @@ -547,15 +543,15 @@ namespace slskd */ (bool Files, bool Megabytes) OverLimits( (int Files, long Bytes) stats, - Options.GroupsOptions.LimitsExtendedOptions options, - Options.GroupsOptions.LimitsExtendedOptions defaults, + Options.LimitsOptions.Limits options, + Options.LimitsOptions.Limits defaults, long size) { var files = false; var megabytes = false; var byteLimit = options?.Megabytes ?? defaults?.Megabytes; - if (byteLimit is not null && byteLimit >= 0 && (stats.Bytes + size) > (byteLimit * 1000 * 1000)) + if (byteLimit is not null && (stats.Bytes + size) > (byteLimit * 1000 * 1000)) { Log.Debug("Projected bytes {Bytes} exceeds limit {Limit}", stats.Bytes + size, byteLimit * 1000 * 1000); megabytes = true; @@ -563,7 +559,7 @@ namespace slskd var fileLimit = options?.Files ?? defaults?.Files; - if (fileLimit is not null && fileLimit >= 0 && (stats.Files + 1) > fileLimit) + if (fileLimit is not null && (stats.Files + 1) > fileLimit) { Log.Debug("Projected file count {Files} exceeds limit {Limit}", stats.Files + 1, fileLimit); files = true; @@ -572,17 +568,20 @@ namespace slskd return (files, megabytes); } + var sw = new Stopwatch(); + sw.Start(); + // start with the queue, since that should contain the fewest files and should be the least expensive to check // "queued" includes both queued and in progress; records with a null EndedAt property, which is guaranteed to be set // for terminal transfers. - if (!IsNull(limits?.Queued, defaults?.Queued) && !IsUnlimited(limits?.Queued, defaults?.Queued)) + if (!IsNull(limits?.Queued, global?.Queued)) { var queued = Transfers.Uploads.Summarize( expression: t => t.Username == username && t.EndedAt == null); Log.Debug("Fetched queue stats: files: {Files}, bytes: {Bytes} ({Time}ms)", queued.Files, queued.Bytes, sw.ElapsedMilliseconds); - var over = OverLimits(queued, limits?.Queued, defaults?.Queued, resolved.Size); + var over = OverLimits(queued, limits?.Queued, global?.Queued, resolved.Size); if (over.Files || over.Megabytes) { @@ -599,7 +598,7 @@ namespace slskd // * started within the last week // * which have or have not ended (assuming queued files will complete) // * that were not errored - if (!IsNull(limits?.Weekly, defaults?.Weekly) && !IsUnlimited(limits?.Weekly, defaults?.Weekly)) + if (!IsNull(limits?.Weekly, global?.Weekly)) { var erroredState = TransferStates.Completed | TransferStates.Errored; var cutoffDateTime = DateTime.UtcNow.AddDays(-7); @@ -612,7 +611,7 @@ namespace slskd Log.Debug("Fetched weekly failures: {Failures} ({Time}ms)", failures.Files, sw.ElapsedMilliseconds); - var failureLimit = limits?.Weekly?.Failures ?? defaults?.Weekly?.Failures; + var failureLimit = limits?.Weekly?.Failures ?? global?.Weekly?.Failures; if (failureLimit is not null && failures.Files >= failureLimit) { @@ -629,7 +628,7 @@ namespace slskd Log.Debug("Fetched weekly stats: files: {Files}, bytes: {Bytes} ({Time}ms)", weekly.Files, weekly.Bytes, sw.ElapsedMilliseconds); - var over = OverLimits(weekly, limits?.Weekly, defaults?.Weekly, resolved.Size); + var over = OverLimits(weekly, limits?.Weekly, global?.Weekly, resolved.Size); if (over.Files || over.Megabytes) { @@ -640,7 +639,7 @@ namespace slskd // lastly, check daily limits. the criteria for this is the same as weekly, just looking over the previous day instead // of the previous 7. - if (!IsNull(limits?.Daily, defaults?.Daily) && !IsUnlimited(limits?.Daily, defaults?.Daily)) + if (!IsNull(limits?.Daily, global?.Daily)) { var erroredState = TransferStates.Completed | TransferStates.Errored; var cutoffDateTime = DateTime.UtcNow.AddDays(-1); @@ -653,7 +652,7 @@ namespace slskd Log.Debug("Fetched daily failures: {Failures} ({Time}ms)", failures.Files, sw.ElapsedMilliseconds); - var failureLimit = limits?.Daily?.Failures ?? defaults?.Daily?.Failures; + var failureLimit = limits?.Daily?.Failures ?? global?.Daily?.Failures; if (failureLimit is not null && failures.Files >= failureLimit) { @@ -670,7 +669,7 @@ namespace slskd Log.Debug("Fetched daily stats: files: {Files}, bytes: {Bytes} ({Time}ms)", daily.Files, daily.Bytes, sw.ElapsedMilliseconds); - var over = OverLimits(daily, limits?.Daily, defaults?.Daily, resolved.Size); + var over = OverLimits(daily, limits?.Daily, global?.Daily, resolved.Size); if (over.Files || over.Megabytes) { -- cgit v1.2.3 From 9dd887709decc7182cf1e7003e870ae3fc160272 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 10 Feb 2024 19:10:46 -0600 Subject: add units --- src/slskd/Common/TokenBucket.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Common/TokenBucket.cs b/src/slskd/Common/TokenBucket.cs index bce2e913..edd6ca7f 100644 --- a/src/slskd/Common/TokenBucket.cs +++ b/src/slskd/Common/TokenBucket.cs @@ -83,7 +83,7 @@ namespace slskd /// Initializes a new instance of the class. /// /// The bucket capacity. - /// The interval at which tokens are replenished. + /// The interval at which tokens are replenished, in milliseconds. public TokenBucket(long capacity, int interval) { if (capacity < 1) -- cgit v1.2.3 From a8c6bb5c5b8a7fc3ee25fd62f4df23d06d6ddb5e Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 10 Feb 2024 19:11:22 -0600 Subject: add units --- src/slskd/Core/Options.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 992541bc..c64da46a 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -814,7 +814,7 @@ namespace slskd public int Slots { get; init; } = 10; /// - /// Gets the total upload speed limit. + /// Gets the total upload speed limit, in kibibytes. /// [Argument(default, "upload-speed-limit")] [EnvironmentVariable("UPLOAD_SPEED_LIMIT")] @@ -839,7 +839,7 @@ namespace slskd public int Slots { get; init; } = int.MaxValue; /// - /// Gets the total download speed limit. + /// Gets the total download speed limit, in kibibytes. /// [Argument(default, "download-speed-limit")] [EnvironmentVariable("DOWNLOAD_SPEED_LIMIT")] @@ -1076,7 +1076,7 @@ namespace slskd public int Slots { get; init; } = int.MaxValue; /// - /// Gets the total upload speed limit for the group. + /// Gets the total upload speed limit for the group, in kibibytes. /// [Range(1, int.MaxValue)] public int SpeedLimit { get; init; } = int.MaxValue; -- cgit v1.2.3 From 1a50deb7b5e05bf6d8cc9ed1ba5480fb80b35177 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 10 Feb 2024 19:17:16 -0600 Subject: refactor upload governor bucket config for clarity --- src/slskd/Transfers/Uploads/UploadGovernor.cs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/slskd/Transfers/Uploads/UploadGovernor.cs b/src/slskd/Transfers/Uploads/UploadGovernor.cs index 0f481e69..11fc4dcf 100644 --- a/src/slskd/Transfers/Uploads/UploadGovernor.cs +++ b/src/slskd/Transfers/Uploads/UploadGovernor.cs @@ -129,11 +129,15 @@ namespace slskd.Transfers private void Configure(Options options) { - static long ComputeBucketCapacity(int speed) - => speed * 1024L / 10; + static TokenBucket CreateBucket(int speedInKiB) + { + var speedInBytes = speedInKiB * 1024L; + var intervalInMs = 100; + var bucketRefreshesPerSecond = 1000 / intervalInMs; + var capacity = speedInBytes / bucketRefreshesPerSecond; - static TokenBucket CreateBucket(int speed) - => new(ComputeBucketCapacity(speed), 100); + return new(capacity, interval: intervalInMs); + } var optionsHash = Compute.Sha1Hash(options.Groups.ToJson()); @@ -149,9 +153,9 @@ namespace slskd.Transfers // also, so transfers in progress will briefly exceed the intended speeds. var tokenBuckets = new Dictionary() { - { Application.PrivilegedGroup, CreateBucket(options.Global.Upload.SpeedLimit) }, - { Application.DefaultGroup, CreateBucket(options.Groups.Default.Upload.SpeedLimit) }, - { Application.LeecherGroup, CreateBucket(options.Groups.Leechers.Upload.SpeedLimit) }, + { Application.PrivilegedGroup, CreateBucket(speedInKiB: options.Global.Upload.SpeedLimit) }, + { Application.DefaultGroup, CreateBucket(speedInKiB: options.Groups.Default.Upload.SpeedLimit) }, + { Application.LeecherGroup, CreateBucket(speedInKiB: options.Groups.Leechers.Upload.SpeedLimit) }, }; foreach (var group in options.Groups.UserDefined) -- cgit v1.2.3 From 5df84627f6a88c016b8907cdd632b3806d12d7ba Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 24 Feb 2024 21:46:41 -0600 Subject: bump dependencies, including Soulseek.NET to 6.4.1 --- src/slskd/slskd.csproj | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/slskd/slskd.csproj b/src/slskd/slskd.csproj index e32be5d6..2283804f 100644 --- a/src/slskd/slskd.csproj +++ b/src/slskd/slskd.csproj @@ -43,17 +43,17 @@ - + - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - - + + + @@ -67,7 +67,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -76,6 +76,6 @@ - + -- cgit v1.2.3 From 3c221c12790ac8ea001ecde7862412382ac440e5 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 24 Feb 2024 22:05:50 -0600 Subject: capture excluded phrases --- src/slskd/Application.cs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index aa6af2bc..27f9e882 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -181,6 +181,8 @@ namespace slskd Client.DownloadDenied += (e, args) => Log.Information("Download of {Filename} from {Username} was denied: {Message}", args.Filename, args.Username, args.Message); Client.DownloadFailed += (e, args) => Log.Information("Download of {Filename} from {Username} failed", args.Filename, args.Username); + Client.ExcludedSearchPhrasesReceived += (e, args) => ExcludedSearchPhrases = args; + ConnectionWatchdog = connectionWatchdog; Clock.EveryMinute += Clock_EveryMinute; @@ -221,6 +223,7 @@ namespace slskd private IEnumerable CompiledSearchResponseFilters { get; set; } private IEnumerable ActiveDownloadIdsAtPreviousShutdown { get; set; } = Enumerable.Empty(); private Options.FlagsOptions Flags { get; set; } + private IReadOnlyCollection ExcludedSearchPhrases { get; set; } = Enumerable.Empty().ToList(); public void CollectGarbage() { -- cgit v1.2.3 From 4cf7752282adb80a9e6d20187d50745e77e67bf0 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 24 Feb 2024 22:23:49 -0600 Subject: append excluded phrases to searches --- src/slskd/Application.cs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 27f9e882..a3bea5b2 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -1374,6 +1374,10 @@ namespace slskd return null; } + // append the list of excluded search phrases supplied by the server + // see https://github.com/jpdillingham/Soulseek.NET/issues/803 + query = new SearchQuery(terms: query.Terms, exclusions: query.Exclusions.Concat(ExcludedSearchPhrases).Distinct()); + try { var sw = new Stopwatch(); -- cgit v1.2.3 From 92e5bd61becad8877d2733696260556db6c8a514 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 25 Feb 2024 14:36:21 -0600 Subject: filter using substring matching on filenames --- src/slskd/Shares/SqliteShareRepository.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src') diff --git a/src/slskd/Shares/SqliteShareRepository.cs b/src/slskd/Shares/SqliteShareRepository.cs index 882ede74..029d74b8 100644 --- a/src/slskd/Shares/SqliteShareRepository.cs +++ b/src/slskd/Shares/SqliteShareRepository.cs @@ -539,6 +539,17 @@ namespace slskd.Shares while (reader.Read()) { var filename = reader.GetString(0); + + // SQLite takes care of matching on tokens, but unfortunately doesn't work in cases where an exclusion appears as a substring. + // example: the token matching works to exclude the result "foo bar baz" given a "bar" exclusion, but does not exclude "foo xbarx baz" + // for this reason we must iterate over the exclusion list and ensure none appear as substrings + // **CAUTION** performance of this will degrade exponentially as the list of exclusions grows + if (query.Exclusions.Any(x => filename.Contains(x, StringComparison.OrdinalIgnoreCase))) + { + Log.Debug("Dropping excluded filename {File}", filename); + continue; + } + var code = reader.GetInt32(1); var size = reader.GetInt64(2); var extension = reader.GetString(3); -- cgit v1.2.3 From 1fd51e9ffadc263942ca36e5e9b1209138bf3e88 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 25 Feb 2024 14:37:07 -0600 Subject: move handler to private function --- src/slskd/Application.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index a3bea5b2..75b2eee6 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -181,7 +181,7 @@ namespace slskd Client.DownloadDenied += (e, args) => Log.Information("Download of {Filename} from {Username} was denied: {Message}", args.Filename, args.Username, args.Message); Client.DownloadFailed += (e, args) => Log.Information("Download of {Filename} from {Username} failed", args.Filename, args.Username); - Client.ExcludedSearchPhrasesReceived += (e, args) => ExcludedSearchPhrases = args; + Client.ExcludedSearchPhrasesReceived += Client_ExcludedSearchPhrasesReceived; ConnectionWatchdog = connectionWatchdog; @@ -1096,6 +1096,12 @@ namespace slskd } } + private void Client_ExcludedSearchPhrasesReceived(object sender, IReadOnlyCollection e) + { + Log.Debug("Excluded search phrases: {Phrases}", string.Join(", ", e)); + ExcludedSearchPhrases = e; + } + private void Client_TransferProgressUpdated(object sender, TransferProgressUpdatedEventArgs args) { // no-op. this is really verbose, use for troubleshooting. @@ -1369,7 +1375,7 @@ namespace slskd // sometimes clients send search queries consisting only of exclusions; drop them. // no other clients send search results for these, even though it is technically possible. - if (!query.Terms.Any()) + if (query.Terms.Count == 0) { return null; } -- cgit v1.2.3 From 35b50db222665d4bb4caf9b2b565c9497891b50f Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 2 Mar 2024 15:38:14 -0600 Subject: raise minimum length of JWT key to 32 characters --- src/slskd/Core/Options.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index c64da46a..cfe78eb1 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -1725,7 +1725,7 @@ namespace slskd [Argument(default, "jwt-key")] [EnvironmentVariable("JWT_KEY")] [Description("JWT signing key")] - [StringLength(255, MinimumLength = 16)] + [StringLength(255, MinimumLength = 32)] [Secret] [RequiresRestart] public string Key { get; init; } = Cryptography.Random.GetBytes(32).ToBase62(); -- cgit v1.2.3 From 9393e973873d637f7c3776437cbeaddeae808d70 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 2 Mar 2024 15:38:33 -0600 Subject: remove warning if jwt key is fewer than 32 characters --- src/slskd/Program.cs | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) (limited to 'src') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index ad50dccd..d7f6caca 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -634,16 +634,7 @@ namespace slskd services.AddDataProtection() .PersistKeysToFileSystem(new DirectoryInfo(Path.Combine(DataDirectory, "misc", ".DataProtection-Keys"))); - var adjustedJwtKey = OptionsAtStartup.Web.Authentication.Jwt.Key; - - // the JWT key must be at least 256 bits in length, due to the HMACSHA256 algorithm being used for signing. - if (adjustedJwtKey.Length < 32) - { - adjustedJwtKey = adjustedJwtKey.PadRight(32, ' '); - Log.Warning("The configured JWT signing key is less than the required 32 characters and has been padded with spaces to make up the difference. The minimum length will be raised from 16 to 32 in a future release, and this will become an error."); - } - - var jwtSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(adjustedJwtKey)); + var jwtSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(OptionsAtStartup.Web.Authentication.Jwt.Key)); services.AddSingleton(jwtSigningKey); services.AddSingleton(); -- cgit v1.2.3