aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Network/API/Hubs
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2022-10-27 09:44:17 -0500
committerGitHub <noreply@github.com>2022-10-27 09:44:17 -0500
commit028af934a264a91c831eac3561c5e24833b6ee38 (patch)
tree125b0dc693c72cf97370f9ee9a9f7e51fbdc9210 /src/slskd/Network/API/Hubs
parenta85c5cf8d414b71ac66455e9d97cac62b29c95ad (diff)
parent218f5d8b2376fbee798c9a4575e5068a602aec04 (diff)
downloadslskd-028af934a264a91c831eac3561c5e24833b6ee38.tar.gz
Merge pull request #711 from slskd/signalr-result
Various improvements to the Network mode functionality
Diffstat (limited to 'src/slskd/Network/API/Hubs')
-rw-r--r--src/slskd/Network/API/Hubs/NetworkHub.cs71
1 files changed, 27 insertions, 44 deletions
diff --git a/src/slskd/Network/API/Hubs/NetworkHub.cs b/src/slskd/Network/API/Hubs/NetworkHub.cs
index f79fecf2..298dd9ec 100644
--- a/src/slskd/Network/API/Hubs/NetworkHub.cs
+++ b/src/slskd/Network/API/Hubs/NetworkHub.cs
@@ -20,40 +20,22 @@ namespace slskd.Network
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
+ using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.SignalR;
using Serilog;
- public static class NetworkHubMethods
+ public interface INetworkHub
{
- public static readonly string RequestFile = "REQUEST_FILE";
- public static readonly string RequestFileInfo = "REQUEST_FILE_INFO";
- public static readonly string AuthenticationChallenge = "AUTHENTICATION_CHALLENGE";
- public static readonly string AuthenticationChallengeAccepted = "AUTHENTICATION_CHALLENGE_ACCEPTED";
- public static readonly string AuthenticationChallengeRejected = "AUTHENTICATION_CHALLENGE_REJECTED";
- }
-
- /// <summary>
- /// Extension methods for the network SignalR hub.
- /// </summary>
- public static class NetworkHubExtensions
- {
- public static Task RequestFileAsync(this IHubContext<NetworkHub> hub, string agent, string filename, Guid id)
- {
- // todo: how to send this to the specified agent?
- return hub.Clients.All.SendAsync(NetworkHubMethods.RequestFile, filename, id);
- }
-
- public static Task RequestFileInfoAsync(this IHubContext<NetworkHub> hub, string agent, string filename, Guid id)
- {
- return hub.Clients.All.SendAsync(NetworkHubMethods.RequestFileInfo, filename, id);
- }
+ Task Challenge(string token);
+ Task RequestFile(string filename, Guid id);
+ Task RequestFileInfo(string filename, Guid id);
}
/// <summary>
/// The network SignalR hub.
/// </summary>
[Authorize]
- public class NetworkHub : Hub
+ public class NetworkHub : Hub<INetworkHub>
{
public NetworkHub(
INetworkService networkService)
@@ -66,15 +48,17 @@ namespace slskd.Network
public override async Task OnConnectedAsync()
{
- Log.Information("Agent connection {Id} established. Sending authentication challenge...", Context.ConnectionId);
- await Clients.Caller.SendAsync(NetworkHubMethods.AuthenticationChallenge, Network.GenerateAuthenticationChallengeToken(Context.ConnectionId));
+ var token = Network.GenerateAuthenticationChallengeToken(Context.ConnectionId);
+
+ Log.Information("Agent connection {Id} established. Sending authentication challenge {Token}...", Context.ConnectionId, token);
+ await Clients.Caller.Challenge(token);
}
public override Task OnDisconnectedAsync(Exception exception)
{
- if (Network.TryRemoveAgentRegistration(Context.ConnectionId, out var agent))
+ if (Network.TryDeregisterAgent(Context.ConnectionId, out var record))
{
- Log.Warning("Agent {Agent} (connection {Id}) disconnected", agent, Context.ConnectionId);
+ Log.Warning("Agent {Agent} (connection {Id}) disconnected", record.Agent.Name, Context.ConnectionId);
}
return Task.CompletedTask;
@@ -82,44 +66,43 @@ namespace slskd.Network
public bool Login(string agent, string challengeResponse)
{
- if (Network.TryValidateAuthenticationChallengeResponse(Context.ConnectionId, agent, challengeResponse))
+ if (Network.TryValidateAuthenticationCredential(Context.ConnectionId, agent, challengeResponse))
{
- Log.Information("Agent connection {Id} authenticated as agent {Agent}", Context.ConnectionId, agent);
- Network.RegisterAgent(Context.ConnectionId, agent);
+ var remoteIp = Context.Features.Get<IHttpConnectionFeature>().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.TryRemoveAgentRegistration(Context.ConnectionId, out var _); // just in case!
+ Network.TryDeregisterAgent(Context.ConnectionId, out var _); // just in case!
return false;
}
public Guid GetShareUploadToken()
{
- if (Network.TryGetAgentRegistration(Context.ConnectionId, out var agent))
+ if (Network.TryGetAgentRegistration(Context.ConnectionId, out var record))
{
- return Network.GetShareUploadToken(agent);
+ var token = Network.GenerateShareUploadToken(record.Agent.Name);
+ Log.Information("Agent {Agent} (connection {Id}) requested share upload token {Token}", record.Agent.Name, record.ConnectionId, token);
+ return token;
}
// this can happen if the agent attempts to upload before logging in
- Log.Information("Agent connection {Id} requested a share upload token, but was not registered.", Context.ConnectionId);
+ Log.Information("Agent connection {Id} requested a share upload token, but is not registered.", Context.ConnectionId);
throw new UnauthorizedAccessException();
}
- public void NotifyUploadFailed(Guid id)
+ public void NotifyUploadFailed(Guid id, Exception exception)
{
- if (Network.PendingFileUploads.TryGetValue(id, out var record))
- {
- record.Stream.TrySetException(new NotFoundException("The file could not be uploaded from the remote agent"));
- }
+ Network.NotifyFileStreamException(id, exception);
}
public void ReturnFileInfo(Guid id, bool exists, long length)
{
- if (Network.PendingFileInquiries.TryGetValue(id, out var record))
- {
- record.TrySetResult((exists, length));
- }
+ Network.HandleFileInfoResponse(id, (exists, length));
}
}
} \ No newline at end of file