简体   繁体   English

Mono C#中的wss WebSocket服务器:SslStream没有提供任何数据,但Stream提供了

[英]wss WebSocket server in Mono C# : SslStream gives me no data but Stream do

Since days I'm trying to get (JS)wss/(c#)SslStream connection to work in order to create a wss server in mono c#. 几天以来,我一直试图使(JS)wss /(c#)SslStream连接正常工作,以便在单声道c#中创建wss服务器。

My Problem : When an incomming secure WebSocket connection is accepted by the server, I can't get data from it in order to begin the handshake process. 我的问题:服务器接受传入的安全WebSocket连接时,我无法从中获取数据以开始握手过程。

What I did so far : 我到目前为止所做的:

  1. I setup a TCPListener that accept the client giving me a TCPClient instance. 我设置了一个TCPListener,它接受客户端给我的TCPClient实例。
  2. I get the stream from the TCPClient and create a SslStream from it. 我从TCPClient获取流并从中创建一个SslStream。
  3. I synchronously authenticate it with AuthenticateAsServer(X509Certificate2) 我使用AuthenticateAsServer(X509Certificate2)同步对其进行身份验证
  4. I unsuccessfully try to read data 我尝试读取数据失败

Other details : 其他详情 :

  1. I note that if I use a Stream object instead of an SslStream one, I succed in getting data from it (but crypted as expected) 我注意到,如果我使用Stream对象而不是SslStream对象,则可以成功地从中获取数据(但按预期进行了加密)
  2. I tested to connect my WebSoket to same adress/port to Fleck (built on my box with Monodevelop too) using the same pfx certificate and got it working properly 我测试了使用相同的pfx证书将WebSoket连接到与Fleck相同的地址/端口(也与Monodevelop一起在我的盒子中构建),并使其正常工作
  3. I note also that Fleck throw messages saying it recieve 0 byte, then it close the connection and (in some way) reconnect it and get data properly from Socket/SslStream. 我还注意到, Fleck抛出消息说它收到了0个字节,然后它关闭了连接并(以某种方式)重新连接它并从Socket / SslStream中正确获取数据。
  4. I don't get any error, SslStream seem to be correctly authenticated 我没有收到任何错误,SslStream似乎已正确验证
  5. I correctly connect clients in https and deal with requests on an another port in the same program 我在https中正确连接客户端,并在同一程序中处理另一个端口上的请求

My Configuration : 我的配置:

  • OS : ArchLinux 操作系统:ArchLinux
  • C# : Mono .Net Framework 4.7 C#:Mono .Net Framework 4.7
  • Browsers (Chromium & Firefox) 浏览器(Chromium和Firefox)

    Despite of all my research so far I've not found the solution , maybe someone can help me to wonder what I miss here ... 尽管到目前为止我一直在研究,但我还没有找到解决方案,也许有人可以帮助我想知道我在这里错过了什么...

Thanks in advance for any help ! 在此先感谢您的帮助!

The listen code : 监听代码:

   public void AudioListen ()
    {
        audioComListener.Start ();
        while (isAudioActive) {
            TcpClient s = audioComListener.AcceptTcpClient ();
            Stream clientStream;
            string hellostr;

            clientStream = getClientStream(s);

            if(debugCommuncation)
            {
                logToFileLine("(KEY) HandShaking begins with "+s.Client.RemoteEndPoint.ToString ().Split (':') [0]);
            }

            if(!WebSocketHandshake(clientStream))
            {
                if(debugCommuncation)
                {
                    logToFileLine("(KEY) (X) HandShake read 0 byte from "+s.Client.RemoteEndPoint.ToString ().Split (':') [0]);
                }

                s.Close();

                return;
            }
            else
            {
                logToFileLine("(KEY) HandShaking OK with "+s.Client.RemoteEndPoint.ToString ().Split (':') [0]);
            }

            hellostr=streamReadLine(clientStream);

            if(hellostr.IndexOf("hello:")==0)
            {
                string usrstr=hellostr.Split(':')[1];
                User usr= Users.GetUser(usrstr);


                if(usr!=null)
                {
                    usr.TcpC=s;

                    User usrdest=usr.corresp;

                    if( usrdest!=null && 
                        usrdest.ByPass==null && 
                        usrdest.TcpC!=null)
                    {
                        Stream clientStream2 = getClientStream(usrdest.TcpC);
                        usr.ByPass=new TCPByPass(clientStream,clientStream2);
                        usrdest.ByPass=usr.ByPass;
                    }                       

                }
            }

            Thread.Sleep (1);
        };
    }

Function to get the SslStream : 获取SslStream的函数:

        private Stream getClientStream(TcpClient s,bool forceHTTP=false)
    {
        Stream clientStream;
        if(isSSL && !forceHTTP)
        {
            clientStream = new SslStream (s.GetStream ());
            ((SslStream)clientStream).AuthenticateAsServer(SrvCert);
            // Set timeouts for the read and write to 5 seconds.
            /**/clientStream.ReadTimeout = 5000;
            clientStream.WriteTimeout = 5000;

            //SecuDiag.MakeAllDiag(((SslStream)clientStream));
        }
        else
        {
            clientStream=s.GetStream ();
        }     

        return clientStream;   
    }

Function that try to get data for hanshake purpose : 尝试出于hanshake目的获取数据的函数:

    public bool WebSocketHandshake(Stream clientStream)
    {
        string hellostr;

        // Here I test trying to get data (Also tried to use Stream.ReadByte())
        Byte[] toto=new Byte[2048];

        ((SslStream)clientStream).Read(toto,0,2048);

        if(toto[0]==0)return false;

        Console.WriteLine("#############################################");
        Console.WriteLine("toto array is {0} bytes long",toto.Length);
        for(int t =0;t<10;t++)
        {
            for(int u =0;u<10;u++)
            {
                Console.Write(toto[t*10+u].ToString());
            }
            Console.WriteLine(";");
        }
        Console.WriteLine("#############################################");

        // Trying to get data

        //hellostr=streamReadLine(clientStream);     

        //Console.WriteLine(hellostr);

        return true;
    }

I think I solved the problem. 我想我解决了问题。 I don't understand why, but I think its necessary to close the accepted connection just after accepting it iaoi the connection is wss. 我不明白为什么,但是我认为有必要在接受连接之后立即关闭接受的连接。 The the websocket wil automagically reconnect :) Websocket将自动重新连接:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM