如何在Mono HTTPListener服务器中捕获SSL异常?

编程入门 行业动态 更新时间:2024-10-28 04:26:32
本文介绍了如何在Mono HTTPListener服务器中捕获SSL异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试将HTTPS支持添加到我的简单Mono HTTP服务器中.它在大多数情况下都能正常运行,但是当发生SSL异常(例如客户端取消握手)时,应用程序将崩溃.显然,这样的事情不会完全崩溃,所以我需要以某种方式捕获该异常.

I'm trying to add HTTPS support to my simple Mono HTTP server. It works perfectly most of the time, but the application crashes when an SSL exception occurs, such as the client canceling the handshake. Obviously something like that shouldn't completely crash it, so I need to catch that exception somehow.

代码:

static void Main(string[] args) { try { var listener = new HttpListener(); string prefix = "*:8443/"; listener.Prefixes.Add(prefix); Console.WriteLine("Starting HTTP server at " + prefix); listener.Start(); while (true) { try { var context = listener.GetContext(); Console.WriteLine(context.Request.Url); } catch (Exception e) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Exception processing request: " + e); Console.ResetColor(); } } } catch (Exception e) { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Exception caught! " + e); } }

这是我遇到一个笨拙的客户端尝试连接时的日志:

Here's the log when I get a wonky client trying to connect:

Starting HTTP server at *:8443/ Unhandled Exception: System.IO.IOException: The authentication or decryption has failed. ---> Mono.Security.Protocol.Tls.TlsException: The client stopped the handshake. at Mono.Security.Protocol.Tls.SslServerStream.EndNegotiateHandshake (IAsyncResult asyncResult) <0x41e581b0 + 0x0022b> in <filename unknown>:0 at Mono.Security.Protocol.Tls.SslStreamBase.AsyncHandshakeCallback (IAsyncResult asyncResult) <0x41e57d20 + 0x00086> in <filename unknown>:0 --- End of inner exception stack trace --- at Mono.Security.Protocol.Tls.SslStreamBase.EndRead (IAsyncResult asyncResult) <0x41e50110 + 0x0015f> in <filename unknown>:0 at Mono.Net.Security.Private.LegacySslStream.EndAuthenticateAsServer (IAsyncResult asyncResult) <0x41e50000 + 0x0003e> in <filename unknown>:0 at Mono.Net.Security.Private.LegacySslStream.AuthenticateAsServer (System.Security.Cryptography.X509Certificates.X509Certificate serverCertificate, Boolean clientCertificateRequired, SslProtocols enabledSslProtocols, Boolean checkCertificateRevocation) <0x41e40990 + 0x00055> in <filename unknown>:0 at System.Net.HttpConnection.Init () <0x41e3e0e0 + 0x0005f> in <filename unknown>:0 at System.Net.HttpConnection..ctor (System.Net.Sockets.Socket sock, System.Net.EndPointListener epl, Boolean secure, System.Security.Cryptography.X509Certificates.X509Certificate cert) <0x41e3b670 + 0x003e7> in <filename unknown>:0 at System.Net.EndPointListener.OnAccept (System.Object sender, System.EventArgs e) <0x41e3b210 + 0x002a7> in <filename unknown>:0 at System.Net.Sockets.SocketAsyncEventArgs.OnCompleted (System.Net.Sockets.SocketAsyncEventArgs e) <0x41e3b1d0 + 0x0002e> in <filename unknown>:0 at System.Net.Sockets.SocketAsyncEventArgs.Complete () <0x41e3b1b0 + 0x00013> in <filename unknown>:0 at System.Net.Sockets.Socket.<AcceptAsyncCallback>m__0 (IAsyncResult ares) <0x41e3aa70 + 0x0037f> in <filename unknown>:0 at System.Net.Sockets.SocketAsyncResult+<Complete>c__AnonStorey0.<>m__0 (System.Object _) <0x41e3a980 + 0x0001d> in <filename unknown>:0 at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem () <0x7f22cac25460 + 0x0002f> in <filename unknown>:0 at System.Threading.ThreadPoolWorkQueue.Dispatch () <0x7f22cac239e0 + 0x001d6> in <filename unknown>:0 at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback () <0x7f22cac252e0 + 0x00008> in <filename unknown>:0

推荐答案

并不是一个解决方案,但是我可以通过使用nginx创建本地反向代理并将单声道服务器设置为仅侦听127.0来解决此问题. .0.1

Not quite a solution, but I was able to work around the problem by creating a local reverse proxy with nginx, and setting the mono server to only listen on 127.0.0.1

www.nginx/resources/admin-guide /reverse-proxy/

更多推荐

如何在Mono HTTPListener服务器中捕获SSL异常?

本文发布于:2023-11-12 19:21:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582310.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:器中   异常   如何在   HTTPListener   Mono

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!