Пример #1
0
        public override async Task OnMessage(WebSocket socket, WebSocketReceiveResult result, WebSocketMessageType type, byte[] buffer)
        {
            Console.WriteLine("Received data on server");
            string socketId = string.Empty;

            try
            {
                socketId = WebSocketConnectionManager.GetId(socket);
            }
            catch (Exception ex)
            {
                // Unkoown socketId
                Console.WriteLine("unknown id");
                return;
            }
            switch (type)
            {
            case WebSocketMessageType.Binary:
            {
                // binary data
                if (StreamManager.Streams.ContainsKey(socketId))
                {
                    Stream stream;
                    bool   couldGetValue = StreamManager.Streams.TryGetValue(socketId, out stream);

                    if (couldGetValue)
                    {
                        byte[] latestSignature           = stream.LatestSignature;
                        AsymmetricKeyParameter publicKey = stream.PublicKey;

                        bool validData = buffer.Validate(latestSignature, publicKey);
                        if (validData)
                        {
                            // Valid file
                            StreamManager.AddToQueue(socketId, buffer);
                        }
                        else
                        {
                            Console.WriteLine($"{socketId}: Data has been tampered with!");
                        }
                        return;
                    }

                    Console.WriteLine("Unable to add to queue");
                }
                break;
            }

            case WebSocketMessageType.Text:
            {
                string data = Encoding.UTF8.GetString(buffer).Trim('\0');
                if (data.StartsWith("PUBKEY:"))
                {
                    Console.WriteLine($"{socketId}: Received public key");
                    // probably public key
                    string publicKey = data.Replace("PUBKEY:", "");

                    TextReader textReader = new StringReader(publicKey);
                    Org.BouncyCastle.OpenSsl.PemReader pemReader      = new Org.BouncyCastle.OpenSsl.PemReader(textReader);
                    AsymmetricKeyParameter             publicKeyParam = (AsymmetricKeyParameter)pemReader.ReadObject();

                    if (StreamManager.Streams.ContainsKey(socketId))
                    {
                        Stream stream;
                        bool   couldGetValue = StreamManager.Streams.TryGetValue(socketId, out stream);

                        if (couldGetValue)
                        {
                            Stream updatedStream = stream;
                            updatedStream.PublicKey = publicKeyParam;
                            StreamManager.Streams.TryUpdate(socketId, updatedStream, stream);
                        }

                        return;
                    }
                    Console.WriteLine("Unable to set public key");
                    return;
                }
                else if (data.StartsWith("HASH:"))
                {
                    Console.WriteLine($"{socketId}: Received hash");

                    // probably public key
                    string signature = data.Replace("HASH:", "");

                    byte[] decodedSignature = Convert.FromBase64String(signature);

                    if (StreamManager.Streams.ContainsKey(socketId))
                    {
                        Stream stream;
                        bool   couldGetValue = StreamManager.Streams.TryGetValue(socketId, out stream);

                        if (couldGetValue)
                        {
                            Stream updatedStream = stream;
                            updatedStream.LatestSignature = decodedSignature;
                            StreamManager.Streams.TryUpdate(socketId, updatedStream, stream);
                        }

                        return;
                    }
                    Console.WriteLine("Unable to set latest signature");
                    return;
                }
                else if (data.StartsWith("{") && data.EndsWith("}"))
                {
                    //The definition for the expected JSON object.
                    var streamInfoDefinition = new { StreamerName = "" };

                    try
                    {
                        //Extract streamer name from JSON, and put it into the streamer's Stream object.
                        var streamInfo = JsonConvert.DeserializeAnonymousType(data, streamInfoDefinition);

                        if (StreamManager.Streams.ContainsKey(socketId))
                        {
                            Stream stream;
                            bool   couldGetValue = StreamManager.Streams.TryGetValue(socketId, out stream);

                            if (couldGetValue)
                            {
                                Stream updatedStream = stream;
                                updatedStream.StreamerName = streamInfo.StreamerName;
                                StreamManager.Streams.TryUpdate(socketId, updatedStream, stream);
                            }

                            return;
                        }
                        Console.WriteLine("Unable to set streamerName");
                    }
                    catch (JsonReaderException e)
                    {
                        Console.WriteLine("Received invalid JSON: {0}", data);
                    }
                }
                else if (data.StartsWith("VIEWERCOUNT"))
                {
                    int count = StreamManager.ReceivingWebSocket.Clients.Where(x => x.Value.Equals(socketId.ToString())).Count();
                    await Send(socket, $"VIEWERCOUNT: {count.ToString()}");
                }

                break;
            }

            default:
            {
                // text data
                Console.WriteLine($"{socketId}: Ignoring received data");
                break;
            }
            }
        }