/// <summary>
 /// Disconnects Client
 /// </summary>
 /// <param name="client"></param>
 public void DisconnectClient(uint client)
 {
     try
     {
         SecureServer.Disconnect(client);
         Debug.Console(2, this, Debug.ErrorLogLevel.Notice, "Disconnected client index: {0}", client);
     }
     catch (Exception ex)
     {
         Debug.Console(2, this, Debug.ErrorLogLevel.Error, "Error Disconnecting client index: {0}. Error: {1}", client, ex);
     }
 }
Пример #2
0
        public void ServerDataReceivedCallback(SecureTCPServer server, uint clientIndex, int bytesReceived)
        {
            if (bytesReceived <= 0)
            {
                CrestronConsole.PrintLine("ServerDataReceivedCallback error: server's connection with client " + clientIndex + " has been closed.");
                server.Disconnect(clientIndex);
                // A connection has closed, so another client may connect if the server stopped listening
                // due to the maximum number of clients connecting
                if ((server.State & ServerState.SERVER_NOT_LISTENING) > 0)
                {
                    server.WaitForConnectionAsync(ServerConnectedCallback);
                }
            }
            else
            {
                CrestronConsole.PrintLine("\n------ incoming message -----------");
                byte[] recvd_bytes = new byte[bytesReceived];

                // Copy the received bytes into a local buffer so that they can be echoed back.
                // Do not pass the reference to the incoming data buffer itself to the SendDataAsync method
                Array.Copy(server.GetIncomingDataBufferForSpecificClient(clientIndex), recvd_bytes, bytesReceived);

                // The server in this example expects ASCII text from the client, but any other encoding is possible
                string recvd_msg = ASCIIEncoding.ASCII.GetString(recvd_bytes, 0, bytesReceived);
                CrestronConsole.PrintLine("Client " + clientIndex + " says: " + recvd_msg + "\r\nEchoing back to client " + clientIndex + "...");

                // echo the received message back to the client who sent it
                server.SendDataAsync(clientIndex, recvd_bytes, recvd_bytes.Length, ServerDataSentCallback);

                // Begin waiting for another message from that same client
                server.ReceiveDataAsync(clientIndex, ServerDataReceivedCallback);

                CrestronConsole.PrintLine("---------- end of message ----------");
            }
        }
Пример #3
0
 public void Disconnect(string args)
 {
     try
     {
         if (server == null)
         {
             CrestronConsole.PrintLine("Server is already disconnected");
             return;
         }
         if (args == "")
         { // When no arguments are provided, disconnect from all clients and destroy the server
             server.DisconnectAll();
             CrestronConsole.PrintLine("Server has disconnected from all clients and is no longer listening on port " + server.PortNumber);
             server = null;
         }
         else
         { // Disconnect from the client specified by the user
             uint clientIndex = uint.Parse(args);
             if (server.ClientConnected(clientIndex))
             {
                 server.Disconnect(clientIndex);
                 CrestronConsole.PrintLine("Server has disconnected from " + clientIndex);
             }
             else
             {
                 CrestronConsole.PrintLine("Client #" + clientIndex + " does not exist currently");
             }
         }
     }
     catch (Exception e)
     {
         PrintAndLog("Error in Disconnect: " + e.Message);
     }
 }
