Almost finished with the TCP Handshake procedure, need to properly handle disconnects (currently pretty forceful)
This commit is contained in:
@@ -32,8 +32,10 @@ int main(int argc, char** argv) {
|
||||
PanicHandler::init();
|
||||
|
||||
try {
|
||||
LibSodiumWrapper sodiumWrapper = LibSodiumWrapper();
|
||||
|
||||
asio::io_context io;
|
||||
auto client = std::make_shared<ColumnLynx::Net::TCP::TCPClient>(io, "127.0.0.1", std::to_string(serverPort()));
|
||||
auto client = std::make_shared<ColumnLynx::Net::TCP::TCPClient>(io, "127.0.0.1", std::to_string(serverPort()), &sodiumWrapper);
|
||||
|
||||
client->start();
|
||||
|
||||
|
||||
@@ -11,24 +11,29 @@ namespace ColumnLynx::Utils {
|
||||
throw std::runtime_error("Failed to initialize libsodium");
|
||||
}
|
||||
|
||||
if (crypto_kx_keypair(mPublicKey, mPrivateKey) != 0) {
|
||||
// Generate keypair
|
||||
if (crypto_sign_keypair(mPublicKey.data(), mPrivateKey.data()) != 0) {
|
||||
throw std::runtime_error("Failed to generate key pair");
|
||||
}
|
||||
|
||||
// Convert to Curve25519 keys for encryption
|
||||
crypto_sign_ed25519_pk_to_curve25519(mXPublicKey.data(), mPublicKey.data());
|
||||
crypto_sign_ed25519_sk_to_curve25519(mXPrivateKey.data(), mPrivateKey.data());
|
||||
|
||||
log("Libsodium initialized and keypair generated");
|
||||
}
|
||||
|
||||
uint8_t* LibSodiumWrapper::getPublicKey() {
|
||||
return mPublicKey;
|
||||
return mPublicKey.data();
|
||||
}
|
||||
|
||||
uint8_t* LibSodiumWrapper::getPrivateKey() {
|
||||
return mPrivateKey;
|
||||
return mPrivateKey.data();
|
||||
}
|
||||
|
||||
uint8_t LibSodiumWrapper::generateRandomAESKey() {
|
||||
uint8_t aesKey[32]; // 256-bit key
|
||||
randombytes_buf(aesKey, sizeof(aesKey));
|
||||
return *aesKey;
|
||||
std::array<uint8_t, 32> LibSodiumWrapper::generateRandom256Bit() {
|
||||
std::array<uint8_t, 32> randbytes; // 256 bits
|
||||
randombytes_buf(randbytes.data(), randbytes.size());
|
||||
return randbytes;
|
||||
}
|
||||
}
|
||||
@@ -43,4 +43,18 @@ namespace ColumnLynx::Utils {
|
||||
unsigned short serverPort() {
|
||||
return 48042;
|
||||
}
|
||||
|
||||
std::string bytesToHexString(const uint8_t* bytes, size_t length) {
|
||||
const char hexChars[] = "0123456789ABCDEF";
|
||||
std::string hexString;
|
||||
hexString.reserve(length * 2);
|
||||
|
||||
for (size_t i = 0; i < length; ++i) {
|
||||
uint8_t byte = bytes[i];
|
||||
hexString.push_back(hexChars[(byte >> 4) & 0x0F]);
|
||||
hexString.push_back(hexChars[byte & 0x0F]);
|
||||
}
|
||||
|
||||
return hexString;
|
||||
}
|
||||
}
|
||||
@@ -21,9 +21,11 @@ int main(int argc, char** argv) {
|
||||
|
||||
// Generate a temporary keypair, replace with actual CA signed keys later (Note, these are stored in memory)
|
||||
LibSodiumWrapper sodiumWrapper = LibSodiumWrapper();
|
||||
log("Server public key: " + bytesToHexString(sodiumWrapper.getPublicKey(), crypto_sign_PUBLICKEYBYTES));
|
||||
log("Server private key: " + bytesToHexString(sodiumWrapper.getPrivateKey(), crypto_sign_SECRETKEYBYTES)); // TEMP, remove later
|
||||
|
||||
asio::io_context io;
|
||||
auto server = std::make_shared<TCPServer>(io, serverPort());
|
||||
auto server = std::make_shared<TCPServer>(io, serverPort(), &sodiumWrapper);
|
||||
|
||||
// Run the IO context in a separate thread
|
||||
std::thread ioThread([&io]() {
|
||||
|
||||
@@ -23,7 +23,8 @@ namespace ColumnLynx::Net::TCP {
|
||||
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,
|
||||
auto client = TCPConnection::create(std::move(socket),
|
||||
mSodiumWrapper,
|
||||
[this](std::shared_ptr<TCPConnection> c) {
|
||||
mClients.erase(c);
|
||||
Utils::log("Client removed.");
|
||||
|
||||
Reference in New Issue
Block a user