Converted some raw pointers to smart pointers

This commit is contained in:
2025-12-01 21:13:46 +01:00
parent 552d78eab2
commit 15d13b6f04
10 changed files with 46 additions and 45 deletions

View File

@@ -38,7 +38,7 @@ endif()
if(WIN32) if(WIN32)
add_compile_definitions(_WIN32_WINNT=0x0A00 NOMINMAX WIN32_LEAN_AND_MEAN) add_compile_definitions(_WIN32_WINNT=0x0A00 NOMINMAX WIN32_LEAN_AND_MEAN)
elseif(UNIX) elseif(UNIX)
add_compile_options(-Wall -Wextra -Wpedantic) add_compile_options(-Wall -Wextra -Wpedantic -O3)
add_link_options(-pthread) add_link_options(-pthread)
endif() endif()

View File

@@ -25,10 +25,10 @@ namespace ColumnLynx::Net::TCP {
TCPClient(asio::io_context& ioContext, TCPClient(asio::io_context& ioContext,
const std::string& host, const std::string& host,
const std::string& port, const std::string& port,
Utils::LibSodiumWrapper* sodiumWrapper, std::shared_ptr<Utils::LibSodiumWrapper> sodiumWrapper,
std::array<uint8_t, 32>* aesKey, std::shared_ptr<std::array<uint8_t, 32>> aesKey,
uint64_t* sessionIDRef, std::shared_ptr<uint64_t> sessionIDRef,
bool* insecureMode, bool insecureMode,
std::shared_ptr<VirtualInterface> tun = nullptr) std::shared_ptr<VirtualInterface> tun = nullptr)
: :
mResolver(ioContext), mResolver(ioContext),
@@ -95,12 +95,12 @@ namespace ColumnLynx::Net::TCP {
std::string mHost, mPort; std::string mHost, mPort;
uint8_t mServerPublicKey[32]; // Assuming 256-bit public key uint8_t mServerPublicKey[32]; // Assuming 256-bit public key
std::array<uint8_t, 32> mSubmittedChallenge{}; std::array<uint8_t, 32> mSubmittedChallenge{};
Utils::LibSodiumWrapper* mLibSodiumWrapper; std::shared_ptr<Utils::LibSodiumWrapper> mLibSodiumWrapper;
uint64_t mConnectionSessionID; uint64_t mConnectionSessionID;
SymmetricKey mConnectionAESKey; SymmetricKey mConnectionAESKey;
std::array<uint8_t, 32>* mGlobalKeyRef; // Reference to global AES key std::shared_ptr<std::array<uint8_t, 32>> mGlobalKeyRef; // Reference to global AES key
uint64_t* mSessionIDRef; // Reference to global Session ID std::shared_ptr<uint64_t> mSessionIDRef; // Reference to global Session ID
bool* mInsecureMode; // Reference to insecure mode flag bool mInsecureMode; // Insecure mode flag
asio::steady_timer mHeartbeatTimer; asio::steady_timer mHeartbeatTimer;
std::chrono::steady_clock::time_point mLastHeartbeatReceived; std::chrono::steady_clock::time_point mLastHeartbeatReceived;
std::chrono::steady_clock::time_point mLastHeartbeatSent; std::chrono::steady_clock::time_point mLastHeartbeatSent;

View File

@@ -17,8 +17,8 @@ namespace ColumnLynx::Net::UDP {
UDPClient(asio::io_context& ioContext, UDPClient(asio::io_context& ioContext,
const std::string& host, const std::string& host,
const std::string& port, const std::string& port,
std::array<uint8_t, 32>* aesKeyRef, std::shared_ptr<std::array<uint8_t, 32>> aesKeyRef,
uint64_t* sessionIDRef, std::shared_ptr<uint64_t> sessionIDRef,
std::shared_ptr<VirtualInterface> tunRef = nullptr) std::shared_ptr<VirtualInterface> tunRef = nullptr)
: mSocket(ioContext), mResolver(ioContext), mHost(host), mPort(port), mAesKeyRef(aesKeyRef), mSessionIDRef(sessionIDRef), mTunRef(tunRef) : mSocket(ioContext), mResolver(ioContext), mHost(host), mPort(port), mAesKeyRef(aesKeyRef), mSessionIDRef(sessionIDRef), mTunRef(tunRef)
{ {
@@ -43,8 +43,8 @@ namespace ColumnLynx::Net::UDP {
asio::ip::udp::endpoint mRemoteEndpoint; asio::ip::udp::endpoint mRemoteEndpoint;
std::string mHost; std::string mHost;
std::string mPort; std::string mPort;
std::array<uint8_t, 32>* mAesKeyRef; std::shared_ptr<std::array<uint8_t, 32>> mAesKeyRef;
uint64_t* mSessionIDRef; std::shared_ptr<uint64_t> mSessionIDRef;
std::shared_ptr<VirtualInterface> mTunRef = nullptr; std::shared_ptr<VirtualInterface> mTunRef = nullptr;
std::array<uint8_t, 2048> mRecvBuffer; // Adjust size as needed std::array<uint8_t, 2048> mRecvBuffer; // Adjust size as needed
}; };

View File

@@ -25,7 +25,7 @@ namespace ColumnLynx::Net::TCP {
static pointer create( static pointer create(
asio::ip::tcp::socket socket, asio::ip::tcp::socket socket,
Utils::LibSodiumWrapper* sodiumWrapper, std::shared_ptr<Utils::LibSodiumWrapper> sodiumWrapper,
std::function<void(pointer)> onDisconnect) std::function<void(pointer)> onDisconnect)
{ {
auto conn = pointer(new TCPConnection(std::move(socket), sodiumWrapper)); auto conn = pointer(new TCPConnection(std::move(socket), sodiumWrapper));
@@ -48,7 +48,7 @@ namespace ColumnLynx::Net::TCP {
std::array<uint8_t, 32> getAESKey() const; std::array<uint8_t, 32> getAESKey() const;
private: private:
TCPConnection(asio::ip::tcp::socket socket, Utils::LibSodiumWrapper* sodiumWrapper) TCPConnection(asio::ip::tcp::socket socket, std::shared_ptr<Utils::LibSodiumWrapper> sodiumWrapper)
: :
mHandler(std::make_shared<MessageHandler>(std::move(socket))), mHandler(std::make_shared<MessageHandler>(std::move(socket))),
mLibSodiumWrapper(sodiumWrapper), mLibSodiumWrapper(sodiumWrapper),
@@ -64,7 +64,7 @@ namespace ColumnLynx::Net::TCP {
std::shared_ptr<MessageHandler> mHandler; std::shared_ptr<MessageHandler> mHandler;
std::function<void(std::shared_ptr<TCPConnection>)> mOnDisconnect; std::function<void(std::shared_ptr<TCPConnection>)> mOnDisconnect;
Utils::LibSodiumWrapper *mLibSodiumWrapper; std::shared_ptr<Utils::LibSodiumWrapper> mLibSodiumWrapper;
std::array<uint8_t, 32> mConnectionAESKey; std::array<uint8_t, 32> mConnectionAESKey;
uint64_t mConnectionSessionID; uint64_t mConnectionSessionID;
AsymPublicKey mConnectionPublicKey; AsymPublicKey mConnectionPublicKey;

View File

@@ -24,8 +24,8 @@ namespace ColumnLynx::Net::TCP {
public: public:
TCPServer(asio::io_context& ioContext, TCPServer(asio::io_context& ioContext,
uint16_t port, uint16_t port,
Utils::LibSodiumWrapper* sodiumWrapper, std::shared_ptr<Utils::LibSodiumWrapper> sodiumWrapper,
bool* hostRunning, bool ipv4Only = false) std::shared_ptr<bool> hostRunning, bool ipv4Only = false)
: mIoContext(ioContext), : mIoContext(ioContext),
mAcceptor(ioContext), mAcceptor(ioContext),
mSodiumWrapper(sodiumWrapper), mSodiumWrapper(sodiumWrapper),
@@ -72,8 +72,8 @@ namespace ColumnLynx::Net::TCP {
asio::io_context &mIoContext; asio::io_context &mIoContext;
asio::ip::tcp::acceptor mAcceptor; asio::ip::tcp::acceptor mAcceptor;
std::unordered_set<TCPConnection::pointer> mClients; std::unordered_set<TCPConnection::pointer> mClients;
Utils::LibSodiumWrapper *mSodiumWrapper; std::shared_ptr<Utils::LibSodiumWrapper> mSodiumWrapper;
bool* mHostRunning; std::shared_ptr<bool> mHostRunning;
std::unordered_map<std::string, std::string> mRawServerConfig; std::unordered_map<std::string, std::string> mRawServerConfig;
}; };

View File

@@ -13,7 +13,7 @@
namespace ColumnLynx::Net::UDP { namespace ColumnLynx::Net::UDP {
class UDPServer { class UDPServer {
public: public:
UDPServer(asio::io_context& ioContext, uint16_t port, bool* hostRunning, bool ipv4Only = false, std::shared_ptr<VirtualInterface> tun = nullptr) UDPServer(asio::io_context& ioContext, uint16_t port, std::shared_ptr<bool> hostRunning, bool ipv4Only = false, std::shared_ptr<VirtualInterface> tun = nullptr)
: mSocket(ioContext), mHostRunning(hostRunning), mTun(tun) : mSocket(ioContext), mHostRunning(hostRunning), mTun(tun)
{ {
asio::error_code ec; asio::error_code ec;
@@ -56,8 +56,8 @@ namespace ColumnLynx::Net::UDP {
asio::ip::udp::socket mSocket; asio::ip::udp::socket mSocket;
asio::ip::udp::endpoint mRemoteEndpoint; asio::ip::udp::endpoint mRemoteEndpoint;
std::array<uint8_t, 2048> mRecvBuffer; // Adjust size as needed std::array<uint8_t, 2048> mRecvBuffer; // 2048 seems stable
bool* mHostRunning; std::shared_ptr<bool> mHostRunning;
std::shared_ptr<VirtualInterface> mTun; std::shared_ptr<VirtualInterface> mTun;
}; };
} }

View File

@@ -74,16 +74,17 @@ int main(int argc, char** argv) {
std::shared_ptr<VirtualInterface> tun = std::make_shared<VirtualInterface>(optionsObj["interface"].as<std::string>()); std::shared_ptr<VirtualInterface> tun = std::make_shared<VirtualInterface>(optionsObj["interface"].as<std::string>());
log("Using virtual interface: " + tun->getName()); log("Using virtual interface: " + tun->getName());
LibSodiumWrapper sodiumWrapper = LibSodiumWrapper(); std::shared_ptr<LibSodiumWrapper> sodiumWrapper = std::make_shared<LibSodiumWrapper>();
debug("Public Key: " + Utils::bytesToHexString(sodiumWrapper.getPublicKey(), 32)); debug("Public Key: " + Utils::bytesToHexString(sodiumWrapper->getPublicKey(), 32));
debug("Private Key: " + Utils::bytesToHexString(sodiumWrapper.getPrivateKey(), 64)); debug("Private Key: " + Utils::bytesToHexString(sodiumWrapper->getPrivateKey(), 64));
std::array<uint8_t, 32> aesKey = {0}; // Defualt zeroed state until modified by handshake std::shared_ptr<std::array<uint8_t, 32>> aesKey = std::make_shared<std::array<uint8_t, 32>>();
uint64_t sessionID = 0; aesKey->fill(0); // Defualt zeroed state until modified by handshake
std::shared_ptr<uint64_t> sessionID = std::make_shared<uint64_t>(0);
asio::io_context io; asio::io_context io;
auto client = std::make_shared<ColumnLynx::Net::TCP::TCPClient>(io, host, port, &sodiumWrapper, &aesKey, &sessionID, &insecureMode, tun); auto client = std::make_shared<ColumnLynx::Net::TCP::TCPClient>(io, host, port, sodiumWrapper, aesKey, sessionID, insecureMode, tun);
auto udpClient = std::make_shared<ColumnLynx::Net::UDP::UDPClient>(io, host, port, &aesKey, &sessionID, tun); auto udpClient = std::make_shared<ColumnLynx::Net::UDP::UDPClient>(io, host, port, aesKey, sessionID, tun);
client->start(); client->start();
udpClient->start(); udpClient->start();

View File

@@ -145,7 +145,7 @@ namespace ColumnLynx::Net::TCP {
// Verify pubkey against whitelisted_keys // Verify pubkey against whitelisted_keys
std::vector<std::string> whitelistedKeys = Utils::getWhitelistedKeys(); std::vector<std::string> whitelistedKeys = Utils::getWhitelistedKeys();
if (std::find(whitelistedKeys.begin(), whitelistedKeys.end(), Utils::bytesToHexString(mServerPublicKey, 32)) == whitelistedKeys.end()) { // Key verification is handled in later steps of the handshake if (std::find(whitelistedKeys.begin(), whitelistedKeys.end(), Utils::bytesToHexString(mServerPublicKey, 32)) == whitelistedKeys.end()) { // Key verification is handled in later steps of the handshake
if (!(*mInsecureMode)) { if (!mInsecureMode) {
Utils::error("Server public key not in whitelisted_keys. Terminating connection."); Utils::error("Server public key not in whitelisted_keys. Terminating connection.");
disconnect(); disconnect();
return; return;

View File

@@ -36,10 +36,9 @@ namespace ColumnLynx::Net::UDP {
reinterpret_cast<uint8_t*>(&hdr), reinterpret_cast<uint8_t*>(&hdr),
reinterpret_cast<uint8_t*>(&hdr) + sizeof(UDPPacketHeader) reinterpret_cast<uint8_t*>(&hdr) + sizeof(UDPPacketHeader)
); );
uint64_t sid = *mSessionIDRef;
packet.insert(packet.end(), packet.insert(packet.end(),
reinterpret_cast<uint8_t*>(&sid), reinterpret_cast<uint8_t*>(mSessionIDRef.get()),
reinterpret_cast<uint8_t*>(&sid) + sizeof(sid) reinterpret_cast<uint8_t*>(mSessionIDRef.get()) + sizeof(uint64_t)
); );
packet.insert(packet.end(), encryptedPayload.begin(), encryptedPayload.end()); packet.insert(packet.end(), encryptedPayload.begin(), encryptedPayload.end());
@@ -90,6 +89,11 @@ namespace ColumnLynx::Net::UDP {
uint64_t sessionID; uint64_t sessionID;
std::memcpy(&sessionID, mRecvBuffer.data() + sizeof(UDPPacketHeader), sizeof(uint64_t)); std::memcpy(&sessionID, mRecvBuffer.data() + sizeof(UDPPacketHeader), sizeof(uint64_t));
if (sessionID != *mSessionIDRef) {
Utils::warn("Got packet that isn't for me! Dropping!");
return;
}
// Decrypt payload // Decrypt payload
std::vector<uint8_t> ciphertext( std::vector<uint8_t> ciphertext(
mRecvBuffer.begin() + sizeof(UDPPacketHeader) + sizeof(uint64_t), mRecvBuffer.begin() + sizeof(UDPPacketHeader) + sizeof(uint64_t),

View File

@@ -77,7 +77,7 @@ int main(int argc, char** argv) {
log("Using virtual interface: " + tun->getName()); log("Using virtual interface: " + tun->getName());
// Generate a temporary keypair, replace with actual CA signed keys later (Note, these are stored in memory) // Generate a temporary keypair, replace with actual CA signed keys later (Note, these are stored in memory)
LibSodiumWrapper sodiumWrapper = LibSodiumWrapper(); std::shared_ptr<LibSodiumWrapper> sodiumWrapper = std::make_shared<LibSodiumWrapper>();
auto itPubkey = config.find("SERVER_PUBLIC_KEY"); auto itPubkey = config.find("SERVER_PUBLIC_KEY");
auto itPrivkey = config.find("SERVER_PRIVATE_KEY"); auto itPrivkey = config.find("SERVER_PRIVATE_KEY");
@@ -91,27 +91,26 @@ int main(int argc, char** argv) {
std::copy_n(Utils::hexStringToBytes(itPrivkey->second).begin(), sk.size(), sk.begin()); std::copy_n(Utils::hexStringToBytes(itPrivkey->second).begin(), sk.size(), sk.begin());
std::copy_n(Utils::hexStringToBytes(itPubkey->second).begin(), pk.size(), pk.begin()); std::copy_n(Utils::hexStringToBytes(itPubkey->second).begin(), pk.size(), pk.begin());
sodiumWrapper.setKeys(pk, sk); sodiumWrapper->setKeys(pk, sk);
} else { } else {
warn("No keypair found in config file! Using random key."); warn("No keypair found in config file! Using random key.");
} }
log("Server public key: " + bytesToHexString(sodiumWrapper.getPublicKey(), crypto_sign_PUBLICKEYBYTES)); log("Server public key: " + bytesToHexString(sodiumWrapper->getPublicKey(), crypto_sign_PUBLICKEYBYTES));
//log("Server private key: " + bytesToHexString(sodiumWrapper.getPrivateKey(), crypto_sign_SECRETKEYBYTES)); // TEMP, remove later
bool hostRunning = true; std::shared_ptr<bool> hostRunning = std::make_shared<bool>(true);
asio::io_context io; asio::io_context io;
auto server = std::make_shared<TCPServer>(io, serverPort(), &sodiumWrapper, &hostRunning, ipv4Only); auto server = std::make_shared<TCPServer>(io, serverPort(), sodiumWrapper, hostRunning, ipv4Only);
auto udpServer = std::make_shared<UDPServer>(io, serverPort(), &hostRunning, ipv4Only, tun); auto udpServer = std::make_shared<UDPServer>(io, serverPort(), hostRunning, ipv4Only, tun);
asio::signal_set signals(io, SIGINT, SIGTERM); asio::signal_set signals(io, SIGINT, SIGTERM);
signals.async_wait([&](const std::error_code&, int) { signals.async_wait([&](const std::error_code&, int) {
log("Received termination signal. Shutting down server gracefully."); log("Received termination signal. Shutting down server gracefully.");
done = 1; done = 1;
asio::post(io, [&]() { asio::post(io, [&]() {
hostRunning = false; *hostRunning = false;
server->stop(); server->stop();
udpServer->stop(); udpServer->stop();
}); });
@@ -145,9 +144,6 @@ int main(int argc, char** argv) {
} }
log("Shutting down server..."); log("Shutting down server...");
/*hostRunning = false;
server->stop();
udpServer->stop();*/
io.stop(); io.stop();
if (ioThread.joinable()) { if (ioThread.joinable()) {