TESTING: Move server stuff to a server session config for central/global resources
This commit is contained in:
@@ -18,6 +18,7 @@
|
|||||||
#include <columnlynx/common/net/session_registry.hpp>
|
#include <columnlynx/common/net/session_registry.hpp>
|
||||||
#include <columnlynx/common/net/protocol_structs.hpp>
|
#include <columnlynx/common/net/protocol_structs.hpp>
|
||||||
#include <columnlynx/common/net/virtual_interface.hpp>
|
#include <columnlynx/common/net/virtual_interface.hpp>
|
||||||
|
#include <columnlynx/server/server_session.hpp>
|
||||||
|
|
||||||
namespace ColumnLynx::Net::TCP {
|
namespace ColumnLynx::Net::TCP {
|
||||||
class TCPConnection : public std::enable_shared_from_this<TCPConnection> {
|
class TCPConnection : public std::enable_shared_from_this<TCPConnection> {
|
||||||
@@ -26,12 +27,9 @@ namespace ColumnLynx::Net::TCP {
|
|||||||
|
|
||||||
static pointer create(
|
static pointer create(
|
||||||
asio::ip::tcp::socket socket,
|
asio::ip::tcp::socket socket,
|
||||||
std::shared_ptr<Utils::LibSodiumWrapper> sodiumWrapper,
|
|
||||||
std::unordered_map<std::string, std::string>* serverConfig,
|
|
||||||
std::string configDirPath,
|
|
||||||
std::function<void(pointer)> onDisconnect)
|
std::function<void(pointer)> onDisconnect)
|
||||||
{
|
{
|
||||||
auto conn = pointer(new TCPConnection(std::move(socket), sodiumWrapper, serverConfig, configDirPath));
|
auto conn = pointer(new TCPConnection(std::move(socket)));
|
||||||
conn->mOnDisconnect = std::move(onDisconnect);
|
conn->mOnDisconnect = std::move(onDisconnect);
|
||||||
return conn;
|
return conn;
|
||||||
}
|
}
|
||||||
@@ -51,15 +49,12 @@ 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, std::shared_ptr<Utils::LibSodiumWrapper> sodiumWrapper, std::unordered_map<std::string, std::string>* serverConfig, std::string configDirPath)
|
TCPConnection(asio::ip::tcp::socket socket)
|
||||||
:
|
:
|
||||||
mHandler(std::make_shared<MessageHandler>(std::move(socket))),
|
mHandler(std::make_shared<MessageHandler>(std::move(socket))),
|
||||||
mLibSodiumWrapper(sodiumWrapper),
|
|
||||||
mRawServerConfig(serverConfig),
|
|
||||||
mHeartbeatTimer(mHandler->socket().get_executor()),
|
mHeartbeatTimer(mHandler->socket().get_executor()),
|
||||||
mLastHeartbeatReceived(std::chrono::steady_clock::now()),
|
mLastHeartbeatReceived(std::chrono::steady_clock::now()),
|
||||||
mLastHeartbeatSent(std::chrono::steady_clock::now()),
|
mLastHeartbeatSent(std::chrono::steady_clock::now())
|
||||||
mConfigDirPath(configDirPath)
|
|
||||||
{}
|
{}
|
||||||
|
|
||||||
// Start the heartbeat routine
|
// Start the heartbeat routine
|
||||||
@@ -69,8 +64,6 @@ 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;
|
||||||
std::shared_ptr<Utils::LibSodiumWrapper> mLibSodiumWrapper;
|
|
||||||
std::unordered_map<std::string, std::string>* mRawServerConfig;
|
|
||||||
std::array<uint8_t, 32> mConnectionAESKey;
|
std::array<uint8_t, 32> mConnectionAESKey;
|
||||||
uint64_t mConnectionSessionID;
|
uint64_t mConnectionSessionID;
|
||||||
AsymPublicKey mConnectionPublicKey;
|
AsymPublicKey mConnectionPublicKey;
|
||||||
@@ -79,6 +72,5 @@ namespace ColumnLynx::Net::TCP {
|
|||||||
std::chrono::steady_clock::time_point mLastHeartbeatSent;
|
std::chrono::steady_clock::time_point mLastHeartbeatSent;
|
||||||
int mMissedHeartbeats = 0;
|
int mMissedHeartbeats = 0;
|
||||||
std::string mRemoteIP; // Cached remote IP to avoid calling remote_endpoint() on closed sockets
|
std::string mRemoteIP; // Cached remote IP to avoid calling remote_endpoint() on closed sockets
|
||||||
std::string mConfigDirPath;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -17,29 +17,23 @@
|
|||||||
#include <columnlynx/server/net/tcp/tcp_connection.hpp>
|
#include <columnlynx/server/net/tcp/tcp_connection.hpp>
|
||||||
#include <columnlynx/common/libsodium_wrapper.hpp>
|
#include <columnlynx/common/libsodium_wrapper.hpp>
|
||||||
#include <columnlynx/common/net/protocol_structs.hpp>
|
#include <columnlynx/common/net/protocol_structs.hpp>
|
||||||
|
#include <columnlynx/server/server_session.hpp>
|
||||||
|
|
||||||
namespace ColumnLynx::Net::TCP {
|
namespace ColumnLynx::Net::TCP {
|
||||||
|
|
||||||
class TCPServer {
|
class TCPServer {
|
||||||
public:
|
public:
|
||||||
TCPServer(asio::io_context& ioContext,
|
TCPServer(asio::io_context& ioContext,
|
||||||
uint16_t port,
|
uint16_t port)
|
||||||
std::shared_ptr<Utils::LibSodiumWrapper> sodiumWrapper,
|
|
||||||
std::shared_ptr<bool> hostRunning,
|
|
||||||
std::string& configPath,
|
|
||||||
bool ipv4Only = false)
|
|
||||||
: mIoContext(ioContext),
|
: mIoContext(ioContext),
|
||||||
mAcceptor(ioContext),
|
mAcceptor(ioContext)
|
||||||
mSodiumWrapper(sodiumWrapper),
|
|
||||||
mHostRunning(hostRunning),
|
|
||||||
mConfigDirPath(configPath)
|
|
||||||
{
|
{
|
||||||
// Preload the config map
|
// Preload the config map
|
||||||
mRawServerConfig = Utils::getConfigMap(configPath + "server_config", {"NETWORK", "SUBNET_MASK"});
|
|
||||||
|
|
||||||
asio::error_code ec_open, ec_v6only, ec_bind;
|
asio::error_code ec_open, ec_v6only, ec_bind;
|
||||||
|
|
||||||
if (!ipv4Only) {
|
bool isIPv4Only = ServerSession::getInstance().isIPv4Only();
|
||||||
|
|
||||||
|
if (!isIPv4Only) {
|
||||||
// Try IPv6 (dual-stack if supported)
|
// Try IPv6 (dual-stack if supported)
|
||||||
asio::ip::tcp::endpoint endpoint_v6(asio::ip::tcp::v6(), port);
|
asio::ip::tcp::endpoint endpoint_v6(asio::ip::tcp::v6(), port);
|
||||||
|
|
||||||
@@ -55,8 +49,8 @@ namespace ColumnLynx::Net::TCP {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If IPv6 bind failed OR IPv6 open failed OR forced IPv4-only
|
// If IPv6 bind failed OR IPv6 open failed OR forced IPv4-only
|
||||||
if (ipv4Only || ec_open || ec_bind) {
|
if (isIPv4Only || ec_open || ec_bind) {
|
||||||
if (!ipv4Only)
|
if (!isIPv4Only)
|
||||||
Utils::warn("TCP: IPv6 unavailable (open=" + ec_open.message() +
|
Utils::warn("TCP: IPv6 unavailable (open=" + ec_open.message() +
|
||||||
", bind=" + ec_bind.message() +
|
", bind=" + ec_bind.message() +
|
||||||
"), falling back to IPv4 only");
|
"), falling back to IPv4 only");
|
||||||
@@ -84,10 +78,6 @@ 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;
|
||||||
std::shared_ptr<Utils::LibSodiumWrapper> mSodiumWrapper;
|
|
||||||
std::shared_ptr<bool> mHostRunning;
|
|
||||||
std::unordered_map<std::string, std::string> mRawServerConfig;
|
|
||||||
std::string mConfigDirPath;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -9,16 +9,18 @@
|
|||||||
#include <columnlynx/common/utils.hpp>
|
#include <columnlynx/common/utils.hpp>
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <columnlynx/common/net/virtual_interface.hpp>
|
#include <columnlynx/common/net/virtual_interface.hpp>
|
||||||
|
#include <columnlynx/common/libsodium_wrapper.hpp>
|
||||||
|
#include <columnlynx/server/server_session.hpp>
|
||||||
|
|
||||||
namespace ColumnLynx::Net::UDP {
|
namespace ColumnLynx::Net::UDP {
|
||||||
class UDPServer {
|
class UDPServer {
|
||||||
public:
|
public:
|
||||||
UDPServer(asio::io_context& ioContext, uint16_t port, std::shared_ptr<bool> hostRunning, bool ipv4Only = false, std::shared_ptr<VirtualInterface> tun = nullptr)
|
UDPServer(asio::io_context& ioContext, uint16_t port)
|
||||||
: mSocket(ioContext), mHostRunning(hostRunning), mTun(tun)
|
: mSocket(ioContext)
|
||||||
{
|
{
|
||||||
asio::error_code ec_open, ec_v6only, ec_bind;
|
asio::error_code ec_open, ec_v6only, ec_bind;
|
||||||
|
|
||||||
if (!ipv4Only) {
|
if (!mIpv4Only) {
|
||||||
asio::ip::udp::endpoint endpoint_v6(asio::ip::udp::v6(), port);
|
asio::ip::udp::endpoint endpoint_v6(asio::ip::udp::v6(), port);
|
||||||
|
|
||||||
// Try opening IPv6 socket
|
// Try opening IPv6 socket
|
||||||
@@ -34,8 +36,8 @@ namespace ColumnLynx::Net::UDP {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Fallback to IPv4 if IPv6 is unusable
|
// Fallback to IPv4 if IPv6 is unusable
|
||||||
if (ipv4Only || ec_open || ec_bind) {
|
if (mIpv4Only || ec_open || ec_bind) {
|
||||||
if (!ipv4Only) {
|
if (!mIpv4Only) {
|
||||||
Utils::warn(
|
Utils::warn(
|
||||||
"UDP: IPv6 unavailable (open=" + ec_open.message() +
|
"UDP: IPv6 unavailable (open=" + ec_open.message() +
|
||||||
", bind=" + ec_bind.message() +
|
", bind=" + ec_bind.message() +
|
||||||
@@ -70,7 +72,7 @@ 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; // 2048 seems stable
|
std::array<uint8_t, 2048> mRecvBuffer; // 2048 seems stable
|
||||||
std::shared_ptr<bool> mHostRunning;
|
bool mIpv4Only = ServerSession::getInstance().isIPv4Only();
|
||||||
std::shared_ptr<VirtualInterface> mTun;
|
const std::shared_ptr<VirtualInterface> mTun = ServerSession::getInstance().getVirtualInterface();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
62
include/columnlynx/server/server_session.hpp
Normal file
62
include/columnlynx/server/server_session.hpp
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
// server_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 ServerState {
|
||||||
|
std::shared_ptr<Utils::LibSodiumWrapper> sodiumWrapper;
|
||||||
|
std::shared_ptr<Net::VirtualInterface> virtualInterface;
|
||||||
|
std::string configPath;
|
||||||
|
std::unordered_map<std::string, std::string> serverConfig;
|
||||||
|
bool ipv4Only;
|
||||||
|
bool hostRunning;
|
||||||
|
|
||||||
|
~ServerState() = default;
|
||||||
|
ServerState(const ServerState&) = delete;
|
||||||
|
ServerState& operator=(const ServerState&) = delete;
|
||||||
|
ServerState(ServerState&&) = default;
|
||||||
|
ServerState& operator=(ServerState&&) = default;
|
||||||
|
|
||||||
|
explicit ServerState() = default;
|
||||||
|
};
|
||||||
|
|
||||||
|
class ServerSession {
|
||||||
|
public:
|
||||||
|
// Return a reference to the Server Session instance
|
||||||
|
static ServerSession& getInstance() { static ServerSession instance; return instance; }
|
||||||
|
|
||||||
|
// Return the current server state
|
||||||
|
std::shared_ptr<ServerState> getServerState() const;
|
||||||
|
|
||||||
|
// Set the server state
|
||||||
|
void setServerState(std::shared_ptr<ServerState> state);
|
||||||
|
|
||||||
|
// Getters
|
||||||
|
std::shared_ptr<Utils::LibSodiumWrapper> getSodiumWrapper() const;
|
||||||
|
const std::string& getConfigPath() const;
|
||||||
|
const std::unordered_map<std::string, std::string>& getRawServerConfig() const;
|
||||||
|
const std::shared_ptr<Net::VirtualInterface>& getVirtualInterface() const;
|
||||||
|
bool isIPv4Only() const;
|
||||||
|
bool isHostRunning() const;
|
||||||
|
|
||||||
|
// Setters
|
||||||
|
void setSodiumWrapper(std::shared_ptr<Utils::LibSodiumWrapper> sodiumWrapper);
|
||||||
|
void setConfigPath(const std::string& configPath);
|
||||||
|
void setRawServerConfig(const std::unordered_map<std::string, std::string>& config);
|
||||||
|
void setVirtualInterface(std::shared_ptr<Net::VirtualInterface> tun);
|
||||||
|
void setIPv4Only(bool ipv4Only);
|
||||||
|
void setHostRunning(bool hostRunning);
|
||||||
|
|
||||||
|
private:
|
||||||
|
mutable std::shared_mutex mMutex;
|
||||||
|
std::shared_ptr<struct ServerState> mServerState{nullptr};
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -15,6 +15,7 @@
|
|||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <cxxopts.hpp>
|
#include <cxxopts.hpp>
|
||||||
#include <columnlynx/common/net/virtual_interface.hpp>
|
#include <columnlynx/common/net/virtual_interface.hpp>
|
||||||
|
#include <columnlynx/server/server_session.hpp>
|
||||||
|
|
||||||
#if defined(__WIN32__)
|
#if defined(__WIN32__)
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
@@ -69,6 +70,8 @@ int main(int argc, char** argv) {
|
|||||||
//WintunInitialize();
|
//WintunInitialize();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
struct ServerState serverState{};
|
||||||
|
|
||||||
// Get the config path, ENV > CLI > /etc/columnlynx
|
// Get the config path, ENV > CLI > /etc/columnlynx
|
||||||
std::string configPath = optionsObj["config-dir"].as<std::string>();
|
std::string configPath = optionsObj["config-dir"].as<std::string>();
|
||||||
const char* envConfigPath = std::getenv("COLUMNLYNX_CONFIG_DIR");
|
const char* envConfigPath = std::getenv("COLUMNLYNX_CONFIG_DIR");
|
||||||
@@ -84,11 +87,23 @@ int main(int argc, char** argv) {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unordered_map<std::string, std::string> config = Utils::getConfigMap(configPath + "server_config");
|
serverState.configPath = configPath;
|
||||||
|
|
||||||
|
#if defined(DEBUG)
|
||||||
|
std::unordered_map<std::string, std::string> config = Utils::getConfigMap(configPath + "server_config", { "NETWORK", "SUBNET_MASK" });
|
||||||
|
#else
|
||||||
|
// A production server should never use random keys. If the config file cannot be read or does not contain keys, the server will fail to start.
|
||||||
|
std::unordered_map<std::string, std::string> config = Utils::getConfigMap(configPath + "server_config", { "NETWORK", "SUBNET_MASK", "SERVER_PUBLIC_KEY", "SERVER_PRIVATE_KEY" });
|
||||||
|
#endif
|
||||||
|
|
||||||
|
serverState.serverConfig = config;
|
||||||
|
|
||||||
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());
|
||||||
|
|
||||||
|
// Store a reference to the tun in the serverState, it will increment and keep a safe reference (we love shared_ptrs)
|
||||||
|
serverState.virtualInterface = tun;
|
||||||
|
|
||||||
// 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)
|
||||||
std::shared_ptr<LibSodiumWrapper> sodiumWrapper = std::make_shared<LibSodiumWrapper>();
|
std::shared_ptr<LibSodiumWrapper> sodiumWrapper = std::make_shared<LibSodiumWrapper>();
|
||||||
|
|
||||||
@@ -117,19 +132,24 @@ int main(int argc, char** argv) {
|
|||||||
|
|
||||||
log("Server public key: " + bytesToHexString(sodiumWrapper->getPublicKey(), crypto_sign_PUBLICKEYBYTES));
|
log("Server public key: " + bytesToHexString(sodiumWrapper->getPublicKey(), crypto_sign_PUBLICKEYBYTES));
|
||||||
|
|
||||||
std::shared_ptr<bool> hostRunning = std::make_shared<bool>(true);
|
serverState.sodiumWrapper = sodiumWrapper;
|
||||||
|
serverState.ipv4Only = ipv4Only;
|
||||||
|
serverState.hostRunning = true;
|
||||||
|
|
||||||
|
// Store the global state; from now on, it should only be accessed through the ServerSession singleton, which will ensure thread safety with its internal mutex
|
||||||
|
ServerSession::getInstance().setServerState(std::make_shared<ServerState>(std::move(serverState)));
|
||||||
|
|
||||||
asio::io_context io;
|
asio::io_context io;
|
||||||
|
|
||||||
auto server = std::make_shared<TCPServer>(io, serverPort(), sodiumWrapper, hostRunning, configPath, ipv4Only);
|
auto server = std::make_shared<TCPServer>(io, serverPort());
|
||||||
auto udpServer = std::make_shared<UDPServer>(io, serverPort(), hostRunning, ipv4Only, tun);
|
auto udpServer = std::make_shared<UDPServer>(io, serverPort());
|
||||||
|
|
||||||
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;
|
ServerSession::getInstance().setHostRunning(false);
|
||||||
server->stop();
|
server->stop();
|
||||||
udpServer->stop();
|
udpServer->stop();
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -145,7 +145,7 @@ namespace ColumnLynx::Net::TCP {
|
|||||||
|
|
||||||
Utils::debug("Key attempted connect: " + Utils::bytesToHexString(signPk.data(), signPk.size()));
|
Utils::debug("Key attempted connect: " + Utils::bytesToHexString(signPk.data(), signPk.size()));
|
||||||
|
|
||||||
std::vector<std::string> whitelistedKeys = Utils::getWhitelistedKeys(mConfigDirPath);
|
std::vector<std::string> whitelistedKeys = Utils::getWhitelistedKeys(ServerSession::getInstance().getConfigPath());
|
||||||
|
|
||||||
if (std::find(whitelistedKeys.begin(), whitelistedKeys.end(), Utils::bytesToHexString(signPk.data(), signPk.size())) == whitelistedKeys.end()) {
|
if (std::find(whitelistedKeys.begin(), whitelistedKeys.end(), Utils::bytesToHexString(signPk.data(), signPk.size())) == whitelistedKeys.end()) {
|
||||||
Utils::warn("Non-whitelisted client attempted to connect, terminating. Client IP: " + reqAddr);
|
Utils::warn("Non-whitelisted client attempted to connect, terminating. Client IP: " + reqAddr);
|
||||||
@@ -156,7 +156,7 @@ namespace ColumnLynx::Net::TCP {
|
|||||||
|
|
||||||
Utils::debug("Client " + reqAddr + " passed authorized_keys");
|
Utils::debug("Client " + reqAddr + " passed authorized_keys");
|
||||||
|
|
||||||
mHandler->sendMessage(ServerMessageType::HANDSHAKE_IDENTIFY, Utils::uint8ArrayToString(mLibSodiumWrapper->getPublicKey(), crypto_sign_PUBLICKEYBYTES)); // This public key should always exist
|
mHandler->sendMessage(ServerMessageType::HANDSHAKE_IDENTIFY, Utils::uint8ArrayToString(ServerSession::getInstance().getSodiumWrapper()->getPublicKey(), crypto_sign_PUBLICKEYBYTES)); // This public key should always exist
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case ClientMessageType::HANDSHAKE_CHALLENGE: {
|
case ClientMessageType::HANDSHAKE_CHALLENGE: {
|
||||||
@@ -169,7 +169,7 @@ namespace ColumnLynx::Net::TCP {
|
|||||||
// Sign the challenge
|
// Sign the challenge
|
||||||
Signature sig = Utils::LibSodiumWrapper::signMessage(
|
Signature sig = Utils::LibSodiumWrapper::signMessage(
|
||||||
challengeData, sizeof(challengeData),
|
challengeData, sizeof(challengeData),
|
||||||
mLibSodiumWrapper->getPrivateKey()
|
ServerSession::getInstance().getSodiumWrapper()->getPrivateKey()
|
||||||
);
|
);
|
||||||
|
|
||||||
mHandler->sendMessage(ServerMessageType::HANDSHAKE_CHALLENGE_RESPONSE, Utils::uint8ArrayToString(sig.data(), sig.size())); // Placeholder response
|
mHandler->sendMessage(ServerMessageType::HANDSHAKE_CHALLENGE_RESPONSE, Utils::uint8ArrayToString(sig.data(), sig.size())); // Placeholder response
|
||||||
@@ -191,8 +191,8 @@ namespace ColumnLynx::Net::TCP {
|
|||||||
std::memcpy(ciphertext.data(), data.data() + nonce.size(), ciphertext.size());
|
std::memcpy(ciphertext.data(), data.data() + nonce.size(), ciphertext.size());
|
||||||
try {
|
try {
|
||||||
std::array<uint8_t, 32> arrayPrivateKey;
|
std::array<uint8_t, 32> arrayPrivateKey;
|
||||||
std::copy(mLibSodiumWrapper->getXPrivateKey(),
|
std::copy(ServerSession::getInstance().getSodiumWrapper()->getXPrivateKey(),
|
||||||
mLibSodiumWrapper->getXPrivateKey() + 32,
|
ServerSession::getInstance().getSodiumWrapper()->getXPrivateKey() + 32,
|
||||||
arrayPrivateKey.begin());
|
arrayPrivateKey.begin());
|
||||||
|
|
||||||
// Decrypt the AES key using the client's public key and server's private key
|
// Decrypt the AES key using the client's public key and server's private key
|
||||||
@@ -217,8 +217,10 @@ namespace ColumnLynx::Net::TCP {
|
|||||||
// Encrypt the Session ID with the established AES key (using symmetric encryption, nonce can be all zeros for this purpose)
|
// Encrypt the Session ID with the established AES key (using symmetric encryption, nonce can be all zeros for this purpose)
|
||||||
Nonce symNonce{}; // All zeros
|
Nonce symNonce{}; // All zeros
|
||||||
|
|
||||||
std::string networkString = mRawServerConfig->find("NETWORK")->second; // The load check guarantees that this value exists
|
const auto& serverConfig = ServerSession::getInstance().getRawServerConfig();
|
||||||
uint8_t configMask = std::stoi(mRawServerConfig->find("SUBNET_MASK")->second); // Same deal here
|
|
||||||
|
std::string networkString = serverConfig.find("NETWORK")->second; // The load check guarantees that this value exists
|
||||||
|
uint8_t configMask = std::stoi(serverConfig.find("SUBNET_MASK")->second); // Same deal here
|
||||||
|
|
||||||
uint32_t baseIP = Net::VirtualInterface::stringToIpv4(networkString);
|
uint32_t baseIP = Net::VirtualInterface::stringToIpv4(networkString);
|
||||||
|
|
||||||
|
|||||||
@@ -27,16 +27,13 @@ namespace ColumnLynx::Net::TCP {
|
|||||||
}
|
}
|
||||||
Utils::error("Accept failed: " + ec.message());
|
Utils::error("Accept failed: " + ec.message());
|
||||||
// Try again only if still running
|
// Try again only if still running
|
||||||
if (mHostRunning && *mHostRunning && mAcceptor.is_open())
|
if (ServerSession::getInstance().isHostRunning() && mAcceptor.is_open())
|
||||||
mStartAccept();
|
mStartAccept();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto client = TCPConnection::create(
|
auto client = TCPConnection::create(
|
||||||
std::move(socket),
|
std::move(socket),
|
||||||
mSodiumWrapper,
|
|
||||||
&mRawServerConfig,
|
|
||||||
mConfigDirPath,
|
|
||||||
[this](std::shared_ptr<TCPConnection> c) {
|
[this](std::shared_ptr<TCPConnection> c) {
|
||||||
mClients.erase(c);
|
mClients.erase(c);
|
||||||
Utils::log("Client removed.");
|
Utils::log("Client removed.");
|
||||||
@@ -46,7 +43,7 @@ namespace ColumnLynx::Net::TCP {
|
|||||||
client->start();
|
client->start();
|
||||||
Utils::log("Accepted new client connection.");
|
Utils::log("Accepted new client connection.");
|
||||||
|
|
||||||
if (mHostRunning && *mHostRunning && mAcceptor.is_open())
|
if (ServerSession::getInstance().isHostRunning() && mAcceptor.is_open())
|
||||||
mStartAccept();
|
mStartAccept();
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -16,11 +16,11 @@ namespace ColumnLynx::Net::UDP {
|
|||||||
if (ec) {
|
if (ec) {
|
||||||
if (ec == asio::error::operation_aborted) return; // Socket closed
|
if (ec == asio::error::operation_aborted) return; // Socket closed
|
||||||
// Other recv error
|
// Other recv error
|
||||||
if (mHostRunning && *mHostRunning) mStartReceive();
|
if (ServerSession::getInstance().isHostRunning()) mStartReceive();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (bytes > 0) mHandlePacket(bytes);
|
if (bytes > 0) mHandlePacket(bytes);
|
||||||
if (mHostRunning && *mHostRunning) mStartReceive();
|
if (ServerSession::getInstance().isHostRunning()) mStartReceive();
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
92
src/server/server_session.cpp
Normal file
92
src/server/server_session.cpp
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
// server_session.cpp - 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.
|
||||||
|
|
||||||
|
#include <columnlynx/server/server_session.hpp>
|
||||||
|
|
||||||
|
namespace ColumnLynx {
|
||||||
|
std::shared_ptr<ServerState> ServerSession::getServerState() const {
|
||||||
|
std::shared_lock lock(mMutex);
|
||||||
|
return mServerState;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ServerSession::setServerState(std::shared_ptr<ServerState> state) {
|
||||||
|
std::unique_lock lock(mMutex);
|
||||||
|
mServerState = std::move(state);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<Utils::LibSodiumWrapper> ServerSession::getSodiumWrapper() const {
|
||||||
|
std::shared_lock lock(mMutex);
|
||||||
|
return mServerState ? mServerState->sodiumWrapper : nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string& ServerSession::getConfigPath() const {
|
||||||
|
static const std::string emptyString;
|
||||||
|
std::shared_ptr<ServerState> state = getServerState();
|
||||||
|
return state ? state->configPath : emptyString;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::unordered_map<std::string, std::string>& ServerSession::getRawServerConfig() const {
|
||||||
|
static const std::unordered_map<std::string, std::string> emptyMap;
|
||||||
|
std::shared_ptr<ServerState> state = getServerState();
|
||||||
|
return state ? state->serverConfig : emptyMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::shared_ptr<Net::VirtualInterface>& ServerSession::getVirtualInterface() const {
|
||||||
|
static const std::shared_ptr<Net::VirtualInterface> nullTun = nullptr;
|
||||||
|
std::shared_ptr<ServerState> state = getServerState();
|
||||||
|
return state ? state->virtualInterface : nullTun;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ServerSession::isIPv4Only() const {
|
||||||
|
std::shared_ptr<ServerState> state = getServerState();
|
||||||
|
return state ? state->ipv4Only : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ServerSession::isHostRunning() const {
|
||||||
|
std::shared_ptr<ServerState> state = getServerState();
|
||||||
|
return state ? state->hostRunning : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ServerSession::setSodiumWrapper(std::shared_ptr<Utils::LibSodiumWrapper> sodiumWrapper) {
|
||||||
|
std::unique_lock lock(mMutex);
|
||||||
|
if (!mServerState)
|
||||||
|
mServerState = std::make_shared<ServerState>();
|
||||||
|
mServerState->sodiumWrapper = std::move(sodiumWrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ServerSession::setConfigPath(const std::string& configPath) {
|
||||||
|
std::unique_lock lock(mMutex);
|
||||||
|
if (!mServerState)
|
||||||
|
mServerState = std::make_shared<ServerState>();
|
||||||
|
mServerState->configPath = configPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ServerSession::setRawServerConfig(const std::unordered_map<std::string, std::string>& config) {
|
||||||
|
std::unique_lock lock(mMutex);
|
||||||
|
if (!mServerState)
|
||||||
|
mServerState = std::make_shared<ServerState>();
|
||||||
|
mServerState->serverConfig = config;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ServerSession::setVirtualInterface(std::shared_ptr<Net::VirtualInterface> tun) {
|
||||||
|
std::unique_lock lock(mMutex);
|
||||||
|
if (!mServerState)
|
||||||
|
mServerState = std::make_shared<ServerState>();
|
||||||
|
mServerState->virtualInterface = std::move(tun);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ServerSession::setIPv4Only(bool ipv4Only) {
|
||||||
|
std::unique_lock lock(mMutex);
|
||||||
|
if (!mServerState)
|
||||||
|
mServerState = std::make_shared<ServerState>();
|
||||||
|
mServerState->ipv4Only = ipv4Only;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ServerSession::setHostRunning(bool hostRunning) {
|
||||||
|
std::unique_lock lock(mMutex);
|
||||||
|
if (!mServerState)
|
||||||
|
mServerState = std::make_shared<ServerState>();
|
||||||
|
mServerState->hostRunning = hostRunning;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user