Exemple #1
0
 public virtual void OnEncryptionSetup(string response, ref ConnectionData connection)
 {
     if (Program.passwordProtected)
     {
         try
         {
             OnPresharedAESCheck(PresharedKeyEncryption.AESDecrypt(Convert.FromBase64String(response), PresharedKeyEncryption.GetAESHash(Program.password)), ref connection);
         }
         catch
         {
             connection.SendMessage("Failed authentication, please try again. If you know the password you entered is correct, please contact your server admin.");
         }
     }
     else
     {
         OnNoEncryption(ref connection);
     }
 }
Exemple #2
0
 public virtual void OnPresharedAESCheck(string passwordToCheck, ref ConnectionData connection)
 {
     if (passwordToCheck == Program.password)
     {
         Debug.Log("A user has established communication. Waiting for a username...", "User Login Success");
         connection.SetStage(ConnectionData.ConnectionStage.AwaitingNickname);
         connection.SendMessage("V");
         string newKey = Convert.ToBase64String(PresharedKeyEncryption.GenerateAESKey());
         connection.SetAESKey(PresharedKeyEncryption.GetAESHash(Program.password));
         connection.SendMessage(newKey);
         connection.SetAESKey(newKey);
         connection.SendMessage("Welcome to the server client! Please send your nickname.");
     }
     else
     {
         Debug.Log("A user tried to connect with a password which didn't match. Notifying user.", "User Login Failed");
         connection.SendMessage("Your password was invalid. Please try again.");
     }
 }
Exemple #3
0
 public void SendMessage(string data)
 {
     Program.server.Send(connectionId,
                         Encoding.UTF8.GetBytes(
                             (aesCommunicationKey != null) ? Convert.ToBase64String(PresharedKeyEncryption.AESEncrypt(data, aesCommunicationKey)) : data
                             ));
 }
Exemple #4
0
        public void Loop()
        {
            bool serverRun = true;

            while (serverRun)
            {
                while (server.GetNextMessage(out Telepathy.Message msg))
                {
                    switch (msg.eventType)
                    {
                    case Telepathy.EventType.Connected:
                        handler.OnConnect(msg.connectionId);
                        break;

                    case Telepathy.EventType.Data:
                        ConnectionData connection = connections[msg.connectionId];

                        string msgContents = (connection.GetAESKey() != null) ?
                                             PresharedKeyEncryption.AESDecrypt(Convert.FromBase64String(Encoding.UTF8.GetString(msg.data)), connection.GetAESKey()) :
                                             Encoding.UTF8.GetString(msg.data);

                        try
                        {
                            switch (connection.GetConnectionStage())
                            {
                            case ConnectionData.ConnectionStage.AwaitingEncryptionSupport:
                                handler.OnEncryptionSupported(msgContents, ref connection);
                                break;

                            case ConnectionData.ConnectionStage.AwaitingEncryptionSetup:
                                handler.OnEncryptionSetup(msgContents, ref connection);
                                break;

                            case ConnectionData.ConnectionStage.AwaitingNickname:
                                handler.OnUsernameSetAttempt(msgContents, ref connection);
                                break;

                            case ConnectionData.ConnectionStage.ConnectionEstablished:
                                handler.OnMessageReceived(msgContents, ref connection);
                                break;

                            default:
                                Debug.Log($"Client connection state {connection.GetConnectionStage().ToString()} is not supported!", "Missing Support");
                                break;
                            }

                            connections[connection.GetConnectionId()] = connection;
                        }
                        catch (Exception e)
                        {
                            Debug.Log($"Exception caused by message with contents {msg.data}. Exception info: {e.ToString()}", "Caught Exception, On Message Received");
                            KickUser(connection, "Caused server error");
                        }
                        break;

                    case Telepathy.EventType.Disconnected:
                        handler.OnDisconnected(msg.connectionId);
                        break;
                    }
                }

                Thread.Sleep(100);
            }
        }