void Init() { if (Platform.IsMicrosoft) { HasAdminRights = new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator); } IsWindowsService = Platform.IsMicrosoft && !Environment.UserInteractive; if (IsWindowsService) { // no log console + service run if (!HasAdminRights) { throw new NotSupportedException("Service requires administration rights!"); } LogSystem = new LogEventLog(EventLog, ServiceName); } else { CommandlineArguments = Arguments.FromEnvironment(); // commandline run or linux daemon ? if (!CommandlineArguments.IsOptionPresent("daemon")) { // no daemon -> log console LogConsole = LogConsole.Create(); LogConsole.Title = ServiceName + " v" + VersionInfo.InformalVersion; if (CommandlineArguments.IsOptionPresent("debug")) { LogConsole.Level = LogLevel.Debug; } if (CommandlineArguments.IsOptionPresent("verbose")) { LogConsole.Level = LogLevel.Verbose; } if (LogConsole.Level < LogLevel.Information) { LogConsole.ExceptionMode = LogExceptionMode.Full; } } // on unix do syslog LogSystem = LogConsole; if (Platform.Type == PlatformType.Linux) { LogSystem = LogSyslog.Create(); } } if (LogSystem != null) { LogSystem.ExceptionMode = LogExceptionMode.Full; } log.LogInfo("Service <cyan>{0}<default> initialized!", ServiceName); }
void Run(Arguments args) { ILogReceiver console = null; if (args.IsOptionPresent("debug")) { if (console == null) { console = LogConsole.Create(); } console.Level = LogLevel.Debug; } if (args.IsOptionPresent("verbose")) { if (console == null) { console = LogConsole.Create(); } console.Level = LogLevel.Verbose; } LoadConfig(); List <XT> results = new List <XT>(); foreach (string dir in Directory.GetDirectories(Path.Combine(LetsEncryptPath, "live"))) { string domainName = Path.GetFileName(dir); var conf = Path.Combine(LetsEncryptPath, "renewal", domainName + ".conf"); if (!File.Exists(conf)) { continue; } if (domainName.Split('.').Length != 2) { continue; } if (!domains.TryGetStruct(nameof(Domain.Name), domainName, out Domain domain)) { var x = XT.Format("<red>Error: <default>Domain <red>{0}<default> removed from imscp!", domainName); results.Add(x); SystemConsole.WriteLine(x); File.Delete(conf); continue; } string certText = File.ReadAllText(Path.Combine(dir, "cert.pem")).GetValidChars(ASCII.Strings.Printable + '\n') + "\n"; string keyText = File.ReadAllText(Path.Combine(dir, "privkey.pem")).GetValidChars(ASCII.Strings.Printable + '\n') + "\n"; string chainText = File.ReadAllText(Path.Combine(dir, "chain.pem")).GetValidChars(ASCII.Strings.Printable + '\n') + "\n"; var sslCerts = ssl_certs.GetStructs(nameof(SslCerts.DomainID), domain.ID); if (sslCerts.Count > 1) { var x = XT.Format("<red>Error: <default>Multiple ssl certs for domain {0}!", domain); results.Add(x); SystemConsole.WriteLine(x); continue; } var newCert = PEM.ReadCert(certText.SplitNewLine()); if (sslCerts.Count == 1) { //already got one, check for update var sslCert = sslCerts[0]; var oldCert = PEM.ReadCert(sslCert.Certificate.SplitNewLine()); if (newCert.Equals(oldCert)) { this.LogInfo("Domain <green>{0}<default> valid till <green>{1}", domainName, oldCert.GetExpirationDateString()); //no change continue; } if (!oldCert.Issuer.Contains("O=Let's Encrypt")) { //do not override users own certs continue; } sslCert.Certificate = certText; sslCert.PrivateKey = keyText; sslCert.CaBundle = chainText; sslCert.Status = "tochange"; ssl_certs.Update(sslCert); } else { var sslCert = new SslCerts() { AllowHsts = "off", CaBundle = chainText, Certificate = certText, DomainID = (int)domain.ID, DomainType = "dmn", HstsIncludeSubdomains = "off", HstsMaxAge = 31536000, PrivateKey = keyText, Status = "tochange", }; ssl_certs.Insert(sslCert); } domain.Status = "tochange"; domains.Update(domain); { var x = XT.Format("<green>Certificate: <default>Domain {0} new certificate {1} valid till {2}!", domain, newCert.Subject, newCert.GetExpirationDateString()); results.Add(x); SystemConsole.WriteLine(x); } } this.LogInfo("Completed."); if (results.Count > 0) { MailMessage msg = new MailMessage(mailSender, "*****@*****.**") { Subject = "CaveSystems LetsEncrypt", IsBodyHtml = true, Body = results.ToHtml() }; smtpClient.Send(msg); } Logger.Flush(); Logger.CloseAll(); console?.Dispose(); }
static void Main() { LogConsole = LogConsole.Create(LogConsoleFlags.None); LogConsole.ExceptionMode = LogExceptionMode.Full; ParseArgs(); Header(); if (LocalEndPoints.Count > 0) { if (!Quiet) { Log.LogInfo(string.Format("Listening at <cyan>{0}", LocalEndPoints.Keys.Join("<default>, <cyan>"))); } bool useConsole = true; try { while (SystemConsole.KeyAvailable) { SystemConsole.ReadKey(); } } catch { useConsole = false; } DateTime lastEscapePress = DateTime.MinValue; List <Task> listenTasks = new List <Task>(); foreach (TcpListener listener in LocalEndPoints.Values) { Task task = Task.Factory.StartNew(() => { while (!Exit) { TcpClient l_Client = listener.AcceptTcpClient(); LogLevel l_Level = ShowConnects ? LogLevel.Information : LogLevel.Verbose; Log.Write(l_Level, string.Format("Connect from <cyan>{0}", l_Client.Client.RemoteEndPoint)); Task.Factory.StartNew(() => Forward(l_Client)); } }, TaskCreationOptions.LongRunning); listenTasks.Add(task); } if (useConsole) { while (!Exit) { if (SystemConsole.KeyAvailable) { switch (SystemConsole.ReadKey().Key) { case ConsoleKey.Escape: if (lastEscapePress.AddSeconds(1) <= DateTime.Now) { Exit = true; break; } lastEscapePress = DateTime.Now; Log.LogInfo("Press escape again within 1s to exit."); break; } } Thread.Sleep(100); } } else { while (!Exit) { Thread.Sleep(1000); } } } Logger.Flush(); LogConsole.Close(); }