streamline login

This commit is contained in:
Lion Kortlepel 2024-03-03 12:58:07 +01:00
parent fe9b2ad0f4
commit 920f682284
No known key found for this signature in database
GPG Key ID: 4322FF2B4C71259B
4 changed files with 48 additions and 15 deletions

View File

@ -79,8 +79,14 @@ void ClientNetwork::handle_connection(ip::tcp::socket&& socket) {
// packet read and respond loop // packet read and respond loop
while (!*m_shutdown) { while (!*m_shutdown) {
auto packet = client_tcp_read(); try {
handle_packet(packet); auto packet = client_tcp_read();
handle_packet(packet);
} catch (const std::exception& e) {
spdlog::error("Unhandled exception in connection handler, connection closing.");
spdlog::debug("Exception: {}", e.what());
m_game_socket.close();
}
} }
} }
@ -151,11 +157,6 @@ void ClientNetwork::handle_client_identification(bmp::ClientPacket& packet) {
spdlog::error("Failed to read json for purpose 0x{:x}: {}", uint16_t(packet.purpose), e.what()); spdlog::error("Failed to read json for purpose 0x{:x}: {}", uint16_t(packet.purpose), e.what());
disconnect(fmt::format("Invalid json in purpose 0x{:x}, see launcher logs for more info", uint16_t(packet.purpose))); disconnect(fmt::format("Invalid json in purpose 0x{:x}, see launcher logs for more info", uint16_t(packet.purpose)));
} }
bmp::ClientPacket state_change {
.purpose = bmp::ClientPurpose::StateChangeLogin,
};
client_tcp_write(state_change);
start_login(); start_login();
break; break;
} }
@ -166,28 +167,49 @@ void ClientNetwork::handle_client_identification(bmp::ClientPacket& packet) {
} }
void ClientNetwork::start_login() { void ClientNetwork::start_login() {
bmp::ClientPacket state_change {
.purpose = bmp::ClientPurpose::StateChangeLogin,
};
client_tcp_write(state_change);
m_client_state = bmp::ClientState::Login;
if (ident::is_login_cached()) { if (ident::is_login_cached()) {
auto login = ident::login_cached(); auto login = ident::login_cached();
// case in which the login is cached and login was successful:
// we just send the login result and continue to the next state.
if (login.has_value()) { if (login.has_value()) {
*m_identity = login.value();
bmp::ClientPacket login_result { bmp::ClientPacket login_result {
.purpose = bmp::ClientPurpose::LoginResult, .purpose = bmp::ClientPurpose::LoginResult,
.raw_data = json_to_vec({ .raw_data = json_to_vec({
{ "success", true }, { "success", true },
{ "message", login.value().Message }, { "message", m_identity->Message },
{ "username", login.value().Username }, { "username", m_identity->Username },
{ "role", login.value().Role }, { "role", m_identity->Role },
}), }),
}; };
client_tcp_write(login_result); client_tcp_write(login_result);
bmp::ClientPacket change_to_quickjoin {
.purpose = bmp::ClientPurpose::StateChangeQuickJoin,
};
client_tcp_write(change_to_quickjoin);
m_client_state = bmp::ClientState::QuickJoin;
return; // done
} else {
spdlog::warn("Failed to automatically login with cached/saved login details: {}", login.error());
spdlog::info("Trying normal login");
} }
// fallthrough to normal login
} }
// first packet in login // first packet in login
// TODO: send LoginResult if already logged in. // TODO: send LoginResult if already logged in.
bmp::ClientPacket ask_for_creds { bmp::ClientPacket ask_for_creds {
.purpose = bmp::ClientPurpose::AskForCredentials, .purpose = bmp::ClientPurpose::AskForCredentials,
}; };
client_tcp_write(ask_for_creds); client_tcp_write(ask_for_creds);
// wait for response, so return
} }
void ClientNetwork::disconnect(const std::string& reason) { void ClientNetwork::disconnect(const std::string& reason) {
@ -207,9 +229,21 @@ void ClientNetwork::handle_login(bmp::ClientPacket& packet) {
std::string username = creds.at("username"); std::string username = creds.at("username");
std::string password = creds.at("password"); std::string password = creds.at("password");
bool remember = creds.at("remember"); bool remember = creds.at("remember");
// TODO: Respect 'remember'
spdlog::debug("Got credentials username: '{}', password: ({} chars) (remember: {})", username, password.size(), remember ? "yes" : "no"); spdlog::debug("Got credentials username: '{}', password: ({} chars) (remember: {})", username, password.size(), remember ? "yes" : "no");
// CONTINUE HERE // login!
auto result = ident::login(username, password, remember);
if (result.has_error()) {
bmp::ClientPacket login_fail {
.purpose = bmp::ClientPurpose::LoginResult,
.raw_data = json_to_vec({
{ "success", false },
{ "message", result.error() },
}),
};
client_tcp_write(login_fail);
return;
}
*m_identity = result.value();
} catch (const std::exception& e) { } catch (const std::exception& e) {
spdlog::error("Failed to read json for purpose 0x{:x}: {}", uint16_t(packet.purpose), e.what()); spdlog::error("Failed to read json for purpose 0x{:x}: {}", uint16_t(packet.purpose), e.what());
disconnect(fmt::format("Invalid json in purpose 0x{:x}, see launcher logs for more info", uint16_t(packet.purpose))); disconnect(fmt::format("Invalid json in purpose 0x{:x}, see launcher logs for more info", uint16_t(packet.purpose)));

View File

@ -45,7 +45,7 @@ private:
Version m_mod_version; Version m_mod_version;
Version m_game_version; Version m_game_version;
ident::Identity m_identity {}; Sync<ident::Identity> m_identity {};
uint16_t m_listen_port {}; uint16_t m_listen_port {};
io_context m_io {}; io_context m_io {};

View File

@ -92,7 +92,7 @@ Result<ident::Identity, std::string> ident::detail::login(const std::string& jso
return id; return id;
} else { } else {
spdlog::info("Auto-Authentication unsuccessful please re-login!"); spdlog::info("Auto-Authentication unsuccessful please re-login!");
return std::string("Failed to auto-login with saved details, please login again."); return json["message"].is_string() ? json["message"].get<std::string>() : "Unknown error, please log in again.";
} }
} catch (const std::exception& e) { } catch (const std::exception& e) {
spdlog::error("Incomplete or invalid answer from auth servers. Please try logging in again."); spdlog::error("Incomplete or invalid answer from auth servers. Please try logging in again.");

View File

@ -53,7 +53,6 @@ private:
boost::scoped_thread<> m_udp_thread; boost::scoped_thread<> m_udp_thread;
boost::scoped_thread<> m_ping_thread; boost::scoped_thread<> m_ping_thread;
Sync<Identity> m_identity {};
Sync<std::string> m_exe_name; Sync<std::string> m_exe_name;
Sync<std::filesystem::path> m_exe_path; Sync<std::filesystem::path> m_exe_path;
boost::asio::io_context m_io {}; boost::asio::io_context m_io {};