From e83b3b3ce1697fd08b5ebcb8375698508e15352c Mon Sep 17 00:00:00 2001 From: MihailRis Date: Thu, 9 Oct 2025 02:13:03 +0300 Subject: [PATCH] add protocol version check --- src/devtools/DebuggingServer.cpp | 31 +++++++++++++++++++++++++++++-- src/devtools/DebuggingServer.hpp | 6 ++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/devtools/DebuggingServer.cpp b/src/devtools/DebuggingServer.cpp index b6a67c7e..09a2b368 100644 --- a/src/devtools/DebuggingServer.cpp +++ b/src/devtools/DebuggingServer.cpp @@ -17,6 +17,27 @@ ClientConnection::~ClientConnection() { } } +bool ClientConnection::initiate(network::ReadableConnection* connection) { + if (connection->available() < 8) { + return false; + } + char buffer[8] {}; + char expected[8] {}; + std::memcpy(expected, VCDBG_MAGIC, sizeof(VCDBG_MAGIC)); + expected[6] = VCDBG_VERSION >> 8; + expected[7] = VCDBG_VERSION & 0xFF; + connection->recv(buffer, sizeof(VCDBG_MAGIC)); + + connection->send(expected, sizeof(VCDBG_MAGIC)); + if (std::memcmp(expected, buffer, sizeof(VCDBG_MAGIC)) == 0) { + initiated = true; + return false; + } else { + connection->close(true); + return true; + } +} + std::string ClientConnection::read() { auto connection = dynamic_cast( network.getConnection(this->connection, true) @@ -24,6 +45,11 @@ std::string ClientConnection::read() { if (connection == nullptr) { return ""; } + if (!initiated) { + if (initiate(connection)) { + return ""; + } + } if (messageLength == 0) { if (connection->available() >= sizeof(int32_t)) { int32_t length = 0; @@ -166,7 +192,7 @@ bool DebuggingServer::update() { bool DebuggingServer::performCommand( const std::string& type, const dv::value& map ) { - if (type == "connect" && !connectionEstablished) { + if (!connectionEstablished && type == "connect") { map.at("disconnect-action").get(disconnectAction); connectionEstablished = true; logger.info() << "client connection established"; @@ -275,8 +301,9 @@ void DebuggingServer::sendValue( } void DebuggingServer::setClient(u64id_t client) { - this->connection = + connection = std::make_unique(engine.getNetwork(), client); + connectionEstablished = false; } std::vector DebuggingServer::pullEvents() { diff --git a/src/devtools/DebuggingServer.hpp b/src/devtools/DebuggingServer.hpp index 533453d7..b02229c6 100644 --- a/src/devtools/DebuggingServer.hpp +++ b/src/devtools/DebuggingServer.hpp @@ -21,6 +21,9 @@ namespace dv { class Engine; namespace devtools { + inline constexpr const char VCDBG_MAGIC[8] = "vc-dbg\0"; + inline constexpr int VCDBG_VERSION = 1; + class ClientConnection { public: ClientConnection(network::Network& network, u64id_t connection) @@ -37,6 +40,9 @@ namespace devtools { network::Network& network; size_t messageLength = 0; u64id_t connection; + bool initiated = false; + + bool initiate(network::ReadableConnection* connection); }; enum class DebuggingEventType {