From f5f11cac497c2eefc4ccee4b13f26e660ab9beac Mon Sep 17 00:00:00 2001 From: Jared Goodwin Date: Thu, 22 Jun 2023 09:52:24 -0700 Subject: [PATCH] Add logging on the server for wake commands sent. --- Server/Components/Devices/DeviceCard.razor.cs | 2 +- .../Components/Devices/DevicesFrame.razor.cs | 2 +- Server/Hubs/CircuitConnection.cs | 36 +++++++++---------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/Server/Components/Devices/DeviceCard.razor.cs b/Server/Components/Devices/DeviceCard.razor.cs index 9ce69b8d..6bbb7fef 100644 --- a/Server/Components/Devices/DeviceCard.razor.cs +++ b/Server/Components/Devices/DeviceCard.razor.cs @@ -329,7 +329,7 @@ namespace Remotely.Server.Components.Devices if (result.IsSuccess) { ToastService.ShowToast2( - $"Wake command sent to {result.Value} peer devices.", + $"Wake command sent to peer devices.", ToastType.Success); } else diff --git a/Server/Components/Devices/DevicesFrame.razor.cs b/Server/Components/Devices/DevicesFrame.razor.cs index 55964846..66cfe24f 100644 --- a/Server/Components/Devices/DevicesFrame.razor.cs +++ b/Server/Components/Devices/DevicesFrame.razor.cs @@ -358,7 +358,7 @@ namespace Remotely.Server.Components.Devices if (result.IsSuccess) { ToastService.ShowToast2( - $"Wake commands sent to {result.Value} peer devices.", + $"Wake commands sent to peer devices.", ToastType.Success); } else diff --git a/Server/Hubs/CircuitConnection.cs b/Server/Hubs/CircuitConnection.cs index 59d3e7c7..73155ac0 100644 --- a/Server/Hubs/CircuitConnection.cs +++ b/Server/Hubs/CircuitConnection.cs @@ -61,16 +61,14 @@ namespace Remotely.Server.Hubs /// Peer devices are those in the same group or the same public IP. /// /// - /// The number of peer devices that broadcasted the WOL packet. - Task> WakeDevice(Device device); + Task WakeDevice(Device device); /// /// Sends a Wake-On-LAN request for the specified device to its peer devices. /// Peer devices are those in the same group or the same public IP. /// /// - /// The number of peer devices that broadcasted the WOL packet. - Task> WakeDevices(Device[] devices); + Task WakeDevices(Device[] devices); } public class CircuitConnection : CircuitHandler, ICircuitConnection @@ -447,13 +445,13 @@ namespace Remotely.Server.Hubs return Task.CompletedTask; } - public async Task> WakeDevice(Device device) + public async Task WakeDevice(Device device) { try { if (!_dataService.DoesUserHaveAccessToDevice(device.ID, User.Id)) { - return Result.Fail("Unauthorized.") ; + return Result.Fail("Unauthorized.") ; } var availableDevices = _serviceSessionCache @@ -465,16 +463,16 @@ namespace Remotely.Server.Hubs await SendWakeCommand(device, availableDevices); - return Result.Ok(availableDevices.Length); + return Result.Ok(); } catch (Exception ex) { _logger.LogError(ex, "Error waking device {deviceId}.", device.ID); - return Result.Fail(ex); + return Result.Fail(ex); } } - public async Task> WakeDevices(Device[] devices) + public async Task WakeDevices(Device[] devices) { try { @@ -498,8 +496,6 @@ namespace Remotely.Server.Hubs { var group = devicesByGroupId.GetOrAdd(device.DeviceGroupID, key => new()); group.Add(device); - // We only need it added to one group. - continue; } if (!string.IsNullOrWhiteSpace(device.PublicIP)) @@ -509,32 +505,27 @@ namespace Remotely.Server.Hubs } } - - var peerCount = 0; - foreach (var deviceToWake in filteredDevices) { if (!string.IsNullOrWhiteSpace(deviceToWake.DeviceGroupID) && devicesByGroupId.TryGetValue(deviceToWake.DeviceGroupID, out var groupList)) { await SendWakeCommand(deviceToWake, groupList); - peerCount += groupList.Count; } if (!string.IsNullOrWhiteSpace(deviceToWake.PublicIP) && devicesByPublicIp.TryGetValue(deviceToWake.PublicIP, out var ipList)) { await SendWakeCommand(deviceToWake, ipList); - peerCount += ipList.Count; } } - return Result.Ok(peerCount); + return Result.Ok(); } catch (Exception ex) { _logger.LogError(ex, "Error while waking devices."); - return Result.Fail(ex); + return Result.Fail(ex); } } @@ -604,6 +595,15 @@ namespace Remotely.Server.Hubs { if (_serviceSessionCache.TryGetConnectionId(peerDevice.ID, out var connectionId)) { + _logger.LogInformation( + "Sending wake command for device {deviceName} ({deviceId}) to " + + "peer device {peerDeviceName} ({peerDeviceId}). " + + "Sender: {username}.", + deviceToWake.DeviceName, + deviceToWake.ID, + peerDevice.DeviceName, + peerDevice.ID, + User.UserName); await _agentHubContext.Clients.Client(connectionId).SendAsync("WakeDevice", mac); } }