diff options
| author | JP Dillingham <jp@dillingham.me> | 2022-10-20 19:28:33 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2022-10-20 19:28:33 -0500 |
| commit | 481d2029d7cadcf5b1985fa912a263d88f2f2cf3 (patch) | |
| tree | 8aa71f2c315674c6126dce6d4075fb342b44e36f /src | |
| parent | a9cca7683a3f3fde4fb0fa9a713c5d2c738165aa (diff) | |
| download | slskd-481d2029d7cadcf5b1985fa912a263d88f2f2cf3.tar.gz | |
use strongly typed hub
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Network/API/Hubs/NetworkHub.cs | 58 |
1 files changed, 21 insertions, 37 deletions
diff --git a/src/slskd/Network/API/Hubs/NetworkHub.cs b/src/slskd/Network/API/Hubs/NetworkHub.cs index cf1dc463..0013423a 100644 --- a/src/slskd/Network/API/Hubs/NetworkHub.cs +++ b/src/slskd/Network/API/Hubs/NetworkHub.cs @@ -20,38 +20,15 @@ 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 static readonly string RequestFile = "REQUEST_FILE"; - public static readonly string RequestFileInfo = "REQUEST_FILE_INFO"; - public static readonly string AuthenticationChallenge = "AUTHENTICATION_CHALLENGE"; - } - - ///// <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); - // } - //} - public interface INetworkHub { - Task RequestFile(string filename, Guid id); - Task RequestFileInfo(string filename, Guid id); - Task AuthenticationChallenge(string token); + Task Challenge(string token); + Task RequestFileAsync(string filename, Guid id); + Task RequestFileInfoAsync(string filename, Guid id); } /// <summary> @@ -71,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.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; @@ -89,25 +68,30 @@ namespace slskd.Network { if (Network.TryValidateAuthenticationChallengeResponse(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(); } |
