doghappy / socket.io-client-csharp

socket.io-client implemention for .NET
MIT License
729 stars 125 forks source link

Can't connect to Socket #221

Open FireBird4 opened 2 years ago

FireBird4 commented 2 years ago

Hello, I'm developing an Android and IOS app using Xamarin Forms. Like in issue #212 I get an error:

{System.Net.WebSockets.WebSocketException (0x80004005): Unable to connect to the remote server ---> System.Security.Authentication.AuthenticationException: Authentication failed, see inner exception. ---> Mono.Btls.MonoBtlsException: Ssl error:1000007d:SSL routines:OPENSSL_internal:CERTIFICATE_VERIFY_FAILED at /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/boringssl/ssl/handshake_client.c:1132 at Mono.Btls.MonoBtlsContext.ProcessHandshake () [0x00042] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/System/Mono.Btls/MonoBtlsContext.cs:220 at Mono.Net.Security.MobileAuthenticatedStream.ProcessHandshake (Mono.Net.Security.AsyncOperationStatus status, System.Boolean renegotiate) [0x000da] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/System/Mono.Net.Security/MobileAuthenticatedStream.cs:715 at (wrapper remoting-invoke-with-check) Mono.Net.Security.MobileAuthenticatedStream.ProcessHandshake(Mono.Net.Security.AsyncOperationStatus,bool) at Mono.Net.Security.AsyncHandshakeRequest.Run (Mono.Net.Security.AsyncOperationStatus status) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/System/Mono.Net.Security/AsyncProtocolRequest.cs:289 at Mono.Net.Security.AsyncProtocolRequest.ProcessOperation (System.Threading.CancellationToken cancellationToken) [0x000fc] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/System/Mono.Net.Security/AsyncProtocolRequest.cs:223 --- End of inner exception stack trace --- at Mono.Net.Security.MobileAuthenticatedStream.ProcessAuthentication (System.Boolean runSynchronously, Mono.Net.Security.MonoSslAuthenticationOptions options, System.Threading.CancellationToken cancellationToken) [0x0025c] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/System/Mono.Net.Security/MobileAuthenticatedStream.cs:310 at System.Net.WebSockets.WebSocketHandle.ConnectAsyncCore (System.Uri uri, System.Threading.CancellationToken cancellationToken, System.Net.WebSockets.ClientWebSocketOptions options) [0x0014f] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/System.Net.WebSockets.Client/src/System/Net/WebSockets/WebSocketHandle.Managed.cs:106 at System.Net.WebSockets.WebSocketHandle.ConnectAsyncCore (System.Uri uri, System.Threading.CancellationToken cancellationToken, System.Net.WebSockets.ClientWebSocketOptions options) [0x00385] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/System.Net.WebSockets.Client/src/System/Net/WebSockets/WebSocketHandle.Managed.cs:150 at System.Net.WebSockets.ClientWebSocket.ConnectAsyncCore (System.Uri uri, System.Threading.CancellationToken cancellationToken) [0x000d1] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/System.Net.WebSockets.Client/src/System/Net/WebSockets/ClientWebSocket.cs:157 at SocketIOClient.Transport.DefaultClientWebSocket.ConnectAsync (System.Uri uri, System.Threading.CancellationToken cancellationToken) [0x00098] in <505997af597d4bc887ff79a8f41072d7>:0 at SocketIOClient.Transport.WebSocketTransport.ConnectAsync (System.Uri uri) [0x000ab] in <505997af597d4bc887ff79a8f41072d7>:0 at SocketIOClient.Transport.TransportRouter.ConnectByWebsocketAsync () [0x00197] in <505997af597d4bc887ff79a8f41072d7>:0 at SocketIOClient.Transport.TransportRouter.ConnectAsync () [0x000a0] in <505997af597d4bc887ff79a8f41072d7>:0 at SocketIOClient.SocketIO.ConnectAsync () [0x000ce] in <505997af597d4bc887ff79a8f41072d7>:0 }

I tried getting further information by getting the sslPolicyErrors, with no success:

client = new SocketIO("https://my.url.com", new SocketIOOptions
{
    ReconnectionDelay = 10000,
    ConnectionTimeout = new TimeSpan(0, 0, 10),
    Reconnection = true,
    EIO=4
});
client.ClientWebSocketProvider = () =>
{
    var clientWebSocket = new DefaultClientWebSocket
    {
        ConfigOptions = o =>
        {
            var options = o as ClientWebSocketOptions;
            options.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
            {
                Console.WriteLine("SslPolicyErrors: " + sslPolicyErrors); //no output
                return true;
            };
        }
    };
    return clientWebSocket;
};

Thanks for your help.

cc : @doghappy, @vikoms

FireBird4 commented 2 years ago

As mentioned in this forum is's a problem with the expired Let's Encrypt Root certificates. Further information Let's Encrypt: DST Root CA X3 Expiration (September 2021) & Let's Encrypt: Extending Android Device Compatibility for Let's Encrypt Certificates

vikoms commented 2 years ago

Hello @FireBird4 you can add code like this

HttpClientHandler httpClientHandler = new HttpClientHandler();
                httpClientHandler.ServerCertificateCustomValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
                App.ChatSocket.HttpClient = new HttpClient(httpClientHandler);

and make sure your server socket must be more than 3.xx and I use socket io client ver 3.0.1

I hope this solves your problem

FireBird4 commented 2 years ago

Thanks for your answer @vikoms. Are the certificates then no longer checked at all? Isn't that a bit risky?