using RyzStudio.IO; using RyzStudio.Windows.Forms; using System; using System.ComponentModel; using System.Diagnostics; using System.Diagnostics.Metrics; using System.Net.Http; using System.Net.Security; using System.Reflection.Emit; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace WebsiteSSLCertificateChecker { public partial class MainForm : Form { protected const string DEFAULLT_LABEL_RESULTS = "SSL Certificate Results"; protected CancellationTokenSource cancellationToken = new CancellationTokenSource(); protected bool isBusy = false; protected bool requestCancel = false; public MainForm() { InitializeComponent(); //memoBox1.TextBox.BackColor = memoBox1.BackColor; memoBox2.ReadOnly = false; Clear(); } protected override void OnClosing(CancelEventArgs e) { base.OnClosing(e); if (this.IsBusy) { e.Cancel = true; return; } } public bool IsBusy { get => isBusy; set { isBusy = value; UIControl.SetEnable(memoBox2, !isBusy); UIControl.SetEnable(memoBox1, !isBusy); UIControl.SetValue(pictureBox1, (isBusy ? UIcon.GetIcon(typeof(UIResource), "loading_block") : null)); UIControl.SetEnable(button5, !isBusy); UIControl.SetEnable(button1, !isBusy); } } /// /// New /// /// /// private void newToolStripMenuItem_Click(object sender, EventArgs e) { if (this.IsBusy) { return; } Clear(); } /// /// Close /// /// /// private void exitToolStripMenuItem2_Click(object sender, EventArgs e) { if (this.IsBusy) { return; } this.Close(); } /// /// Options /// /// /// private void optionsToolStripMenuItem_Click(object sender, EventArgs e) { // do nothing } /// /// Help /// /// /// private void viewHelpToolStripMenuItem1_Click(object sender, EventArgs e) { try { System.Diagnostics.Process.Start(new ProcessStartInfo() { FileName = AppResource.AppHelpURL, UseShellExecute = true }); } catch { // do nothing } } /// /// About /// /// /// private void aboutToolStripMenuItem1_Click(object sender, EventArgs e) { MessageBox.Show(Application.ProductName + " v" + Application.ProductVersion, "About", MessageBoxButtons.OK, MessageBoxIcon.Information); } /// /// Clear URL address list /// /// /// private void memoBox2_OnButtonClick(object sender, EventArgs e) { if (this.IsBusy) { return; } UIControl.SetText(memoBox2, string.Empty); } /// /// Clear SSL certificate results /// /// /// private void memoBox1_OnButtonClick(object sender, EventArgs e) { if (this.IsBusy) { return; } UIControl.SetText(label1, DEFAULLT_LABEL_RESULTS); UIControl.SetText(memoBox1, string.Empty); } /// /// Run /// /// /// private async void button5_MouseClick(object sender, MouseEventArgs e) { if (this.IsBusy) { return; } this.IsBusy = true; int counter = 0; foreach (string line in memoBox2.Lines) { if (string.IsNullOrWhiteSpace(line)) { continue; } counter++; UIControl.SetText(label1, string.Format("{0} ({1})", DEFAULLT_LABEL_RESULTS, counter.ToString())); await RetrieveSSLInfo(line); } this.IsBusy = false; } /// /// Close /// /// /// private void button1_MouseClick(object sender, MouseEventArgs e) { if (this.IsBusy) { return; } this.Close(); } protected void AddText(string text) { UIControl.AddLine(memoBox1, text); } private void Clear() { label1.Text = DEFAULLT_LABEL_RESULTS; memoBox1.Text = memoBox2.Text = string.Empty; memoBox1.WordWrap = memoBox2.WordWrap = false; } protected string ReFormatDateTime(string datetime) { if (string.IsNullOrWhiteSpace(datetime)) { return string.Empty; } if (!DateTime.TryParse(datetime, out DateTime result)) { return string.Empty; } return result.ToString("yyyy-MM-dd HH:mm:ss"); } private async Task RetrieveSSLInfo(string url) { AddText("> " + url); HttpClientHandler clientHandler = new HttpClientHandler(); clientHandler.ServerCertificateCustomValidationCallback = (requestMessage, certificate, chain, sslErrors) => { AddText("Requested URL = " + requestMessage.RequestUri); AddText("Subject = " + certificate?.Subject ?? string.Empty); AddText("Issuer = " + certificate?.Issuer ?? string.Empty); AddText("Effective Date = " + ReFormatDateTime(certificate!.GetEffectiveDateString())); AddText("Expiration Date = " + ReFormatDateTime(certificate.GetExpirationDateString())); AddText("Thumbprint = " + certificate.Thumbprint); AddText("Error = " + sslErrors); AddText(String.Empty); return sslErrors == SslPolicyErrors.None; }; HttpClient webClient = new HttpClient(clientHandler); try { HttpResponseMessage response = await webClient.SendAsync(new HttpRequestMessage(HttpMethod.Head, url)); response.EnsureSuccessStatusCode(); } catch (HttpRequestException exc) { AddText("Error = " + exc.Message); } catch (Exception exc) { AddText("Error = " + exc.Message); } AddText(String.Empty.PadLeft(84, '-')); clientHandler.Dispose(); webClient.Dispose(); } } }