Initial Commit

This commit is contained in:
2025-11-06 15:51:56 +01:00
commit 0f7191ad54
648 changed files with 170981 additions and 0 deletions

60
src/client/main.cpp Normal file
View File

@@ -0,0 +1,60 @@
// main.cpp - Client entry point for ColumnLynx
// Copyright (C) 2025 DcruBro
// Distributed under the GPLv3 license. See LICENSE for details.
#include <asio/asio.hpp>
#include <csignal>
#include <iostream>
#include <columnlynx/common/utils.hpp>
#include <columnlynx/common/panic_handler.hpp>
#include <columnlynx/client/net/tcp/tcp_client.hpp>
using asio::ip::tcp;
using namespace ColumnLynx::Utils;
volatile sig_atomic_t done = 0;
void signalHandler(int signum) {
if (signum == SIGINT || signum == SIGTERM) {
log("Received termination signal. Shutting down client.");
done = 1;
}
}
int main(int argc, char** argv) {
// Capture SIGINT and SIGTERM for graceful shutdown
struct sigaction action;
memset(&action, 0, sizeof(struct sigaction));
action.sa_handler = signalHandler;
sigaction(SIGINT, &action, nullptr);
sigaction(SIGTERM, &action, nullptr);
PanicHandler::init();
try {
asio::io_context io;
auto client = std::make_shared<ColumnLynx::Net::TCP::TCPClient>(io, "127.0.0.1", std::to_string(serverPort()));
client->start();
// Run the IO context in a separate thread
std::thread ioThread([&io]() {
io.run();
});
ioThread.detach();
log("Client connected to 127.0.0.1:" + std::to_string(serverPort()));
// Client is running
while (!done) {
std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Temp wait
}
log("Client shutting down.");
client->disconnect();
io.stop();
ioThread.join();
} catch (const std::exception& e) {
error("Client error: " + std::string(e.what()));
}
}

View 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;
}
}

View 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
View 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;
}
}

39
src/server/main.cpp Normal file
View File

@@ -0,0 +1,39 @@
// main.cpp - Server entry point for ColumnLynx
// Copyright (C) 2025 DcruBro
// Distributed under the GPLv3 license. See LICENSE for details.
#include <asio/asio.hpp>
#include <iostream>
#include <columnlynx/common/utils.hpp>
#include <columnlynx/common/panic_handler.hpp>
#include <columnlynx/server/net/tcp/tcp_server.hpp>
#include <columnlynx/common/libsodium_wrapper.hpp>
using asio::ip::tcp;
using namespace ColumnLynx::Utils;
using namespace ColumnLynx::Net::TCP;
int main(int argc, char** argv) {
PanicHandler::init();
try {
// TODO: Catch SIGINT and SIGTERM for graceful shutdown
// Generate a temporary keypair, replace with actual CA signed keys later (Note, these are stored in memory)
LibSodiumWrapper sodiumWrapper = LibSodiumWrapper();
asio::io_context io;
auto server = std::make_shared<TCPServer>(io, serverPort());
// Run the IO context in a separate thread
std::thread ioThread([&io]() {
io.run();
});
ioThread.join();
log("Server started on port " + std::to_string(serverPort()));
} catch (const std::exception& e) {
error("Server error: " + std::string(e.what()));
}
}

View File

@@ -0,0 +1,45 @@
// tcp_server.cpp - TCP Server for ColumnLynx
// Copyright (C) 2025 DcruBro
// Distributed under the GPLv3 license. See LICENSE for details.
#pragma once
#include <columnlynx/server/net/tcp/tcp_server.hpp>
#include <cstdint>
#include <string>
#include <unordered_map>
#include <vector>
#include <memory>
#include <unordered_set>
#include <asio/asio.hpp>
#include <columnlynx/common/net/tcp/tcp_message_type.hpp>
#include <columnlynx/common/net/tcp/net_helper.hpp>
#include <columnlynx/common/utils.hpp>
namespace ColumnLynx::Net::TCP {
void TCPServer::mStartAccept() {
mAcceptor.async_accept(
[this](asio::error_code ec, asio::ip::tcp::socket socket) {
if (!NetHelper::isExpectedDisconnect(ec)) {
auto client = TCPConnection::create(std::move(socket), mSodiumWrapper,
[this](std::shared_ptr<TCPConnection> c) {
mClients.erase(c);
Utils::log("Client removed.");
});
mClients.insert(client);
client->start();
Utils::log("Accepted new client connection.");
} else {
Utils::error("Accept failed: " + ec.message());
}
TCPServer::mStartAccept(); // Accept next
}
);
}
}