From d88e4a31f35d9197e79ff0d55ecd75085404be9a Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 25 Nov 2022 15:02:08 -0600 Subject: add IsRelayAgent to Program, simplify checks --- src/slskd/Core/API/Controllers/ServerController.cs | 5 ++--- .../API/Controllers/ConversationsController.cs | 13 ++++++----- .../API/Controllers/PublicChatController.cs | 5 ++--- .../Messaging/API/Controllers/RoomsController.cs | 21 +++++++++--------- src/slskd/Program.cs | 7 ++++++ .../Search/API/Controllers/SearchesController.cs | 13 ++++++----- .../API/Controllers/TransfersController.cs | 25 +++++++++++----------- src/slskd/Users/API/Controllers/UsersController.cs | 13 ++++++----- 8 files changed, 51 insertions(+), 51 deletions(-) (limited to 'src') diff --git a/src/slskd/Core/API/Controllers/ServerController.cs b/src/slskd/Core/API/Controllers/ServerController.cs index d9823a23..983fd0a0 100644 --- a/src/slskd/Core/API/Controllers/ServerController.cs +++ b/src/slskd/Core/API/Controllers/ServerController.cs @@ -48,7 +48,6 @@ namespace slskd.Core.API private ISoulseekClient Client { get; } private IOptionsSnapshot OptionsSnapshot { get; } private IStateSnapshot StateSnapshot { get; } - private bool IsAgent => OptionsSnapshot.Value.Relay.Mode.ToEnum() == RelayMode.Agent; /// /// Connects the client. @@ -61,7 +60,7 @@ namespace slskd.Core.API [ProducesResponseType(403)] public async Task Connect() { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -86,7 +85,7 @@ namespace slskd.Core.API [ProducesResponseType(403)] public IActionResult Disconnect([FromBody] string message) { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } diff --git a/src/slskd/Messaging/API/Controllers/ConversationsController.cs b/src/slskd/Messaging/API/Controllers/ConversationsController.cs index 90e7f380..d39c2c10 100644 --- a/src/slskd/Messaging/API/Controllers/ConversationsController.cs +++ b/src/slskd/Messaging/API/Controllers/ConversationsController.cs @@ -60,7 +60,6 @@ namespace slskd.Messaging.API private IStateMonitor ApplicationStateMonitor { get; } private IConversationTracker Tracker { get; } private IOptionsSnapshot OptionsSnapshot { get; } - private bool IsAgent => OptionsSnapshot.Value.Relay.Mode.ToEnum() == RelayMode.Agent; /// /// Acknowledges the given message id for the given username. @@ -78,7 +77,7 @@ namespace slskd.Messaging.API [ProducesResponseType(404)] public async Task Acknowledge([FromRoute]string username, [FromRoute]int id) { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -107,7 +106,7 @@ namespace slskd.Messaging.API [ProducesResponseType(404)] public async Task AcknowledgeAll([FromRoute]string username) { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -146,7 +145,7 @@ namespace slskd.Messaging.API [ProducesResponseType(204)] public IActionResult Delete([FromRoute]string username) { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -171,7 +170,7 @@ namespace slskd.Messaging.API [ProducesResponseType(typeof(Dictionary>), 200)] public IActionResult GetAll() { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -198,7 +197,7 @@ namespace slskd.Messaging.API [ProducesResponseType(404)] public IActionResult GetByUsername([FromRoute]string username) { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -229,7 +228,7 @@ namespace slskd.Messaging.API [ProducesResponseType(400)] public async Task Send([FromRoute]string username, [FromBody]string message) { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } diff --git a/src/slskd/Messaging/API/Controllers/PublicChatController.cs b/src/slskd/Messaging/API/Controllers/PublicChatController.cs index 8b6ce4f8..b3779fe3 100644 --- a/src/slskd/Messaging/API/Controllers/PublicChatController.cs +++ b/src/slskd/Messaging/API/Controllers/PublicChatController.cs @@ -44,7 +44,6 @@ namespace slskd.Messaging.API private ISoulseekClient Client { get; } private IOptionsSnapshot OptionsSnapshot { get; } - private bool IsAgent => OptionsSnapshot.Value.Relay.Mode.ToEnum() == RelayMode.Agent; /// /// Starts public chat. @@ -54,7 +53,7 @@ namespace slskd.Messaging.API [Authorize(Policy = AuthPolicy.Any)] public async Task Start() { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -71,7 +70,7 @@ namespace slskd.Messaging.API [Authorize(Policy = AuthPolicy.Any)] public async Task Stop() { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } diff --git a/src/slskd/Messaging/API/Controllers/RoomsController.cs b/src/slskd/Messaging/API/Controllers/RoomsController.cs index 1036e10e..0f2ccf25 100644 --- a/src/slskd/Messaging/API/Controllers/RoomsController.cs +++ b/src/slskd/Messaging/API/Controllers/RoomsController.cs @@ -58,7 +58,6 @@ namespace slskd.Messaging.API private IStateMonitor ApplicationStateMonitor { get; } private IRoomTracker Tracker { get; } private IOptionsSnapshot OptionsSnapshot { get; } - private bool IsAgent => OptionsSnapshot.Value.Relay.Mode.ToEnum() == RelayMode.Agent; /// /// Gets all rooms. @@ -70,7 +69,7 @@ namespace slskd.Messaging.API [ProducesResponseType(typeof(Dictionary>), 200)] public IActionResult GetAll() { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -91,7 +90,7 @@ namespace slskd.Messaging.API [ProducesResponseType(404)] public IActionResult GetByRoomName([FromRoute]string roomName) { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -118,7 +117,7 @@ namespace slskd.Messaging.API [ProducesResponseType(404)] public async Task SendMessage([FromRoute]string roomName, [FromBody]string message) { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -146,7 +145,7 @@ namespace slskd.Messaging.API [ProducesResponseType(404)] public async Task SetTicker([FromRoute] string roomName, [FromBody] string message) { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -174,7 +173,7 @@ namespace slskd.Messaging.API [ProducesResponseType(404)] public async Task AddRoomMember([FromRoute]string roomName, [FromBody]string username) { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -201,7 +200,7 @@ namespace slskd.Messaging.API [ProducesResponseType(404)] public IActionResult GetUsersByRoomName([FromRoute]string roomName) { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -230,7 +229,7 @@ namespace slskd.Messaging.API [ProducesResponseType(404)] public IActionResult GetMessagesByRoomName([FromRoute]string roomName) { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -255,7 +254,7 @@ namespace slskd.Messaging.API [ProducesResponseType(typeof(List), 200)] public async Task GetRooms() { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -289,7 +288,7 @@ namespace slskd.Messaging.API [ProducesResponseType(304)] public async Task JoinRoom([FromBody]string roomName) { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -330,7 +329,7 @@ namespace slskd.Messaging.API [ProducesResponseType(404)] public async Task LeaveRoom([FromRoute]string roomName) { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 58b1252f..0b82e825 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -152,6 +152,11 @@ namespace slskd /// public static bool IsDevelopment { get; } = new Version(0, 0, 0, 0) == AssemblyVersion; + /// + /// Gets a value indicating whether the application is being run in Relay Agent mode. + /// + public static bool IsRelayAgent { get; private set; } + /// /// Gets the path where application data is saved. /// @@ -358,6 +363,8 @@ namespace slskd return; } + IsRelayAgent = OptionsAtStartup.Relay.Enabled && OptionsAtStartup.Relay.Mode.ToEnum() == RelayMode.Agent; + ConfigureGlobalLogger(); Log = Serilog.Log.ForContext(typeof(Program)); diff --git a/src/slskd/Search/API/Controllers/SearchesController.cs b/src/slskd/Search/API/Controllers/SearchesController.cs index 507de5d6..74c1cd07 100644 --- a/src/slskd/Search/API/Controllers/SearchesController.cs +++ b/src/slskd/Search/API/Controllers/SearchesController.cs @@ -50,7 +50,6 @@ namespace slskd.Search.API private ISearchService Searches { get; } private IOptionsSnapshot OptionsSnapshot { get; } - private bool IsAgent => OptionsSnapshot.Value.Relay.Mode.ToEnum() == RelayMode.Agent; /// /// Performs a search for the specified . @@ -64,7 +63,7 @@ namespace slskd.Search.API [Authorize(Policy = AuthPolicy.Any)] public async Task Post([FromBody] SearchRequest request) { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -106,7 +105,7 @@ namespace slskd.Search.API [Authorize(Policy = AuthPolicy.Any)] public async Task GetById([FromRoute] Guid id, [FromQuery] bool includeResponses = false) { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -132,7 +131,7 @@ namespace slskd.Search.API [Authorize(Policy = AuthPolicy.Any)] public async Task GetResponsesById([FromRoute] Guid id) { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -155,7 +154,7 @@ namespace slskd.Search.API [Authorize(Policy = AuthPolicy.Any)] public async Task GetAll() { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -177,7 +176,7 @@ namespace slskd.Search.API [ProducesResponseType(304)] public async Task Cancel([FromRoute] Guid id) { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -210,7 +209,7 @@ namespace slskd.Search.API [ProducesResponseType(404)] public async Task Delete([FromRoute] Guid id) { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } diff --git a/src/slskd/Transfers/API/Controllers/TransfersController.cs b/src/slskd/Transfers/API/Controllers/TransfersController.cs index 6918888d..d664ae5d 100644 --- a/src/slskd/Transfers/API/Controllers/TransfersController.cs +++ b/src/slskd/Transfers/API/Controllers/TransfersController.cs @@ -52,7 +52,6 @@ namespace slskd.Transfers.API private ITransferService Transfers { get; } private IOptionsSnapshot OptionsSnapshot { get; } - private bool IsAgent => OptionsSnapshot.Value.Relay.Mode.ToEnum() == RelayMode.Agent; /// /// Cancels the specified download. @@ -69,7 +68,7 @@ namespace slskd.Transfers.API [ProducesResponseType(404)] public IActionResult CancelDownloadAsync([FromRoute, Required] string username, [FromRoute, Required]string id, [FromQuery]bool remove = false) { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -106,7 +105,7 @@ namespace slskd.Transfers.API [ProducesResponseType(204)] public IActionResult ClearCompletedDownloads() { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -138,7 +137,7 @@ namespace slskd.Transfers.API [ProducesResponseType(404)] public IActionResult CancelUpload([FromRoute, Required] string username, [FromRoute, Required]string id, [FromQuery]bool remove = false) { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -175,7 +174,7 @@ namespace slskd.Transfers.API [ProducesResponseType(204)] public IActionResult ClearCompletedUploads() { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -208,7 +207,7 @@ namespace slskd.Transfers.API [ProducesResponseType(typeof(string), 500)] public async Task EnqueueAsync([FromRoute, Required]string username, [FromBody]IEnumerable requests) { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -234,7 +233,7 @@ namespace slskd.Transfers.API [ProducesResponseType(200)] public IActionResult GetDownloadsAsync([FromQuery]bool includeRemoved = false) { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -266,7 +265,7 @@ namespace slskd.Transfers.API [ProducesResponseType(200)] public IActionResult GetDownloadsAsync([FromRoute, Required] string username) { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -298,7 +297,7 @@ namespace slskd.Transfers.API [ProducesResponseType(404)] public IActionResult GetDownload([FromRoute, Required] string username, [FromRoute, Required] string id) { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -333,7 +332,7 @@ namespace slskd.Transfers.API [ProducesResponseType(404)] public async Task GetPlaceInQueueAsync([FromRoute, Required] string username, [FromRoute, Required] string id) { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -368,7 +367,7 @@ namespace slskd.Transfers.API [ProducesResponseType(200)] public IActionResult GetUploads([FromQuery] bool includeRemoved = false) { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -400,7 +399,7 @@ namespace slskd.Transfers.API [ProducesResponseType(200)] public IActionResult GetUploads([FromRoute, Required] string username) { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -438,7 +437,7 @@ namespace slskd.Transfers.API [ProducesResponseType(200)] public IActionResult GetUploads([FromRoute, Required] string username, [FromRoute, Required] string id) { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } diff --git a/src/slskd/Users/API/Controllers/UsersController.cs b/src/slskd/Users/API/Controllers/UsersController.cs index 852d15a8..1404523e 100644 --- a/src/slskd/Users/API/Controllers/UsersController.cs +++ b/src/slskd/Users/API/Controllers/UsersController.cs @@ -56,7 +56,6 @@ namespace slskd.Users.API private ISoulseekClient Client { get; } private IUserService Users { get; } private IOptionsSnapshot OptionsSnapshot { get; } - private bool IsAgent => OptionsSnapshot.Value.Relay.Mode.ToEnum() == RelayMode.Agent; /// /// Retrieves the address of the specified . @@ -70,7 +69,7 @@ namespace slskd.Users.API [ProducesResponseType(404)] public async Task Endpoint([FromRoute, Required] string username) { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -97,7 +96,7 @@ namespace slskd.Users.API [ProducesResponseType(404)] public async Task Browse([FromRoute, Required] string username) { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -131,7 +130,7 @@ namespace slskd.Users.API [ProducesResponseType(404)] public IActionResult BrowseStatus([FromRoute, Required] string username) { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -156,7 +155,7 @@ namespace slskd.Users.API [ProducesResponseType(404)] public async Task Directory([FromRoute, Required] string username, [FromRoute, Required] string directory) { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -183,7 +182,7 @@ namespace slskd.Users.API [ProducesResponseType(404)] public async Task Info([FromRoute, Required] string username) { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } @@ -210,7 +209,7 @@ namespace slskd.Users.API [ProducesResponseType(404)] public async Task Status([FromRoute, Required] string username) { - if (IsAgent) + if (Program.IsRelayAgent) { return Forbid(); } -- cgit v1.2.3