Prepare auth data

This commit is contained in:
Maurice Heumann 2023-04-03 19:03:54 +02:00
parent 859bd373c7
commit 27ab6193e5
6 changed files with 217 additions and 9 deletions

View File

@ -11,6 +11,7 @@
#include <utils/hook.hpp> #include <utils/hook.hpp>
#include <utils/string.hpp> #include <utils/string.hpp>
#include <utils/smbios.hpp> #include <utils/smbios.hpp>
#include <utils/byte_buffer.hpp>
#include <utils/info_string.hpp> #include <utils/info_string.hpp>
#include <utils/cryptography.hpp> #include <utils/cryptography.hpp>
@ -94,6 +95,14 @@ namespace auth
return !is_first; return !is_first;
} }
std::string serialize_connect_data(const std::vector<char>& data)
{
utils::byte_buffer buffer{};
buffer.write_vector(data);
return buffer.move_buffer();
}
int send_connect_data_stub(const game::netsrc_t sock, game::netadr_t* adr, const char* data, int len) int send_connect_data_stub(const game::netsrc_t sock, game::netadr_t* adr, const char* data, int len)
{ {
std::string buffer{}; std::string buffer{};
@ -101,8 +110,12 @@ namespace auth
const auto is_connect_sequence = len >= 7 && strncmp("connect", data, 7) == 0; const auto is_connect_sequence = len >= 7 && strncmp("connect", data, 7) == 0;
if (is_connect_sequence) if (is_connect_sequence)
{ {
buffer.append("connect "); std::vector<char> connect_data{};
buffer.append(data, len); connect_data.assign(data, data + len);
buffer.append("connect");
buffer.push_back(' ');
buffer.append(serialize_connect_data(connect_data));
data = buffer.data(); data = buffer.data();
len = static_cast<int>(buffer.size()); len = static_cast<int>(buffer.size());
@ -113,10 +126,10 @@ namespace auth
void handle_connect_packet(const game::netadr_t& target, const network::data_view& data) void handle_connect_packet(const game::netadr_t& target, const network::data_view& data)
{ {
const std::string text(data.begin(), data.end()); utils::byte_buffer buffer(data);
command::params_sv params(text);
MessageBoxA(0, "Connecting", 0, 0); const auto text = buffer.read_vector<char>();
command::params_sv params(std::string(text.data(), text.size()));
game::SV_DirectConnect(target); game::SV_DirectConnect(target);
} }

View File

@ -35,7 +35,18 @@ namespace network
const std::basic_string_view data(message->data + offset, message->cursize - offset); const std::basic_string_view data(message->data + offset, message->cursize - offset);
try
{
handler->second(*address, data); handler->second(*address, data);
}
catch (const std::exception& e)
{
printf("Error: %s\n", e.what());
}
catch (...)
{
}
return 0; return 0;
} }

View File

@ -55,7 +55,8 @@ namespace party
} }
void connect_to_lobby_with_mode(const game::netadr_t& addr, const game::eModes mode, const std::string& mapname, void connect_to_lobby_with_mode(const game::netadr_t& addr, const game::eModes mode, const std::string& mapname,
const std::string& gametype, const std::string& pub_id, const bool was_retried = false) const std::string& gametype, const std::string& pub_id,
const bool was_retried = false)
{ {
if (game::Com_SessionMode_IsMode(mode)) if (game::Com_SessionMode_IsMode(mode))
{ {
@ -144,6 +145,13 @@ namespace party
is_connecting_to_dedi = info.get("dedicated") == "1"; is_connecting_to_dedi = info.get("dedicated") == "1";
if (atoi(info.get("protocol").data()) != PROTOCOL)
{
const auto str = "Invalid protocol.";
printf("%s\n", str);
return;
}
const auto gamename = info.get("gamename"); const auto gamename = info.get("gamename");
if (gamename != "T7"s) if (gamename != "T7"s)
{ {

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#define PROTOCOL 1 #define PROTOCOL 2
#ifdef __cplusplus #ifdef __cplusplus
namespace game namespace game

View File

@ -0,0 +1,85 @@
#include "byte_buffer.hpp"
#include <cstring>
#include <stdexcept>
namespace utils
{
byte_buffer::byte_buffer()
: writing_(true)
{
}
byte_buffer::byte_buffer(std::string buffer)
: writing_(false)
, buffer_(std::move(buffer))
{
}
void byte_buffer::write(const void* buffer, const size_t length)
{
if (!writing_)
{
throw std::runtime_error("Writing to readable byte buffer");
}
buffer_.append(static_cast<const char*>(buffer), length);
}
void byte_buffer::read(void* data, const size_t length)
{
if (writing_)
{
throw std::runtime_error("Reading from writable byte buffer");
}
if (offset_ + length > buffer_.size())
{
throw std::runtime_error("Out of bounds read from byte buffer");
}
memcpy(data, buffer_.data() + offset_, length);
offset_ += length;
}
std::string byte_buffer::read_string()
{
std::string result{};
while (true)
{
const auto b = read<char>();
if (!b)
{
break;
}
result.push_back(b);
}
return result;
}
std::string byte_buffer::read_string(const size_t length)
{
std::string result{};
result.reserve(length);
for (size_t i = 0; i < length; ++i)
{
result.push_back(read<char>());
}
return result;
}
std::vector<uint8_t> byte_buffer::read_data(const size_t length)
{
std::vector<uint8_t> result{};
result.resize(length);
read(result.data(), result.size());
return result;
}
}

View File

@ -0,0 +1,91 @@
#pragma once
#include <string>
#include <vector>
namespace utils
{
class byte_buffer
{
public:
byte_buffer();
byte_buffer(std::string buffer);
template <typename T>
byte_buffer(const std::basic_string_view<T>& buffer)
: byte_buffer(std::string(reinterpret_cast<const char*>(buffer.data()), buffer.size() * sizeof(T)))
{
}
void write(const void* buffer, size_t length);
void write(const std::string& string, const bool null_terminate = false)
{
const size_t addend = null_terminate ? 1 : 0;
write(string.data(), string.size() + addend);
}
void write_string(const std::string& string)
{
write(string, true);
}
template <typename T>
void write(const T& object)
{
write(&object, sizeof(object));
}
template <typename T>
void write(const std::vector<T>& vec)
{
write(vec.data(), vec.size() * sizeof(T));
}
template <typename T>
void write_vector(const std::vector<T>& vec)
{
write<uint32_t>(static_cast<uint32_t>(vec.size()));
write(vec);
}
const std::string& get_buffer() const
{
return buffer_;
}
std::string move_buffer()
{
return std::move(buffer_);
}
void read(void* data, size_t length);
template <typename T>
T read()
{
T object{};
read(&object, sizeof(object));
return object;
}
template <typename T>
std::vector<T> read_vector()
{
std::vector<T> result{};
result.resize(read<uint32_t>());
read(result.data(), result.size() * sizeof(T));
return result;
}
std::string read_string();
std::string read_string(size_t length);
std::vector<uint8_t> read_data(size_t length);
private:
bool writing_{false};
size_t offset_{0};
std::string buffer_{};
};
}