First working alpha, version a0.4 #7

Merged
dcrubro merged 22 commits from beta into master 2025-11-18 20:09:11 +00:00
7 changed files with 28 additions and 28 deletions
Showing only changes of commit b37a999274 - Show all commits

View File

@@ -40,7 +40,8 @@ namespace ColumnLynx::Net::TCP {
mInsecureMode(insecureMode), mInsecureMode(insecureMode),
mHeartbeatTimer(mSocket.get_executor()), mHeartbeatTimer(mSocket.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()),
mTun(tun)
{} {}
void start(); void start();

View File

@@ -20,7 +20,10 @@ namespace ColumnLynx::Net::UDP {
std::array<uint8_t, 32>* aesKeyRef, std::array<uint8_t, 32>* aesKeyRef,
uint64_t* sessionIDRef, 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) { mStartReceive(); } : mSocket(ioContext), mResolver(ioContext), mHost(host), mPort(port), mAesKeyRef(aesKeyRef), mSessionIDRef(sessionIDRef), mTunRef(tunRef)
{
mStartReceive();
}
void start(); void start();
void sendMessage(const std::string& data = ""); void sendMessage(const std::string& data = "");

View File

@@ -1 +0,0 @@
=

View File

@@ -21,7 +21,7 @@ volatile sig_atomic_t done = 0;
void signalHandler(int signum) { void signalHandler(int signum) {
if (signum == SIGINT || signum == SIGTERM) { if (signum == SIGINT || signum == SIGTERM) {
log("Received termination signal. Shutting down client."); //log("Received termination signal. Shutting down client.");
done = 1; done = 1;
} }
} }
@@ -63,7 +63,7 @@ int main(int argc, char** argv) {
WintunInitialize(); WintunInitialize();
#endif #endif
std::shared_ptr<VirtualInterface> tun = std::make_shared<VirtualInterface>("utun0"); std::shared_ptr<VirtualInterface> tun = std::make_shared<VirtualInterface>("utun1");
log("Using virtual interface: " + tun->getName()); log("Using virtual interface: " + tun->getName());
LibSodiumWrapper sodiumWrapper = LibSodiumWrapper(); LibSodiumWrapper sodiumWrapper = LibSodiumWrapper();
@@ -82,36 +82,30 @@ int main(int argc, char** argv) {
std::thread ioThread([&io]() { std::thread ioThread([&io]() {
io.run(); io.run();
}); });
ioThread.detach(); //ioThread.join();
log("Client connected to " + host + ":" + port); log("Client connected to " + host + ":" + port);
// Client is running // Client is running
// TODO: SIGINT or SIGTERM seems to not kill this instantly!
while ((client->isConnected() || !client->isHandshakeComplete()) && !done) { while ((client->isConnected() || !client->isHandshakeComplete()) && !done) {
auto packet = tun->readPacket(); auto packet = tun->readPacket();
if (!client->isConnected() || done) {
break; // Bail out if connection died or signal set while blocked
}
Nonce nonce{}; if (packet.empty()) {
randombytes_buf(nonce.data(), nonce.size()); continue;
}
auto ciphertext = LibSodiumWrapper::encryptMessage( udpClient->sendMessage(std::string(packet.begin(), packet.end()));
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."); log("Client shutting down.");
udpClient->stop(); udpClient->stop();
client->disconnect(); client->disconnect();
io.stop(); io.stop();
if (ioThread.joinable())
ioThread.join(); ioThread.join();
} catch (const std::exception& e) { } catch (const std::exception& e) {

View File

@@ -6,6 +6,7 @@
namespace ColumnLynx::Net::UDP { namespace ColumnLynx::Net::UDP {
void UDPClient::start() { void UDPClient::start() {
// TODO: Add IPv6
auto endpoints = mResolver.resolve(asio::ip::udp::v4(), mHost, mPort); auto endpoints = mResolver.resolve(asio::ip::udp::v4(), mHost, mPort);
mRemoteEndpoint = *endpoints.begin(); mRemoteEndpoint = *endpoints.begin();
mSocket.open(asio::ip::udp::v4()); mSocket.open(asio::ip::udp::v4());

View File

@@ -93,8 +93,12 @@ namespace ColumnLynx::Net {
#if defined(__linux__) || defined(__APPLE__) #if defined(__linux__) || defined(__APPLE__)
std::vector<uint8_t> buf(4096); std::vector<uint8_t> buf(4096);
ssize_t n = read(mFd, buf.data(), buf.size()); ssize_t n = read(mFd, buf.data(), buf.size());
if (n < 0) if (n < 0) {
if (errno == EINTR) {
return {}; // Interrupted, return empty
}
throw std::runtime_error("read() failed: " + std::string(strerror(errno))); throw std::runtime_error("read() failed: " + std::string(strerror(errno)));
}
buf.resize(n); buf.resize(n);
return buf; return buf;

View File

@@ -84,8 +84,6 @@ int main(int argc, char** argv) {
hostRunning = false; hostRunning = false;
server->stop(); server->stop();
udpServer->stop(); udpServer->stop();
tun.reset();
tun = nullptr;
}); });
}); });