From 53273459fcbeb1de0449aab50670f1bf7a44afd3 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 29 Oct 2022 17:24:12 -0500 Subject: clean up and tweak NetworkHub --- src/slskd/Network/API/Hubs/NetworkHub.cs | 89 ++++++++++++++++++++++++++------ 1 file changed, 73 insertions(+), 16 deletions(-) (limited to 'src/slskd/Network/API') diff --git a/src/slskd/Network/API/Hubs/NetworkHub.cs b/src/slskd/Network/API/Hubs/NetworkHub.cs index 0ee80f9a..cf29f15f 100644 --- a/src/slskd/Network/API/Hubs/NetworkHub.cs +++ b/src/slskd/Network/API/Hubs/NetworkHub.cs @@ -24,11 +24,33 @@ namespace slskd.Network using Microsoft.AspNetCore.SignalR; using Serilog; + /// + /// Methods for the . + /// public interface INetworkHub { + /// + /// Sends an authentication challenge token to the newly connected agent. + /// + /// The token. + /// The operation context. Task Challenge(string token); - Task RequestFile(string filename, Guid id); + + /// + /// Requests information about the specified from the agent. + /// + /// The name of the file. + /// The unique identifier for the request. + /// The operation context. Task RequestFileInfo(string filename, Guid id); + + /// + /// Requests the specified from the agent. + /// + /// The name of the file. + /// The unique identifier for the request. + /// The operation context. + Task RequestFileUpload(string filename, Guid id); } /// @@ -37,15 +59,23 @@ namespace slskd.Network [Authorize] public class NetworkHub : Hub { + /// + /// Initializes a new instance of the class. + /// + /// public NetworkHub( INetworkService networkService) { Network = networkService; } - private INetworkService Network { get; } private ILogger Log { get; } = Serilog.Log.ForContext(); + private INetworkService Network { get; } + /// + /// Executed when a new connection is established. + /// + /// public override async Task OnConnectedAsync() { var token = Network.GenerateAuthenticationChallengeToken(Context.ConnectionId); @@ -54,6 +84,11 @@ namespace slskd.Network await Clients.Caller.Challenge(token); } + /// + /// Executed when a connection is disconnected. + /// + /// The Exception that caused the disconnect. + /// public override Task OnDisconnectedAsync(Exception exception) { if (Network.TryDeregisterAgent(Context.ConnectionId, out var record)) @@ -64,25 +99,34 @@ namespace slskd.Network return Task.CompletedTask; } - public bool Login(string agent, string challengeResponse) + /// + /// Executed by the agent after receipt of an authentication challenge token, shortly after the connection is established. + /// + /// The agent's name. + /// The response to the challenge token. + /// Thrown when the challenge response is invalid. + public void Login(string agent, string challengeResponse) { - if (Network.TryValidateAuthenticationCredential(Context.ConnectionId, agent, challengeResponse)) + if (!Network.TryValidateAuthenticationCredential(Context.ConnectionId, agent, challengeResponse)) { - var remoteIp = Context.Features.Get().RemoteIpAddress.ToString(); - var record = new Agent { Name = agent, ConnectedAt = DateTime.UtcNow, IPAddress = remoteIp }; - - Log.Information("Agent connection {Id} ({IP}) authenticated as agent {Agent}", Context.ConnectionId, remoteIp, agent); - Network.RegisterAgent(Context.ConnectionId, record); - return true; + Log.Information("Agent connection {Id} authentication failed", Context.ConnectionId); + Network.TryDeregisterAgent(Context.ConnectionId, out var _); // just in case! + throw new UnauthorizedAccessException(); } - Log.Information("Agent connection {Id} authentication failed", Context.ConnectionId); - Network.TryDeregisterAgent(Context.ConnectionId, out var _); // just in case! - return false; - // todo: throw UnauthorizedAccessException()? + var remoteIp = Context.Features.Get().RemoteIpAddress.ToString(); + var record = new Agent { Name = agent, ConnectedAt = DateTime.UtcNow, IPAddress = remoteIp }; + + Log.Information("Agent connection {Id} ({IP}) authenticated as agent {Agent}", Context.ConnectionId, remoteIp, agent); + Network.RegisterAgent(Context.ConnectionId, record); } - public Guid GetShareUploadToken() + /// + /// Initiates the share upload workflow by generating and retrieving a request token. + /// + /// The generated token. + /// Thrown when the agent is not fully authenticated. + public Guid BeginShareUpload() { if (Network.TryGetAgentRegistration(Context.ConnectionId, out var record)) { @@ -96,7 +140,13 @@ namespace slskd.Network throw new UnauthorizedAccessException(); } - public void NotifyUploadFailed(Guid id, Exception exception) + /// + /// Notifies the controller that the agent was unable to upload the file requested by a call to . + /// + /// The unique identifier of the request. + /// The Exception that caused the failure. + /// Thrown when the agent is not fully authenticated. + public void NotifyFileUploadFailed(Guid id, Exception exception) { if (Network.TryGetAgentRegistration(Context.ConnectionId, out var record)) { @@ -107,6 +157,13 @@ namespace slskd.Network throw new UnauthorizedAccessException(); } + /// + /// Returns the response to a call to . + /// + /// The unique identifier for the request. + /// A value indicating whether the requested file exists on the agent's filesystem. + /// The length of the file, or 0 if the file does not exist. + /// Thrown when the agent is not fully authenticated. public void ReturnFileInfo(Guid id, bool exists, long length) { if (Network.TryGetAgentRegistration(Context.ConnectionId, out var record)) -- cgit v1.2.3