testing remote stuff

This commit is contained in:
kkb 2023-11-17 17:12:41 +01:00
parent 1e05d4576d
commit 3c9ccaafeb
374 changed files with 10526 additions and 2037 deletions

View file

@ -1,11 +1,11 @@
using Insight.Domain.Interfaces;
using Insight.Domain.Messages;
using Insight.Domain.Messages.Agent;
using Insight.Server.Network.Handlers.Agent;
using Insight.Domain.Network;
using Insight.Domain.Network.Agent.Messages;
using Insight.Server.Network.Agent.Handlers;
using Microsoft.Extensions.Logging;
using Vaitr.Network;
namespace Insight.Server.Network;
namespace Insight.Server.Network.Agent;
public class AgentSession : TcpSession<IMessage>
{
@ -56,7 +56,7 @@ public class AgentSession : TcpSession<IMessage>
{
await base.OnReceivedAsync(context, cancellationToken);
if (Id is null && context.Packet is not Authentication) return;
if (Id is null && context.Packet is not AuthenticationResponse) return;
await _agentHandler.StatisticUpdateAsync(this, cancellationToken);
@ -69,13 +69,6 @@ public class AgentSession : TcpSession<IMessage>
catch (Exception ex)
{
_logger.LogWarning("Agent ({ep?}) {ex}", RemoteEndPoint, ex.ToString());
//await _mediator.Send(new AgentLog(new AgentLogEntity
//{
// Category = CategoryEnum.Network.ToString(),
// Status = StatusEnum.Error.ToString(),
// Message = e.StackTrace
//}, this), cancellationToken).ConfigureAwait(false);
}
}
}

View file

