using Microsoft.AspNetCore.Server.Kestrel.Https; using ServiceInside.Controllers; using ServiceInside.Filter; using ServiceInside.Service; using ServiceShared; using ServiceShared.Crypto; using ServiceShared.Database; using System.Diagnostics; using System.Reflection; using System.Security.Cryptography.X509Certificates; WebApplicationBuilder builder = WebApplication.CreateBuilder(args); IConfigurationSection logSection = builder.Configuration.GetSection("Log"); if (logSection != null) { string type = logSection.GetValue("Type"); string logPath = logSection.GetValue("path"); string SmtpServer = logSection.GetValue("SmtpServer"); int SmtpPort = logSection.GetValue("SmtpPort"); bool SmtpSSL = logSection.GetValue("SmtpSSL"); string SmtpSenderEmail = logSection.GetValue("SmtpSenderEmail"); string SmtpSenderName = logSection.GetValue("SmtpSenderName"); string SmtpRecipients = logSection.GetValue("SmtpRecipients"); string SmtpUsername = logSection.GetValue("SmtpUsername"); string SmtpPassword = logSection.GetValue("SmtpPassword"); /** check if smtp username string was stored as encrypted string in config **/ if(!string.IsNullOrEmpty(SmtpUsername)) { string decryptedSmtpUsername = AES.Decrypt(SmtpUsername); if(!string.IsNullOrEmpty(decryptedSmtpUsername)) { SmtpUsername = decryptedSmtpUsername; } } /** check if smtp password string was stored as encrypted string in config **/ if (!string.IsNullOrEmpty(SmtpPassword)) { string decryptedSmtpPassword = AES.Decrypt(SmtpPassword); if (!string.IsNullOrEmpty(decryptedSmtpPassword)) { SmtpPassword = decryptedSmtpPassword; } } Log.Types logType = Log.Types.INFO; Enum.TryParse(type, out logType); Log.SetLogPath(logPath); Log.SetLog(logType); Mail.SetSMTPData(SmtpServer, SmtpPort, SmtpSSL, SmtpSenderEmail, SmtpSenderName, SmtpRecipients, null, SmtpUsername, SmtpPassword); int CleanUpDays = builder.Configuration.GetValue("CleanUpDays", 30); string privateDirectory = builder.Configuration.GetSection("PrivateDirectory").Value; string publicDirectory = builder.Configuration.GetSection("PublicDirectory").Value; int maxWorkerThreads = builder.Configuration.GetValue("MaxWorkerThreads", 10); int maxTryNotFoundResults = builder.Configuration.GetValue("MaxTryNotFoundResults", 2); int checkNotFoundResultsIntervalMinutes = builder.Configuration.GetValue("CheckIntervalNotFoundResultsInMinutes", 1440); string serviceOutsideIP = builder.Configuration.GetSection("ServiceOutsideIP").Value; string serviceOutsideURL = builder.Configuration.GetSection("ServiceOutsideURL").Value; string serviceInsideURL = builder.Configuration.GetSection("ServiceInsideURL").Value; string defaultConnection = builder.Configuration.GetConnectionString("default"); if (string.IsNullOrEmpty(serviceOutsideIP)) { Log.Error(new Exception("ServiceOutsideIP was not found in the appsettings.json"), "InsideServie", "Programm"); } else if (string.IsNullOrEmpty(serviceOutsideURL)) { Log.Error(new Exception("ServiceOutsideURL was not found in the appsettings.json"), "InsideServie", "Programm"); } else if (string.IsNullOrEmpty(privateDirectory)) { Log.Error(new Exception("PrivateDirectory was not found in the appsettings.json"), "InsideServie", "Programm"); } else if (string.IsNullOrEmpty(publicDirectory)) { Log.Error(new Exception("PublicDirectory was not found in the appsettings.json"), "InsideServie", "Programm"); } else if (string.IsNullOrEmpty(defaultConnection)) { Log.Error(new Exception("Default ConnectionString was not found in the appsettings.json"), "InsideServie", "Programm"); } else { string connectionString = builder.Configuration.GetConnectionString(defaultConnection); if (string.IsNullOrEmpty(connectionString)) { Log.Error(new Exception("ConnectionString(" + connectionString + ") was not found in the appsettings.json"), "InsideServie", "Programm"); } else { /** check if connection string was stored as encrypted string in config **/ string decryptedConnectionString = AES.Decrypt(connectionString); if(!string.IsNullOrEmpty(decryptedConnectionString)) { connectionString = decryptedConnectionString; } string appVersion = Assembly.GetEntryAssembly().GetName().Version.ToString(); Log.Info("ServiceInside(" + appVersion + ") starting..."); DbContext dbContext = new DbContext(connectionString); DbContext.setApplicationVersion(appVersion); builder.Services.Add(new ServiceDescriptor(typeof(DbContext), dbContext)); builder.Services.Add(new ServiceDescriptor(typeof(KeyPair), Curve25519.GenerateKeyPair())); builder.Services.AddControllers(); WebApplication app = builder.Build(); app.UseHttpsRedirection(); app.UseAuthorization(); app.UseRouting(); app.MapControllers(); Log.SetTraceDbController(new ServiceShared.Database.Controllers.Traces(dbContext)); Log.Info("ServiceInside successfully started"); //Mail.SendLogs("ServiceInside - started", "ServiceInside successfully started"); TrustedHeader.SetLog(logType); ServiceOutside.SetServiceOutsideURL(serviceOutsideURL); ServiceOutsideRequest.SetServiceInsideIP(serviceOutsideIP); BackgroundWorker.SetParameters(privateDirectory, publicDirectory, dbContext, maxWorkerThreads, maxTryNotFoundResults, checkNotFoundResultsIntervalMinutes); BackgroundWorker.Start(); CleanUp.SetOldDays(CleanUpDays, logPath); CleanUp.SetDbContext(new ServiceShared.Database.Controllers.Results(dbContext), new ServiceShared.Database.Controllers.Traces(dbContext)); CleanUp.CleanUpInBackground(); /** send WakeUp request on application stoped **/ app.Lifetime.ApplicationStopped.Register(() => { WakeUp(serviceInsideURL); }); app.Run(); } } } else { Log.Error(new Exception("Log configuration was not found in appsettings.json"), "InsideService", "Programm"); } void WakeUp(string serviceInsideURL) { if (!string.IsNullOrEmpty(serviceInsideURL)) { try { string wakeUpExe = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "WakeUp.exe"); Process wakeUp = new Process(); wakeUp.StartInfo = new ProcessStartInfo(); wakeUp.StartInfo.FileName = wakeUpExe; wakeUp.StartInfo.Arguments = serviceInsideURL; wakeUp.Start(); } catch (Exception ex) { Log.Critical(ex, "Programm", "ApplicationStopped"); } } }