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 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);
            }
        }