From e21eb94376f242145b07b49c923f2f997d83611c Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 28 Oct 2024 18:35:52 -0500 Subject: stub webhook configuration --- src/slskd/Core/Options.cs | 66 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) (limited to 'src/slskd/Core') diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 306f9b87..cca24ee9 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -2005,6 +2005,12 @@ namespace slskd /// public class IntegrationOptions { + /// + /// Gets webhook configuration. + /// + [Validate] + public Dictionary Webhooks { get; init; } = []; + /// /// Gets script configuration. /// @@ -2023,6 +2029,66 @@ namespace slskd [Validate] public PushbulletOptions Pushbullet { get; init; } = new PushbulletOptions(); + /// + /// Webhook configuration. + /// + public class WebhookOptions + { + /// + /// Gets the list of Event types that trigger the webhook. + /// + [Enum(typeof(EventType))] + public string[] On { get; init; } = Array.Empty(); + + /// + /// Gets details about the webhook call. + /// + [Validate] + public WebhookDetails Call { get; init; } = new WebhookDetails(); + } + + /// + /// Webhook details. + /// + public class WebhookDetails : IValidatableObject + { + /// + /// Gets the fully qualified URL for the webhook. + /// + [NotNullOrWhiteSpace] + public string Url { get; init; } + + /// + /// Gets the HTTP headers to include with the webhook. + /// + [Validate] + public IEnumerable Headers { get; init; } = []; + + /// + /// Gets a value indicating whether HTTPS certificate errors should be ignored. + /// + public bool IgnoreCertificateErrors { get; init; } = false; + + public IEnumerable Validate(ValidationContext validationContext) + { + var results = new List(); + + if (!Url.StartsWith("http://", StringComparison.OrdinalIgnoreCase) && !Url.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) + { + results.Add(new ValidationResult($"The {nameof(Url)} field must contain a fully qualified URL, including protocol (e.g. http:// or https://)")); + } + + return results; + } + } + + public class WebhookDetailHeader + { + [NotNullOrWhiteSpace] + public string Name { get; init; } + public string Value { get; init; } + } + /// /// Script configuration. /// -- cgit v1.2.3 From b94dfe45ad62b3fa6cad0e6358e5fcba49ff7d37 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 28 Oct 2024 20:18:57 -0500 Subject: use an array for headers or everything breaks --- src/slskd/Core/Options.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/slskd/Core') diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index cca24ee9..a9d6aeb8 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -2062,7 +2062,7 @@ namespace slskd /// Gets the HTTP headers to include with the webhook. /// [Validate] - public IEnumerable Headers { get; init; } = []; + public WebhookDetailHeader[] Headers { get; init; } = []; /// /// Gets a value indicating whether HTTPS certificate errors should be ignored. -- cgit v1.2.3 From 308a0afd86318b86d3ca1e88260c17de627bef11 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 2 Nov 2024 20:08:11 -0500 Subject: add loopback endpoint --- src/slskd/Core/API/Controllers/ApplicationController.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'src/slskd/Core') diff --git a/src/slskd/Core/API/Controllers/ApplicationController.cs b/src/slskd/Core/API/Controllers/ApplicationController.cs index 790b6b3d..be646ae7 100644 --- a/src/slskd/Core/API/Controllers/ApplicationController.cs +++ b/src/slskd/Core/API/Controllers/ApplicationController.cs @@ -26,6 +26,7 @@ namespace slskd.Core.API using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Hosting; + using Serilog; /// /// Application. @@ -53,6 +54,7 @@ namespace slskd.Core.API private IStateMonitor ApplicationStateMonitor { get; } private IOptionsMonitor OptionsMonitor { get; } private IHostApplicationLifetime Lifetime { get; } + private ILogger Log { get; } = Serilog.Log.ForContext(); /// /// Gets the current state of the application. @@ -149,5 +151,13 @@ namespace slskd.Core.API return PhysicalFile(file, "application/octet-stream", "slskd.dmp"); } + + [HttpPost("loopback")] + [Authorize(Policy = AuthPolicy.Any)] + public IActionResult Loopback([FromBody] object body) + { + Log.Information("Loopback POST: {Body}", body); + return Ok(); + } } -} +} \ No newline at end of file -- cgit v1.2.3 From 28e7090da1ec2a3711f660263edf37f78506a68b Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 2 Nov 2024 20:08:36 -0500 Subject: add timeout and retry options for webhooks --- src/slskd/Core/Options.cs | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) (limited to 'src/slskd/Core') diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index a9d6aeb8..e5f16b1e 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -2044,13 +2044,25 @@ namespace slskd /// Gets details about the webhook call. /// [Validate] - public WebhookDetails Call { get; init; } = new WebhookDetails(); + public WebhookHttpOptions Call { get; init; } = new WebhookHttpOptions(); + + /// + /// Gets the time to wait before timing out, in milliseconds. + /// + [Range(500, int.MaxValue)] + public int Timeout { get; init; } = 5000; + + /// + /// Gets the retry configuration. + /// + [Validate] + public RetryOptions Retry { get; init; } = new RetryOptions(); } /// - /// Webhook details. + /// Webhook HTTP options. /// - public class WebhookDetails : IValidatableObject + public class WebhookHttpOptions : IValidatableObject { /// /// Gets the fully qualified URL for the webhook. @@ -2062,7 +2074,7 @@ namespace slskd /// Gets the HTTP headers to include with the webhook. /// [Validate] - public WebhookDetailHeader[] Headers { get; init; } = []; + public WebhookHttpHeader[] Headers { get; init; } = []; /// /// Gets a value indicating whether HTTPS certificate errors should be ignored. @@ -2082,13 +2094,35 @@ namespace slskd } } - public class WebhookDetailHeader + /// + /// Webhook HTTP header configuration. + /// + public class WebhookHttpHeader { + /// + /// Gets the name of the header. + /// [NotNullOrWhiteSpace] public string Name { get; init; } + + /// + /// Gets the header's value. + /// public string Value { get; init; } } + /// + /// Retry configuration. + /// + public class RetryOptions + { + /// + /// Gets the number of attempts to make before failing. + /// + [Range(1, int.MaxValue)] + public int Attempts { get; init; } = 1; + } + /// /// Script configuration. /// -- cgit v1.2.3 From 4d5c0e437065e104a784b7080915ca2bfdd2218c Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 3 Nov 2024 16:47:29 -0600 Subject: add an IHttpClientFactory with SSL disabled --- src/slskd/Core/Constants.cs | 23 +++++++++++++++++++++++ src/slskd/Program.cs | 10 ++++++++++ 2 files changed, 33 insertions(+) create mode 100644 src/slskd/Core/Constants.cs (limited to 'src/slskd/Core') diff --git a/src/slskd/Core/Constants.cs b/src/slskd/Core/Constants.cs new file mode 100644 index 00000000..380a3519 --- /dev/null +++ b/src/slskd/Core/Constants.cs @@ -0,0 +1,23 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +namespace slskd; + +public static class Constants +{ + public static readonly string IgnoreCertificateErrors = nameof(IgnoreCertificateErrors); +} \ No newline at end of file diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 3ded2e6b..1ac798c1 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -26,6 +26,7 @@ namespace slskd using System.IO; using System.Linq; using System.Net; + using System.Net.Http; using System.Reflection; using System.Security.Cryptography.X509Certificates; using System.Text; @@ -546,6 +547,15 @@ namespace slskd // this is important to prevent memory leaks services.AddHttpClient(); + // add a special HttpClientFactory to DI that disables SSL. access it via: + // 'using var http = HttpClientFactory.CreateClient(Constants.IgnoreCertificateErrors)' + // thanks Microsoft, makes total sense and surely won't be easy to fuck up later! + services.AddHttpClient(Constants.IgnoreCertificateErrors) + .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler + { + ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator, + }); + // add a partially configured instance of SoulseekClient. the Application instance will // complete configuration at startup. services.AddSingleton(_ => -- cgit v1.2.3