Browse Source

SSL Fixes

Stuart Johnson 1 year ago
parent
commit
406c6ef71a

+ 1 - 1
TFA-Bot/DataClasses/clsSSLCerts.cs

@@ -14,7 +14,7 @@ namespace TFABot
         [ASheetColumnHeader("contacts")]
         public String Contacts {get;set;}
         
-        public DateTime CertExpiry {get;set;}
+        public DateTime? CertExpiry {get;set;}
         
         
         public void Update(clsSSLCerts user)

+ 17 - 9
TFA-Bot/DiscordBot/Commands/clsSSL.cs

@@ -27,24 +27,32 @@ namespace TFABot.DiscordBot.Commands
                 if (e.Message.Content == "ssl")
                 {
                     var cd = new clsColumnDisplay();
-                    cd.ColumnChar=' ';
+                    cd.ColumnChar = ' ';
                     cd.Append("```");
                     cd.AppendLine($"SSL Certificates checked {clsSSLCertMonitor.LastTest.Value:yyyy-MM-dd HH:mm}");
                     cd.AppendCol("URL");
-                    cd.AppendCol("Days");                     
+                    cd.AppendCol("Days");
                     cd.AppendCharLine('-');
 
                     foreach (var ssl in Program.SSLCertsList.Values)
                     {
-                        cd.AppendCol(ssl.URL);                        
-                        cd.AppendCol((ssl.CertExpiry - DateTime.UtcNow).Days.ToString());
+                        cd.AppendCol(ssl.URL);
+                        if (ssl.CertExpiry.HasValue)
+                            cd.AppendCol((ssl.CertExpiry - DateTime.UtcNow).Value.Days.ToString());
+                        else
+                            cd.AppendCol("?");
                         cd.NewLine();
                     }
                     cd.Append("```");
                     e.Channel.SendMessageAsync(cd.ToString());
-                    
+
+                }
+
+                else if (e.Message.Content == "ssl update")
+                {
+                    clsSSLCertMonitor.HeartbeatCheck(true);
                 }
-                else if (e.Message.Content.Length>4)
+                else if (e.Message.Content.Length > 4)
                 {
                     var url = e.Message.Content.Substring(4).Trim();
                     if (!url.StartsWith("https://")) url = $"https://{url}";
@@ -57,13 +65,13 @@ namespace TFABot.DiscordBot.Commands
                         response.Close();
                     }
                     catch { }
-                    
-                    
+
+
                     var cert = request.ServicePoint.Certificate;
                     var cert2 = new X509Certificate2(cert);
                     var days = (DateTime.Parse(cert2.GetExpirationDateString()) - DateTime.UtcNow).Days;
                     e.Channel.SendMessageAsync($"{days} days");
-                    
+
                 }
             }
             catch (Exception ex)

+ 9 - 7
TFA-Bot/clsSSLCertMonitor.cs

@@ -18,9 +18,9 @@ namespace TFABot
             
         }
 
-        internal static void HeartbeatCheck()
+        internal static void HeartbeatCheck(bool force = false)
         {
-            if (!LastTest.HasValue || (DateTime.UtcNow - LastTest.Value).Hours >= 24)
+            if (force || !LastTest.HasValue || (DateTime.UtcNow - LastTest.Value).Hours >= 24)
             {
                 Console.WriteLine("clsSSLCertMonitor: HeartbeatCheck");
                 LastTest = DateTime.UtcNow;
@@ -29,13 +29,13 @@ namespace TFABot
                 {
                     foreach (var ssl in Program.SSLCertsList.Values)
                     {
-                        CheckSSL(ssl);
+                        CheckSSL(ssl, force || DateTime.UtcNow.Hour==0);
                     }
                 });
             }
         }
 
-        private static async void CheckSSL(clsSSLCerts ssl)
+        private static async void CheckSSL(clsSSLCerts ssl, bool report)
         {       
             try
             {
@@ -59,14 +59,16 @@ namespace TFABot
                 var cert2 = new X509Certificate2(cert);
                 
                 ssl.CertExpiry = DateTime.Parse(cert2.GetExpirationDateString());
-                var days = (ssl.CertExpiry - DateTime.UtcNow).Days;
+                var days = (ssl.CertExpiry - DateTime.UtcNow).Value.Days;
 
-                if (days <= 7) SendMessage(ssl, $"{ssl.URL} certificate expires in {days} days");
+                if (report && days <= 7) SendMessage(ssl, $"{ssl.URL} certificate expires in {days} days");
                 
             }
             catch (Exception ex)
             {
-                SendMessage(ssl, $"{ssl.URL} certificate read error");
+                var errorMsg = $"{ssl.URL} certificate read error: {ex.Message}";
+                if (report) SendMessage(ssl, errorMsg);
+                Console.WriteLine(errorMsg);
             }
         }