fix crash in authentication

This commit is contained in:
Lion Kortlepel 2023-12-03 17:43:41 +01:00
parent 47e64a7343
commit 22805af716
No known key found for this signature in database
GPG Key ID: 4322FF2B4C71259B

View File

@ -177,16 +177,29 @@ void TNetwork::Identify(TConnection&& RawConnection) {
return; return;
} }
std::shared_ptr<TClient> Client { nullptr }; std::shared_ptr<TClient> Client { nullptr };
if (Code == 'C') { try {
Client = Authentication(std::move(RawConnection)); if (Code == 'C') {
} else if (Code == 'D') { Client = Authentication(std::move(RawConnection));
HandleDownload(std::move(RawConnection)); } else if (Code == 'D') {
} else if (Code == 'P') { HandleDownload(std::move(RawConnection));
} else if (Code == 'P') {
boost::system::error_code ec;
write(RawConnection.Socket, buffer("P"), ec);
return;
} else {
beammp_errorf("Invalid code got in Identify: '{}'", Code);
}
} catch(const std::exception& e) {
beammp_errorf("Error during handling of code {} - client left in invalid state, closing socket", Code);
boost::system::error_code ec; boost::system::error_code ec;
write(RawConnection.Socket, buffer("P"), ec); RawConnection.Socket.shutdown(socket_base::shutdown_both, ec);
return; if (ec) {
} else { beammp_debugf("Failed to shutdown client socket: {}", ec.message());
beammp_errorf("Invalid code got in Identify: '{}'", Code); }
RawConnection.Socket.close(ec);
if (ec) {
beammp_debugf("Failed to close client socket: {}", ec.message());
}
} }
} }
@ -246,12 +259,24 @@ std::shared_ptr<TClient> TNetwork::Authentication(TConnection&& RawConnection) {
return nullptr; return nullptr;
} }
nlohmann::json AuthReq { std::string key(reinterpret_cast<const char*>(Data.data()), Data.size());
{ "key", std::string(reinterpret_cast<const char*>(Data.data()), Data.size()) }
}; nlohmann::json AuthReq{};
auto Target = "/pkToUser"; std::string AuthResStr{};
unsigned int ResponseCode = 0; try {
const auto AuthResStr = Http::POST(Application::GetBackendUrlForAuth(), 443, Target, AuthReq.dump(), "application/json", &ResponseCode); AuthReq = nlohmann::json {
{ "key", key }
};
auto Target = "/pkToUser";
unsigned int ResponseCode = 0;
AuthResStr = Http::POST(Application::GetBackendUrlForAuth(), 443, Target, AuthReq.dump(), "application/json", &ResponseCode);
} catch (const std::exception& e) {
beammp_debugf("Invalid json sent by client, kicking: {}", e.what());
ClientKick(*Client, "Invalid Key (invalid UTF8 string)!");
return nullptr;
}
try { try {
nlohmann::json AuthRes = nlohmann::json::parse(AuthResStr); nlohmann::json AuthRes = nlohmann::json::parse(AuthResStr);