Пример #4
0
 /// <summary>
 /// Secure Received Data Async Callback
 /// </summary>
 /// <param name="mySecureTCPServer"></param>
 /// <param name="clientIndex"></param>
 /// <param name="numberOfBytesReceived"></param>
 void SecureReceivedDataAsyncCallback(SecureTCPServer mySecureTCPServer, uint clientIndex, int numberOfBytesReceived)
 {
     if (numberOfBytesReceived > 0)
     {
         string received = "Nothing";
         byte[] bytes    = mySecureTCPServer.GetIncomingDataBufferForSpecificClient(clientIndex);
         received = System.Text.Encoding.GetEncoding(28591).GetString(bytes, 0, numberOfBytesReceived);
         if (WaitingForSharedKey.Contains(clientIndex))
         {
             received = received.Replace("\r", "");
             received = received.Replace("\n", "");
             if (received != SharedKey)
             {
                 byte[] b = Encoding.GetEncoding(28591).GetBytes("Shared key did not match server. Disconnecting");
                 Debug.Console(2, "Client at index {0} Shared key did not match the server, disconnecting client", clientIndex);
                 ErrorLog.Error("Client at index {0} Shared key did not match the server, disconnecting client", clientIndex);
                 mySecureTCPServer.SendDataAsync(clientIndex, b, b.Length, null);
                 mySecureTCPServer.Disconnect(clientIndex);
             }
             if (mySecureTCPServer.NumberOfClientsConnected > 0)
             {
                 mySecureTCPServer.ReceiveDataAsync(SecureReceivedDataAsyncCallback);
             }
             WaitingForSharedKey.Remove(clientIndex);
             byte[] skResponse = Encoding.GetEncoding(28591).GetBytes("Shared Key Match, Connected and ready for communication");
             mySecureTCPServer.SendDataAsync(clientIndex, skResponse, skResponse.Length, null);
             mySecureTCPServer.ReceiveDataAsync(SecureReceivedDataAsyncCallback);
         }
         else
         {
             mySecureTCPServer.ReceiveDataAsync(SecureReceivedDataAsyncCallback);
             Debug.Console(2, "Secure Server Listening on Port: {0}, client IP: {1}, NumberOfBytesReceived: {2}, Received: {3}\r\n",
                           mySecureTCPServer.PortNumber, mySecureTCPServer.GetAddressServerAcceptedConnectionFromForSpecificClient(clientIndex), numberOfBytesReceived, received);
             onTextReceived(received);
         }
         checkHeartbeat(clientIndex, received);
     }
     if (mySecureTCPServer.GetServerSocketStatusForSpecificClient(clientIndex) == SocketStatus.SOCKET_STATUS_CONNECTED)
     {
         mySecureTCPServer.ReceiveDataAsync(clientIndex, SecureReceivedDataAsyncCallback);
     }
 }
Пример #5
0
        void HeartbeatTimer_CallbackFunction(object o)
        {
            uint clientIndex = (uint)o;

            string address = Secure ? SecureServer.GetAddressServerAcceptedConnectionFromForSpecificClient(clientIndex) :
                             UnsecureServer.GetAddressServerAcceptedConnectionFromForSpecificClient(clientIndex);

            ErrorLog.Error("Heartbeat not received for Client at IP: {0}, DISCONNECTING BECAUSE HEARTBEAT REQUIRED IS TRUE", address);
            Debug.Console(2, "Heartbeat not received for Client at IP: {0}, DISCONNECTING BECAUSE HEARTBEAT REQUIRED IS TRUE", address);

            SendTextToClient("Heartbeat not received by server, closing connection", clientIndex);

            if (Secure)
            {
                SecureServer.Disconnect(clientIndex);
            }
            else
            {
                UnsecureServer.Disconnect(clientIndex);
            }
            HeartbeatTimerDictionary.Remove(clientIndex);
        }
