По сути, идея состоит в том, что у меня есть два приложения: сервер Blazor. приложение и классическое приложение WPF. Приложение Blazor может просматривать все компьютеры, на которых запущено настольное приложение WPF, и может выполнять некоторые глобальные команды, аналогичные командам программного обеспечения RAT, например: выключение, перезапуск, блокировку пользователя на компьютере.Это делается через веб-API. Приложение Blazor отправит нужную команду приложению WPF через SignalR и API.
Итак, вот что я делаю прямо сейчас:
В приложении Blazor у меня была бы такая кнопка
Код: Выделить всё
[*]
Restart
@code
{
private async Task SendCommand(string machineId, string command)
{
await JSRuntime.InvokeVoidAsync("sendCommandToMachine", machineId, command);
}
}
Код: Выделить всё
using System.Diagnostics;
using System.Reflection.PortableExecutable;
using System.Runtime.InteropServices;
using System.Windows;
using Microsoft.AspNetCore.SignalR.Client;
namespace PA.Desktop.Services
{
public class SignalRService
{
private HubConnection _connection;
public SignalRService(string machineId)
{
_connection = new HubConnectionBuilder()
.WithUrl($"https://mywebapi.azurewebsites.net/machinesHub?posteId={machineId}")
.WithAutomaticReconnect()
.Build();
_connection.On("ReceiveCommand", (command) =>
{
switch (command.ToLower())
{
case "restart":
RestartMachine();
break;
}
});
}
public async Task StartAsync()
{
await _connection.StartAsync();
}
public async Task StopAsync()
{
await _connection.StopAsync();
}
void RestartMachine()
{
Process.Start("shutdown", "/r /t 0");
}
}
}
Код: Выделить всё
protected override async void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var systemInfoService = SystemInfoService.Instance;
httpClient = new HttpClient();
userRepository = new UserRepository(httpClient);
notifyIcon = (TaskbarIcon)FindResource("MyNotifyIcon");
bool postIdReady = await systemInfoService.PostIdReadyTask;
if (postIdReady && systemInfoService.postId.HasValue)
{
signalRService = new SignalRService(systemInfoService.postId.Value.ToString());
await signalRService.StartAsync();
}
else
{
Debug.WriteLine("PostId is not available or could not be obtained.");
}
}
Код: Выделить всё
using Microsoft.AspNetCore.SignalR;
namespace PA.DataPoint.Server
{
public class MachinesHub : Hub
{
private static readonly Dictionary machineConnections = new Dictionary();
public override Task OnConnectedAsync()
{
var machineId = Context.GetHttpContext().Request.Query["posteId"];
if (!string.IsNullOrEmpty(machineId))
{
machineConnections[machineId] = Context.ConnectionId;
}
return base.OnConnectedAsync();
}
public override Task OnDisconnectedAsync(Exception exception)
{
var item = machineConnections.FirstOrDefault(kvp => kvp.Value == Context.ConnectionId);
if (!string.IsNullOrEmpty(item.Key))
{
machineConnections.Remove(item.Key);
}
return base.OnDisconnectedAsync(exception);
}
public async Task SendCommandToMachine(string machineId, string command)
{
if (machineConnections.TryGetValue(machineId, out var connectionId))
{
await Clients.Client(connectionId).SendAsync("ReceiveCommand", command);
}
else
{
Console.WriteLine($"MachineId {machineId} not connected or not found.");
}
}
}
}
Код: Выделить всё
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHub("/machinesHub"); // Map the SignalR Hub
});
Подробнее здесь: https://stackoverflow.com/questions/782 ... d-required
Мобильная версия