<commit_before>﻿#region

using System;
using System.Diagnostics;
using System.DirectoryServices.AccountManagement;
using System.DirectoryServices.ActiveDirectory;
using System.IO;
using System.Reflection;
using UlteriusServer.Api.Network.Messages;
using UlteriusServer.Utilities.Security;
using UlteriusServer.WebSocketAPI.Authentication;

#endregion

namespace UlteriusServer.Api.Network.PacketHandlers
{
    public class ServerPacketHandler : PacketHandler
    {
        private AuthClient _authClient;
        private MessageBuilder _builder;
        private Packet _packet;


        public void AesHandshake()
        {
            try
            {
                var authKey = _authClient.Client.GetHashCode().ToString();
                AuthClient authClient;
                UlteriusApiServer.AllClients.TryGetValue(authKey, 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[authKey] = authClient;
                    var endData = new
                    {
                        shook = true
                    };
                    _builder.WriteMessage(endData);
                }
                else
                {
                    throw new Exception("Auth client is null");
                }
            }
            catch (Exception e)
            {
                var endData = new
                {
                    shook = false,
                    message = e.Message
                };
                _builder.WriteMessage(endData);
            }
        }

        private string GetUsername()
        {
            return Environment.UserName;
        }


        public void Login()
        {
            

            var password = _packet.Args[0].ToString();
            var authenticated = WindowsAuth.Auth(password);
            var authKey = _authClient.Client.GetHashCode().ToString();
            AuthClient authClient;
            UlteriusApiServer.AllClients.TryGetValue(authKey, out authClient);
            if (authClient != null)
            {
                if (authClient.Authenticated)
                {
                    _builder.WriteMessage(new
                    {
                        authenticated,
                        message = "Already logged in."
                    });
                    return;
                }
                authClient.Authenticated = authenticated;
                UlteriusApiServer.AllClients[authKey] = authClient;
            }
            var authenticationData = new
            {
                authenticated,
                message = authenticated ? "Login was successfull" : "Login was unsuccessful"
            };
            _builder.WriteMessage(authenticationData);
        }

        public void CheckForUpdate()
        {
        }

        public void RestartServer()
        {
            var data = new
            {
                serverRestarting = true
            };
            _builder.WriteMessage(data);
            // Starts a new instance of the program itself
            var fileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Bootstrapper.exe");
            Process.Start(fileName);<commit_msg>Update ServerPacketHandler.cs<commit_after>﻿#region

using System;
using System.Diagnostics;
using System.DirectoryServices.AccountManagement;
using System.DirectoryServices.ActiveDirectory;
using System.IO;
using System.Reflection;
using UlteriusServer.Api.Network.Messages;
using UlteriusServer.Utilities.Security;
using UlteriusServer.WebSocketAPI.Authentication;

#endregion

namespace UlteriusServer.Api.Network.PacketHandlers
{
    public class ServerPacketHandler : PacketHandler
    {
        private AuthClient _authClient;
        private MessageBuilder _builder;
        private Packet _packet;


        public void AesHandshake()
        {
            try
            {
                var authKey = _authClient.Client.GetHashCode().ToString();
                AuthClient authClient;
                UlteriusApiServer.AllClients.TryGetValue(authKey, 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[authKey] = authClient;
                    var endData = new
                    {
                        shook = true
                    };
                    _builder.WriteMessage(endData);
                }
                else
                {
                    throw new Exception("Auth client is null");
                }
            }
            catch (Exception e)
            {
                var endData = new
                {
                    shook = false,
                    message = e.Message
                };
                _builder.WriteMessage(endData);
            }
        }

        private string GetUsername()
        {
            return Environment.UserName;
        }


        public void Login()
        {
            

            var password = _packet.Args[0].ToString();
            var authenticated = WindowsAuth.Auth(password);
            var authKey = _authClient.Client.GetHashCode().ToString();
            AuthClient authClient;
            UlteriusApiServer.AllClients.TryGetValue(authKey, out authClient);
            if (authClient != null)
            {
                if (authClient.Authenticated)
                {
                    _builder.WriteMessage(new
                    {
                        authenticated,
                        message = "Already logged in."
                    });
                    return;
                }
                authClient.Authenticated = authenticated;
                UlteriusApiServer.AllClients[authKey] = authClient;
            }
            var authenticationData = new
            {
                authenticated,
                message = authenticated ? "Login was successful" : "Login was unsuccessful"
            };
            _builder.WriteMessage(authenticationData);
        }

        public void CheckForUpdate()
        {
        }

        public void RestartServer()
        {
            var data = new
            {
                serverRestarting = true
            };
            _builder.WriteMessage(data);
            // Starts a new instance of the program itself
            var fileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Bootstrapper.exe");
            Process.Start(fileName);
{'fraction_non_alphanumeric': 0.0717664549263414, 'fraction_numerical': 0.0008109203946479254, 'mean_word_length': 1.3619533992977977, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '138527', 'n_tokens_mistral': 1779, 'n_tokens_neox': 1662, 'n_words': 346}