Exemplo n.º 1
0
        protected virtual void HandleClientHelloResponse(byte[] packet)
        {
            // Get the response
            ToffeePacketIterator iterator = new ToffeePacketIterator(this, packet);
            ClientHelloResponse  response = iterator.ReadStruct <ClientHelloResponse>();

            // Was the request successful?
            if (response.Success)
            {
                // Does this server require a heartbeat?
                if (response.HeartbeatTime != 0)
                {
                    HeartbeatTimer          = new Timer(response.HeartbeatTime * 1000);
                    HeartbeatTimer.Elapsed += SendHeartbeat;
                    HeartbeatTimer.Start();
                }

                // Do we have encryption keys?
                ForceEncryption = response.ForcedEncryption;
                if ((!UseEncryption) && (response.ForcedEncryption))
                {
                    // The server requires encryption that we don't have enabled.
                    Disconnect();
                    return;
                }
            }
            else
            {
                // OnConnectionFailed(this);
                Disconnect();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Handles a ClientHello packet.
        /// </summary>
        /// <param name="session">The session that sent this packet.</param>
        /// <param name="hello">The ClientHello data that was sent.</param>
        private void HandleClientHello(ClientAgentSession sender, ClientHello hello)
        {
            // Logging
            Log?.Info("Handling ClientHello from session {0}.", sender.SessionId);

            // Let's see if this client is valid...
            ClientHelloResponse response = new ClientHelloResponse
            {
                Success          = true,
                ErrorCode        = 0,
                Message          = "",
                HeartbeatTime    = Configuration.HeartbeatTime,
                ForcedEncryption = Configuration.ForceEncryption
            };

            // Get the correct wanted hash
            uint wantedHash = Configuration.Network.Hash != 0 ?
                              Configuration.Network.Hash : NetworkFile.Hash;

            // Is the client the wrong application?
            if (hello.AppName != Configuration.Application.Name)
            {
                response.Success   = false;
                response.ErrorCode = (byte)ToffeeErrorCodes.ClientHelloApplicationInvalid;
                Log?.Warning("Invalid ClientHello from session {0}. ({1})",
                             sender.SessionId, ToffeeErrorCodes.ClientHelloApplicationInvalid);
            }
            // Is the client outdated?
            else if (hello.AppVersion != Configuration.Application.Version)
            {
                response.Success   = false;
                response.ErrorCode = (byte)ToffeeErrorCodes.ClientHelloApplicationOutOfDate;
                Log?.Warning("Invalid ClientHello from session {0}. ({1})",
                             sender.SessionId, ToffeeErrorCodes.ClientHelloApplicationOutOfDate);
            }
            // Is their encryption the same as ours?
            else if ((Configuration.EncryptionKey == 0x00) && (hello.HasEncryption))
            {
                response.Success   = false;
                response.ErrorCode = (byte)ToffeeErrorCodes.ClientHelloEncryptionInvalid;
                response.Message   = "The server is not configured to have encryption.";
                Log?.Warning("Invalid ClientHello from session {0}. ({1}, {2})",
                             sender.SessionId, ToffeeErrorCodes.ClientHelloEncryptionInvalid, response.Message);
            }
            // If we force encryption, do they have encryption?
            else if ((Configuration.ForceEncryption) && (!hello.HasEncryption))
            {
                response.Success   = false;
                response.ErrorCode = (byte)ToffeeErrorCodes.ClientHelloEncryptionInvalid;
                response.Message   = "The server is configured to force encryption.";
                Log?.Warning("Invalid ClientHello from session {0}. ({1}, {2})",
                             sender.SessionId, ToffeeErrorCodes.ClientHelloEncryptionInvalid, response.Message);
            }
            // Do they have the same network hash?
            else if (hello.NetworkHash != wantedHash)
            {
                response.Success   = false;
                response.ErrorCode = (byte)ToffeeErrorCodes.ClientHelloHashInvalid;
                Log?.Warning("Invalid ClientHello from session {0}. ({1})",
                             sender.SessionId, ToffeeErrorCodes.ClientHelloHashInvalid);
            }
            else
            {
                sender.Validated = true;
                Log?.Info("ClientHello was valid! Session {0} has been validated.", sender.SessionId);
            }

            // Send the response!
            sender.Send(ToffeeOpCode.ClientHelloResponse, response);

            // Debug information
            Log?.Debug("=== Response Info ===");
            Log?.Debug("OpCode: 0x02 (ClientHelloResponse)");
            Log?.Debug("Success: {0}", response.Success);
            Log?.Debug("ErrorCode: {0}", response.ErrorCode);
            Log?.Debug("Message: {0}", response.Message);
            Log?.Debug("HeartbeatTime: {0}", response.HeartbeatTime);
            Log?.Debug("ForcedEncryption: {0}", response.ForcedEncryption);
            Log?.Debug("=====================");
            Log?.Debug("");
        }