clsSSL.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.Net;
  3. using System.Security.Cryptography.X509Certificates;
  4. using System.Text.RegularExpressions;
  5. using DSharpPlus.EventArgs;
  6. namespace TFABot.DiscordBot.Commands
  7. {
  8. public class clsSSL : IBotCommand
  9. {
  10. public String[] MatchCommand {get; private set;}
  11. public String[] MatchSubstring {get; private set;}
  12. public Regex[] MatchRegex {get; private set;}
  13. public clsSSL()
  14. {
  15. MatchCommand = new []{"ssl"};
  16. }
  17. void IBotCommand.Run(MessageCreateEventArgs e)
  18. {
  19. try
  20. {
  21. if (e.Message.Content == "ssl")
  22. {
  23. var cd = new clsColumnDisplay();
  24. cd.ColumnChar=' ';
  25. cd.Append("```");
  26. cd.AppendLine($"SSL Certificates checked {clsSSLCertMonitor.LastTest.Value:yyyy-MM-dd HH:mm}");
  27. cd.AppendCol("URL");
  28. cd.AppendCol("Days");
  29. cd.AppendCharLine('-');
  30. foreach (var ssl in Program.SSLCertsList.Values)
  31. {
  32. cd.AppendCol(ssl.URL);
  33. cd.AppendCol((ssl.CertExpiry - DateTime.UtcNow).Days.ToString());
  34. cd.NewLine();
  35. }
  36. cd.Append("```");
  37. e.Channel.SendMessageAsync(cd.ToString());
  38. }
  39. else if (e.Message.Content.Length>4)
  40. {
  41. var url = e.Message.Content.Substring(4).Trim();
  42. if (!url.StartsWith("https://")) url = $"https://{url}";
  43. HttpWebRequest request;
  44. request = (HttpWebRequest)WebRequest.Create(url);
  45. try
  46. {
  47. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  48. response.Close();
  49. }
  50. catch { }
  51. var cert = request.ServicePoint.Certificate;
  52. var cert2 = new X509Certificate2(cert);
  53. var days = (DateTime.Parse(cert2.GetExpirationDateString()) - DateTime.UtcNow).Days;
  54. e.Channel.SendMessageAsync($"{days} days");
  55. }
  56. }
  57. catch (Exception ex)
  58. {
  59. e.Channel.SendMessageAsync(ex.Message);
  60. }
  61. }
  62. public void HelpString (ref clsColumnDisplay columnDisplay)
  63. {
  64. columnDisplay.AppendCol("ssl","<https url>");
  65. }
  66. }
  67. }