Moved client data passing to a dedicated ClientSession class instead of passing through a bunch of pointers at init
This commit is contained in:
71
include/columnlynx/client/client_session.hpp
Normal file
71
include/columnlynx/client/client_session.hpp
Normal file
@@ -0,0 +1,71 @@
|
||||
// client_session.hpp - Client Session data for ColumnLynx
|
||||
// Copyright (C) 2026 DcruBro
|
||||
// Distributed under the terms of the GNU General Public License, either version 2 only or version 3. See LICENSES/ for details.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <columnlynx/common/libsodium_wrapper.hpp>
|
||||
#include <array>
|
||||
#include <columnlynx/common/net/virtual_interface.hpp>
|
||||
#include <shared_mutex>
|
||||
|
||||
namespace ColumnLynx {
|
||||
struct ClientState {
|
||||
std::shared_ptr<Utils::LibSodiumWrapper> sodiumWrapper;
|
||||
SymmetricKey aesKey;
|
||||
bool insecureMode;
|
||||
std::string configPath;
|
||||
std::shared_ptr<Net::VirtualInterface> virtualInterface;
|
||||
uint64_t sessionID;
|
||||
|
||||
~ClientState() { sodium_memzero(aesKey.data(), aesKey.size()); }
|
||||
ClientState(const ClientState&) = delete;
|
||||
ClientState& operator=(const ClientState&) = delete;
|
||||
ClientState(ClientState&&) = default;
|
||||
ClientState& operator=(ClientState&&) = default;
|
||||
|
||||
explicit ClientState() = default;
|
||||
|
||||
explicit ClientState(std::shared_ptr<Utils::LibSodiumWrapper> sodium, SymmetricKey& k, bool insecure,
|
||||
std::string& config, std::shared_ptr<Net::VirtualInterface> tun, uint64_t session)
|
||||
: sodiumWrapper(sodium), aesKey(k), insecureMode(insecure), configPath(config), virtualInterface(tun), sessionID(session) {}
|
||||
};
|
||||
|
||||
class ClientSession {
|
||||
public:
|
||||
// Return a reference to the Client Session instance
|
||||
static ClientSession& getInstance() { static ClientSession instance; return instance; }
|
||||
|
||||
// Return the current client state
|
||||
std::shared_ptr<ClientState> getClientState() const;
|
||||
|
||||
// Set the client state
|
||||
void setClientState(std::shared_ptr<ClientState> state);
|
||||
|
||||
// Get the wrapper for libsodium
|
||||
const std::shared_ptr<Utils::LibSodiumWrapper>& getSodiumWrapper() const;
|
||||
// Get the AES key
|
||||
const SymmetricKey& getAESKey() const;
|
||||
// Get whether insecure mode is enabled
|
||||
bool isInsecureMode() const;
|
||||
// Get the config path
|
||||
const std::string& getConfigPath() const;
|
||||
// Get the virtual interface
|
||||
const std::shared_ptr<Net::VirtualInterface>& getVirtualInterface() const;
|
||||
// Get the session ID
|
||||
uint64_t getSessionID() const;
|
||||
|
||||
// Setters
|
||||
void setSodiumWrapper(std::shared_ptr<Utils::LibSodiumWrapper> sodiumWrapper);
|
||||
void setAESKey(const SymmetricKey& aesKey);
|
||||
void setInsecureMode(bool insecureMode);
|
||||
void setConfigPath(const std::string& configPath);
|
||||
void setVirtualInterface(std::shared_ptr<Net::VirtualInterface> virtualInterface);
|
||||
void setSessionID(uint64_t sessionID);
|
||||
|
||||
private:
|
||||
mutable std::shared_mutex mMutex;
|
||||
std::shared_ptr<struct ClientState> mClientState{nullptr};
|
||||
};
|
||||
}
|
||||
@@ -17,6 +17,7 @@
|
||||
#include <string>
|
||||
#include <columnlynx/common/net/protocol_structs.hpp>
|
||||
#include <columnlynx/common/net/virtual_interface.hpp>
|
||||
#include <columnlynx/client/client_session.hpp>
|
||||
|
||||
using asio::ip::tcp;
|
||||
|
||||
@@ -25,28 +26,20 @@ namespace ColumnLynx::Net::TCP {
|
||||
public:
|
||||
TCPClient(asio::io_context& ioContext,
|
||||
const std::string& host,
|
||||
const std::string& port,
|
||||
std::shared_ptr<Utils::LibSodiumWrapper> sodiumWrapper,
|
||||
std::shared_ptr<std::array<uint8_t, 32>> aesKey,
|
||||
std::shared_ptr<uint64_t> sessionIDRef,
|
||||
bool insecureMode,
|
||||
std::string& configPath,
|
||||
std::shared_ptr<VirtualInterface> tun = nullptr)
|
||||
const std::string& port)
|
||||
:
|
||||
mResolver(ioContext),
|
||||
mSocket(ioContext),
|
||||
mHost(host),
|
||||
mPort(port),
|
||||
mLibSodiumWrapper(sodiumWrapper),
|
||||
mGlobalKeyRef(aesKey),
|
||||
mSessionIDRef(sessionIDRef),
|
||||
mInsecureMode(insecureMode),
|
||||
mHeartbeatTimer(mSocket.get_executor()),
|
||||
mLastHeartbeatReceived(std::chrono::steady_clock::now()),
|
||||
mLastHeartbeatSent(std::chrono::steady_clock::now()),
|
||||
mTun(tun),
|
||||
mConfigDirPath(configPath)
|
||||
mLastHeartbeatSent(std::chrono::steady_clock::now())
|
||||
{
|
||||
// Get initial client config
|
||||
std::string configPath = ClientSession::getInstance().getConfigPath();
|
||||
std::shared_ptr<Utils::LibSodiumWrapper> mLibSodiumWrapper = ClientSession::getInstance().getSodiumWrapper();
|
||||
|
||||
// Preload the config map
|
||||
mRawClientConfig = Utils::getConfigMap(configPath + "client_config");
|
||||
|
||||
@@ -104,20 +97,14 @@ namespace ColumnLynx::Net::TCP {
|
||||
std::string mHost, mPort;
|
||||
uint8_t mServerPublicKey[32]; // Assuming 256-bit public key
|
||||
std::array<uint8_t, 32> mSubmittedChallenge{};
|
||||
std::shared_ptr<Utils::LibSodiumWrapper> mLibSodiumWrapper;
|
||||
uint64_t mConnectionSessionID;
|
||||
SymmetricKey mConnectionAESKey;
|
||||
std::shared_ptr<std::array<uint8_t, 32>> mGlobalKeyRef; // Reference to global AES key
|
||||
std::shared_ptr<uint64_t> mSessionIDRef; // Reference to global Session ID
|
||||
bool mInsecureMode; // Insecure mode flag
|
||||
asio::steady_timer mHeartbeatTimer;
|
||||
std::chrono::steady_clock::time_point mLastHeartbeatReceived;
|
||||
std::chrono::steady_clock::time_point mLastHeartbeatSent;
|
||||
int mMissedHeartbeats = 0;
|
||||
bool mIsHostDomain;
|
||||
Protocol::TunConfig mTunConfig;
|
||||
std::shared_ptr<VirtualInterface> mTun = nullptr;
|
||||
std::unordered_map<std::string, std::string> mRawClientConfig;
|
||||
std::string mConfigDirPath;
|
||||
};
|
||||
}
|
||||
@@ -10,17 +10,15 @@
|
||||
#include <columnlynx/common/libsodium_wrapper.hpp>
|
||||
#include <array>
|
||||
#include <columnlynx/common/net/virtual_interface.hpp>
|
||||
#include <columnlynx/client/client_session.hpp>
|
||||
|
||||
namespace ColumnLynx::Net::UDP {
|
||||
class UDPClient {
|
||||
public:
|
||||
UDPClient(asio::io_context& ioContext,
|
||||
const std::string& host,
|
||||
const std::string& port,
|
||||
std::shared_ptr<std::array<uint8_t, 32>> aesKeyRef,
|
||||
std::shared_ptr<uint64_t> sessionIDRef,
|
||||
std::shared_ptr<VirtualInterface> tunRef = nullptr)
|
||||
: mSocket(ioContext), mResolver(ioContext), mHost(host), mPort(port), mAesKeyRef(aesKeyRef), mSessionIDRef(sessionIDRef), mTunRef(tunRef)
|
||||
const std::string& port)
|
||||
: mSocket(ioContext), mResolver(ioContext), mHost(host), mPort(port)
|
||||
{
|
||||
mStartReceive();
|
||||
}
|
||||
@@ -43,9 +41,6 @@ namespace ColumnLynx::Net::UDP {
|
||||
asio::ip::udp::endpoint mRemoteEndpoint;
|
||||
std::string mHost;
|
||||
std::string mPort;
|
||||
std::shared_ptr<std::array<uint8_t, 32>> mAesKeyRef;
|
||||
std::shared_ptr<uint64_t> mSessionIDRef;
|
||||
std::shared_ptr<VirtualInterface> mTunRef = nullptr;
|
||||
std::array<uint8_t, 2048> mRecvBuffer; // Adjust size as needed
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user