- add hash function

- add password config
- add debug messages for password stages
- add pass boolean for heartbeat
- adjust network codes
This commit is contained in:
Anonymous275
2022-12-21 22:26:45 +00:00
committed by Simon
parent 6a11bcd20b
commit 9f59c27b1f
5 changed files with 37 additions and 2 deletions
+29 -1
View File
@@ -235,7 +235,8 @@ std::shared_ptr<TClient> TNetwork::Authentication(TConnection&& RawConnection) {
ClientKick(*Client, fmt::format("Invalid version header: '{}' ({})", std::string(reinterpret_cast<const char*>(Data.data()), Data.size()), Data.size()));
return nullptr;
}
if (!TCPSend(*Client, StringToVector("S"))) {
if (!TCPSend(*Client, StringToVector("A"))) { //changed to A for Accepted version
// TODO: handle
}
@@ -322,12 +323,30 @@ std::shared_ptr<TClient> TNetwork::Authentication(TConnection&& RawConnection) {
}
if (mServer.ClientCount() < size_t(Application::Settings.MaxPlayers)) {
if(!Application::Settings.Password.empty()) { // ask password
if(!TCPSend(*Client, StringToVector("S"))) {
// TODO: handle
}
beammp_info("Waiting for password");
Data = TCPRcv(*Client);
std::string Pass = std::string(reinterpret_cast<const char*>(Data.data()), Data.size());
if(Pass != Hash(Application::Settings.Password)) {
beammp_debug(Client->GetName() + " attempted to connect with a wrong password");
ClientKick(*Client, "Wrong password!");
return {};
} else {
beammp_debug(Client->GetName() + " used the correct password");
}
}
beammp_info("Identification success");
mServer.InsertClient(Client);
TCPClient(Client);
} else {
ClientKick(*Client, "Server full!");
}
return Client;
}
@@ -943,3 +962,12 @@ std::vector<uint8_t> TNetwork::UDPRcvFromClient(ip::udp::endpoint& ClientEndpoin
beammp_assert(Rcv <= Ret.size());
return std::vector<uint8_t>(Ret.begin(), Ret.begin() + Rcv);
}
std::string TNetwork::Hash(const std::string& str) {
std::stringstream ret;
unsigned char* hash = SHA256(reinterpret_cast<const unsigned char*>(str.c_str()), str.length(), nullptr);
for (int i = 0; i < 32; i++) {
ret << std::hex << (int)hash[i];
}
return ret.str();
}