Example #1
0
        public async Task <bool> ConnectAsync(Uri uri, bool secure = false)
        {
            if (Connected)
            {
                return(true);
            }

            try
            {
                TcpClient client = new TcpClient();
                client.NoDelay = true;

                await client.ConnectAsync(uri.Host, uri.Port).ConfigureAwait(false);

                NetwrkWebClient webClient = new NetwrkWebClient(client);

                if (secure)
                {
                    await webClient.SslAuthenticateAsClientAsync(uri.Host);
                }

                NetwrkWebRequest request = new NetwrkWebRequest
                {
                    Path = uri.AbsolutePath
                };

                string webSocketKey = CreateSecWebSocketKey();

                request.Headers[NetwrkKnownHttpHeaders.Connection]          = "Upgrade";
                request.Headers[NetwrkKnownHttpHeaders.Upgrade]             = "websocket";
                request.Headers[NetwrkKnownHttpHeaders.Host]                = uri.Host;
                request.Headers[NetwrkKnownHttpHeaders.SecWebSocketVersion] = "13";
                request.Headers[NetwrkKnownHttpHeaders.SecWebSocketKey]     = webSocketKey;

                await webClient.SendAsync(request);

                NetwrkWebResponse response = await webClient.ReceiveAsync <NetwrkWebResponse>();

                if (!response.IsWebSocketAccepted || !response.IsKeyValid(webSocketKey))
                {
                    return(false);
                }

                InitializeConnected(client, webClient.Stream);
                Start();

                return(true);
            }
            catch
            {
                return(false);
            }
        }
Example #2
0
        private async Task <bool> HandleClient(TcpClient client)
        {
            NetwrkWebClient webClient = new NetwrkWebClient(client);

            if (Certificate != null)
            {
                await webClient.SslAuthenticateAsServerAsync(Certificate);
            }

            NetwrkWebRequest request = await webClient.ReceiveAsync <NetwrkWebRequest>();

            if (request == null)
            {
                return(false);
            }

            if (request.IsWebSocketRequest)
            {
                if (OnWebsocketRequest?.Invoke(this, request) ?? true)
                {
                    NetwrkWebResponse response = new NetwrkWebResponse
                    {
                        Version    = request.Version,
                        StatusCode = NetwrkHttpStatusCode.SwitchingProtocols
                    };

                    response.Headers[NetwrkKnownHttpHeaders.Upgrade]    = "websocket";
                    response.Headers[NetwrkKnownHttpHeaders.Connection] = "Upgrade";
                    response.Headers[NetwrkKnownHttpHeaders.Server]     = "Netwrk/1.0";

                    using (var sha1 = SHA1.Create())
                    {
                        string accept      = request.Headers[NetwrkKnownHttpHeaders.SecWebSocketKey] + NetwrkWebSocket.ConstantKey;
                        byte[] acceptBytes = Encoding.ASCII.GetBytes(accept);
                        byte[] acceptSha1  = sha1.ComputeHash(acceptBytes);

                        response.Headers[NetwrkKnownHttpHeaders.SecWebSocketAccept] = Convert.ToBase64String(acceptSha1);
                    }

                    await webClient.SendAsync(response);

                    NetwrkWebSocket webSocket = new NetwrkWebSocket(client, webClient.Stream);

                    OnWebSocketConnection?.Invoke(this, webSocket);

                    webSocket.Start();
                }
            }
            else
            {
                NetwrkWebResponse response = OnRequest?.Invoke(this, request) ?? new NetwrkWebResponse
                {
                    Version    = request.Version,
                    StatusCode = NetwrkHttpStatusCode.InternalServerError
                };

                await webClient.SendAsync(response);

                client.Close();
            }

            return(true);
        }