TESTING: Move server stuff to a server session config for central/global resources
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
#include <unordered_map>
|
||||
#include <cxxopts.hpp>
|
||||
#include <columnlynx/common/net/virtual_interface.hpp>
|
||||
#include <columnlynx/server/server_session.hpp>
|
||||
|
||||
#if defined(__WIN32__)
|
||||
#include <windows.h>
|
||||
@@ -69,6 +70,8 @@ int main(int argc, char** argv) {
|
||||
//WintunInitialize();
|
||||
#endif
|
||||
|
||||
struct ServerState serverState{};
|
||||
|
||||
// Get the config path, ENV > CLI > /etc/columnlynx
|
||||
std::string configPath = optionsObj["config-dir"].as<std::string>();
|
||||
const char* envConfigPath = std::getenv("COLUMNLYNX_CONFIG_DIR");
|
||||
@@ -84,11 +87,23 @@ int main(int argc, char** argv) {
|
||||
#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>());
|
||||
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)
|
||||
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));
|
||||
|
||||
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;
|
||||
|
||||
auto server = std::make_shared<TCPServer>(io, serverPort(), sodiumWrapper, hostRunning, configPath, ipv4Only);
|
||||
auto udpServer = std::make_shared<UDPServer>(io, serverPort(), hostRunning, ipv4Only, tun);
|
||||
auto server = std::make_shared<TCPServer>(io, serverPort());
|
||||
auto udpServer = std::make_shared<UDPServer>(io, serverPort());
|
||||
|
||||
asio::signal_set signals(io, SIGINT, SIGTERM);
|
||||
signals.async_wait([&](const std::error_code&, int) {
|
||||
log("Received termination signal. Shutting down server gracefully.");
|
||||
done = 1;
|
||||
asio::post(io, [&]() {
|
||||
*hostRunning = false;
|
||||
ServerSession::getInstance().setHostRunning(false);
|
||||
server->stop();
|
||||
udpServer->stop();
|
||||
});
|
||||
|
||||
@@ -145,7 +145,7 @@ namespace ColumnLynx::Net::TCP {
|
||||
|
||||
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()) {
|
||||
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");
|
||||
|
||||
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;
|
||||
}
|
||||
case ClientMessageType::HANDSHAKE_CHALLENGE: {
|
||||
@@ -169,7 +169,7 @@ namespace ColumnLynx::Net::TCP {
|
||||
// Sign the challenge
|
||||
Signature sig = Utils::LibSodiumWrapper::signMessage(
|
||||
challengeData, sizeof(challengeData),
|
||||
mLibSodiumWrapper->getPrivateKey()
|
||||
ServerSession::getInstance().getSodiumWrapper()->getPrivateKey()
|
||||
);
|
||||
|
||||
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());
|
||||
try {
|
||||
std::array<uint8_t, 32> arrayPrivateKey;
|
||||
std::copy(mLibSodiumWrapper->getXPrivateKey(),
|
||||
mLibSodiumWrapper->getXPrivateKey() + 32,
|
||||
std::copy(ServerSession::getInstance().getSodiumWrapper()->getXPrivateKey(),
|
||||
ServerSession::getInstance().getSodiumWrapper()->getXPrivateKey() + 32,
|
||||
arrayPrivateKey.begin());
|
||||
|
||||
// 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)
|
||||
Nonce symNonce{}; // All zeros
|
||||
|
||||
std::string networkString = mRawServerConfig->find("NETWORK")->second; // The load check guarantees that this value exists
|
||||
uint8_t configMask = std::stoi(mRawServerConfig->find("SUBNET_MASK")->second); // Same deal here
|
||||
const auto& serverConfig = ServerSession::getInstance().getRawServerConfig();
|
||||
|
||||
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);
|
||||
|
||||
|
||||
@@ -27,16 +27,13 @@ namespace ColumnLynx::Net::TCP {
|
||||
}
|
||||
Utils::error("Accept failed: " + ec.message());
|
||||
// Try again only if still running
|
||||
if (mHostRunning && *mHostRunning && mAcceptor.is_open())
|
||||
if (ServerSession::getInstance().isHostRunning() && mAcceptor.is_open())
|
||||
mStartAccept();
|
||||
return;
|
||||
}
|
||||
|
||||
auto client = TCPConnection::create(
|
||||
std::move(socket),
|
||||
mSodiumWrapper,
|
||||
&mRawServerConfig,
|
||||
mConfigDirPath,
|
||||
[this](std::shared_ptr<TCPConnection> c) {
|
||||
mClients.erase(c);
|
||||
Utils::log("Client removed.");
|
||||
@@ -46,7 +43,7 @@ namespace ColumnLynx::Net::TCP {
|
||||
client->start();
|
||||
Utils::log("Accepted new client connection.");
|
||||
|
||||
if (mHostRunning && *mHostRunning && mAcceptor.is_open())
|
||||
if (ServerSession::getInstance().isHostRunning() && mAcceptor.is_open())
|
||||
mStartAccept();
|
||||
}
|
||||
);
|
||||
|
||||
@@ -16,11 +16,11 @@ namespace ColumnLynx::Net::UDP {
|
||||
if (ec) {
|
||||
if (ec == asio::error::operation_aborted) return; // Socket closed
|
||||
// Other recv error
|
||||
if (mHostRunning && *mHostRunning) mStartReceive();
|
||||
if (ServerSession::getInstance().isHostRunning()) mStartReceive();
|
||||
return;
|
||||
}
|
||||
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