Added a basic TUN, testing the implementation.
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
using asio::ip::tcp;
|
||||
using namespace ColumnLynx::Utils;
|
||||
using namespace ColumnLynx::Net;
|
||||
using namespace ColumnLynx;
|
||||
|
||||
volatile sig_atomic_t done = 0;
|
||||
|
||||
@@ -62,7 +63,7 @@ int main(int argc, char** argv) {
|
||||
WintunInitialize();
|
||||
#endif
|
||||
|
||||
VirtualInterface tun("columnlynxtun0");
|
||||
VirtualInterface tun("utun1");
|
||||
log("Using virtual interface: " + tun.getName());
|
||||
|
||||
LibSodiumWrapper sodiumWrapper = LibSodiumWrapper();
|
||||
@@ -71,8 +72,8 @@ int main(int argc, char** argv) {
|
||||
uint64_t sessionID = 0;
|
||||
|
||||
asio::io_context io;
|
||||
auto client = std::make_shared<ColumnLynx::Net::TCP::TCPClient>(io, host, port, &sodiumWrapper, &aesKey, &sessionID, &insecureMode);
|
||||
auto udpClient = std::make_shared<ColumnLynx::Net::UDP::UDPClient>(io, host, port, &aesKey, &sessionID);
|
||||
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);
|
||||
|
||||
client->start();
|
||||
udpClient->start();
|
||||
@@ -88,18 +89,25 @@ int main(int argc, char** argv) {
|
||||
// Client is running
|
||||
// TODO: SIGINT or SIGTERM seems to not kill this instantly!
|
||||
while ((client->isConnected() || !client->isHandshakeComplete()) && !done) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Temp wait
|
||||
auto packet = tun.readPacket();
|
||||
|
||||
if (client->isHandshakeComplete()) {
|
||||
// Send a test UDP message every 5 seconds after handshake is complete
|
||||
static auto lastSendTime = std::chrono::steady_clock::now();
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
if (std::chrono::duration_cast<std::chrono::seconds>(now - lastSendTime).count() >= 5) {
|
||||
udpClient->sendMessage("Hello from UDP client!");
|
||||
lastSendTime = now;
|
||||
}
|
||||
}
|
||||
Nonce nonce{};
|
||||
randombytes_buf(nonce.data(), nonce.size());
|
||||
|
||||
auto ciphertext = LibSodiumWrapper::encryptMessage(
|
||||
packet.data(), packet.size(),
|
||||
aesKey,
|
||||
nonce,
|
||||
"udp-data"
|
||||
);
|
||||
|
||||
std::vector<uint8_t> udpPayload;
|
||||
udpPayload.insert(udpPayload.end(), nonce.begin(), nonce.end());
|
||||
udpPayload.insert(udpPayload.end(), reinterpret_cast<uint8_t*>(&sessionID), reinterpret_cast<uint8_t*>(&sessionID) + sizeof(sessionID));
|
||||
udpPayload.insert(udpPayload.end(), ciphertext.begin(), ciphertext.end());
|
||||
udpClient->sendMessage(std::string(udpPayload.begin(), udpPayload.end()));
|
||||
}
|
||||
|
||||
log("Client shutting down.");
|
||||
udpClient->stop();
|
||||
client->disconnect();
|
||||
|
||||
@@ -239,19 +239,29 @@ namespace ColumnLynx::Net::TCP {
|
||||
mConnectionAESKey, symNonce
|
||||
);
|
||||
|
||||
if (decrypted.size() != sizeof(mConnectionSessionID)) {
|
||||
Utils::error("Decrypted session ID has invalid size. Terminating connection.");
|
||||
if (decrypted.size() != sizeof(mConnectionSessionID) + sizeof(Protocol::TunConfig)) {
|
||||
Utils::error("Decrypted config has invalid size. Terminating connection.");
|
||||
disconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
std::memcpy(&mConnectionSessionID, decrypted.data(), sizeof(mConnectionSessionID));
|
||||
std::memcpy(&mTunConfig, decrypted.data() + sizeof(mConnectionSessionID), sizeof(Protocol::TunConfig));
|
||||
Utils::log("Connection established with Session ID: " + std::to_string(mConnectionSessionID));
|
||||
|
||||
if (mSessionIDRef) { // Copy to the global reference
|
||||
*mSessionIDRef = mConnectionSessionID;
|
||||
}
|
||||
|
||||
uint32_t clientIP = ntohl(mTunConfig.clientIP);
|
||||
uint32_t serverIP = ntohl(mTunConfig.serverIP);
|
||||
uint8_t prefixLen = mTunConfig.prefixLength;
|
||||
uint16_t mtu = mTunConfig.mtu;
|
||||
|
||||
if (mTun) {
|
||||
mTun->configureIP(clientIP, serverIP, prefixLen, mtu);
|
||||
}
|
||||
|
||||
mHandshakeComplete = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -107,5 +107,10 @@ namespace ColumnLynx::Net::UDP {
|
||||
}
|
||||
|
||||
Utils::log("UDP Client received packet from " + mRemoteEndpoint.address().to_string() + " - Packet size: " + std::to_string(bytes));
|
||||
|
||||
// Write to TUN
|
||||
if (mTunRef) {
|
||||
mTunRef->writePacket(plaintext);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,37 +14,37 @@ namespace ColumnLynx::Net {
|
||||
mFd = open("/dev/net/tun", O_RDWR);
|
||||
if (mFd < 0)
|
||||
throw std::runtime_error("Failed to open /dev/net/tun: " + std::string(strerror(errno)));
|
||||
|
||||
|
||||
struct ifreq ifr {};
|
||||
ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
|
||||
std::strncpy(ifr.ifr_name, ifName.c_str(), IFNAMSIZ);
|
||||
|
||||
|
||||
if (ioctl(mFd, TUNSETIFF, &ifr) < 0) {
|
||||
close(mFd);
|
||||
throw std::runtime_error("TUNSETIFF failed: " + std::string(strerror(errno)));
|
||||
}
|
||||
|
||||
|
||||
#elif defined(__APPLE__)
|
||||
// ---- macOS: UTUN (system control socket) ----
|
||||
mFd = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL);
|
||||
if (mFd < 0)
|
||||
throw std::runtime_error("socket(PF_SYSTEM) failed: " + std::string(strerror(errno)));
|
||||
|
||||
|
||||
struct ctl_info ctlInfo {};
|
||||
std::strncpy(ctlInfo.ctl_name, UTUN_CONTROL_NAME, sizeof(ctlInfo.ctl_name));
|
||||
if (ioctl(mFd, CTLIOCGINFO, &ctlInfo) == -1)
|
||||
throw std::runtime_error("ioctl(CTLIOCGINFO) failed: " + std::string(strerror(errno)));
|
||||
|
||||
|
||||
struct sockaddr_ctl sc {};
|
||||
sc.sc_len = sizeof(sc);
|
||||
sc.sc_family = AF_SYSTEM;
|
||||
sc.ss_sysaddr = AF_SYS_CONTROL;
|
||||
sc.sc_id = ctlInfo.ctl_id;
|
||||
sc.sc_unit = 0; // utun0 (0 = auto-assign)
|
||||
|
||||
|
||||
if (connect(mFd, (struct sockaddr*)&sc, sizeof(sc)) < 0)
|
||||
throw std::runtime_error("connect(AF_SYS_CONTROL) failed: " + std::string(strerror(errno)));
|
||||
|
||||
|
||||
// Retrieve actual utun device name
|
||||
struct sockaddr_storage addr;
|
||||
socklen_t addrlen = sizeof(addr);
|
||||
@@ -54,28 +54,28 @@ namespace ColumnLynx::Net {
|
||||
} else {
|
||||
mIfName = "utunX";
|
||||
}
|
||||
|
||||
|
||||
#elif defined(_WIN32)
|
||||
// ---- Windows: Wintun (WireGuard virtual adapter) ----
|
||||
WINTUN_ADAPTER_HANDLE adapter =
|
||||
WintunOpenAdapter(L"ColumnLynx", std::wstring(ifName.begin(), ifName.end()).c_str());
|
||||
if (!adapter)
|
||||
throw std::runtime_error("Wintun adapter not found or not installed");
|
||||
|
||||
|
||||
WINTUN_SESSION_HANDLE session =
|
||||
WintunStartSession(adapter, 0x200000); // ring buffer size
|
||||
if (!session)
|
||||
throw std::runtime_error("Failed to start Wintun session");
|
||||
|
||||
|
||||
mHandle = WintunGetReadWaitEvent(session);
|
||||
mFd = -1; // not used on Windows
|
||||
mIfName = ifName;
|
||||
|
||||
|
||||
#else
|
||||
throw std::runtime_error("Unsupported platform");
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
// ------------------------------ Destructor ------------------------------
|
||||
VirtualInterface::~VirtualInterface() {
|
||||
#if defined(__linux__) || defined(__APPLE__)
|
||||
@@ -87,7 +87,7 @@ namespace ColumnLynx::Net {
|
||||
// WintunEndSession(mSession);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
// ------------------------------ Read ------------------------------
|
||||
std::vector<uint8_t> VirtualInterface::readPacket() {
|
||||
#if defined(__linux__) || defined(__APPLE__)
|
||||
@@ -97,7 +97,7 @@ namespace ColumnLynx::Net {
|
||||
throw std::runtime_error("read() failed: " + std::string(strerror(errno)));
|
||||
buf.resize(n);
|
||||
return buf;
|
||||
|
||||
|
||||
#elif defined(_WIN32)
|
||||
WINTUN_PACKET* packet = WintunReceivePacket(mSession, nullptr);
|
||||
if (!packet) return {};
|
||||
@@ -108,14 +108,14 @@ namespace ColumnLynx::Net {
|
||||
return {};
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
// ------------------------------ Write ------------------------------
|
||||
void VirtualInterface::writePacket(const std::vector<uint8_t>& packet) {
|
||||
#if defined(__linux__) || defined(__APPLE__)
|
||||
ssize_t n = write(mFd, packet.data(), packet.size());
|
||||
if (n < 0)
|
||||
throw std::runtime_error("write() failed: " + std::string(strerror(errno)));
|
||||
|
||||
|
||||
#elif defined(_WIN32)
|
||||
WINTUN_PACKET* tx = WintunAllocateSendPacket(mSession, (DWORD)packet.size());
|
||||
if (!tx) throw std::runtime_error("WintunAllocateSendPacket failed");
|
||||
@@ -123,9 +123,93 @@ namespace ColumnLynx::Net {
|
||||
WintunSendPacket(mSession, tx);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
// ------------------------------ Accessors ------------------------------
|
||||
const std::string& VirtualInterface::getName() const { return mIfName; }
|
||||
|
||||
|
||||
int VirtualInterface::getFd() const { return mFd; }
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------
|
||||
// IP CONFIGURATION
|
||||
// ------------------------------------------------------------
|
||||
bool VirtualInterface::configureIP(uint32_t clientIP, uint32_t serverIP,
|
||||
uint8_t prefixLen, uint16_t mtu)
|
||||
{
|
||||
#if defined(__linux__)
|
||||
return mApplyLinuxIP(clientIP, serverIP, prefixLen, mtu);
|
||||
#elif defined(__APPLE__)
|
||||
return mApplyMacOSIP(clientIP, serverIP, prefixLen, mtu);
|
||||
#elif defined(_WIN32)
|
||||
return mApplyWindowsIP(clientIP, serverIP, prefixLen, mtu);
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------
|
||||
// Linux
|
||||
// ------------------------------------------------------------
|
||||
bool VirtualInterface::mApplyLinuxIP(uint32_t clientIP, uint32_t serverIP,
|
||||
uint8_t prefixLen, uint16_t mtu)
|
||||
{
|
||||
char cmd[512];
|
||||
|
||||
std::string ipStr = ipToString(clientIP);
|
||||
std::string peerStr = ipToString(serverIP);
|
||||
|
||||
snprintf(cmd, sizeof(cmd),
|
||||
"ip addr add %s/%d peer %s dev %s",
|
||||
ipStr.c_str(), prefixLen, peerStr.c_str(), mIfName.c_str());
|
||||
system(cmd);
|
||||
|
||||
snprintf(cmd, sizeof(cmd),
|
||||
"ip link set dev %s up mtu %d", mIfName.c_str(), mtu);
|
||||
system(cmd);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------
|
||||
// macOS (utun)
|
||||
// ------------------------------------------------------------
|
||||
bool VirtualInterface::mApplyMacOSIP(uint32_t clientIP, uint32_t serverIP,
|
||||
uint8_t prefixLen, uint16_t mtu)
|
||||
{
|
||||
char cmd[512];
|
||||
|
||||
std::string ipStr = ipToString(clientIP);
|
||||
std::string peerStr = ipToString(serverIP);
|
||||
|
||||
snprintf(cmd, sizeof(cmd),
|
||||
"ifconfig utun0 %s %s mtu %d up",
|
||||
ipStr.c_str(), peerStr.c_str(), mtu);
|
||||
system(cmd);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------
|
||||
// Windows (Wintun)
|
||||
// ------------------------------------------------------------
|
||||
bool VirtualInterface::mApplyWindowsIP(uint32_t clientIP, uint32_t serverIP,
|
||||
uint8_t prefixLen, uint16_t mtu)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
char ip[32], gw[32];
|
||||
strcpy(ip, ipToString(clientIP).c_str());
|
||||
strcpy(gw, ipToString(serverIP).c_str());
|
||||
|
||||
char cmd[256];
|
||||
snprintf(cmd, sizeof(cmd),
|
||||
"netsh interface ip set address name=\"%s\" static %s %d.%d.%d.%d",
|
||||
mIfName.c_str(), ip,
|
||||
(prefixLen <= 8) ? ((prefixLen << 3) & 255) : 255,
|
||||
255, 255, 255);
|
||||
system(cmd);
|
||||
|
||||
return true;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
} // namespace ColumnLynx::Net
|
||||
@@ -11,11 +11,14 @@
|
||||
#include <columnlynx/common/libsodium_wrapper.hpp>
|
||||
#include <unordered_set>
|
||||
#include <cxxopts/cxxopts.hpp>
|
||||
#include <columnlynx/common/net/virtual_interface.hpp>
|
||||
|
||||
using asio::ip::tcp;
|
||||
using namespace ColumnLynx::Utils;
|
||||
using namespace ColumnLynx::Net::TCP;
|
||||
using namespace ColumnLynx::Net::UDP;
|
||||
using namespace ColumnLynx::Net;
|
||||
using namespace ColumnLynx;
|
||||
|
||||
volatile sig_atomic_t done = 0;
|
||||
|
||||
@@ -54,6 +57,13 @@ int main(int argc, char** argv) {
|
||||
log("ColumnLynx Server, Version " + getVersion());
|
||||
log("This software is licensed under the GPLv2 only OR the GPLv3. See LICENSES/ for details.");
|
||||
|
||||
#if defined(__WIN32__)
|
||||
WintunInitialize();
|
||||
#endif
|
||||
|
||||
VirtualInterface tun("utun0");
|
||||
log("Using virtual interface: " + tun.getName());
|
||||
|
||||
// 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));
|
||||
@@ -64,7 +74,7 @@ int main(int argc, char** argv) {
|
||||
asio::io_context io;
|
||||
|
||||
auto server = std::make_shared<TCPServer>(io, serverPort(), &sodiumWrapper, &hostRunning, ipv4Only);
|
||||
auto udpServer = std::make_shared<UDPServer>(io, serverPort(), &hostRunning, ipv4Only);
|
||||
auto udpServer = std::make_shared<UDPServer>(io, serverPort(), &hostRunning, ipv4Only, &tun);
|
||||
|
||||
asio::signal_set signals(io, SIGINT, SIGTERM);
|
||||
signals.async_wait([&](const std::error_code&, int) {
|
||||
@@ -87,7 +97,21 @@ int main(int argc, char** argv) {
|
||||
log("Server started on port " + std::to_string(serverPort()));
|
||||
|
||||
while (!done) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
auto packet = tun.readPacket();
|
||||
if (packet.empty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const uint8_t* ip = packet.data();
|
||||
uint32_t dstIP = ntohl(*(uint32_t*)(ip + 16)); // IPv4 destination address offset in IPv6-mapped header
|
||||
|
||||
auto session = SessionRegistry::getInstance().getByIP(dstIP);
|
||||
if (!session) {
|
||||
Utils::warn("TUN: No session found for destination IP " + VirtualInterface::ipToString(dstIP));
|
||||
continue;
|
||||
}
|
||||
|
||||
udpServer->sendData(session->sessionID, std::string(packet.begin(), packet.end()));
|
||||
}
|
||||
|
||||
log("Shutting down server...");
|
||||
|
||||
@@ -41,6 +41,9 @@ namespace ColumnLynx::Net::TCP {
|
||||
mHandler->socket().shutdown(asio::ip::tcp::socket::shutdown_both, ec);
|
||||
mHandler->socket().close(ec);
|
||||
|
||||
SessionRegistry::getInstance().erase(mConnectionSessionID);
|
||||
SessionRegistry::getInstance().deallocIP(mConnectionSessionID);
|
||||
|
||||
Utils::log("Closed connection to " + ip);
|
||||
|
||||
if (mOnDisconnect) {
|
||||
@@ -174,16 +177,33 @@ 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::vector<uint8_t> encryptedSessionID = Utils::LibSodiumWrapper::encryptMessage(
|
||||
reinterpret_cast<uint8_t*>(&mConnectionSessionID), sizeof(mConnectionSessionID),
|
||||
|
||||
uint32_t clientIP = SessionRegistry::getInstance().getFirstAvailableIP();
|
||||
Protocol::TunConfig tunConfig{};
|
||||
tunConfig.version = Utils::protocolVersion();
|
||||
tunConfig.prefixLength = 24;
|
||||
tunConfig.mtu = 1420;
|
||||
tunConfig.serverIP = htonl(0x0A0A0001); // 10.10.0.1
|
||||
tunConfig.clientIP = htonl(clientIP); // 10.10.0.X
|
||||
tunConfig.dns1 = htonl(0x08080808); // 8.8.8.8
|
||||
tunConfig.dns2 = 0;
|
||||
|
||||
SessionRegistry::getInstance().lockIP(mConnectionSessionID, clientIP);
|
||||
|
||||
std::vector<uint8_t> payload(sizeof(uint64_t) + sizeof(tunConfig));
|
||||
std::memcpy(payload.data(), &mConnectionSessionID, sizeof(uint64_t));
|
||||
std::memcpy(payload.data() + sizeof(uint64_t), &tunConfig, sizeof(tunConfig));
|
||||
|
||||
std::vector<uint8_t> encryptedPayload = Utils::LibSodiumWrapper::encryptMessage(
|
||||
payload.data(), payload.size(),
|
||||
mConnectionAESKey, symNonce
|
||||
);
|
||||
|
||||
mHandler->sendMessage(ServerMessageType::HANDSHAKE_EXCHANGE_KEY_CONFIRM, Utils::uint8ArrayToString(encryptedSessionID.data(), encryptedSessionID.size()));
|
||||
mHandler->sendMessage(ServerMessageType::HANDSHAKE_EXCHANGE_KEY_CONFIRM, Utils::uint8ArrayToString(encryptedPayload.data(), encryptedPayload.size()));
|
||||
|
||||
// Add to session registry
|
||||
Utils::log("Handshake with " + reqAddr + " completed successfully. Session ID assigned.");
|
||||
auto session = std::make_shared<SessionState>(mConnectionAESKey, std::chrono::hours(12));
|
||||
auto session = std::make_shared<SessionState>(mConnectionAESKey, std::chrono::hours(12), clientIP, htonl(0x0A0A0001), mConnectionSessionID);
|
||||
SessionRegistry::getInstance().put(mConnectionSessionID, std::move(session));
|
||||
|
||||
} catch (const std::exception& e) {
|
||||
|
||||
@@ -62,16 +62,16 @@ namespace ColumnLynx::Net::UDP {
|
||||
// For now, just log the decrypted payload
|
||||
std::string payloadStr(plaintext.begin(), plaintext.end());
|
||||
Utils::log("UDP: Received packet from " + mRemoteEndpoint.address().to_string() + " - Payload: " + payloadStr);
|
||||
|
||||
// TODO: Process the packet payload, for now just echo back
|
||||
mSendData(sessionID, std::string(plaintext.begin(), plaintext.end()));
|
||||
if (mTun) {
|
||||
mTun->writePacket(plaintext); // Send to virtual interface
|
||||
}
|
||||
} catch (...) {
|
||||
Utils::warn("UDP: Failed to decrypt payload from " + mRemoteEndpoint.address().to_string());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void UDPServer::mSendData(const uint64_t sessionID, const std::string& data) {
|
||||
void UDPServer::sendData(const uint64_t sessionID, const std::string& data) {
|
||||
// Find the IPv4/IPv6 endpoint for the session
|
||||
std::shared_ptr<const SessionState> session = SessionRegistry::getInstance().get(sessionID);
|
||||
if (!session) {
|
||||
|
||||
Reference in New Issue
Block a user