From d4481e3868f39c03f6a0c08387f474bb430517eb Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 31 Jul 2022 12:23:24 -0500 Subject: add search request filter options --- src/slskd/Core/Options.cs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'src') diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 9deababa..197c3412 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -624,6 +624,12 @@ namespace slskd [Description("regular expressions to filter files from shares")] public string[] Share { get; init; } = Array.Empty(); + /// + /// Gets search filter options. + /// + [Validate] + public SearchOptions Search { get; init; } = new SearchOptions(); + /// /// Extended validation. /// @@ -643,6 +649,40 @@ namespace slskd return results; } + + /// + /// Search filter options. + /// + public class SearchOptions : IValidatableObject + { + /// + /// Gets the list of search request filters. + /// + [Argument(default, "search-request-filter")] + [EnvironmentVariable("SEARCH_REQUEST_FILTER")] + [Description("regular expressions to filter incoming search requests")] + public string[] Request { get; init; } = Array.Empty(); + + /// + /// Extended validation. + /// + /// + /// + public IEnumerable Validate(ValidationContext validationContext) + { + var results = new List(); + + foreach (var filter in Request) + { + if (!filter.IsValidRegex()) + { + results.Add(new ValidationResult($"Search request filter '{filter}' is not a valid regular expression")); + } + } + + return results; + } + } } /// -- cgit v1.2.3 From a7225aa52d71f76e5426dda3c5071f113491b0dd Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 31 Jul 2022 12:24:12 -0500 Subject: replace 3 character search request check for check of filters from options --- src/slskd/Application.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index a2598d1e..1c7031cc 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -21,6 +21,7 @@ namespace slskd { using System; using System.Collections.Concurrent; + using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; @@ -28,6 +29,7 @@ namespace slskd using System.Net.Sockets; using System.Reflection; using System.Runtime; + using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.SignalR; @@ -94,6 +96,8 @@ namespace slskd PreviousOptions = OptionsMonitor.CurrentValue; + CompiledSearchResponseFilters = OptionsAtStartup.Filters.Search.Request.Select(f => new Regex(f, RegexOptions.Compiled)); + State = state; State.OnChange(state => State_OnChange(state)); @@ -164,6 +168,7 @@ namespace slskd private IUserService Users { get; set; } private IShareService Shares { get; set; } private IMemoryCache Cache { get; set; } = new MemoryCache(new MemoryCacheOptions()); + private IEnumerable CompiledSearchResponseFilters { get; set; } public void CollectGarbage() { @@ -735,6 +740,13 @@ namespace slskd Log.Information("File filter configuration changed. Shares must be re-scanned for changes to take effect."); } + if (PreviousOptions.Filters.Search.Request.Except(newOptions.Filters.Search.Request).Any() + || newOptions.Filters.Search.Request.Except(PreviousOptions.Filters.Search.Request).Any()) + { + CompiledSearchResponseFilters = newOptions.Filters.Search.Request.Select(f => new Regex(f, RegexOptions.Compiled)); + Log.Information("Updated and re-compiled search response filters"); + } + if (PreviousOptions.Rooms.Except(newOptions.Rooms).Any() || newOptions.Rooms.Except(PreviousOptions.Rooms).Any()) { @@ -856,8 +868,7 @@ namespace slskd /// A Task resolving a SearchResponse, or null. private async Task SearchResponseResolver(string username, int token, SearchQuery query) { - // some bots and perhaps users search for very short terms. only respond to queries >= 3 characters. sorry, U2 fans. - if (query.Query.Length < 3) + if (CompiledSearchResponseFilters.Any(filter => filter.IsMatch(query.SearchText))) { return null; } -- cgit v1.2.3