Пример #6
0
        /// <summary>
        /// The callback for receiving data from the secure server client.
        /// </summary>
        /// <param name="server">The server the callback is executed on.</param>
        /// <param name="clientIndex">The index of the client data is received from.</param>
        /// <param name="count">The number of bytes of data received.</param>
        private void SecureReceiveDataCallback(SecureTCPServer server, uint clientIndex, int count)
        {
            if (count <= 0)
            {
                if (server.ClientConnected(clientIndex))
                {
                    server.ReceiveDataAsync(clientIndex, SecureReceiveDataCallback);
                }

                return;
            }

            var dataBuffer = server.GetIncomingDataBufferForSpecificClient(clientIndex);
            var data       = Encoding.UTF8.GetString(dataBuffer, 0, count);

            if (Regex.IsMatch(data, "^GET", RegexOptions.IgnoreCase))
            {
                var key = Regex.Match(data, "Sec-WebSocket-Key: (.*)").Groups[1].Value.Trim();
                key = key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
                byte[] keySha1 = Crestron.SimplSharp.Cryptography.SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(key));
                string sha64   = Convert.ToBase64String(keySha1);

                var responseMessage = "HTTP/1.1 101 Switching Protocols\r\n" +
                                      "Connection: Upgrade\r\n" +
                                      "Upgrade: websocket\r\n" +
                                      "Sec-WebSocket-Accept: " + sha64 + "\r\n\r\n";

                this.Send(responseMessage, clientIndex, false);
            }
            else
            {
                CrestronConsole.PrintLine("Received '{0}' bytes.", count);
                bool  fin        = (dataBuffer[0] & 128) != 0;
                bool  mask       = (dataBuffer[1] & 128) != 0;
                int   opcode     = dataBuffer[0] & 15;
                ulong dataLength = (ulong)(dataBuffer[1] - 128);
                ulong offset     = 2;

                if (opcode == 0x0)
                {
                    CrestronConsole.PrintLine("Received continuation frame opcode.");
                }
                else if (opcode == 0x1)
                {
                    CrestronConsole.PrintLine("Received text frame opcode.");
                }
                else if (opcode == 0x2)
                {
                    CrestronConsole.PrintLine("Received binary frame opcode.");
                }
                else if (opcode >= 0x3 && opcode <= 0x7)
                {
                    CrestronConsole.PrintLine("Received reserved non-control opcode.");
                }
                else if (opcode == 0x8)
                {
                    this.Send(new byte[]
                    {
                        BitConverter.GetBytes(0x88)[0],
                        BitConverter.GetBytes(0x00)[0]
                    }, clientIndex, false);
                    server.Disconnect(clientIndex);
                    CrestronConsole.PrintLine("Received disconnect OpCode. Disconnected.");
                    return;
                }
                else if (opcode == 0x9)
                {
                    this.Send(new byte[]
                    {
                        BitConverter.GetBytes(0x8A)[0],
                        BitConverter.GetBytes(0x00)[0]
                    }, clientIndex, false);
                    CrestronConsole.PrintLine("Received Ping and sent a Pong.");

                    if (server.ClientConnected(clientIndex))
                    {
                        server.ReceiveDataAsync(clientIndex, SecureReceiveDataCallback);
                    }

                    return;
                }
                else if (opcode == 0xA)
                {
                    CrestronConsole.PrintLine("Received Pong.");
                    if (server.ClientConnected(clientIndex))
                    {
                        server.ReceiveDataAsync(clientIndex, SecureReceiveDataCallback);
                    }

                    return;
                }
                else if (opcode >= 0xB && opcode <= 0xF)
                {
                    CrestronConsole.PrintLine("Received reserved control frame opcode.");
                }

                if (dataLength == 126)
                {
                    CrestronConsole.PrintLine("Data length is 126.");
                    dataLength = BitConverter.ToUInt16(new byte[] { dataBuffer[3], dataBuffer[2] }, 0);
                    offset     = 4;
                }
                else if (dataLength == 127)
                {
                    CrestronConsole.PrintLine("Data length is 127.");
                    dataLength = BitConverter.ToUInt64(new byte[] { dataBuffer[9], dataBuffer[8], dataBuffer[7], dataBuffer[6], dataBuffer[5], dataBuffer[4], dataBuffer[3], dataBuffer[2] }, 0);
                    offset     = 10;
                }

                if (dataLength == 0)
                {
                    CrestronConsole.PrintLine("Data length was zero.");
                    this.Send(string.Format("{0}>", InitialParametersClass.ControllerPromptName), clientIndex, true);
                }
                else if (mask)
                {
                    byte[] de = new byte[dataLength];
                    byte[] mk = new byte[4] {
                        dataBuffer[offset], dataBuffer[offset + 1], dataBuffer[offset + 2], dataBuffer[offset + 3]
                    };
                    offset += 4;

                    for (ulong i = 0; i < dataLength; i++)
                    {
                        de[i] = (byte)(dataBuffer[offset + i] ^ mk[i % 4]);
                    }

                    var text = Encoding.UTF8.GetString(de, 0, de.Length);
                    var dr   = DataReceived;
                    if (dr != null)
                    {
                        dr.Invoke(text, clientIndex);
                    }
                }
                else
                {
                    CrestronConsole.PrintLine("Data length was '{0}', but mask bit not set. Invalid message received.", dataLength);
                    var dbg = string.Empty;
                    for (var i = 0; i < count; i++)
                    {
                        dbg += string.Format("[{0}]", Convert.ToString(dataBuffer[i], 16));
                    }

                    Debug.WriteDebugLine(this, dbg);
                }
            }

            if (server.ClientConnected(clientIndex))
            {
                server.ReceiveDataAsync(clientIndex, SecureReceiveDataCallback);
            }
        }
        /// <summary>
        /// Secure Received Data Async Callback
        /// </summary>
        /// <param name="mySecureTCPServer"></param>
        /// <param name="clientIndex"></param>
        /// <param name="numberOfBytesReceived"></param>
        void SecureReceivedDataAsyncCallback(SecureTCPServer mySecureTCPServer, uint clientIndex, int numberOfBytesReceived)
        {
            if (numberOfBytesReceived > 0)
            {
                string received = "Nothing";
                var    handler  = TextReceivedQueueInvoke;
                try
                {
                    byte[] bytes = mySecureTCPServer.GetIncomingDataBufferForSpecificClient(clientIndex);
                    received = System.Text.Encoding.GetEncoding(28591).GetString(bytes, 0, numberOfBytesReceived);
                    if (WaitingForSharedKey.Contains(clientIndex))
                    {
                        received = received.Replace("\r", "");
                        received = received.Replace("\n", "");
                        if (received != SharedKey)
                        {
                            byte[] b = Encoding.GetEncoding(28591).GetBytes("Shared key did not match server. Disconnecting");
                            Debug.Console(1, this, Debug.ErrorLogLevel.Warning, "Client at index {0} Shared key did not match the server, disconnecting client. Key: {1}", clientIndex, received);
                            mySecureTCPServer.SendData(clientIndex, b, b.Length);
                            mySecureTCPServer.Disconnect(clientIndex);

                            return;
                        }

                        WaitingForSharedKey.Remove(clientIndex);
                        byte[] success = Encoding.GetEncoding(28591).GetBytes("Shared Key Match");
                        mySecureTCPServer.SendDataAsync(clientIndex, success, success.Length, null);
                        OnServerClientReadyForCommunications(clientIndex);
                        Debug.Console(1, this, Debug.ErrorLogLevel.Notice, "Client with index {0} provided the shared key and successfully connected to the server", clientIndex);
                    }
                    else if (!string.IsNullOrEmpty(checkHeartbeat(clientIndex, received)))
                    {
                        onTextReceived(received, clientIndex);
                        if (handler != null)
                        {
                            MessageQueue.TryToEnqueue(new GenericTcpServerCommMethodReceiveTextArgs(received, clientIndex));
                        }
                    }
                }
                catch (Exception ex)
                {
                    Debug.Console(2, this, Debug.ErrorLogLevel.Error, "Error Receiving data: {0}. Error: {1}", received, ex);
                }
                if (mySecureTCPServer.GetServerSocketStatusForSpecificClient(clientIndex) == SocketStatus.SOCKET_STATUS_CONNECTED)
                {
                    mySecureTCPServer.ReceiveDataAsync(clientIndex, SecureReceivedDataAsyncCallback);
                }

                //Check to see if there is a subscription to the TextReceivedQueueInvoke event. If there is start the dequeue thread.
                if (handler != null)
                {
                    var gotLock = DequeueLock.TryEnter();
                    if (gotLock)
                    {
                        CrestronInvoke.BeginInvoke((o) => DequeueEvent());
                    }
                }
            }
            else
            {
                mySecureTCPServer.Disconnect(clientIndex);
            }
        }