Beispiel #1
0
        public void Login()
        {
            var authenticated = false;
            var connectionId  = CookieManager.GetConnectionId(_client);
            var password      = _packet.Args[0].ToString();

            authenticated = !string.IsNullOrEmpty(password) && AuthUtils.Authenticate(password);
            AuthClient authClient;

            UlteriusApiServer.AllClients.TryGetValue(connectionId, out authClient);
            if (authClient != null)
            {
                if (authClient.Authenticated)
                {
                    _builder.WriteMessage(new
                    {
                        authenticated,
                        message = "Already logged in."
                    });
                    return;
                }
                authClient.Authenticated = authenticated;
                UlteriusApiServer.AllClients[connectionId] = authClient;
            }
            var authenticationData = new
            {
                authenticated,
                message = authenticated ? "Login was successful" : "Login was unsuccessful"
            };

            _builder.WriteMessage(authenticationData);
        }
Beispiel #2
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);
        }
Beispiel #3
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();
            }
        }
Beispiel #4
0
        /// <summary>
        ///     Remove a client when it disconnects
        /// </summary>
        /// <param name="clientSocket"></param>
        private static void HandleDisconnect(WebSocket clientSocket)
        {
            var        connectionId = CookieManager.GetConnectionId(clientSocket);
            AuthClient temp         = null;

            if (AllClients.TryRemove(connectionId, out temp))
            {
                Console.WriteLine("Disconnection from " + clientSocket.RemoteEndpoint);
                var userCount = AllClients.Count;
                var extra     = userCount < 1 ? "s" : string.Empty;
                UlteriusTray.ShowMessage($"There are now {userCount} user{extra} connected.", "A user disconnected!");
            }
        }
Beispiel #5
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);
        }
 public void Login()
 {
     try
     {
         var authenticated = false;
         var connectionId  = CookieManager.GetConnectionId(_client);
         var username      = _packet.Args[0].ToString();
         var password      = _packet.Args[1].ToString();
         var loginInfo     = AuthUtils.AuthWindows(username, password);
         if (!loginInfo.LoggedIn)
         {
             throw new Exception(loginInfo.Message);
         }
         if (loginInfo.LoggedIn && !loginInfo.IsAdmin)
         {
             throw new Exception($"The account {username} is not a Windows administrator.");
         }
         authenticated = loginInfo.LoggedIn && loginInfo.IsAdmin;
         AuthClient authClient;
         UlteriusApiServer.AllClients.TryGetValue(connectionId, out authClient);
         if (authClient != null)
         {
             if (authClient.Authenticated)
             {
                 throw new Exception("A login session is already active for this Ulterius ID");
             }
             authClient.Authenticated = authenticated;
             UlteriusApiServer.AllClients[connectionId] = authClient;
         }
         if (authenticated)
         {
             AppEnvironment.Setting("LastUsername", username);
         }
         var authenticationData = new
         {
             authenticated,
             message = authenticated ? "Login was successful" : "Login was unsuccessful"
         };
         _builder.WriteMessage(authenticationData);
     }
     catch (Exception ex)
     {
         var authenticationData = new
         {
             authenticated = false,
             message       = ex.Message
         };
         _builder.WriteMessage(authenticationData);
     }
 }
Beispiel #7
0
 public void AesHandshake()
 {
     try
     {
         var        connectionId = CookieManager.GetConnectionId(_client);
         AuthClient authClient;
         UlteriusApiServer.AllClients.TryGetValue(connectionId, out authClient);
         if (authClient != null)
         {
             var privateKey   = authClient.PrivateKey;
             var encryptedKey = _packet.Args[0].ToString();
             var encryptedIv  = _packet.Args[1].ToString();
             authClient.AesKey   = Rsa.Decryption(privateKey, encryptedKey);
             authClient.AesIv    = Rsa.Decryption(privateKey, encryptedIv);
             authClient.AesShook = true;
             //update the auth client
             UlteriusApiServer.AllClients[connectionId] = authClient;
             var endData = new
             {
                 shook = true
             };
             _builder.WriteMessage(endData);
         }
         else
         {
             throw new Exception("AuthWindows client is null");
         }
     }
     catch (Exception e)
     {
         var endData = new
         {
             shook   = false,
             message = e.Message
         };
         _builder.WriteMessage(endData);
     }
 }