diff --git a/WebSocketTool/Client/ClientViewModel.cs b/WebSocketTool/Client/ClientViewModel.cs index f5896fb..2f8e61b 100644 --- a/WebSocketTool/Client/ClientViewModel.cs +++ b/WebSocketTool/Client/ClientViewModel.cs @@ -1,5 +1,6 @@ using System; using System.Net.Security; +using System.Security.Cryptography.X509Certificates; using System.Timers; using log4net; using WebSocketSharp; @@ -222,7 +223,7 @@ namespace WebSocketTool.Client public void StopPing() { - view.AppendInfo($"You {TimeUtil.GetCurrentDateTime()} \n StopPing"); + App.RunOnUIThread(() => view.AppendInfo($"You {TimeUtil.GetCurrentDateTime()} \n StopPing")); if (pingTimer != null) { pingTimer.Stop(); @@ -234,23 +235,10 @@ namespace WebSocketTool.Client { view.AppendInfo($"Hint {TimeUtil.GetCurrentDateTime()} \n InitSocketClient"); mClient = new SocketClient(WsUrl); - mClient.SetServerCertificateValidationCallback((sender, certificate, chain, errors) => + if (WsUrl.StartsWith("wss:")) { - App.RunOnUIThread(() => { view.AppendInfo($"ServerCertificateError:{errors}"); }); - Log.Info($"error:{errors}"); - Log.Info($"Issuer: {certificate.Issuer},Subject:{certificate.Subject}"); - Log.Info("ChainStatus:"); - foreach (var status in chain.ChainStatus) - { - Log.Info($"{status.StatusInformation}: {status}"); - } - Log.Info("ChainElements:"); - foreach (var element in chain.ChainElements) - { - Log.Info($"element: {element.Certificate},{element.Information}"); - } - return errors == SslPolicyErrors.None; - }); + mClient.SetServerCertificateValidationCallback(CertificateValidationCallback); + } mClient.OpenEvent += ClientOnOpenEvent; mClient.CloseEvent += ClientOnCloseEvent; mClient.ErrorEvent += ClientOnErrorEvent; @@ -262,6 +250,26 @@ namespace WebSocketTool.Client } + private bool CertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) + { + App.RunOnUIThread(() => { view.AppendInfo($"ServerCertificateError:{errors}"); }); + Log.Info($"error:{errors}"); + Log.Info($"Issuer: {certificate.Issuer},Subject:{certificate.Subject}"); + Log.Info("ChainStatus:"); + foreach (var status in chain.ChainStatus) + { + Log.Info($"{status.StatusInformation}: {status}"); + } + + Log.Info("ChainElements:"); + foreach (var element in chain.ChainElements) + { + Log.Info($"element: {element.Certificate},{element.Information}"); + } + + return errors == SslPolicyErrors.None; + } + private void ClientOnMessageEvent(object sender, MessageEventArgs e) { App.RunOnUIThread(() => diff --git a/WebSocketTool/Client/SocketClient.cs b/WebSocketTool/Client/SocketClient.cs index ead20f8..723498e 100644 --- a/WebSocketTool/Client/SocketClient.cs +++ b/WebSocketTool/Client/SocketClient.cs @@ -16,10 +16,12 @@ namespace WebSocketTool.Client public event EventHandler ErrorEvent; public event EventHandler OpenEvent; public event EventHandler CloseEvent; + public string Url { get; private set; } public SocketClient(string url) { Log.Info($"create socket:{url}"); + Url = url; mSocket = new WebSocket(url); if (url.StartsWith("wss")) {