@ -1,13 +1,12 @@
using Insight.Domain.Enums;
using Insight.Domain.Interfaces;
using Insight.Domain.Messages;
using Insight.Domain.Messages.Agent;
using Insight.Infrastructure;
using Insight.Domain.Network;
using Insight.Domain.Network.Agent.Messages;
using Insight.Infrastructure.Entities;
using Microsoft.Extensions.Logging;
using MongoDB.Driver;
namespace Insight.Server.Network.Handlers.Agent;
namespace Insight.Server.Network.Agent.Handlers;
public class AgentHandler : IMessageHandler<AgentSession>
{
@ -22,18 +21,18 @@ public class AgentHandler : IMessageHandler<AgentSession>
public async ValueTask HandleAsync<TMessage>(AgentSession sender, TMessage message, CancellationToken cancellationToken) where TMessage : IMessage
{
if (message is AuthenticationRequest authenticationRequest)
switch (message)
{
await AuthenticationRequestAsync(sender, authenticationRequest, cancellationToken);
}
if (message is Authentication authentication)
{
await AuthenticationAsync(sender, authentication, cancellationToken);
case AuthenticationRequest authenticationRequest:
await OnAuthenticationRequestAsync(sender, authenticationRequest, cancellationToken);
break;
case AuthenticationResponse authenticationResponse:
await OnAuthenticationResponseAsync(sender, authenticationResponse, cancellationToken);
break;
}
}
private async ValueTask AuthenticationRequestAsync(AgentSession session, AuthenticationRequest message, CancellationToken cancellationToken)
private async ValueTask OnAuthenticationRequestAsync(AgentSession session, AuthenticationRequest message, CancellationToken cancellationToken)
{
await session.SendAsync(message, cancellationToken);
@ -52,7 +51,7 @@ public class AgentHandler : IMessageHandler<AgentSession>
session.Disconnect();
}
private async ValueTask AuthenticationAsync(AgentSession session, Authentication authentication, CancellationToken cancellationToken)
private async ValueTask OnAuthenticationResponseAsync(AgentSession session, AuthenticationResponse authentication, CancellationToken cancellationToken)
{
if (authentication is null)
{

View file

@ -0,0 +1,31 @@
using Insight.Domain.Interfaces;
using Insight.Domain.Network;
using Insight.Domain.Network.Agent.Messages;
using Microsoft.Extensions.Logging;
namespace Insight.Server.Network.Agent.Handlers;
public class CustomHandler : IMessageHandler<AgentSession>
{
private readonly ILogger<CustomHandler> _logger;
public CustomHandler(ILogger<CustomHandler> logger)
{
_logger = logger;
}
public async ValueTask HandleAsync<TMessage>(AgentSession sender, TMessage message, CancellationToken cancellationToken) where TMessage : IMessage
{
switch (message)
{
case Response response:
await OnResponseAsync(sender, response, cancellationToken);
break;
}
}
private async ValueTask OnResponseAsync(AgentSession sender, Response response, CancellationToken cancellationToken)
{
_logger.LogWarning($"Response: {response.ResponseData}");
}
}

View file

@ -1,12 +1,11 @@
using Insight.Domain.Interfaces;
using Insight.Domain.Messages;
using Insight.Domain.Messages.Agent;
using Insight.Infrastructure;
using Insight.Domain.Network;
using Insight.Domain.Network.Agent.Messages;
using Insight.Infrastructure.Entities;
using MongoDB.Bson;
using MongoDB.Driver;
namespace Insight.Server.Network.Handlers.Agent;
namespace Insight.Server.Network.Agent.Handlers;
public class DriveHandler : IMessageHandler<AgentSession>
{
@ -19,9 +18,11 @@ public class DriveHandler : IMessageHandler<AgentSession>
public async ValueTask HandleAsync<TMessage>(AgentSession sender, TMessage message, CancellationToken cancellationToken) where TMessage : IMessage
{
if (message is DriveList drives)
switch (message)
{
await OnDrivesAsync(sender, drives, cancellationToken);
case Collection<Drive> drives:
await OnDrivesAsync(sender, drives, cancellationToken);
break;
}
}

View file

@ -1,14 +1,13 @@
using Insight.Domain.Enums;
using Insight.Domain.Interfaces;
using Insight.Domain.Messages;
using Insight.Domain.Messages.Agent;
using Insight.Infrastructure;
using Insight.Domain.Network;
using Insight.Domain.Network.Agent.Messages;
using Insight.Infrastructure.Entities;
using Microsoft.Extensions.Logging;
using MongoDB.Driver;
using static Insight.Domain.Messages.Agent.Event;
using static Insight.Domain.Network.Agent.Messages.Event;
namespace Insight.Server.Network.Handlers.Agent;
namespace Insight.Server.Network.Agent.Handlers;
public class EventHandler : IMessageHandler<AgentSession>
{
@ -23,9 +22,11 @@ public class EventHandler : IMessageHandler<AgentSession>
public async ValueTask HandleAsync<TMessage>(AgentSession sender, TMessage message, CancellationToken cancellationToken) where TMessage : IMessage
{
if (message is Event @event)
switch (message)
{
await OnEventAsync(sender, @event, cancellationToken);
case Event @event:
await OnEventAsync(sender, @event, cancellationToken);
break;
}
}

View file

@ -1,12 +1,11 @@
using Insight.Domain.Interfaces;
using Insight.Domain.Messages;
using Insight.Domain.Messages.Agent;
using Insight.Infrastructure;
using Insight.Domain.Network;
using Insight.Domain.Network.Agent.Messages;
using Insight.Infrastructure.Entities;
using MongoDB.Bson;
using MongoDB.Driver;
namespace Insight.Server.Network.Handlers.Agent;
namespace Insight.Server.Network.Agent.Handlers;
public class InterfaceHandler : IMessageHandler<AgentSession>
{
@ -19,9 +18,11 @@ public class InterfaceHandler : IMessageHandler<AgentSession>
public async ValueTask HandleAsync<TMessage>(AgentSession sender, TMessage message, CancellationToken cancellationToken) where TMessage : IMessage
{
if (message is InterfaceList interfaces)
switch (message)
{
await OnInterfacesAsync(sender, interfaces, cancellationToken);
case Collection<Interface> interfaces:
await OnInterfacesAsync(sender, interfaces, cancellationToken);
break;
}
}

View file

@ -1,11 +1,10 @@
using Insight.Domain.Interfaces;
using Insight.Domain.Messages;
using Insight.Domain.Messages.Agent;
using Insight.Infrastructure;
using Insight.Domain.Network;
using Insight.Domain.Network.Agent.Messages;
using Insight.Infrastructure.Entities;
using MongoDB.Driver;
namespace Insight.Server.Network.Handlers.Agent
namespace Insight.Server.Network.Agent.Handlers
{
public class MainboardHandler : IMessageHandler<AgentSession>
{
@ -18,9 +17,11 @@ namespace Insight.Server.Network.Handlers.Agent
public async ValueTask HandleAsync<TMessage>(AgentSession sender, TMessage message, CancellationToken cancellationToken) where TMessage : IMessage
{
if (message is Mainboard mainboard)
switch (message)
{
await OnMainboardAsync(sender, mainboard, cancellationToken);
case Mainboard mainboard:
await OnMainboardAsync(sender, mainboard, cancellationToken);
break;
}
}

View file

@ -1,12 +1,11 @@
using Insight.Domain.Interfaces;
using Insight.Domain.Messages;
using Insight.Domain.Messages.Agent;
using Insight.Infrastructure;
using Insight.Domain.Network;
using Insight.Domain.Network.Agent.Messages;
using Insight.Infrastructure.Entities;
using MongoDB.Bson;
using MongoDB.Driver;
namespace Insight.Server.Network.Handlers.Agent;
namespace Insight.Server.Network.Agent.Handlers;
public class MemoryHandler : IMessageHandler<AgentSession>
{
@ -19,9 +18,11 @@ public class MemoryHandler : IMessageHandler<AgentSession>
public async ValueTask HandleAsync<TMessage>(AgentSession sender, TMessage message, CancellationToken cancellationToken) where TMessage : IMessage
{
if (message is MemoryList memory)
switch (message)
{
await OnMemoryAsync(sender, memory, cancellationToken);
case Collection<Memory> memory:
await OnMemoryAsync(sender, memory, cancellationToken);
break;
}
}

View file

@ -1,11 +1,10 @@
using Insight.Domain.Interfaces;
using Insight.Domain.Messages;
using Insight.Domain.Messages.Agent;
using Insight.Infrastructure;
using Insight.Domain.Network;
using Insight.Domain.Network.Agent.Messages;
using Insight.Infrastructure.Entities;
using MongoDB.Driver;
namespace Insight.Server.Network.Handlers.Agent;
namespace Insight.Server.Network.Agent.Handlers;
public class OperationSystemHandler : IMessageHandler<AgentSession>
{
@ -18,9 +17,11 @@ public class OperationSystemHandler : IMessageHandler<AgentSession>
public async ValueTask HandleAsync<TMessage>(AgentSession sender, TMessage message, CancellationToken cancellationToken) where TMessage : IMessage
{
if (message is OperationSystem os)
switch (message)
{
await OnOperationSystemAsync(sender, os, cancellationToken);
case OperationSystem os:
await OnOperationSystemAsync(sender, os, cancellationToken);
break;
}
}

View file

@ -1,12 +1,11 @@
using Insight.Domain.Interfaces;
using Insight.Domain.Messages;
using Insight.Domain.Messages.Agent;
using Insight.Infrastructure;
using Insight.Domain.Network;
using Insight.Domain.Network.Agent.Messages;
using Insight.Infrastructure.Entities;
using MongoDB.Bson;
using MongoDB.Driver;
namespace Insight.Server.Network.Handlers.Agent;
namespace Insight.Server.Network.Agent.Handlers;
public class PrinterHandler : IMessageHandler<AgentSession>
{
@ -19,9 +18,11 @@ public class PrinterHandler : IMessageHandler<AgentSession>
public async ValueTask HandleAsync<TMessage>(AgentSession sender, TMessage message, CancellationToken cancellationToken) where TMessage : IMessage
{
if (message is PrinterList printers)
switch (message)
{
await OnPrintersAsync(sender, printers, cancellationToken);
case Collection<Printer> printers:
await OnPrintersAsync(sender, printers, cancellationToken);
break;
}
}

View file

@ -1,12 +1,11 @@
using Insight.Domain.Interfaces;
using Insight.Domain.Messages;
using Insight.Domain.Messages.Agent;
using Insight.Infrastructure;
using Insight.Domain.Network;
using Insight.Domain.Network.Agent.Messages;
using Insight.Infrastructure.Entities;
using MongoDB.Bson;
using MongoDB.Driver;
namespace Insight.Server.Network.Handlers.Agent;
namespace Insight.Server.Network.Agent.Handlers;
public class ProcessorHandler : IMessageHandler<AgentSession>
{
@ -19,9 +18,11 @@ public class ProcessorHandler : IMessageHandler<AgentSession>
public async ValueTask HandleAsync<TMessage>(AgentSession sender, TMessage message, CancellationToken cancellationToken) where TMessage : IMessage
{
if (message is ProcessorList processors)
switch (message)
{
await OnProcessorsAsync(sender, processors, cancellationToken);
case Collection<Processor> processors:
await OnProcessorsAsync(sender, processors, cancellationToken);
break;
}
}

View file

@ -1,12 +1,11 @@
using Insight.Domain.Interfaces;
using Insight.Domain.Messages;
using Insight.Domain.Messages.Agent;
using Insight.Infrastructure;
using Insight.Domain.Network;
using Insight.Domain.Network.Agent.Messages;
using Insight.Infrastructure.Entities;
using MongoDB.Bson;
using MongoDB.Driver;
namespace Insight.Server.Network.Handlers.Agent;
namespace Insight.Server.Network.Agent.Handlers;
public class ServiceHandler : IMessageHandler<AgentSession>
{
@ -19,9 +18,11 @@ public class ServiceHandler : IMessageHandler<AgentSession>
public async ValueTask HandleAsync<TMessage>(AgentSession sender, TMessage message, CancellationToken cancellationToken) where TMessage : IMessage
{
if (message is ServiceList services)
switch (message)
{
await OnServicesAsync(sender, services, cancellationToken);
case Collection<Service> services:
await OnServicesAsync(sender, services, cancellationToken);
break;
}
}

View file

@ -1,12 +1,11 @@
using Insight.Domain.Interfaces;
using Insight.Domain.Messages;
using Insight.Domain.Messages.Agent;
using Insight.Infrastructure;
using Insight.Domain.Network;
using Insight.Domain.Network.Agent.Messages;
using Insight.Infrastructure.Entities;
using MongoDB.Bson;
using MongoDB.Driver;
namespace Insight.Server.Network.Handlers.Agent;
namespace Insight.Server.Network.Agent.Handlers;
public class SessionHandler : IMessageHandler<AgentSession>
{
@ -19,9 +18,11 @@ public class SessionHandler : IMessageHandler<AgentSession>
public async ValueTask HandleAsync<TMessage>(AgentSession sender, TMessage message, CancellationToken cancellationToken) where TMessage : IMessage
{
if (message is SessionList sessions)
switch (message)
{
await OnSessionsAsync(sender, sessions, cancellationToken);
case Collection<Session> sessions:
await OnSessionsAsync(sender, sessions, cancellationToken);
break;
}
}

View file

@ -1,12 +1,11 @@
using Insight.Domain.Interfaces;
using Insight.Domain.Messages;
using Insight.Domain.Messages.Agent;
using Insight.Infrastructure;
using Insight.Domain.Network;
using Insight.Domain.Network.Agent.Messages;
using Insight.Infrastructure.Entities;
using MongoDB.Bson;
using MongoDB.Driver;
namespace Insight.Server.Network.Handlers.Agent;
namespace Insight.Server.Network.Agent.Handlers;
public class SoftwareHandler : IMessageHandler<AgentSession>
{
@ -19,9 +18,11 @@ public class SoftwareHandler : IMessageHandler<AgentSession>
public async ValueTask HandleAsync<TMessage>(AgentSession sender, TMessage message, CancellationToken cancellationToken) where TMessage : IMessage
{
if (message is ApplicationList applications)
switch (message)
{
await OnApplicationsAsync(sender, applications, cancellationToken);
case Collection<Application> applications:
await OnApplicationsAsync(sender, applications, cancellationToken);
break;
}
}

View file

@ -1,12 +1,11 @@
using Insight.Domain.Interfaces;
using Insight.Domain.Messages;
using Insight.Domain.Messages.Agent;
using Insight.Infrastructure;
using Insight.Domain.Network;
using Insight.Domain.Network.Agent.Messages;
using Insight.Infrastructure.Entities;
using MongoDB.Bson;
using MongoDB.Driver;
namespace Insight.Server.Network.Handlers.Agent;
namespace Insight.Server.Network.Agent.Handlers;
public class StoragePoolHandler : IMessageHandler<AgentSession>
{
@ -19,9 +18,11 @@ public class StoragePoolHandler : IMessageHandler<AgentSession>
public async ValueTask HandleAsync<TMessage>(AgentSession sender, TMessage message, CancellationToken cancellationToken) where TMessage : IMessage
{
if (message is StoragePoolList storagePools)
switch (message)
{
await OnStoragePoolsAsync(sender, storagePools, cancellationToken);
case Collection<StoragePool> storagePools:
await OnStoragePoolsAsync(sender, storagePools, cancellationToken);
break;
}
}

View file

@ -1,11 +1,10 @@
using Insight.Domain.Interfaces;
using Insight.Domain.Messages;
using Insight.Domain.Messages.Agent;
using Insight.Infrastructure;
using Insight.Domain.Network;
using Insight.Domain.Network.Agent.Messages;
using Insight.Infrastructure.Entities;
using MongoDB.Driver;
namespace Insight.Server.Network.Handlers.Agent;
namespace Insight.Server.Network.Agent.Handlers;
public class SystemInfoHandler : IMessageHandler<AgentSession>
{
@ -18,9 +17,11 @@ public class SystemInfoHandler : IMessageHandler<AgentSession>
public async ValueTask HandleAsync<TMessage>(AgentSession sender, TMessage message, CancellationToken cancellationToken) where TMessage : IMessage
{
if (message is SystemInfo systemInfo)
switch (message)
{
await OnSystemInfoAsync(sender, systemInfo, cancellationToken);
case SystemInfo systemInfo:
await OnSystemInfoAsync(sender, systemInfo, cancellationToken);
break;
}
}

View file

@ -1,8 +1,7 @@
using Insight.Domain.Enums;
using Insight.Domain.Interfaces;
using Insight.Domain.Messages;
using Insight.Domain.Messages.Agent;
using Insight.Infrastructure;
using Insight.Domain.Network;
using Insight.Domain.Network.Agent.Messages;
using Insight.Infrastructure.Entities;
using Insight.Server.Models;
using MongoDB.Driver;
@ -10,7 +9,7 @@ using System.Text;
using System.Text.RegularExpressions;
using static Insight.Server.Models.MonitorMessage;
namespace Insight.Server.Network.Handlers.Agent;
namespace Insight.Server.Network.Agent.Handlers;
public class TrapHandler : IMessageHandler<AgentSession>
{
@ -23,9 +22,11 @@ public class TrapHandler : IMessageHandler<AgentSession>
public async ValueTask HandleAsync<TMessage>(AgentSession sender, TMessage message, CancellationToken cancellationToken) where TMessage : IMessage
{
if (message is Trap trap)
switch (message)
{
await OnTrapAsync(sender, trap, cancellationToken);
case Trap trap:
await OnTrapAsync(sender, trap, cancellationToken);
break;
}
}

View file

@ -1,12 +1,11 @@
using Insight.Domain.Interfaces;
using Insight.Domain.Messages;
using Insight.Domain.Messages.Agent;
using Insight.Infrastructure;
using Insight.Domain.Network;
using Insight.Domain.Network.Agent.Messages;
using Insight.Infrastructure.Entities;
using MongoDB.Bson;
using MongoDB.Driver;
namespace Insight.Server.Network.Handlers.Agent;
namespace Insight.Server.Network.Agent.Handlers;
public class UpdateHandler : IMessageHandler<AgentSession>
{
@ -19,13 +18,15 @@ public class UpdateHandler : IMessageHandler<AgentSession>
public async ValueTask HandleAsync<TMessage>(AgentSession sender, TMessage message, CancellationToken cancellationToken) where TMessage : IMessage
{
if (message is UpdateList updates)
switch (message)
{
await OnUpdatesAsync(sender, updates, cancellationToken);
case UpdateCollection updates:
await OnUpdatesAsync(sender, updates, cancellationToken);
break;
}
}
private async ValueTask OnUpdatesAsync(AgentSession session, UpdateList updates, CancellationToken cancellationToken)
private async ValueTask OnUpdatesAsync(AgentSession session, UpdateCollection updates, CancellationToken cancellationToken)
{
var agentEntity = await _database.Agent().Find(Builders<AgentEntity>.Filter.Eq(p => p.Id, session.Id)).FirstOrDefaultAsync(cancellationToken);
if (agentEntity is null) return;

View file

@ -1,12 +1,11 @@
using Insight.Domain.Interfaces;
using Insight.Domain.Messages;
using Insight.Domain.Messages.Agent;
using Insight.Infrastructure;
using Insight.Domain.Network;
using Insight.Domain.Network.Agent.Messages;
using Insight.Infrastructure.Entities;
using MongoDB.Bson;
using MongoDB.Driver;
namespace Insight.Server.Network.Handlers.Agent;
namespace Insight.Server.Network.Agent.Handlers;
public class UserHandler : IMessageHandler<AgentSession>
{
@ -19,9 +18,11 @@ public class UserHandler : IMessageHandler<AgentSession>
public async ValueTask HandleAsync<TMessage>(AgentSession sender, TMessage message, CancellationToken cancellationToken) where TMessage : IMessage
{
if (message is UserList users)
switch (message)
{
await OnUsersAsync(sender, users, cancellationToken);
case Collection<User> users:
await OnUsersAsync(sender, users, cancellationToken);
break;
}
}
@ -40,18 +41,18 @@ public class UserHandler : IMessageHandler<AgentSession>
if (users is not null && users.Any())
{
var userBulk = new List<WriteModel<HostUserEntity>>();
var userBulk = new List<WriteModel<HostSysUserEntity>>();
foreach (var user in users)
{
var userFilter = Builders<HostUserEntity>.Filter.And(new List<FilterDefinition<HostUserEntity>>
var userFilter = Builders<HostSysUserEntity>.Filter.And(new List<FilterDefinition<HostSysUserEntity>>
{
Builders<HostUserEntity>.Filter.Eq(x => x.Host, hostEntity.Id),
Builders<HostUserEntity>.Filter.Eq(x => x.Domain, user?.Domain),
Builders<HostUserEntity>.Filter.Eq(x => x.Name, user?.Name)
Builders<HostSysUserEntity>.Filter.Eq(x => x.Host, hostEntity.Id),
Builders<HostSysUserEntity>.Filter.Eq(x => x.Domain, user?.Domain),
Builders<HostSysUserEntity>.Filter.Eq(x => x.Name, user?.Name)
});
var userUpdate = Builders<HostUserEntity>.Update
var userUpdate = Builders<HostSysUserEntity>.Update
.SetOnInsert(p => p.Insert, date)
.SetOnInsert(p => p.Host, hostEntity.Id)
.SetOnInsert(p => p.Domain, user?.Domain)
@ -70,16 +71,16 @@ public class UserHandler : IMessageHandler<AgentSession>
.Set(p => p.PasswordExpires, user?.PasswordExpires)
.Set(p => p.PasswordRequired, user?.PasswordRequired);
userBulk.Add(new UpdateOneModel<HostUserEntity>(userFilter, userUpdate)
userBulk.Add(new UpdateOneModel<HostSysUserEntity>(userFilter, userUpdate)
{
IsUpsert = true
});
}
userBulk.Add(new DeleteManyModel<HostUserEntity>(Builders<HostUserEntity>.Filter.And(new List<FilterDefinition<HostUserEntity>>
userBulk.Add(new DeleteManyModel<HostSysUserEntity>(Builders<HostSysUserEntity>.Filter.And(new List<FilterDefinition<HostSysUserEntity>>
{
Builders<HostUserEntity>.Filter.Eq(x => x.Host, hostEntity.Id),
Builders<HostUserEntity>.Filter.Ne(x => x.Batch, batch)
Builders<HostSysUserEntity>.Filter.Eq(x => x.Host, hostEntity.Id),
Builders<HostSysUserEntity>.Filter.Ne(x => x.Batch, batch)
})));
var userResult = await _database.HostSystemUser().BulkWriteAsync(userBulk, cancellationToken: cancellationToken);
@ -89,7 +90,7 @@ public class UserHandler : IMessageHandler<AgentSession>
if (users is not null && users.Any())
{
var groupBulk = new List<WriteModel<HostGroupEntity>>();
var groupBulk = new List<WriteModel<HostSysGroupEntity>>();
var distinctGroups = users.SelectMany(p => p.Groups)
.GroupBy(p => new { p?.Domain, p?.Name })
@ -97,14 +98,14 @@ public class UserHandler : IMessageHandler<AgentSession>
foreach (var group in distinctGroups)
{
var groupFilter = Builders<HostGroupEntity>.Filter.And(new List<FilterDefinition<HostGroupEntity>>
var groupFilter = Builders<HostSysGroupEntity>.Filter.And(new List<FilterDefinition<HostSysGroupEntity>>
{
Builders<HostGroupEntity>.Filter.Eq(x => x.Host, hostEntity.Id),
Builders<HostGroupEntity>.Filter.Eq(x => x.Domain, group?.Domain),
Builders<HostGroupEntity>.Filter.Eq(x => x.Name, group?.Name)
Builders<HostSysGroupEntity>.Filter.Eq(x => x.Host, hostEntity.Id),
Builders<HostSysGroupEntity>.Filter.Eq(x => x.Domain, group?.Domain),
Builders<HostSysGroupEntity>.Filter.Eq(x => x.Name, group?.Name)
});
var groupUpdate = Builders<HostGroupEntity>.Update
var groupUpdate = Builders<HostSysGroupEntity>.Update
.SetOnInsert(p => p.Insert, date)
.SetOnInsert(p => p.Host, hostEntity.Id)
.SetOnInsert(p => p.Domain, group?.Domain)
@ -116,16 +117,16 @@ public class UserHandler : IMessageHandler<AgentSession>
.Set(p => p.Description, group?.Description)
.Set(p => p.LocalAccount, group?.LocalAccount);
groupBulk.Add(new UpdateOneModel<HostGroupEntity>(groupFilter, groupUpdate)
groupBulk.Add(new UpdateOneModel<HostSysGroupEntity>(groupFilter, groupUpdate)
{
IsUpsert = true
});
}
groupBulk.Add(new DeleteManyModel<HostGroupEntity>(Builders<HostGroupEntity>.Filter.And(new List<FilterDefinition<HostGroupEntity>>
groupBulk.Add(new DeleteManyModel<HostSysGroupEntity>(Builders<HostSysGroupEntity>.Filter.And(new List<FilterDefinition<HostSysGroupEntity>>
{
Builders<HostGroupEntity>.Filter.Eq(x => x.Host, hostEntity.Id),
Builders<HostGroupEntity>.Filter.Ne(x => x.Batch, batch)
Builders<HostSysGroupEntity>.Filter.Eq(x => x.Host, hostEntity.Id),
Builders<HostSysGroupEntity>.Filter.Ne(x => x.Batch, batch)
})));
var groupResult = await _database.HostSystemGroup().BulkWriteAsync(groupBulk, cancellationToken: cancellationToken);
@ -135,7 +136,7 @@ public class UserHandler : IMessageHandler<AgentSession>
if (users is not null && users.Any())
{
var relationBulk = new List<WriteModel<HostUserGroupEntity>>();
var relationBulk = new List<WriteModel<HostSysUserSysGroupEntity>>();
foreach (var user in users)
{
@ -153,14 +154,14 @@ public class UserHandler : IMessageHandler<AgentSession>
.Project(p => p.Id)
.FirstOrDefaultAsync();
var relationFilter = Builders<HostUserGroupEntity>.Filter.And(new List<FilterDefinition<HostUserGroupEntity>>
var relationFilter = Builders<HostSysUserSysGroupEntity>.Filter.And(new List<FilterDefinition<HostSysUserSysGroupEntity>>
{
Builders<HostUserGroupEntity>.Filter.Eq(x => x.Host, hostEntity.Id),
Builders<HostUserGroupEntity>.Filter.Eq(x => x.User, userId),
Builders<HostUserGroupEntity>.Filter.Eq(x => x.Group, groupId)
Builders<HostSysUserSysGroupEntity>.Filter.Eq(x => x.Host, hostEntity.Id),
Builders<HostSysUserSysGroupEntity>.Filter.Eq(x => x.User, userId),
Builders<HostSysUserSysGroupEntity>.Filter.Eq(x => x.Group, groupId)
});
var relationUpdate = Builders<HostUserGroupEntity>.Update
var relationUpdate = Builders<HostSysUserSysGroupEntity>.Update
.SetOnInsert(p => p.Insert, date)
.SetOnInsert(p => p.Host, hostEntity.Id)
.SetOnInsert(p => p.User, userId)
@ -168,7 +169,7 @@ public class UserHandler : IMessageHandler<AgentSession>
.Set(p => p.Update, date)
.Set(p => p.Batch, batch);
relationBulk.Add(new UpdateOneModel<HostUserGroupEntity>(relationFilter, relationUpdate)
relationBulk.Add(new UpdateOneModel<HostSysUserSysGroupEntity>(relationFilter, relationUpdate)
{
IsUpsert = true
});
@ -176,10 +177,10 @@ public class UserHandler : IMessageHandler<AgentSession>
}
}
relationBulk.Add(new DeleteManyModel<HostUserGroupEntity>(Builders<HostUserGroupEntity>.Filter.And(new List<FilterDefinition<HostUserGroupEntity>>
relationBulk.Add(new DeleteManyModel<HostSysUserSysGroupEntity>(Builders<HostSysUserSysGroupEntity>.Filter.And(new List<FilterDefinition<HostSysUserSysGroupEntity>>
{
Builders<HostUserGroupEntity>.Filter.Eq(x => x.Host, hostEntity.Id),
Builders<HostUserGroupEntity>.Filter.Ne(x => x.Batch, batch)
Builders<HostSysUserSysGroupEntity>.Filter.Eq(x => x.Host, hostEntity.Id),
Builders<HostSysUserSysGroupEntity>.Filter.Ne(x => x.Batch, batch)
})));
var relationResult = await _database.HostSystemUserSystemGroup().BulkWriteAsync(relationBulk, cancellationToken: cancellationToken);

View file

@ -1,12 +1,11 @@
using Insight.Domain.Interfaces;
using Insight.Domain.Messages;
using Insight.Domain.Messages.Agent;
using Insight.Infrastructure;
using Insight.Domain.Network;
using Insight.Domain.Network.Agent.Messages;
using Insight.Infrastructure.Entities;
using MongoDB.Bson;
using MongoDB.Driver;
namespace Insight.Server.Network.Handlers.Agent;
namespace Insight.Server.Network.Agent.Handlers;
public class VideocardHandler : IMessageHandler<AgentSession>
{
@ -19,9 +18,11 @@ public class VideocardHandler : IMessageHandler<AgentSession>
public async ValueTask HandleAsync<TMessage>(AgentSession sender, TMessage message, CancellationToken cancellationToken) where TMessage : IMessage
{
if (message is VideocardList videocards)
switch (message)
{
await OnVideocardsAsync(sender, videocards, cancellationToken);
case Collection<Videocard> videocards:
await OnVideocardsAsync(sender, videocards, cancellationToken);
break;
}
}

View file

@ -1,12 +1,11 @@
using Insight.Domain.Interfaces;
using Insight.Domain.Messages;
using Insight.Domain.Messages.Agent;
using Insight.Infrastructure;
using Insight.Domain.Network;
using Insight.Domain.Network.Agent.Messages;
using Insight.Infrastructure.Entities;
using MongoDB.Bson;
using MongoDB.Driver;
namespace Insight.Server.Network.Handlers.Agent;
namespace Insight.Server.Network.Agent.Handlers;
public class VirtualMaschineHandler : IMessageHandler<AgentSession>
{
@ -19,9 +18,11 @@ public class VirtualMaschineHandler : IMessageHandler<AgentSession>
public async ValueTask HandleAsync<TMessage>(AgentSession sender, TMessage message, CancellationToken cancellationToken) where TMessage : IMessage
{
if (message is VirtualMaschineList virtualMaschines)
switch (message)
{
await OnVirtualMaschinesAsync(sender, virtualMaschines, cancellationToken);
case Collection<VirtualMaschine> virtualMaschines:
await OnVirtualMaschinesAsync(sender, virtualMaschines, cancellationToken);
break;
}
}

View file

@ -1,41 +0,0 @@
using Insight.Domain.Interfaces;
using Insight.Domain.Messages;
using Insight.Domain.Messages.Agent;
using Microsoft.Extensions.Logging;
using MongoDB.Driver;
using Vaitr.Network;
namespace Insight.Server.Network.Handlers.Agent;
public class ConsoleHandler : IMessageHandler<AgentSession>
{
private readonly ISessionPool<WebSession, IMessage> _webPool;
private readonly IMongoDatabase _database;
private readonly ILogger<ConsoleHandler> _logger;
public ConsoleHandler(
ISessionPool<WebSession, IMessage> webPool,
IMongoDatabase database,
ILogger<ConsoleHandler> logger)
{
_webPool = webPool;
_database = database;
_logger = logger;
}
public async ValueTask HandleAsync<TMessage>(AgentSession sender, TMessage message, CancellationToken cancellationToken) where TMessage : IMessage
{
if (message is Proxy<ConsoleQuery> consoleResponse)
{
await OnConsoleQueryAsync(sender, consoleResponse, cancellationToken);
}
}
private async ValueTask OnConsoleQueryAsync(AgentSession session, Proxy<ConsoleQuery> consoleResponse, CancellationToken cancellationToken)
{
// check if web online
if (_webPool.FirstOrDefault().Value is not WebSession web) return;
await web.SendAsync(consoleResponse, cancellationToken);
}
}

View file

@ -0,0 +1,111 @@
using Insight.Domain.Interfaces;
using Insight.Domain.Network;
using Insight.Domain.Network.Remote.Messages;
using Microsoft.Extensions.Logging;
using Vaitr.Bus;
using Vaitr.Network;
namespace Insight.Server.Network.Remote.Handlers;
public class RemoteHandler : IMessageHandler<RemoteSession>
{
private readonly Bus _bus;
private readonly ISessionPool<RemoteSession, IMessage> _remotePool;
private readonly ILogger<RemoteHandler> _logger;
public RemoteHandler(Bus bus, ISessionPool<RemoteSession, IMessage> remotePool, ILogger<RemoteHandler> logger)
{
_bus = bus;
_remotePool = remotePool;
_logger = logger;
}
public async ValueTask HandleAsync<TMessage>(RemoteSession sender, TMessage message, CancellationToken cancellationToken) where TMessage : IMessage
{
if (message is RemoteSessionRequest sessionRequest)
{
await OnSessionRequest(sender, sessionRequest, cancellationToken);
}
else if (message is CastRequestResponse castRequestResponse)
{
await OnCastRequestResponse(sender, castRequestResponse, cancellationToken);
}
else if (message is CastMetric metricData)
{
await OnMetricData(sender, metricData, cancellationToken);
}
else if (message is CastScreen screenData)
{
await OnScreenData(sender, screenData, cancellationToken);
}
else if (message is CastCursor cursorData)
{
await OnCursorData(sender, cursorData, cancellationToken);
}
else if (message is CastClipboardReceived clipboardData)
{
await OnClipboardData(sender, clipboardData, cancellationToken);
}
else if (message is CastAudio audioData)
{
await OnAudioData(sender, audioData, cancellationToken);
}
}
private async Task OnSessionRequest(RemoteSession session, RemoteSessionRequest sessionRequest, CancellationToken cancellationToken)
{
_logger.LogInformation($"Remote {session.Id} => SessionRequest");
session.Mode = sessionRequest.Mode;
await session.SendAsync(new RemoteSessionResponse
{
SessionId = session.Id
}, cancellationToken);
}
private async Task OnCastRequestResponse(RemoteSession session, CastRequestResponse castRequestResponse, CancellationToken cancellationToken)
{
await _bus.PublishAsync(castRequestResponse, cancellationToken);
}
private async Task OnMetricData(RemoteSession session, CastMetric streamMetrics, CancellationToken cancellationToken)
{
//_logger.LogInformation($"Remote {session.Id} => MetricData");
await _bus.PublishAsync(streamMetrics, cancellationToken);
}
private async Task OnScreenData(RemoteSession session, CastScreen screenData, CancellationToken cancellationToken)
{
//_logger.LogInformation($"Remote {session.Id} => ScreenData");
await _bus.PublishAsync(screenData, cancellationToken);
await session.SendAsync(new CastScreenReceived
{
Timestamp = screenData.Timestamp
}, cancellationToken);
}
private async Task OnCursorData(RemoteSession session, CastCursor cursorChanged, CancellationToken cancellationToken)
{
//_logger.LogInformation($"Remote {session.Id} => CursorData");
await _bus.PublishAsync(cursorChanged, cancellationToken);
}
private async Task OnClipboardData(RemoteSession session, CastClipboardReceived clipboardChanged, CancellationToken cancellationToken)
{
_logger.LogInformation($"Remote {session.Id} => ClipboardData");
await _bus.PublishAsync(clipboardChanged, cancellationToken);
}
private async Task OnAudioData(RemoteSession session, CastAudio audioSample, CancellationToken cancellationToken)
{
_logger.LogInformation($"Remote {session.Id} => AudioData");
await _bus.PublishAsync(audioSample, cancellationToken);
}
}

View file

@ -0,0 +1,92 @@
using Insight.Domain.Enums;
using Insight.Domain.Interfaces;
using Insight.Domain.Network;
using Insight.Server.Network.Web;
using Microsoft.Extensions.Logging;
using Vaitr.Bus;
using Vaitr.Network;
namespace Insight.Server.Network.Remote;
public class RemoteSession : TcpSession<IMessage>
{
public string Id { get; }
public RemoteControlMode Mode { get; set; }
private readonly Bus _bus;
private readonly ISessionPool<WebSession, IMessage> _webPool;
private readonly IEnumerable<IMessageHandler<RemoteSession>> _handlers;
public RemoteSession(
ISessionPool<WebSession, IMessage> webPool,
IEnumerable<IMessageHandler<RemoteSession>> handlers,
ISerializer<IMessage> serializer,
ILogger<RemoteSession> logger) : base(serializer, logger)
{
Id = GenerateRandomId();
_webPool = webPool;
_handlers = handlers;
}
public async ValueTask ProxyAsync<TMessage>(TMessage message, CancellationToken cancellationToken)
where TMessage : IMessage
{
// check if web online
if (_webPool.FirstOrDefault().Value is not WebSession web) return;
// proxy-send request packet to web
await web.SendAsync(new Proxy<TMessage>
{
Message = message
}, cancellationToken);
}
protected override async ValueTask OnConnectedAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Remote ({ep?}) connected", RemoteEndPoint);
if (_webPool.FirstOrDefault().Value is not WebSession webSession) return;
}
protected override async ValueTask OnDisconnectedAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Remote ({ep?}) disconnected", RemoteEndPoint);
}
protected override async ValueTask OnSentAsync(IPacketContext<IMessage> context, CancellationToken cancellationToken)
{
await base.OnSentAsync(context, cancellationToken);
}
protected override async ValueTask OnReceivedAsync(IPacketContext<IMessage> context, CancellationToken cancellationToken)
{
await base.OnReceivedAsync(context, cancellationToken);
foreach (var handler in _handlers)
{
try
{
await handler.HandleAsync(this, context.Packet, cancellationToken);
}
catch (Exception ex)
{
_logger.LogWarning("Remote ({ep?}) {ex}", RemoteEndPoint, ex.ToString());
}
}
}
protected override async ValueTask OnHeartbeatAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Remote ({ep?}) Heartbeat", RemoteEndPoint);
}
private static string GenerateRandomId()
{
var random = new Random();
string? sessionId = string.Empty;
for (var i = 0; i < 3; i++) sessionId += random.Next(0, 999).ToString().PadLeft(3, '0');
return sessionId;
}
}

View file

@ -1,55 +1,61 @@
using Insight.Domain.Interfaces;
using Insight.Domain.Messages;
using Insight.Domain.Messages.Agent;
using Insight.Infrastructure;
using Insight.Domain.Network;
using Insight.Domain.Network.Agent.Messages;
using Insight.Infrastructure.Entities;
using Insight.Server.Network.Agent;
using Insight.Server.Network.Web;
using Microsoft.Extensions.Logging;
using MongoDB.Driver;
using Vaitr.Bus;
using Vaitr.Network;
namespace Insight.Server.Network.Handlers.Web;
namespace Insight.Server.Network.Globals;
public class ConsoleProxyHandler : IMessageHandler<WebSession>
public class ProxyHandler : IMessageHandler<AgentSession>, IMessageHandler<WebSession>
{
private readonly List<IDisposable> _subscriptions = new();
private readonly ISessionPool<AgentSession, IMessage> _agentPool;
private readonly ISessionPool<WebSession, IMessage> _webPool;
private readonly IMongoDatabase _database;
private readonly Bus _bus;
private readonly ILogger<ConsoleProxyHandler> _logger;
private readonly ILogger<ProxyHandler> _logger;
public ConsoleProxyHandler(
public ProxyHandler(
ISessionPool<AgentSession, IMessage> agentPool,
ISessionPool<WebSession, IMessage> webPool,
IMongoDatabase database,
Bus bus,
ILogger<ConsoleProxyHandler> logger)
ILogger<ProxyHandler> logger)
{
_agentPool = agentPool;
_webPool = webPool;
_database = database;
_bus = bus;
_logger = logger;
}
public async ValueTask HandleAsync<TMessage>(WebSession sender, TMessage message, CancellationToken cancellationToken) where TMessage : IMessage
{
if (message is Proxy<ConsoleQueryRequest> consoleRequest)
switch (message)
{
_logger.LogCritical("received: {0}" + consoleRequest.Message);
await OnConsoleQueryRequestAsync(sender, consoleRequest, cancellationToken);
case Proxy<Request> proxyRequest:
await OnProxyRequestAsync(sender, proxyRequest, cancellationToken);
break;
}
}
private async ValueTask OnConsoleQueryRequestAsync(WebSession session, Proxy<ConsoleQueryRequest> request, CancellationToken cancellationToken)
public async ValueTask HandleAsync<TMessage>(AgentSession sender, TMessage message, CancellationToken cancellationToken) where TMessage : IMessage
{
switch (message)
{
case Proxy<Response> proxyResponse:
await OnProxyResponseAsync(sender, proxyResponse, cancellationToken);
break;
}
}
private async ValueTask OnProxyRequestAsync(WebSession session, Proxy<Request> request, CancellationToken cancellationToken)
{
// get host
var hostEntity = await _database.Host()
.Find(Builders<HostEntity>
.Filter
.Eq(p => p.Id, request.HostId))
.Eq(p => p.Id, request.ProxyId))
.FirstOrDefaultAsync(cancellationToken);
if (hostEntity is null)
@ -77,4 +83,12 @@ public class ConsoleProxyHandler : IMessageHandler<WebSession>
// proxy-send request packet to agent
await agent.SendAsync(request, cancellationToken);
}
private async ValueTask OnProxyResponseAsync(AgentSession session, Proxy<Response> response, CancellationToken cancellationToken)
{
// check if web online
if (_webPool.FirstOrDefault().Value is not WebSession web) return;
await web.SendAsync(response, cancellationToken);
}
}

View file

@ -1,9 +1,9 @@
using Insight.Domain.Interfaces;
using Insight.Domain.Messages;
using Insight.Domain.Network;
using Microsoft.Extensions.Logging;
using Vaitr.Network;
namespace Insight.Server.Network;
namespace Insight.Server.Network.Web;
public class WebSession : TcpSession<IMessage>
{