From 0b99e719bee99699376fe7e02bea2c92f3321034 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 24 Aug 2021 08:14:03 -0500 Subject: add IsValidRegex --- src/slskd/Common/Extensions.cs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'src') diff --git a/src/slskd/Common/Extensions.cs b/src/slskd/Common/Extensions.cs index 20620e44..077f5b17 100644 --- a/src/slskd/Common/Extensions.cs +++ b/src/slskd/Common/Extensions.cs @@ -23,6 +23,7 @@ namespace slskd using System.Linq; using System.Reflection; using System.Text.Json; + using System.Text.RegularExpressions; /// /// Extensions. @@ -117,6 +118,30 @@ namespace slskd } } + /// + /// Determines whether the string is a valid regular expression. + /// + /// The string to validate. + /// A value indicating whether the string is a valid regular expression. + public static bool IsValidRegex(this string pattern) + { + if (string.IsNullOrWhiteSpace(pattern)) + { + return false; + } + + try + { + Regex.Match(string.Empty, pattern); + } + catch (ArgumentException) + { + return false; + } + + return true; + } + /// /// Deeply compares this object with the specified object and returns a list of properties that are different. /// -- cgit v1.2.3 From bc08e38a614eda311f5eca38029816b5fcabc1a5 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 24 Aug 2021 08:14:59 -0500 Subject: add Filters options --- src/slskd/Options.cs | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Options.cs b/src/slskd/Options.cs index ec1eab06..fb4ec087 100644 --- a/src/slskd/Options.cs +++ b/src/slskd/Options.cs @@ -187,6 +187,12 @@ namespace slskd [RequiresRestart] public DirectoriesOptions Directories { get; private set; } = new DirectoriesOptions(); + /// + /// Gets filter options. + /// + [Validate] + public FiltersOptions Filters { get; private set; } = new FiltersOptions(); + /// /// Gets options for the web UI. /// @@ -255,7 +261,7 @@ namespace slskd public string Downloads { get; private set; } = Program.DefaultDownloadsDirectory; /// - /// Gets the path to shared files. + /// Gets the list of paths to shared files. /// [Argument('s', "shared")] [EnvironmentVariable("SHARED_DIR")] @@ -339,6 +345,40 @@ namespace slskd public bool Swagger { get; private set; } = false; } + /// + /// Filter options. + /// + public class FiltersOptions : IValidatableObject + { + /// + /// Gets the list of file filters. + /// + [Argument(default, "file-filter")] + [EnvironmentVariable("FILE_FILTER")] + [Description("regular expressions to filter files from shares and results")] + public string[] File { get; private set; } = Array.Empty(); + + /// + /// Extended validation. + /// + /// + /// + public IEnumerable Validate(ValidationContext validationContext) + { + var results = new List(); + + foreach (var filter in File) + { + if (!filter.IsValidRegex()) + { + results.Add(new ValidationResult($"File filter '{filter}' is not a valid regular expression")); + } + } + + return results; + } + } + /// /// Logger options. /// -- cgit v1.2.3 From 6795ced13828958514fbf65323e824da257c0f9d Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 24 Aug 2021 08:25:03 -0500 Subject: require share rescan on filter change --- src/slskd/Application/Application.cs | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'src') diff --git a/src/slskd/Application/Application.cs b/src/slskd/Application/Application.cs index c317f467..6d2bd0bb 100644 --- a/src/slskd/Application/Application.cs +++ b/src/slskd/Application/Application.cs @@ -241,6 +241,13 @@ namespace slskd Logger.Information("Shared directory configuration changed. Shares must be re-scanned for changes to take effect."); } + if (PreviousOptions.Filters.File.Except(newOptions.Filters.File).Any() + || newOptions.Filters.File.Except(PreviousOptions.Filters.File).Any()) + { + StateMonitor.SetValue(state => state with { PendingShareRescan = true }); + Logger.Information("File filter configuration changed. Shares must be re-scanned for changes to take effect."); + } + // determine whether any Soulseek options changed. if so, we need to construct a patch // and invoke ReconfigureOptionsAsync(). var slskDiff = PreviousOptions.Soulseek.DiffWith(newOptions.Soulseek); -- cgit v1.2.3 From b49de6c5458e9febc884d090df330b485a1f15b9 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 24 Aug 2021 08:25:32 -0500 Subject: filter files when scanning shares --- src/slskd/Application/SharedFileCache.cs | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src') diff --git a/src/slskd/Application/SharedFileCache.cs b/src/slskd/Application/SharedFileCache.cs index c2200daf..7bdc69f3 100644 --- a/src/slskd/Application/SharedFileCache.cs +++ b/src/slskd/Application/SharedFileCache.cs @@ -192,6 +192,11 @@ namespace slskd // qualified name the only time this *should* cause problems is if one of the shares is a subdirectory of another. foreach (var file in newFiles) { + if (OptionsMonitor.CurrentValue.Filters.File.Any(pattern => Regex.IsMatch(file.Key, pattern))) + { + continue; + } + if (files.ContainsKey(file.Key)) { Log.Warning($"File {file.Key} shared in directory {directory} has already been cached. This is probably a misconfiguration of the shared directories (is a subdirectory being re-shared?)."); -- cgit v1.2.3 From 8c54dd97758d43aee8f11d47bb7835be1f57d7e5 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 25 Aug 2021 07:51:09 -0500 Subject: update example yaml --- src/slskd/Properties/slskd.yml | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/slskd/Properties/slskd.yml b/src/slskd/Properties/slskd.yml index 938d2a70..99df8080 100644 --- a/src/slskd/Properties/slskd.yml +++ b/src/slskd/Properties/slskd.yml @@ -1,6 +1,20 @@ # debug: false # no_logo: false +# no_start: false +# no_connect: false +# no_share_scan: false # instance_name: default +# directories: +# app: ~ +# incomplete: ~ +# downloads: ~ +# shared: +# - ~ +# filters: +# file: +# - .ini$ +# - ^Thumbs.db$ +# - .DS_Store$ # web: # port: 5000 # https: @@ -18,11 +32,6 @@ # jwt: # key: ~ # ttl: 604800000 -# directories: -# app: ~ -# incomplete: ~ -# downloads: ~ -# shared: ~ # logger: # loki: ~ # feature: -- cgit v1.2.3 From f76808ace5a967f23556c022b3fe4fcc84f55e29 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 25 Aug 2021 07:51:22 -0500 Subject: compile filter regexes --- src/slskd/Application/SharedFileCache.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/slskd/Application/SharedFileCache.cs b/src/slskd/Application/SharedFileCache.cs index 7bdc69f3..eddef5e0 100644 --- a/src/slskd/Application/SharedFileCache.cs +++ b/src/slskd/Application/SharedFileCache.cs @@ -167,12 +167,16 @@ namespace slskd unmaskedDirectories = unmaskedDirectories.Except(excludedDirectories).ToHashSet(); State.SetValue(state => state with { Directories = unmaskedDirectories.Count, ExcludedDirectories = excludedDirectories.Count() }); - Log.Debug("Found {Directories} shared directories (and {Excluded} were filtered) in {Elapsed}ms. Starting file scan.", unmaskedDirectories.Count, excludedDirectories.Count(), sw.ElapsedMilliseconds - swSnapshot); + Log.Debug("Found {Directories} shared directories (and {Excluded} were excluded) in {Elapsed}ms. Starting file scan.", unmaskedDirectories.Count, excludedDirectories.Count(), sw.ElapsedMilliseconds - swSnapshot); swSnapshot = sw.ElapsedMilliseconds; + var filters = OptionsMonitor.CurrentValue.Filters.File + .Select(filter => new Regex(filter, RegexOptions.Compiled)); + var files = new Dictionary(); var maskedDirectories = new HashSet(); var current = 0; + var filtered = 0; foreach (var directory in unmaskedDirectories) { @@ -192,8 +196,9 @@ namespace slskd // qualified name the only time this *should* cause problems is if one of the shares is a subdirectory of another. foreach (var file in newFiles) { - if (OptionsMonitor.CurrentValue.Filters.File.Any(pattern => Regex.IsMatch(file.Key, pattern))) + if (filters.Any(filter => filter.IsMatch(file.Key))) { + filtered++; continue; } @@ -214,7 +219,7 @@ namespace slskd State.SetValue(state => state with { FillProgress = current / (double)unmaskedDirectories.Count, Files = files.Count }); } - Log.Debug("Directory scan found {Files} in {Elapsed}ms. Populating filename database", files.Count, sw.ElapsedMilliseconds - swSnapshot); + Log.Debug("Directory scan found {Files} files (and {Filtered} were filtered) in {Elapsed}ms. Populating filename database", files.Count, filtered, sw.ElapsedMilliseconds - swSnapshot); swSnapshot = sw.ElapsedMilliseconds; // potentially optimize with multi-valued insert https://stackoverflow.com/questions/16055566/insert-multiple-rows-in-sqlite -- cgit v1.2.3 From 4a029673ef45ae77ba4e84fac268ded0ee959b9e Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 25 Aug 2021 08:52:05 -0500 Subject: add WithFilters options extension --- src/slskd/Application/Search/Extensions.cs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'src') diff --git a/src/slskd/Application/Search/Extensions.cs b/src/slskd/Application/Search/Extensions.cs index d52975d2..149e0a14 100644 --- a/src/slskd/Application/Search/Extensions.cs +++ b/src/slskd/Application/Search/Extensions.cs @@ -70,6 +70,34 @@ namespace slskd.Search }); } + /// + /// Returns a copy of the specified with the specified filter delegates overridden. + /// + /// + /// + /// + /// + public static SearchOptions WithFilters( + this SearchOptions options, + Func responseFilter = null, + Func fileFilter = null) + { + return new SearchOptions( + options.SearchTimeout, + options.ResponseLimit, + filterResponses: options.FilterResponses || responseFilter != null || fileFilter != null, + options.MinimumResponseFileCount, + options.MinimumPeerFreeUploadSlots, + options.MaximumPeerQueueLength, + options.MinimumPeerUploadSpeed, + options.FileLimit, + options.RemoveSingleCharacterSearchTerms, + responseFilter, + fileFilter, + options.StateChanged, + options.ResponseReceived); + } + /// /// Creates a projection over the specified which omits responses. /// -- cgit v1.2.3 From bb5f2cdfb27b3e1773d622ee92b05aa6b8f1950b Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 25 Aug 2021 08:52:19 -0500 Subject: filter search results --- src/slskd/Application/Search/SearchService.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src') diff --git a/src/slskd/Application/Search/SearchService.cs b/src/slskd/Application/Search/SearchService.cs index 84cfd756..87ac5e22 100644 --- a/src/slskd/Application/Search/SearchService.cs +++ b/src/slskd/Application/Search/SearchService.cs @@ -15,6 +15,8 @@ // along with this program. If not, see https://www.gnu.org/licenses/. // +using Microsoft.Extensions.Options; + namespace slskd.Search { using System; @@ -22,6 +24,7 @@ namespace slskd.Search using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; + using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; @@ -41,14 +44,17 @@ namespace slskd.Search /// /// Initializes a new instance of the class. /// + /// /// The client instance to use. /// The database context to use. /// The logger. public SearchService( + IOptionsMonitor optionsMonitor, ISoulseekClient client, IDbContextFactory contextFactory, ILogger log) { + OptionsMonitor = optionsMonitor; Client = client; ContextFactory = contextFactory; Log = log; @@ -57,6 +63,7 @@ namespace slskd.Search private ConcurrentDictionary CancellationTokens { get; } = new ConcurrentDictionary(); + private IOptionsMonitor OptionsMonitor { get; } private ISoulseekClient Client { get; } private IDbContextFactory ContextFactory { get; } private ILogger Log { get; set; } @@ -83,10 +90,15 @@ namespace slskd.Search StartedAt = DateTime.UtcNow, }; + var filters = OptionsMonitor.CurrentValue.Filters.File + .Select(filter => new Regex(filter, RegexOptions.Compiled)); + options ??= new SearchOptions(); options = options.WithActions( stateChanged: (args) => UpdateSearchState(search, args.Search), responseReceived: (args) => UpdateSearchState(search, args.Search)); + options = options.WithFilters( + fileFilter: (file) => !filters.Any(filter => filter.IsMatch(file.Filename))); try { -- cgit v1.2.3 From c6b15d4b3c662461da0661c13be7f37440487476 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 26 Aug 2021 09:02:03 -0500 Subject: remove search response filtering --- src/slskd/Application/Search/SearchService.cs | 5 ----- 1 file changed, 5 deletions(-) (limited to 'src') diff --git a/src/slskd/Application/Search/SearchService.cs b/src/slskd/Application/Search/SearchService.cs index 87ac5e22..4841fed0 100644 --- a/src/slskd/Application/Search/SearchService.cs +++ b/src/slskd/Application/Search/SearchService.cs @@ -90,15 +90,10 @@ namespace slskd.Search StartedAt = DateTime.UtcNow, }; - var filters = OptionsMonitor.CurrentValue.Filters.File - .Select(filter => new Regex(filter, RegexOptions.Compiled)); - options ??= new SearchOptions(); options = options.WithActions( stateChanged: (args) => UpdateSearchState(search, args.Search), responseReceived: (args) => UpdateSearchState(search, args.Search)); - options = options.WithFilters( - fileFilter: (file) => !filters.Any(filter => filter.IsMatch(file.Filename))); try { -- cgit v1.2.3 From 45da80db80aff2930c182544c7ae2a268a25a768 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 26 Aug 2021 09:20:58 -0500 Subject: remove unused import --- src/slskd/Application/Search/SearchService.cs | 1 - 1 file changed, 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Application/Search/SearchService.cs b/src/slskd/Application/Search/SearchService.cs index 4841fed0..8de18977 100644 --- a/src/slskd/Application/Search/SearchService.cs +++ b/src/slskd/Application/Search/SearchService.cs @@ -24,7 +24,6 @@ namespace slskd.Search using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; - using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; -- cgit v1.2.3 From e1c8e8234cec53671b9250907f35376f963b1143 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 26 Aug 2021 09:21:46 -0500 Subject: fix regex --- src/slskd/Properties/slskd.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Properties/slskd.yml b/src/slskd/Properties/slskd.yml index 99df8080..a1dbd056 100644 --- a/src/slskd/Properties/slskd.yml +++ b/src/slskd/Properties/slskd.yml @@ -13,7 +13,7 @@ # filters: # file: # - .ini$ -# - ^Thumbs.db$ +# - Thumbs.db$ # - .DS_Store$ # web: # port: 5000 -- cgit v1.2.3 From 2ad9abd717620765dd1a44cd378cddb1a490baf1 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 26 Aug 2021 09:22:40 -0500 Subject: escape . --- src/slskd/Properties/slskd.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/slskd/Properties/slskd.yml b/src/slskd/Properties/slskd.yml index a1dbd056..d5b1eacd 100644 --- a/src/slskd/Properties/slskd.yml +++ b/src/slskd/Properties/slskd.yml @@ -12,9 +12,9 @@ # - ~ # filters: # file: -# - .ini$ +# - \.ini$ # - Thumbs.db$ -# - .DS_Store$ +# - \.DS_Store$ # web: # port: 5000 # https: -- cgit v1.2.3 From 682d69c0bb6cbef7d0deb6027102db3cb26cf006 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 26 Aug 2021 09:42:02 -0500 Subject: rename Filters.File to Filters.Share --- src/slskd/Application/Application.cs | 4 ++-- src/slskd/Application/SharedFileCache.cs | 2 +- src/slskd/Options.cs | 14 +++++++------- src/slskd/Properties/slskd.yml | 2 +- 4 files changed, 11 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/slskd/Application/Application.cs b/src/slskd/Application/Application.cs index 6d2bd0bb..3e054dc8 100644 --- a/src/slskd/Application/Application.cs +++ b/src/slskd/Application/Application.cs @@ -241,8 +241,8 @@ namespace slskd Logger.Information("Shared directory configuration changed. Shares must be re-scanned for changes to take effect."); } - if (PreviousOptions.Filters.File.Except(newOptions.Filters.File).Any() - || newOptions.Filters.File.Except(PreviousOptions.Filters.File).Any()) + if (PreviousOptions.Filters.Share.Except(newOptions.Filters.Share).Any() + || newOptions.Filters.Share.Except(PreviousOptions.Filters.Share).Any()) { StateMonitor.SetValue(state => state with { PendingShareRescan = true }); Logger.Information("File filter configuration changed. Shares must be re-scanned for changes to take effect."); diff --git a/src/slskd/Application/SharedFileCache.cs b/src/slskd/Application/SharedFileCache.cs index eddef5e0..ee4e0c72 100644 --- a/src/slskd/Application/SharedFileCache.cs +++ b/src/slskd/Application/SharedFileCache.cs @@ -170,7 +170,7 @@ namespace slskd Log.Debug("Found {Directories} shared directories (and {Excluded} were excluded) in {Elapsed}ms. Starting file scan.", unmaskedDirectories.Count, excludedDirectories.Count(), sw.ElapsedMilliseconds - swSnapshot); swSnapshot = sw.ElapsedMilliseconds; - var filters = OptionsMonitor.CurrentValue.Filters.File + var filters = OptionsMonitor.CurrentValue.Filters.Share .Select(filter => new Regex(filter, RegexOptions.Compiled)); var files = new Dictionary(); diff --git a/src/slskd/Options.cs b/src/slskd/Options.cs index fb4ec087..aac43c0a 100644 --- a/src/slskd/Options.cs +++ b/src/slskd/Options.cs @@ -351,12 +351,12 @@ namespace slskd public class FiltersOptions : IValidatableObject { /// - /// Gets the list of file filters. + /// Gets the list of shared file filters. /// - [Argument(default, "file-filter")] - [EnvironmentVariable("FILE_FILTER")] - [Description("regular expressions to filter files from shares and results")] - public string[] File { get; private set; } = Array.Empty(); + [Argument(default, "share-filter")] + [EnvironmentVariable("SHARE_FILTER")] + [Description("regular expressions to filter files from shares")] + public string[] Share { get; private set; } = Array.Empty(); /// /// Extended validation. @@ -367,11 +367,11 @@ namespace slskd { var results = new List(); - foreach (var filter in File) + foreach (var filter in Share) { if (!filter.IsValidRegex()) { - results.Add(new ValidationResult($"File filter '{filter}' is not a valid regular expression")); + results.Add(new ValidationResult($"Share filter '{filter}' is not a valid regular expression")); } } diff --git a/src/slskd/Properties/slskd.yml b/src/slskd/Properties/slskd.yml index d5b1eacd..4f25179c 100644 --- a/src/slskd/Properties/slskd.yml +++ b/src/slskd/Properties/slskd.yml @@ -11,7 +11,7 @@ # shared: # - ~ # filters: -# file: +# share: # - \.ini$ # - Thumbs.db$ # - \.DS_Store$ -- cgit v1.2.3