Exemple #1
0
        /// <summary>
        ///     When a client connects, assign them a unique RSA keypair for handshake.
        /// </summary>
        /// <param name="clientSocket"></param>
        private static void HandleConnect(WebSocket clientSocket)
        {
            var        connectionId = CookieManager.GetConnectionId(clientSocket);
            AuthClient authClient;

            AllClients.TryGetValue(connectionId, out authClient);
            if (authClient != null)
            {
                MessageQueueManager manager;
                //check if a manager for that port exist, if not, create one
                if (!authClient.MessageQueueManagers.TryGetValue(clientSocket.LocalEndpoint.Port, out manager))
                {
                    if (authClient.MessageQueueManagers.TryAdd(clientSocket.LocalEndpoint.Port,
                                                               new MessageQueueManager()))
                    {
                        Console.WriteLine("Manager started for " + clientSocket.LocalEndpoint.Port);
                    }
                }
                return;
            }
            Console.WriteLine("Connection from " + clientSocket.RemoteEndpoint);
            var rsa = new Rsa();

            rsa.GenerateKeyPairs();
            var client = new AuthClient
            {
                PublicKey            = rsa.PublicKey,
                PrivateKey           = rsa.PrivateKey,
                MessageQueueManagers = new ConcurrentDictionary <int, MessageQueueManager>()
            };

            client.MessageQueueManagers.TryAdd(clientSocket.LocalEndpoint.Port, new MessageQueueManager());
            AllClients.AddOrUpdate(connectionId, client, (key, value) => value);
            SendWelcomeMessage(client, clientSocket);
        }
Exemple #2
0
        /// <summary>
        ///     Handles plaintext JSON packets.
        /// </summary>
        /// <param name="clientSocket"></param>
        /// <param name="message"></param>
        private static void HandlePlainTextMessage(WebSocket clientSocket, string message)
        {
            var        connectionId = CookieManager.GetConnectionId(clientSocket);
            AuthClient authClient;

            if (AllClients.TryGetValue(connectionId, out authClient))
            {
                var packetManager = new PacketManager(authClient, clientSocket, message);
                var packet        = packetManager.GetPacket();
                packet?.HandlePacket();
            }
        }
Exemple #3
0
        /// <summary>
        /// Handles encrypted binary messages
        /// </summary>
        /// <param name="websocket"></param>
        /// <param name="message"></param>
        private static void HandleEncryptedMessage(WebSocket websocket, byte[] message)
        {
            var        authKey = websocket.GetHashCode().ToString();
            AuthClient authClient;

            if (AllClients.TryGetValue(authKey, out authClient))
            {
                var packetManager = new PacketManager(authClient, message);
                var packet        = packetManager.GetPacket();
                packet?.HandlePacket();
            }
        }
Exemple #4
0
        /// <summary>
        ///     When a client connects, assign them a unique RSA keypair for handshake.
        /// </summary>
        /// <param name="clientSocket"></param>
        private static async void HandleConnect(WebSocket clientSocket)
        {
            var        connectionId = CookieManager.GetConnectionId(clientSocket);
            AuthClient authClient;

            AllClients.TryGetValue(connectionId, out authClient);
            var host = new Uri($"ws://{clientSocket.HttpRequest.Headers[RequestHeader.Host]}", UriKind.Absolute);

            if (authClient != null)
            {
                if (RunningAsService)
                {
                    MessageQueueManager agentManager;
                    if (!authClient.MessageQueueManagers.TryGetValue(22005, out agentManager))
                    {
                        if (authClient.MessageQueueManagers.TryAdd(22005,
                                                                   new MessageQueueManager()))
                        {
                            Console.WriteLine("Service Manager Started");
                        }
                    }
                }
                MessageQueueManager manager;
                //check if a manager for that port exist, if not, create one

                if (!authClient.MessageQueueManagers.TryGetValue(host.Port, out manager))
                {
                    if (authClient.MessageQueueManagers.TryAdd(host.Port,
                                                               new MessageQueueManager()))
                    {
                        Console.WriteLine($"Manager started for {host.Port}");
                    }
                }
                return;
            }
            Console.WriteLine("Connection from " + clientSocket.RemoteEndpoint);
            var rsa = new Rsa();

            rsa.GenerateKeyPairs();
            var client = new AuthClient
            {
                PublicKey            = rsa.PublicKey,
                PrivateKey           = rsa.PrivateKey,
                MessageQueueManagers = new ConcurrentDictionary <int, MessageQueueManager>()
            };

            client.MessageQueueManagers.TryAdd(host.Port, new MessageQueueManager());
            AllClients.AddOrUpdate(connectionId, client, (key, value) => value);
            await SendWelcomeMessage(client, clientSocket);
        }