From 7b129b69211909a3af9bbeddf0acc1dc3d84e20c Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 17 Nov 2022 09:09:50 -0600 Subject: forbid API calls that require server interaction when running in Agent mode --- .../Messaging/API/Controllers/PublicChatController.cs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'src/slskd/Messaging/API/Controllers/PublicChatController.cs') diff --git a/src/slskd/Messaging/API/Controllers/PublicChatController.cs b/src/slskd/Messaging/API/Controllers/PublicChatController.cs index 83f7ede7..66f0951d 100644 --- a/src/slskd/Messaging/API/Controllers/PublicChatController.cs +++ b/src/slskd/Messaging/API/Controllers/PublicChatController.cs @@ -15,12 +15,15 @@ // along with this program. If not, see https://www.gnu.org/licenses/. // +using Microsoft.Extensions.Options; + namespace slskd.Messaging.API { using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; + using slskd.Relay; using Soulseek; /// @@ -33,12 +36,15 @@ namespace slskd.Messaging.API [Consumes("application/json")] public class PublicChatController : ControllerBase { - public PublicChatController(ISoulseekClient soulseekClient) + public PublicChatController(ISoulseekClient soulseekClient, IOptionsSnapshot optionsSnapshot) { Client = soulseekClient; + OptionsSnapshot = optionsSnapshot; } private ISoulseekClient Client { get; } + private IOptionsSnapshot OptionsSnapshot { get; } + private bool IsAgent => OptionsSnapshot.Value.Relay.Mode.ToEnum() == OperationMode.Agent; /// /// Starts public chat. @@ -48,6 +54,11 @@ namespace slskd.Messaging.API [Authorize(Policy = AuthPolicy.Any)] public async Task Start() { + if (IsAgent) + { + return Forbid(); + } + await Client.StartPublicChatAsync(); return StatusCode(StatusCodes.Status201Created); } @@ -60,6 +71,11 @@ namespace slskd.Messaging.API [Authorize(Policy = AuthPolicy.Any)] public async Task Stop() { + if (IsAgent) + { + return Forbid(); + } + await Client.StopPublicChatAsync(); return StatusCode(StatusCodes.Status204NoContent); } -- cgit v1.2.3