TESTING: protocol version 2
This commit is contained in:
@@ -66,7 +66,7 @@ namespace ColumnLynx::Net::TCP {
|
||||
Utils::log("Initiated graceful disconnect (half-close) to " + mRemoteIP);
|
||||
}
|
||||
|
||||
uint64_t TCPConnection::getSessionID() const {
|
||||
uint32_t TCPConnection::getSessionID() const {
|
||||
return mConnectionSessionID;
|
||||
}
|
||||
|
||||
@@ -247,11 +247,11 @@ namespace ColumnLynx::Net::TCP {
|
||||
tunConfig.dns1 = htonl(0x08080808); // 8.8.8.8
|
||||
tunConfig.dns2 = 0;
|
||||
|
||||
uint64_t sessionIDNet = Utils::chtobe64(mConnectionSessionID);
|
||||
uint32_t sessionIDNet = htonl(mConnectionSessionID);
|
||||
|
||||
std::vector<uint8_t> payload(sizeof(uint64_t) + sizeof(tunConfig));
|
||||
std::memcpy(payload.data(), &sessionIDNet, sizeof(uint64_t));
|
||||
std::memcpy(payload.data() + sizeof(uint64_t), &tunConfig, sizeof(tunConfig));
|
||||
std::vector<uint8_t> payload(sizeof(uint32_t) + sizeof(tunConfig));
|
||||
std::memcpy(payload.data(), &sessionIDNet, sizeof(uint32_t));
|
||||
std::memcpy(payload.data() + sizeof(uint32_t), &tunConfig, sizeof(tunConfig));
|
||||
|
||||
std::vector<uint8_t> encryptedPayload = Utils::LibSodiumWrapper::encryptMessage(
|
||||
payload.data(), payload.size(),
|
||||
@@ -263,6 +263,7 @@ namespace ColumnLynx::Net::TCP {
|
||||
// Add to session registry
|
||||
Utils::log("Handshake with " + reqAddr + " completed successfully. Session ID assigned (" + std::to_string(mConnectionSessionID) + ").");
|
||||
auto session = std::make_shared<SessionState>(mConnectionAESKey, std::chrono::hours(12), clientIP, htonl(0x0A0A0001), mConnectionSessionID);
|
||||
session->setBaseNonce(); // Set it
|
||||
SessionRegistry::getInstance().put(mConnectionSessionID, std::move(session));
|
||||
SessionRegistry::getInstance().lockIP(mConnectionSessionID, clientIP);
|
||||
|
||||
|
||||
@@ -26,16 +26,17 @@ namespace ColumnLynx::Net::UDP {
|
||||
}
|
||||
|
||||
void UDPServer::mHandlePacket(std::size_t bytes) {
|
||||
if (bytes < sizeof(UDPPacketHeader))
|
||||
if (bytes < sizeof(UDPPacketHeader) + sizeof(uint32_t))
|
||||
return;
|
||||
|
||||
const auto* hdr = reinterpret_cast<UDPPacketHeader*>(mRecvBuffer.data());
|
||||
|
||||
// Get plaintext session ID (assuming first 8 bytes after nonce (header))
|
||||
uint64_t sessionID = 0;
|
||||
std::memcpy(&sessionID, mRecvBuffer.data() + sizeof(UDPPacketHeader), sizeof(uint64_t));
|
||||
// Get plaintext session ID (assuming first 4 bytes after nonce (header))
|
||||
uint32_t sessionIDNet = 0;
|
||||
std::memcpy(&sessionIDNet, mRecvBuffer.data() + sizeof(UDPPacketHeader), sizeof(uint32_t));
|
||||
uint32_t sessionID = ntohl(sessionIDNet);
|
||||
|
||||
auto it = mRecvBuffer.begin() + sizeof(UDPPacketHeader) + sizeof(uint64_t);
|
||||
auto it = mRecvBuffer.begin() + sizeof(UDPPacketHeader) + sizeof(uint32_t);
|
||||
std::vector<uint8_t> encryptedPayload(it, mRecvBuffer.begin() + bytes);
|
||||
|
||||
// Get associated session state
|
||||
@@ -54,7 +55,7 @@ namespace ColumnLynx::Net::UDP {
|
||||
encryptedPayload.data(), encryptedPayload.size(),
|
||||
session->aesKey,
|
||||
hdr->nonce, "udp-data"
|
||||
//std::string(reinterpret_cast<const char*>(&sessionID), sizeof(uint64_t))
|
||||
//std::string(reinterpret_cast<const char*>(&sessionID), sizeof(uint32_t))
|
||||
);
|
||||
|
||||
Utils::debug("Passed decryption");
|
||||
@@ -76,7 +77,7 @@ namespace ColumnLynx::Net::UDP {
|
||||
}
|
||||
}
|
||||
|
||||
void UDPServer::sendData(const uint64_t sessionID, const std::string& data) {
|
||||
void UDPServer::sendData(uint32_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) {
|
||||
@@ -92,23 +93,29 @@ namespace ColumnLynx::Net::UDP {
|
||||
|
||||
// Prepare packet
|
||||
UDPPacketHeader hdr{};
|
||||
randombytes_buf(hdr.nonce.data(), hdr.nonce.size());
|
||||
uint8_t nonce[12];
|
||||
uint32_t prefix = session->noncePrefix;
|
||||
uint64_t sendCount = const_cast<SessionState*>(session.get())->send_ctr.fetch_add(1, std::memory_order_relaxed);
|
||||
memcpy(nonce, &prefix, sizeof(uint32_t)); // Prefix nonce
|
||||
memcpy(nonce + sizeof(uint32_t), &sendCount, sizeof(uint64_t)); // Use send count as nonce suffix to ensure uniqueness
|
||||
std::copy_n(nonce, 12, hdr.nonce.data());
|
||||
|
||||
auto encryptedPayload = Utils::LibSodiumWrapper::encryptMessage(
|
||||
reinterpret_cast<const uint8_t*>(data.data()), data.size(),
|
||||
session->aesKey, hdr.nonce, "udp-data"
|
||||
//std::string(reinterpret_cast<const char*>(&sessionID), sizeof(uint64_t))
|
||||
//std::string(reinterpret_cast<const char*>(&sessionID), sizeof(uint32_t))
|
||||
);
|
||||
|
||||
std::vector<uint8_t> packet;
|
||||
packet.reserve(sizeof(UDPPacketHeader) + sizeof(uint64_t) + encryptedPayload.size());
|
||||
packet.reserve(sizeof(UDPPacketHeader) + sizeof(uint32_t) + encryptedPayload.size());
|
||||
packet.insert(packet.end(),
|
||||
reinterpret_cast<uint8_t*>(&hdr),
|
||||
reinterpret_cast<uint8_t*>(&hdr) + sizeof(UDPPacketHeader)
|
||||
);
|
||||
uint32_t sessionIDNet = htonl(sessionID);
|
||||
packet.insert(packet.end(),
|
||||
reinterpret_cast<const uint8_t*>(&sessionID),
|
||||
reinterpret_cast<const uint8_t*>(&sessionID) + sizeof(sessionID)
|
||||
reinterpret_cast<const uint8_t*>(&sessionIDNet),
|
||||
reinterpret_cast<const uint8_t*>(&sessionIDNet) + sizeof(sessionIDNet)
|
||||
);
|
||||
packet.insert(packet.end(), encryptedPayload.begin(), encryptedPayload.end());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user