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(+) 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(-) 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 From 6884c594fe97c18f63cd1d4fe3227351df85c771 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 31 Jul 2022 12:24:22 -0500 Subject: add example search request filter --- config/slskd.example.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/config/slskd.example.yml b/config/slskd.example.yml index c2c11668..16dabb22 100644 --- a/config/slskd.example.yml +++ b/config/slskd.example.yml @@ -49,6 +49,9 @@ # - \.ini$ # - Thumbs.db$ # - \.DS_Store$ +# search: +# request: +# - ^.{1,2}$ # web: # port: 5000 # https: -- cgit v1.2.3 From c52557bb2630d87067a57e32434ee1596498f5c1 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 31 Jul 2022 12:30:52 -0500 Subject: docs for search request filters --- docs/config.md | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/docs/config.md b/docs/config.md index 74d7b24d..2254c9e2 100644 --- a/docs/config.md +++ b/docs/config.md @@ -542,11 +542,14 @@ web: A number of filters can be configured to control various aspects of how the application interacts with the Soulseek network. -The share filters can be used to prevent certain types of files from being shared. This option is an array that can take any number of filters. Filters must be a valid regular expression; a few examples are included below and in the example configuration included with the application, but the list is empty by default. +Share filters can be used to prevent certain types of files from being shared. This option is an array that can take any number of filters. Filters must be a valid regular expression; a few examples are included below and in the example configuration included with the application, but the list is empty by default. -| Command Line | Environment Variable | Description | -| ---------------- | -------------------- | -------------------------------------------------------------- | -| `--share-filter` | `SHARE_FILTER` | A list of regular expressions used to filter files from shares | +Search request filters can be used to discard incoming search requests that match one or more filters. Like share filters, this option is an array of regular expressions, and it defaults to an empty array (no request filtering is applied). + +| Command Line | Environment Variable | Description | +| ------------------------- | ----------------------- | --------------------------------------------------------------------- | +| `--share-filter` | `SHARE_FILTER` | A list of regular expressions used to filter files from shares | +| `--search-request-filter` | `SEARCH_REQUEST_FILTER` | A list of regular expressions used to filter incoming search requests | #### **YAML** ```yaml @@ -555,6 +558,9 @@ filters: - \.ini$ - Thumbs.db$ - \.DS_Store$ + search: + request: + - ^.{1,2}$ ``` # Integrations -- cgit v1.2.3