using Insight.Agent.Interfaces; using Insight.Agent.Messages; using Insight.Domain.Constants; using Insight.Infrastructure; using Insight.Server.Extensions; using Insight.Server.Network; using Insight.Server.Network.Handlers.Agent; using Insight.Server.Network.Handlers.Web; using Insight.Server.Services; using Insight.Web.Interfaces; using Insight.Web.Messages; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using System.Net; using Vaitr.Bus; using Vaitr.Network; using Vaitr.Network.Hosting; namespace Insight.Server { internal class Program { public static async Task Main(string[] args) { var builder = Host.CreateDefaultBuilder(args); builder.UseWindowsService(); builder.UseSystemd(); builder.ConfigureAppConfiguration(options => { options.Defaults(); }); builder.ConfigureLogging(options => { options.ClearProviders(); options.SetMinimumLevel(LogLevel.Trace); options.AddSimpleConsole(options => { options.IncludeScopes = true; options.SingleLine = true; options.TimestampFormat = "yyyy-MM-dd HH:mm:ss.fff "; }); options.AddFile($"{Configuration.AppDirectory?.FullName}/" + "logs/server_{Date}.log", LogLevel.Trace, fileSizeLimitBytes: 104857600, retainedFileCountLimit: 10, outputTemplate: "{Timestamp:o} [{Level:u3}] {Message} {NewLine}{Exception}"); }); builder.ConfigureServices((host, services) => { //var databaseLoggerFactory = LoggerFactory.Create(b => //{ // b.AddSimpleConsole(); // b.SetMinimumLevel(LogLevel.Debug); //}); // INFRASTRUCTURE services.AddDatabase(host.Configuration); // AGENT SERVER services.UseHostedServer(options => { options.Address = IPAddress.Any; options.Port = host.Configuration.GetValue(Appsettings.AgentServerPort) ?? throw new Exception($"{Appsettings.AgentServerPort} value not set (appsettings)"); options.Keepalive = 10000; options.Timeout = 30000; options.Backlog = 128; options.Encryption = Encryption.Tls12; options.Certificate = host.Configuration.GetValue(Appsettings.AgentServerCertificate) ?? throw new Exception($"{Appsettings.AgentServerCertificate} value not set (appsettings)"); options.CertificatePassword = host.Configuration.GetValue(Appsettings.AgentServerCertificatePassword) ?? throw new Exception($"{Appsettings.AgentServerCertificatePassword} value not set (appsettings)"); options.UseSerializer, IAgentMessage>(); }); services.AddSingleton(); services.AddSingleton, AgentHandler>(); services.AddSingleton, DriveHandler>(); services.AddSingleton, Network.Handlers.Agent.EventHandler>(); services.AddSingleton, InterfaceHandler>(); services.AddSingleton, MainboardHandler>(); services.AddSingleton, MemoryHandler>(); services.AddSingleton, OperationSystemHandler>(); services.AddSingleton, PrinterHandler>(); services.AddSingleton, ProcessorHandler>(); services.AddSingleton, ServiceHandler>(); services.AddSingleton, SessionHandler>(); services.AddSingleton, SoftwareHandler>(); services.AddSingleton, StoragePoolHandler>(); services.AddSingleton, SystemInfoHandler>(); services.AddSingleton, TrapHandler>(); services.AddSingleton, UpdateHandler>(); services.AddSingleton, UserHandler>(); services.AddSingleton, VideocardHandler>(); services.AddSingleton, VirtualMaschineHandler>(); services.AddSingleton, ConsoleHandler>(); // WEB (FRONTEND-PROXY) SERVER services.UseHostedServer(options => { options.Address = IPAddress.Any; options.Port = host.Configuration.GetValue(Appsettings.WebServerPort) ?? throw new Exception($"{Appsettings.WebServerPort} value not set (appsettings)"); options.Keepalive = 10000; options.Timeout = 30000; options.Backlog = 128; options.Encryption = Encryption.Tls12; options.Certificate = host.Configuration.GetValue(Appsettings.WebServerCertificate) ?? throw new Exception($"{Appsettings.WebServerCertificate} value not set (appsettings)"); options.CertificatePassword = host.Configuration.GetValue(Appsettings.WebServerCertificatePassword) ?? throw new Exception($"{Appsettings.WebServerCertificatePassword} value not set (appsettings)"); options.UseSerializer, IWebMessage>(); }); services.AddSingleton, ConsoleProxyHandler>(); // DISPATCH services.AddHostedService(); services.AddHostedService(); // GLOBAL DEPENDENCIES services.AddSingleton(); services.AddTransient(provider => new HttpClient(new HttpClientHandler { ClientCertificateOptions = ClientCertificateOption.Manual, ServerCertificateCustomValidationCallback = (httpRequestMessage, cert, cetChain, policyErrors) => true })); }); var host = builder.Build(); await host.RunAsync().ConfigureAwait(false); } } }