Initial Commit
This commit is contained in:
34
src/common/libsodium_wrapper.cpp
Normal file
34
src/common/libsodium_wrapper.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
// libsodium_wrapper.cpp - Libsodium Wrapper for ColumnLynx
|
||||
// Copyright (C) 2025 DcruBro
|
||||
// Distributed under the GPLv3 license. See LICENSE for details.
|
||||
|
||||
#include <columnlynx/common/libsodium_wrapper.hpp>
|
||||
|
||||
namespace ColumnLynx::Utils {
|
||||
|
||||
LibSodiumWrapper::LibSodiumWrapper() {
|
||||
if (sodium_init() < 0) {
|
||||
throw std::runtime_error("Failed to initialize libsodium");
|
||||
}
|
||||
|
||||
if (crypto_kx_keypair(mPublicKey, mPrivateKey) != 0) {
|
||||
throw std::runtime_error("Failed to generate key pair");
|
||||
}
|
||||
|
||||
log("Libsodium initialized and keypair generated");
|
||||
}
|
||||
|
||||
uint8_t* LibSodiumWrapper::getPublicKey() {
|
||||
return mPublicKey;
|
||||
}
|
||||
|
||||
uint8_t* LibSodiumWrapper::getPrivateKey() {
|
||||
return mPrivateKey;
|
||||
}
|
||||
|
||||
uint8_t LibSodiumWrapper::generateRandomAESKey() {
|
||||
uint8_t aesKey[32]; // 256-bit key
|
||||
randombytes_buf(aesKey, sizeof(aesKey));
|
||||
return *aesKey;
|
||||
}
|
||||
}
|
||||
103
src/common/tcp_message_handler.cpp
Normal file
103
src/common/tcp_message_handler.cpp
Normal file
@@ -0,0 +1,103 @@
|
||||
// tcp_message_handler.cpp - TCP Message Handler for ColumnLynx
|
||||
// Copyright (C) 2025 DcruBro
|
||||
// Distributed under the GPLv3 license. See LICENSE for details.
|
||||
|
||||
#include <columnlynx/common/net/tcp/tcp_message_handler.hpp>
|
||||
#include <columnlynx/common/net/tcp/net_helper.hpp>
|
||||
#include <columnlynx/common/utils.hpp>
|
||||
|
||||
namespace ColumnLynx::Net::TCP {
|
||||
void MessageHandler::start() {
|
||||
mReadHeader();
|
||||
}
|
||||
void MessageHandler::sendMessage(AnyMessageType type, const std::string &payload) {
|
||||
// Type is a variant between ServerMessageType and ClientMessageType
|
||||
// Convert to uint8_t dynamically
|
||||
uint8_t typeByte = std::visit([](auto type) -> uint8_t {
|
||||
return static_cast<uint8_t>(type);
|
||||
}, type);
|
||||
|
||||
std::vector<uint8_t> data;
|
||||
data.push_back(typeByte);
|
||||
uint16_t length = payload.size();
|
||||
|
||||
data.push_back(length >> 8);
|
||||
data.push_back(length & 0xFF);
|
||||
|
||||
data.insert(data.end(), payload.begin(), payload.end());
|
||||
auto self = shared_from_this();
|
||||
asio::async_write(mSocket, asio::buffer(data),
|
||||
[self](asio::error_code ec, std::size_t) {
|
||||
if (ec) {
|
||||
Utils::error("Send failed: " + ec.message());
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
void MessageHandler::onMessage(std::function<void(AnyMessageType, std::string)> callback) {
|
||||
mOnMessage = std::move(callback);
|
||||
}
|
||||
|
||||
void MessageHandler::mReadHeader() {
|
||||
auto self = shared_from_this();
|
||||
asio::async_read(mSocket, asio::buffer(mHeader),
|
||||
[this, self](asio::error_code ec, std::size_t) {
|
||||
if (!NetHelper::isExpectedDisconnect(ec)) {
|
||||
mCurrentType = decodeMessageType(mHeader[0]);
|
||||
|
||||
uint16_t len = (mHeader[1] << 8) | mHeader[2];
|
||||
mReadBody(len);
|
||||
} else {
|
||||
Utils::error("Header read failed: " + ec.message());
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
void MessageHandler::mReadBody(uint16_t length) {
|
||||
auto self = shared_from_this();
|
||||
mBody.resize(length);
|
||||
|
||||
asio::async_read(mSocket, asio::buffer(mBody),
|
||||
[this, self](asio::error_code ec, std::size_t) {
|
||||
if (!NetHelper::isExpectedDisconnect(ec)) {
|
||||
std::string payload(mBody.begin(), mBody.end());
|
||||
|
||||
// Dispatch based on message type
|
||||
if (mOnMessage) {
|
||||
mOnMessage(mCurrentType, payload);
|
||||
}
|
||||
|
||||
mReadHeader(); // Keep listening
|
||||
} else {
|
||||
Utils::error("Body read failed: " + ec.message());
|
||||
|
||||
if (mOnDisconnect) {
|
||||
mOnDisconnect(ec);
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
AnyMessageType MessageHandler::decodeMessageType(uint8_t code) {
|
||||
switch (code) {
|
||||
case 0xFE: return ServerMessageType::GRACEFUL_DISCONNECT;
|
||||
case 0xFF: return ServerMessageType::KILL_CONNECTION;
|
||||
default: break;
|
||||
}
|
||||
|
||||
if (code >= 0xA0) {
|
||||
return static_cast<ClientMessageType>(code);
|
||||
} else {
|
||||
return static_cast<ServerMessageType>(code);
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t MessageHandler::toUint8(const AnyMessageType& type) {
|
||||
return std::visit([](auto t) -> uint8_t {
|
||||
return static_cast<uint8_t>(t);
|
||||
}, type);
|
||||
}
|
||||
}
|
||||
46
src/common/utils.cpp
Normal file
46
src/common/utils.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
// utils.cpp - Utility functions for ColumnLynx
|
||||
// Copyright (C) 2025 DcruBro
|
||||
// Distributed under the GPLv3 license. See LICENSE for details.
|
||||
|
||||
#include <columnlynx/common/utils.hpp>
|
||||
|
||||
namespace ColumnLynx::Utils {
|
||||
void log(const std::string &msg) {
|
||||
std::cout << "[LOG] " << msg << std::endl;
|
||||
}
|
||||
|
||||
void warn(const std::string &msg) {
|
||||
std::cerr << "[WARN] " << msg << std::endl;
|
||||
}
|
||||
|
||||
void error(const std::string &msg) {
|
||||
std::cerr << "[ERROR] " << msg << std::endl;
|
||||
}
|
||||
|
||||
std::string getHostname() {
|
||||
#ifdef _WIN32
|
||||
char hostname[256];
|
||||
DWORD size = sizeof(hostname);
|
||||
if (GetComputerNameA(hostname, &size)) {
|
||||
return std::string(hostname);
|
||||
} else {
|
||||
return "UnknownHost";
|
||||
}
|
||||
#else
|
||||
char hostname[256];
|
||||
if (gethostname(hostname, sizeof(hostname)) == 0) {
|
||||
return std::string(hostname);
|
||||
} else {
|
||||
return "UnknownHost";
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
std::string getVersion() {
|
||||
return "a0.1";
|
||||
}
|
||||
|
||||
unsigned short serverPort() {
|
||||
return 48042;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user