Пример #1
0
        ClientWebSocket CreateConnectedClient(ServiceEndPoint endPoint)
        {
            if (!endPoint.IsWebSocketEndpoint)
            {
                throw new Exception("Only wss:// endpoints are supported");
            }

            var connectionId = Guid.NewGuid().ToString();

            var client = new ClientWebSocket();

            client.Options.ClientCertificates = new X509Certificate2Collection(new X509Certificate2Collection(clientCertificate));
            client.Options.AddSubProtocol("Octopus");
            client.Options.SetRequestHeader(ServerCertificateInterceptor.Header, connectionId);
            if (endPoint.Proxy != null)
            {
                client.Options.Proxy = new WebSocketProxy(endPoint.Proxy);
            }

            try
            {
                ServerCertificateInterceptor.Expect(connectionId);
                using (var cts = new CancellationTokenSource(endPoint.TcpClientConnectTimeout))
                    client.ConnectAsync(endPoint.BaseUri, cts.Token)
                    .ConfigureAwait(false).GetAwaiter().GetResult();
                ServerCertificateInterceptor.Validate(connectionId, endPoint);
            }
            finally
            {
                ServerCertificateInterceptor.Remove(connectionId);
            }


            return(client);
        }
        ClientWebSocket CreateConnectedClient(ServiceEndPoint serviceEndpoint, CancellationToken cancellationToken)
        {
            if (!serviceEndpoint.IsWebSocketEndpoint)
            {
                throw new Exception("Only wss:// endpoints are supported");
            }

            var connectionId = Guid.NewGuid().ToString();

            var client = new ClientWebSocket();

            client.Options.ClientCertificates = new X509Certificate2Collection(new X509Certificate2Collection(clientCertificate));
            client.Options.AddSubProtocol("Octopus");
            client.Options.SetRequestHeader(ServerCertificateInterceptor.Header, connectionId);
            if (serviceEndpoint.Proxy != null)
            {
                client.Options.Proxy = new WebSocketProxy(serviceEndpoint.Proxy);
            }

            try
            {
                ServerCertificateInterceptor.Expect(connectionId);
                using (var cts = new CancellationTokenSource(serviceEndpoint.TcpClientConnectTimeout))
                {
                    using (cancellationToken.Register(() => cts?.Cancel()))
                        client.ConnectAsync(serviceEndpoint.BaseUri, cts.Token)
                        .ConfigureAwait(false).GetAwaiter().GetResult();
                }
                ServerCertificateInterceptor.Validate(connectionId, serviceEndpoint);
            }
            catch
            {
                if (client.State == WebSocketState.Open)
                {
                    using (var sendCancel = new CancellationTokenSource(TimeSpan.FromSeconds(1)))
                        client.CloseAsync(WebSocketCloseStatus.ProtocolError, "Certificate thumbprint not recognised", sendCancel.Token)
                        .ConfigureAwait(false).GetAwaiter().GetResult();
                }

                client.Dispose();
                throw;
            }
            finally
            {
                ServerCertificateInterceptor.Remove(connectionId);
            }

            return(client);
        }