2014-07-09 19:05:07 -04:00
|
|
|
#pragma once
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "NetMessage.h"
|
2016-02-10 23:07:42 -05:00
|
|
|
#include "EmulationSettings.h"
|
2018-06-16 14:02:12 -04:00
|
|
|
#include "../Utilities/sha1.h"
|
2014-07-09 19:05:07 -04:00
|
|
|
|
|
|
|
class HandShakeMessage : public NetMessage
|
|
|
|
{
|
|
|
|
private:
|
2018-07-01 15:21:05 -04:00
|
|
|
static constexpr int CurrentVersion = 2;
|
2016-02-10 23:07:42 -05:00
|
|
|
uint32_t _mesenVersion = 0;
|
2015-07-01 23:17:14 -04:00
|
|
|
uint32_t _protocolVersion = CurrentVersion;
|
2015-07-11 08:27:22 -04:00
|
|
|
char* _playerName = nullptr;
|
2015-07-01 23:17:14 -04:00
|
|
|
uint32_t _playerNameLength = 0;
|
2018-06-16 14:02:12 -04:00
|
|
|
char* _hashedPassword = nullptr;
|
|
|
|
uint32_t _hashedPasswordLength = 0;
|
2016-02-06 15:33:45 -05:00
|
|
|
bool _spectator = false;
|
2018-06-16 14:02:12 -04:00
|
|
|
|
2014-07-09 19:05:07 -04:00
|
|
|
protected:
|
2015-07-01 23:17:14 -04:00
|
|
|
virtual void ProtectedStreamState()
|
2014-07-09 19:05:07 -04:00
|
|
|
{
|
2016-02-10 23:07:42 -05:00
|
|
|
Stream<uint32_t>(_mesenVersion);
|
2015-07-01 23:17:14 -04:00
|
|
|
Stream<uint32_t>(_protocolVersion);
|
|
|
|
StreamArray((void**)&_playerName, _playerNameLength);
|
2018-06-16 14:02:12 -04:00
|
|
|
StreamArray((void**)&_hashedPassword, _hashedPasswordLength);
|
2016-02-06 15:33:45 -05:00
|
|
|
Stream<bool>(_spectator);
|
2014-07-09 19:05:07 -04:00
|
|
|
}
|
|
|
|
|
2015-07-01 23:17:14 -04:00
|
|
|
public:
|
2018-06-16 14:02:12 -04:00
|
|
|
HandShakeMessage(void* buffer, uint32_t length) : NetMessage(buffer, length) {}
|
|
|
|
HandShakeMessage(string playerName, string hashedPassword, bool spectator) : NetMessage(MessageType::HandShake)
|
2014-07-09 19:05:07 -04:00
|
|
|
{
|
2016-02-10 23:07:42 -05:00
|
|
|
_mesenVersion = EmulationSettings::GetMesenVersion();
|
|
|
|
_protocolVersion = HandShakeMessage::CurrentVersion;
|
2015-07-01 23:17:14 -04:00
|
|
|
CopyString(&_playerName, _playerNameLength, playerName);
|
2018-06-16 14:02:12 -04:00
|
|
|
CopyString(&_hashedPassword, _hashedPasswordLength, hashedPassword);
|
2016-02-06 15:33:45 -05:00
|
|
|
_spectator = spectator;
|
2015-07-01 23:17:14 -04:00
|
|
|
}
|
2016-02-10 23:07:42 -05:00
|
|
|
|
2015-07-11 08:27:22 -04:00
|
|
|
string GetPlayerName()
|
2015-07-01 23:17:14 -04:00
|
|
|
{
|
2015-07-11 08:27:22 -04:00
|
|
|
return string(_playerName);
|
2014-07-09 19:05:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IsValid()
|
|
|
|
{
|
2016-02-10 23:07:42 -05:00
|
|
|
return _protocolVersion == CurrentVersion && _mesenVersion == EmulationSettings::GetMesenVersion();
|
2014-07-09 19:05:07 -04:00
|
|
|
}
|
2016-02-06 15:33:45 -05:00
|
|
|
|
2018-06-16 14:02:12 -04:00
|
|
|
bool CheckPassword(string serverPassword, string connectionHash)
|
|
|
|
{
|
|
|
|
return GetPasswordHash(serverPassword, connectionHash) == string(_hashedPassword);
|
|
|
|
}
|
|
|
|
|
2016-02-06 15:33:45 -05:00
|
|
|
bool IsSpectator()
|
|
|
|
{
|
|
|
|
return _spectator;
|
|
|
|
}
|
2018-06-16 14:02:12 -04:00
|
|
|
|
|
|
|
static string GetPasswordHash(string serverPassword, string connectionHash)
|
|
|
|
{
|
|
|
|
string saltedPassword = serverPassword + connectionHash;
|
|
|
|
vector<uint8_t> dataToHash = vector<uint8_t>(saltedPassword.c_str(), saltedPassword.c_str() + saltedPassword.size());
|
|
|
|
return SHA1::GetHash(dataToHash);
|
|
|
|
}
|
2014-07-09 19:05:07 -04:00
|
|
|
};
|