故事情节:
我想用 C# 创建我自己的网络服务器(第一次尝试)。进展顺利(我正在使用Visual Studio 到代码申请和火狐浏览器检查如果我做得对的话)并且我成功地制作了一个基本的 TCPServer。当我尝试向其添加 SSL 支持时,遇到了问题。
- 在过去 7 天里,我一直在尝试使用 SSLStream.AuthenticateAsServer([自签名证书]) 作为具有 SSL 支持的 TcpServer 进行身份验证
Problem:
当我从我心爱的 Firefox 收到 [Upgrade-Insecure-Requests: 1] 时,我正在尝试 [SSLStream.AuthenticateAsServer([自签名证书])]。这样做我的代码被卡住了(但不会冻结/崩溃)在这一行同时我的 Firefox 一直在加载似乎没有给我发送回复。
Code:
starting my server
TCPServer.ServerStart(8080);
(listener is being defined in my TCPServer class)
internal static TcpListener listener;
async internal static void ServerStart(int port)
{
if (listener == null)
{
listener = new TcpListener(IPAddress.Any, port);
}
listener.Start();
//clients
await Task.Run(()=> SucheNachBesuchern());
listener.Stop();
}
accepting clients
private static void SucheNachBesuchern(){
TcpClient Besucher;
while (true)
{
Besucher = listener.AcceptTcpClient();
ThreadPool.QueueUserWorkItem(ThreadProzess, Besucher);
}
}
handling accepted clients
private static void ThreadProzess(object Besucher) {
TcpClient besucher = (TcpClient)Besucher;
Abfertige(besucher);
besucher.Close();
besucher.Dispose();
}
private static void Abfertige(TcpClient Besucher)
{
//Reading the Request
StreamReader Auftrag = new StreamReader(Besucher.GetStream());
List<String> AuftragNachricht= new List<String>();
while (Auftrag.Peek()!=-1) {
AuftragNachricht.Add(Auftrag.ReadLine());
}
//Anfrage = request Class with bool Anfrage.SSLAnfrage being true
//if the request contains 'Upgrade-Insecure-Requests: 1'
Anfrage Anfrage = Anfrage.VerarbeiteAuftrag(AuftragNachricht);
if (Anfrage.SSLAnfrage)// = if([request conatined 'Upgrade-Insecure-Requests: 1'])
{
//opening an SslStream to the TcpClient Besucher
SslStream SSLStream = new SslStream(Besucher.GetStream(), false);
try
{
//Authenticating as TcpServer supporting SSL !CODE IS STUCK AT THIS LINE!
SSLStream.AuthenticateAsServer([SELFSINGEDX509CERTIFICATE.cer using openssl pkcs12], clientCertificateRequired: false, enabledSslProtocols: System.Security.Authentication.SslProtocols.Default, checkCertificateRevocation: false);
//set timeouts for read and write
SSLStream.ReadTimeout = 5000;
SSLStream.WriteTimeout = 5000;
//tryig to catch my Firefox as new client on SSL port 443
TcpListener SSLListener = new TcpListener(IPAddress.Parse("192.168.178.72"), 443);
SSLListener.Start();
TcpClient SSLBesucher = SSLListener.AcceptTcpClient();
Debug.WriteLine("I'VE GOT A CLIENT HERE!!!!111");
}
catch (Exception Error) {
Debug.WriteLine($"---Error gefangen: {Error.ToString()}");
}
}//[...more Code]
(For not having any knowledge in SSL
I used this example-code https://learn.microsoft.com/en-us/dotnet/api/system.net.security.sslstream?view=netframework-4.7.2)