aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2022-07-31 12:38:52 -0500
committerGitHub <noreply@github.com>2022-07-31 12:38:52 -0500
commit0791bd27e6fc2986d8ad12b0bd7d797af92fc383 (patch)
tree58130b20f4220924d2a78d39897ccb1760de3b8f
parent001a7c0f708de3d7374d9234e07a6ba477cfbc31 (diff)
parentc52557bb2630d87067a57e32434ee1596498f5c1 (diff)
downloadslskd-0791bd27e6fc2986d8ad12b0bd7d797af92fc383.tar.gz
Merge pull request #570 from slskd/search-response
Add the ability to filter incoming search requests
-rw-r--r--config/slskd.example.yml3
-rw-r--r--docs/config.md14
-rw-r--r--src/slskd/Application.cs15
-rw-r--r--src/slskd/Core/Options.cs40
4 files changed, 66 insertions, 6 deletions
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:
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
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<Regex> 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
/// <returns>A Task resolving a SearchResponse, or null.</returns>
private async Task<SearchResponse> 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;
}
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
@@ -625,6 +625,12 @@ namespace slskd
public string[] Share { get; init; } = Array.Empty<string>();
/// <summary>
+ /// Gets search filter options.
+ /// </summary>
+ [Validate]
+ public SearchOptions Search { get; init; } = new SearchOptions();
+
+ /// <summary>
/// Extended validation.
/// </summary>
/// <param name="validationContext"></param>
@@ -643,6 +649,40 @@ namespace slskd
return results;
}
+
+ /// <summary>
+ /// Search filter options.
+ /// </summary>
+ public class SearchOptions : IValidatableObject
+ {
+ /// <summary>
+ /// Gets the list of search request filters.
+ /// </summary>
+ [Argument(default, "search-request-filter")]
+ [EnvironmentVariable("SEARCH_REQUEST_FILTER")]
+ [Description("regular expressions to filter incoming search requests")]
+ public string[] Request { get; init; } = Array.Empty<string>();
+
+ /// <summary>
+ /// Extended validation.
+ /// </summary>
+ /// <param name="validationContext"></param>
+ /// <returns></returns>
+ public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
+ {
+ var results = new List<ValidationResult>();
+
+ foreach (var filter in Request)
+ {
+ if (!filter.IsValidRegex())
+ {
+ results.Add(new ValidationResult($"Search request filter '{filter}' is not a valid regular expression"));
+ }
+ }
+
+ return results;
+ }
+ }
}
/// <summary>