Compare commits
18 Commits
cb0f674c52
...
b0.3
| Author | SHA1 | Date | |
|---|---|---|---|
| f99036c523 | |||
| 3eadd41a00 | |||
| 8923f45356 | |||
| 471224b043 | |||
| 714aa52f98 | |||
| d5bf741650 | |||
| ae507c3fb9 | |||
| 072fb69a4a | |||
| 6031d9655a | |||
| 16cd980c0a | |||
| 68a825b7df | |||
| 17cc314c26 | |||
| 225aa2a55d | |||
| cab1362053 | |||
| c047cb90f0 | |||
| 5e3aef78a5 | |||
| 4ff33ffdab | |||
| 7d9018043d |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -12,4 +12,5 @@ _deps
|
|||||||
CMakeUserPresets.json
|
CMakeUserPresets.json
|
||||||
|
|
||||||
build/
|
build/
|
||||||
.vscode/
|
.vscode/
|
||||||
|
.DS_Store
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ cmake_minimum_required(VERSION 3.16)
|
|||||||
# If MAJOR is 0, and MINOR > 0, Version is BETA
|
# If MAJOR is 0, and MINOR > 0, Version is BETA
|
||||||
|
|
||||||
project(ColumnLynx
|
project(ColumnLynx
|
||||||
VERSION 0.1.0
|
VERSION 0.3.0
|
||||||
LANGUAGES CXX
|
LANGUAGES CXX
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -80,15 +80,6 @@ FetchContent_MakeAvailable(Sodium)
|
|||||||
FetchContent_MakeAvailable(asio)
|
FetchContent_MakeAvailable(asio)
|
||||||
FetchContent_MakeAvailable(cxxopts)
|
FetchContent_MakeAvailable(cxxopts)
|
||||||
|
|
||||||
# OpenSSL
|
|
||||||
find_package(OpenSSL REQUIRED)
|
|
||||||
if(OPENSSL_FOUND)
|
|
||||||
message(STATUS "Found OpenSSL version ${OPENSSL_VERSION}")
|
|
||||||
include_directories(${OPENSSL_INCLUDE_DIR})
|
|
||||||
else()
|
|
||||||
message(FATAL_ERROR "OpenSSL not found")
|
|
||||||
endif()
|
|
||||||
|
|
||||||
# ---------------------------------------------------------
|
# ---------------------------------------------------------
|
||||||
# Output directories
|
# Output directories
|
||||||
# ---------------------------------------------------------
|
# ---------------------------------------------------------
|
||||||
@@ -108,7 +99,17 @@ endforeach()
|
|||||||
# ---------------------------------------------------------
|
# ---------------------------------------------------------
|
||||||
file(GLOB_RECURSE COMMON_SRC CONFIGURE_DEPENDS src/common/*.cpp)
|
file(GLOB_RECURSE COMMON_SRC CONFIGURE_DEPENDS src/common/*.cpp)
|
||||||
add_library(common STATIC ${COMMON_SRC})
|
add_library(common STATIC ${COMMON_SRC})
|
||||||
target_link_libraries(common PUBLIC sodium OpenSSL::SSL OpenSSL::Crypto cxxopts::cxxopts)
|
target_link_libraries(common PUBLIC sodium cxxopts::cxxopts)
|
||||||
|
|
||||||
|
if (WIN32)
|
||||||
|
target_link_libraries(common PUBLIC
|
||||||
|
ws2_32
|
||||||
|
iphlpapi
|
||||||
|
advapi32
|
||||||
|
mswsock
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
|
||||||
target_include_directories(common PUBLIC
|
target_include_directories(common PUBLIC
|
||||||
${PROJECT_SOURCE_DIR}/include
|
${PROJECT_SOURCE_DIR}/include
|
||||||
${sodium_SOURCE_DIR}/src/libsodium/include
|
${sodium_SOURCE_DIR}/src/libsodium/include
|
||||||
@@ -122,7 +123,12 @@ target_compile_definitions(common PUBLIC ASIO_STANDALONE)
|
|||||||
# ---------------------------------------------------------
|
# ---------------------------------------------------------
|
||||||
file(GLOB_RECURSE CLIENT_SRC CONFIGURE_DEPENDS src/client/*.cpp)
|
file(GLOB_RECURSE CLIENT_SRC CONFIGURE_DEPENDS src/client/*.cpp)
|
||||||
add_executable(client ${CLIENT_SRC})
|
add_executable(client ${CLIENT_SRC})
|
||||||
target_link_libraries(client PRIVATE common sodium OpenSSL::SSL OpenSSL::Crypto cxxopts::cxxopts)
|
target_link_libraries(client PRIVATE common sodium cxxopts::cxxopts)
|
||||||
|
if (WIN32)
|
||||||
|
target_link_libraries(client PRIVATE
|
||||||
|
dbghelp
|
||||||
|
)
|
||||||
|
endif()
|
||||||
target_include_directories(client PRIVATE
|
target_include_directories(client PRIVATE
|
||||||
${PROJECT_SOURCE_DIR}/include
|
${PROJECT_SOURCE_DIR}/include
|
||||||
${sodium_SOURCE_DIR}/src/libsodium/include
|
${sodium_SOURCE_DIR}/src/libsodium/include
|
||||||
@@ -137,7 +143,12 @@ set_target_properties(client PROPERTIES OUTPUT_NAME "columnlynx_client")
|
|||||||
# ---------------------------------------------------------
|
# ---------------------------------------------------------
|
||||||
file(GLOB_RECURSE SERVER_SRC CONFIGURE_DEPENDS src/server/*.cpp)
|
file(GLOB_RECURSE SERVER_SRC CONFIGURE_DEPENDS src/server/*.cpp)
|
||||||
add_executable(server ${SERVER_SRC})
|
add_executable(server ${SERVER_SRC})
|
||||||
target_link_libraries(server PRIVATE common sodium OpenSSL::SSL OpenSSL::Crypto cxxopts::cxxopts)
|
target_link_libraries(server PRIVATE common sodium cxxopts::cxxopts)
|
||||||
|
if (WIN32)
|
||||||
|
target_link_libraries(server PRIVATE
|
||||||
|
dbghelp
|
||||||
|
)
|
||||||
|
endif()
|
||||||
target_include_directories(server PRIVATE
|
target_include_directories(server PRIVATE
|
||||||
${PROJECT_SOURCE_DIR}/include
|
${PROJECT_SOURCE_DIR}/include
|
||||||
${sodium_SOURCE_DIR}/src/libsodium/include
|
${sodium_SOURCE_DIR}/src/libsodium/include
|
||||||
|
|||||||
@@ -11,10 +11,6 @@
|
|||||||
#include <columnlynx/common/utils.hpp>
|
#include <columnlynx/common/utils.hpp>
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <openssl/x509.h>
|
|
||||||
#include <openssl/x509_vfy.h>
|
|
||||||
#include <openssl/pem.h>
|
|
||||||
#include <openssl/x509v3.h>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
@@ -211,4 +207,4 @@ namespace ColumnLynx::Utils {
|
|||||||
std::array<uint8_t, crypto_scalarmult_curve25519_BYTES> mXPublicKey;
|
std::array<uint8_t, crypto_scalarmult_curve25519_BYTES> mXPublicKey;
|
||||||
std::array<uint8_t, crypto_scalarmult_curve25519_BYTES> mXPrivateKey;
|
std::array<uint8_t, crypto_scalarmult_curve25519_BYTES> mXPrivateKey;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -89,4 +89,4 @@ namespace ColumnLynx::Net {
|
|||||||
std::unordered_map<uint64_t, uint32_t> mSessionIPs;
|
std::unordered_map<uint64_t, uint32_t> mSessionIPs;
|
||||||
std::unordered_map<uint32_t, std::shared_ptr<SessionState>> mIPSessions;
|
std::unordered_map<uint32_t, std::shared_ptr<SessionState>> mIPSessions;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,11 +28,14 @@
|
|||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <sys/poll.h>
|
#include <sys/poll.h>
|
||||||
#elif defined(_WIN32)
|
#elif defined(_WIN32)
|
||||||
|
#define WIN32_LEAN_AND_MEAN
|
||||||
|
#define WINTUN_STATIC
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#include <ws2tcpip.h>
|
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
|
#include <ws2tcpip.h>
|
||||||
|
#include <locale>
|
||||||
|
#include <codecvt>
|
||||||
#include <wintun/wintun.h>
|
#include <wintun/wintun.h>
|
||||||
#pragma comment(lib, "advapi32.lib")
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace ColumnLynx::Net {
|
namespace ColumnLynx::Net {
|
||||||
@@ -44,6 +47,8 @@ namespace ColumnLynx::Net {
|
|||||||
bool configureIP(uint32_t clientIP, uint32_t serverIP,
|
bool configureIP(uint32_t clientIP, uint32_t serverIP,
|
||||||
uint8_t prefixLen, uint16_t mtu);
|
uint8_t prefixLen, uint16_t mtu);
|
||||||
|
|
||||||
|
void resetIP();
|
||||||
|
|
||||||
std::vector<uint8_t> readPacket();
|
std::vector<uint8_t> readPacket();
|
||||||
void writePacket(const std::vector<uint8_t>& packet);
|
void writePacket(const std::vector<uint8_t>& packet);
|
||||||
|
|
||||||
@@ -75,6 +80,42 @@ namespace ColumnLynx::Net {
|
|||||||
return ntohl(addr.s_addr);
|
return ntohl(addr.s_addr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline std::string ipv6ToString(IPv6Addr &ip,
|
||||||
|
bool flip = false)
|
||||||
|
{
|
||||||
|
struct in6_addr addr;
|
||||||
|
|
||||||
|
if (flip) {
|
||||||
|
IPv6Addr flipped;
|
||||||
|
for (size_t i = 0; i < 16; ++i)
|
||||||
|
flipped[i] = ip[15 - i];
|
||||||
|
memcpy(addr.s6_addr, flipped.data(), 16);
|
||||||
|
} else {
|
||||||
|
memcpy(addr.s6_addr, ip.data(), 16);
|
||||||
|
}
|
||||||
|
|
||||||
|
char buf[INET6_ADDRSTRLEN];
|
||||||
|
if (!inet_ntop(AF_INET6, &addr, buf, sizeof(buf)))
|
||||||
|
return "::"; // Fallback
|
||||||
|
|
||||||
|
return std::string(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline IPv6Addr stringToIpv6(const std::string &ipStr)
|
||||||
|
{
|
||||||
|
IPv6Addr result{};
|
||||||
|
struct in6_addr addr;
|
||||||
|
|
||||||
|
if (inet_pton(AF_INET6, ipStr.c_str(), &addr) != 1) {
|
||||||
|
// "::"
|
||||||
|
result.fill(0);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(result.data(), addr.s6_addr, 16);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
static inline uint32_t prefixLengthToNetmask(uint8_t prefixLen) {
|
static inline uint32_t prefixLengthToNetmask(uint8_t prefixLen) {
|
||||||
if (prefixLen == 0) return 0;
|
if (prefixLen == 0) return 0;
|
||||||
uint32_t mask = (0xFFFFFFFF << (32 - prefixLen)) & 0xFFFFFFFF;
|
uint32_t mask = (0xFFFFFFFF << (32 - prefixLen)) & 0xFFFFFFFF;
|
||||||
@@ -89,7 +130,9 @@ namespace ColumnLynx::Net {
|
|||||||
std::string mIfName;
|
std::string mIfName;
|
||||||
int mFd; // POSIX
|
int mFd; // POSIX
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
HANDLE mHandle; // Windows
|
WINTUN_ADAPTER_HANDLE mAdapter = nullptr;
|
||||||
|
WINTUN_SESSION_HANDLE mSession = nullptr;
|
||||||
|
HANDLE mHandle = nullptr;
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -186,7 +186,7 @@ namespace ColumnLynx::Utils {
|
|||||||
|
|
||||||
// Panic the main thread and instantly halt execution. This produces a stack trace dump. Do not use by itself, throw an error instead.
|
// Panic the main thread and instantly halt execution. This produces a stack trace dump. Do not use by itself, throw an error instead.
|
||||||
static void panic(const std::string& reason) {
|
static void panic(const std::string& reason) {
|
||||||
std::cerr << "\n***\033[31m MAIN THREAD PANIC! \033[0m***\n";
|
std::cerr << "\n***\033[31m MASTER THREAD PANIC! \033[0m***\n";
|
||||||
std::cerr << "Reason: " << reason << "\n";
|
std::cerr << "Reason: " << reason << "\n";
|
||||||
std::cerr << "Dumping panic trace...\n";
|
std::cerr << "Dumping panic trace...\n";
|
||||||
|
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ namespace ColumnLynx::Net::TCP {
|
|||||||
// Set callback for disconnects
|
// Set callback for disconnects
|
||||||
void setDisconnectCallback(std::function<void(std::shared_ptr<TCPConnection>)> cb);
|
void setDisconnectCallback(std::function<void(std::shared_ptr<TCPConnection>)> cb);
|
||||||
// Disconnect the client
|
// Disconnect the client
|
||||||
void disconnect();
|
void disconnect(bool echo = true);
|
||||||
|
|
||||||
// Get the assigned session ID
|
// Get the assigned session ID
|
||||||
uint64_t getSessionID() const;
|
uint64_t getSessionID() const;
|
||||||
@@ -76,5 +76,6 @@ namespace ColumnLynx::Net::TCP {
|
|||||||
std::chrono::steady_clock::time_point mLastHeartbeatReceived;
|
std::chrono::steady_clock::time_point mLastHeartbeatReceived;
|
||||||
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
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -34,24 +34,33 @@ namespace ColumnLynx::Net::TCP {
|
|||||||
// Preload the config map
|
// Preload the config map
|
||||||
mRawServerConfig = Utils::getConfigMap("server_config", {"NETWORK", "SUBNET_MASK"});
|
mRawServerConfig = Utils::getConfigMap("server_config", {"NETWORK", "SUBNET_MASK"});
|
||||||
|
|
||||||
asio::error_code ec;
|
asio::error_code ec_open, ec_v6only, ec_bind;
|
||||||
|
|
||||||
if (!ipv4Only) {
|
if (!ipv4Only) {
|
||||||
// Try IPv6 first (dual-stack check)
|
// 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);
|
||||||
mAcceptor.open(endpoint_v6.protocol(), ec);
|
|
||||||
if (!ec) {
|
mAcceptor.open(endpoint_v6.protocol(), ec_open);
|
||||||
mAcceptor.set_option(asio::ip::v6_only(false), ec); // Allow dual-stack if possible
|
|
||||||
mAcceptor.bind(endpoint_v6, ec);
|
if (!ec_open) {
|
||||||
|
// Try enabling dual-stack, but DO NOT treat failure as fatal
|
||||||
|
mAcceptor.set_option(asio::ip::v6_only(false), ec_v6only);
|
||||||
|
|
||||||
|
// Try binding IPv6
|
||||||
|
mAcceptor.bind(endpoint_v6, ec_bind);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fallback to IPv4 if anything failed
|
// If IPv6 bind failed OR IPv6 open failed OR forced IPv4-only
|
||||||
if (ec || ipv4Only) {
|
if (ipv4Only || ec_open || ec_bind) {
|
||||||
Utils::warn("TCP: IPv6 unavailable (" + ec.message() + "), falling back to IPv4 only");
|
if (!ipv4Only)
|
||||||
|
Utils::warn("TCP: IPv6 unavailable (open=" + ec_open.message() +
|
||||||
|
", bind=" + ec_bind.message() +
|
||||||
|
"), falling back to IPv4 only");
|
||||||
|
|
||||||
asio::ip::tcp::endpoint endpoint_v4(asio::ip::tcp::v4(), port);
|
asio::ip::tcp::endpoint endpoint_v4(asio::ip::tcp::v4(), port);
|
||||||
mAcceptor.close(); // ensure clean state
|
|
||||||
|
mAcceptor.close(); // guarantee clean state
|
||||||
mAcceptor.open(endpoint_v4.protocol());
|
mAcceptor.open(endpoint_v4.protocol());
|
||||||
mAcceptor.bind(endpoint_v4);
|
mAcceptor.bind(endpoint_v4);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,24 +16,37 @@ namespace ColumnLynx::Net::UDP {
|
|||||||
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, std::shared_ptr<bool> hostRunning, bool ipv4Only = false, std::shared_ptr<VirtualInterface> tun = nullptr)
|
||||||
: mSocket(ioContext), mHostRunning(hostRunning), mTun(tun)
|
: mSocket(ioContext), mHostRunning(hostRunning), mTun(tun)
|
||||||
{
|
{
|
||||||
asio::error_code ec;
|
asio::error_code ec_open, ec_v6only, ec_bind;
|
||||||
|
|
||||||
if (!ipv4Only) {
|
if (!ipv4Only) {
|
||||||
// Try IPv6 first (dual-stack check)
|
|
||||||
asio::ip::udp::endpoint endpoint_v6(asio::ip::udp::v6(), port);
|
asio::ip::udp::endpoint endpoint_v6(asio::ip::udp::v6(), port);
|
||||||
mSocket.open(endpoint_v6.protocol(), ec);
|
|
||||||
if (!ec) {
|
// Try opening IPv6 socket
|
||||||
mSocket.set_option(asio::ip::v6_only(false), ec); // Allow dual-stack if possible
|
mSocket.open(endpoint_v6.protocol(), ec_open);
|
||||||
mSocket.bind(endpoint_v6, ec);
|
|
||||||
|
if (!ec_open) {
|
||||||
|
// Try enabling dual-stack (non fatal if it fails)
|
||||||
|
mSocket.set_option(asio::ip::v6_only(false), ec_v6only);
|
||||||
|
|
||||||
|
// Attempt bind
|
||||||
|
mSocket.bind(endpoint_v6, ec_bind);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fallback to IPv4 if anything failed
|
// Fallback to IPv4 if IPv6 is unusable
|
||||||
if (ec || ipv4Only) {
|
if (ipv4Only || ec_open || ec_bind) {
|
||||||
Utils::warn("UDP: IPv6 unavailable (" + ec.message() + "), falling back to IPv4 only");
|
if (!ipv4Only) {
|
||||||
|
Utils::warn(
|
||||||
|
"UDP: IPv6 unavailable (open=" + ec_open.message() +
|
||||||
|
", bind=" + ec_bind.message() +
|
||||||
|
"), falling back to IPv4 only"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
asio::ip::udp::endpoint endpoint_v4(asio::ip::udp::v4(), port);
|
asio::ip::udp::endpoint endpoint_v4(asio::ip::udp::v4(), port);
|
||||||
mSocket.close(); // ensure clean state
|
|
||||||
|
mSocket.close();
|
||||||
|
mSocket = asio::ip::udp::socket(ioContext); // fully reset internal state
|
||||||
mSocket.open(endpoint_v4.protocol());
|
mSocket.open(endpoint_v4.protocol());
|
||||||
mSocket.bind(endpoint_v4);
|
mSocket.bind(endpoint_v4);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,18 +21,19 @@ 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.");
|
|
||||||
done = 1;
|
done = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
// Capture SIGINT and SIGTERM for graceful shutdown
|
// Capture SIGINT and SIGTERM for graceful shutdown
|
||||||
|
#if !defined(_WIN32)
|
||||||
struct sigaction action;
|
struct sigaction action;
|
||||||
memset(&action, 0, sizeof(struct sigaction));
|
memset(&action, 0, sizeof(struct sigaction));
|
||||||
action.sa_handler = signalHandler;
|
action.sa_handler = signalHandler;
|
||||||
sigaction(SIGINT, &action, nullptr);
|
sigaction(SIGINT, &action, nullptr);
|
||||||
sigaction(SIGTERM, &action, nullptr);
|
sigaction(SIGTERM, &action, nullptr);
|
||||||
|
#endif
|
||||||
|
|
||||||
PanicHandler::init();
|
PanicHandler::init();
|
||||||
|
|
||||||
@@ -68,7 +69,7 @@ int main(int argc, char** argv) {
|
|||||||
log("This software is licensed under the GPLv2 only OR the GPLv3. See LICENSES/ for details.");
|
log("This software is licensed under the GPLv2 only OR the GPLv3. See LICENSES/ for details.");
|
||||||
|
|
||||||
#if defined(__WIN32__)
|
#if defined(__WIN32__)
|
||||||
WintunInitialize();
|
//WintunInitialize();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
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>());
|
||||||
@@ -95,14 +96,18 @@ int main(int argc, char** argv) {
|
|||||||
});
|
});
|
||||||
//ioThread.join();
|
//ioThread.join();
|
||||||
|
|
||||||
log("Client connected to " + host + ":" + port);
|
log("Attempting connection to " + host + ":" + port);
|
||||||
|
debug("Client connection flag: " + std::to_string(client->isConnected()));
|
||||||
|
debug("Client handshake flag: " + std::to_string(client->isHandshakeComplete()));
|
||||||
|
debug("isDone flag: " + std::to_string(done));
|
||||||
|
|
||||||
// Client is running
|
// Client is running
|
||||||
while ((client->isConnected() || !client->isHandshakeComplete()) && !done) {
|
while ((client->isConnected() || !client->isHandshakeComplete()) && !done) {
|
||||||
|
//debug("Client connection flag: " + std::to_string(client->isConnected()));
|
||||||
auto packet = tun->readPacket();
|
auto packet = tun->readPacket();
|
||||||
if (!client->isConnected() || done) {
|
/*if (!client->isConnected() || done) {
|
||||||
break; // Bail out if connection died or signal set while blocked
|
break; // Bail out if connection died or signal set while blocked
|
||||||
}
|
}*/
|
||||||
|
|
||||||
if (packet.empty()) {
|
if (packet.empty()) {
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
// Distributed under the terms of the GNU General Public License, either version 2 only or version 3. See LICENSES/ for details.
|
// Distributed under the terms of the GNU General Public License, either version 2 only or version 3. See LICENSES/ for details.
|
||||||
|
|
||||||
#include <columnlynx/client/net/tcp/tcp_client.hpp>
|
#include <columnlynx/client/net/tcp/tcp_client.hpp>
|
||||||
#include <arpa/inet.h>
|
//#include <arpa/inet.h>
|
||||||
|
|
||||||
namespace ColumnLynx::Net::TCP {
|
namespace ColumnLynx::Net::TCP {
|
||||||
void TCPClient::start() {
|
void TCPClient::start() {
|
||||||
@@ -13,22 +13,35 @@ namespace ColumnLynx::Net::TCP {
|
|||||||
if (!ec) {
|
if (!ec) {
|
||||||
asio::async_connect(mSocket, endpoints,
|
asio::async_connect(mSocket, endpoints,
|
||||||
[this, self](asio::error_code ec, const tcp::endpoint&) {
|
[this, self](asio::error_code ec, const tcp::endpoint&) {
|
||||||
if (!NetHelper::isExpectedDisconnect(ec)) {
|
if (!ec) {
|
||||||
mConnected = true;
|
mConnected = true;
|
||||||
Utils::log("Client connected.");
|
Utils::log("Client connected.");
|
||||||
mHandler = std::make_shared<MessageHandler>(std::move(mSocket));
|
mHandler = std::make_shared<MessageHandler>(std::move(mSocket));
|
||||||
mHandler->onMessage([this](AnyMessageType type, const std::string& data) {
|
mHandler->onMessage([this](AnyMessageType type, const std::string& data) {
|
||||||
mHandleMessage(static_cast<ServerMessageType>(MessageHandler::toUint8(type)), data);
|
mHandleMessage(static_cast<ServerMessageType>(MessageHandler::toUint8(type)), data);
|
||||||
});
|
});
|
||||||
|
// Close only after peer FIN to avoid RSTs
|
||||||
|
mHandler->onDisconnect([this](const asio::error_code& ec) {
|
||||||
|
asio::error_code ec2;
|
||||||
|
if (mHandler) {
|
||||||
|
mHandler->socket().close(ec2);
|
||||||
|
}
|
||||||
|
mConnected = false;
|
||||||
|
Utils::log(std::string("Server disconnected: ") + ec.message());
|
||||||
|
});
|
||||||
mHandler->start();
|
mHandler->start();
|
||||||
|
|
||||||
// Init connection handshake
|
// Init connection handshake
|
||||||
Utils::log("Sending handshake init to server.");
|
Utils::log("Sending handshake init to server.");
|
||||||
|
|
||||||
// Check if hostname or IPv4/IPv6
|
// Check if hostname or IPv4/IPv6
|
||||||
sockaddr_in addr4{};
|
try {
|
||||||
sockaddr_in6 addr6{};
|
asio::ip::make_address(mHost);
|
||||||
self->mIsHostDomain = inet_pton(AF_INET, mHost.c_str(), (void*)(&addr4)) != 1 && inet_pton(AF_INET6, mHost.c_str(), (void*)(&addr6)) != 1; // Voodoo black magic
|
self->mIsHostDomain = false; // IPv4 or IPv6 literal
|
||||||
|
} catch (const asio::system_error&) {
|
||||||
|
self->mIsHostDomain = true; // hostname / domain
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
std::vector<uint8_t> payload;
|
std::vector<uint8_t> payload;
|
||||||
payload.reserve(1 + crypto_box_PUBLICKEYBYTES);
|
payload.reserve(1 + crypto_box_PUBLICKEYBYTES);
|
||||||
@@ -46,7 +59,9 @@ namespace ColumnLynx::Net::TCP {
|
|||||||
|
|
||||||
mStartHeartbeat();
|
mStartHeartbeat();
|
||||||
} else {
|
} else {
|
||||||
Utils::error("Client connect failed: " + ec.message());
|
if (!NetHelper::isExpectedDisconnect(ec)) {
|
||||||
|
Utils::error("Client connect failed: " + ec.message());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
@@ -77,18 +92,14 @@ namespace ColumnLynx::Net::TCP {
|
|||||||
asio::error_code ec;
|
asio::error_code ec;
|
||||||
mHeartbeatTimer.cancel();
|
mHeartbeatTimer.cancel();
|
||||||
|
|
||||||
mHandler->socket().shutdown(tcp::socket::shutdown_both, ec);
|
// Half-close: stop sending, keep reading until peer FIN
|
||||||
|
mHandler->socket().shutdown(tcp::socket::shutdown_send, ec);
|
||||||
if (ec) {
|
if (ec) {
|
||||||
Utils::error("Error during socket shutdown: " + ec.message());
|
Utils::error("Error during socket shutdown: " + ec.message());
|
||||||
}
|
}
|
||||||
|
|
||||||
mHandler->socket().close(ec);
|
// Do not close immediately; rely on onDisconnect to finalize
|
||||||
if (ec) {
|
Utils::log("Client initiated graceful disconnect (half-close).");
|
||||||
Utils::error("Error during socket close: " + ec.message());
|
|
||||||
}
|
|
||||||
|
|
||||||
mConnected = false;
|
|
||||||
Utils::log("Client disconnected.");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -269,6 +280,12 @@ namespace ColumnLynx::Net::TCP {
|
|||||||
disconnect(false);
|
disconnect(false);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case ServerMessageType::KILL_CONNECTION:
|
||||||
|
Utils::warn("Server is killing the connection: " + data);
|
||||||
|
if (mConnected) {
|
||||||
|
disconnect(false);
|
||||||
|
}
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
Utils::log("Received unknown message type from server.");
|
Utils::log("Received unknown message type from server.");
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -6,10 +6,41 @@
|
|||||||
|
|
||||||
namespace ColumnLynx::Net::UDP {
|
namespace ColumnLynx::Net::UDP {
|
||||||
void UDPClient::start() {
|
void UDPClient::start() {
|
||||||
// TODO: Add IPv6
|
asio::error_code ec;
|
||||||
auto endpoints = mResolver.resolve(asio::ip::udp::v4(), mHost, mPort);
|
|
||||||
|
// Resolve using an unspecified protocol (allows both IPv4 and IPv6)
|
||||||
|
auto endpoints = mResolver.resolve(
|
||||||
|
asio::ip::udp::v6(), // Try IPv6 first (dual-stack with v4)
|
||||||
|
mHost,
|
||||||
|
mPort,
|
||||||
|
ec
|
||||||
|
);
|
||||||
|
|
||||||
|
if (ec) {
|
||||||
|
// If IPv6 fails (host has no AAAA), try IPv4
|
||||||
|
endpoints = mResolver.resolve(
|
||||||
|
asio::ip::udp::v4(),
|
||||||
|
mHost,
|
||||||
|
mPort,
|
||||||
|
ec
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ec) {
|
||||||
|
Utils::error("UDP resolve failed: " + ec.message());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use whichever endpoint resolved
|
||||||
mRemoteEndpoint = *endpoints.begin();
|
mRemoteEndpoint = *endpoints.begin();
|
||||||
mSocket.open(asio::ip::udp::v4());
|
|
||||||
|
// Open socket using the resolved endpoint's protocol
|
||||||
|
mSocket.open(mRemoteEndpoint.protocol(), ec);
|
||||||
|
if (ec) {
|
||||||
|
Utils::error("UDP socket open failed: " + ec.message());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Utils::log("UDP Client ready to send to " + mRemoteEndpoint.address().to_string() + ":" + std::to_string(mRemoteEndpoint.port()));
|
Utils::log("UDP Client ready to send to " + mRemoteEndpoint.address().to_string() + ":" + std::to_string(mRemoteEndpoint.port()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -43,13 +43,19 @@ namespace ColumnLynx::Net::TCP {
|
|||||||
auto self = shared_from_this();
|
auto self = shared_from_this();
|
||||||
asio::async_read(mSocket, asio::buffer(mHeader),
|
asio::async_read(mSocket, asio::buffer(mHeader),
|
||||||
[this, self](asio::error_code ec, std::size_t) {
|
[this, self](asio::error_code ec, std::size_t) {
|
||||||
if (!NetHelper::isExpectedDisconnect(ec)) {
|
if (!ec) {
|
||||||
mCurrentType = decodeMessageType(mHeader[0]);
|
mCurrentType = decodeMessageType(mHeader[0]);
|
||||||
|
|
||||||
uint16_t len = (mHeader[1] << 8) | mHeader[2];
|
uint16_t len = (mHeader[1] << 8) | mHeader[2];
|
||||||
mReadBody(len);
|
mReadBody(len);
|
||||||
} else {
|
} else {
|
||||||
Utils::error("Header read failed: " + ec.message());
|
if (!NetHelper::isExpectedDisconnect(ec)) {
|
||||||
|
Utils::error("Header read failed: " + ec.message());
|
||||||
|
}
|
||||||
|
// Connection closed, trigger disconnect handler
|
||||||
|
if (mOnDisconnect) {
|
||||||
|
mOnDisconnect(ec);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
@@ -61,7 +67,7 @@ namespace ColumnLynx::Net::TCP {
|
|||||||
|
|
||||||
asio::async_read(mSocket, asio::buffer(mBody),
|
asio::async_read(mSocket, asio::buffer(mBody),
|
||||||
[this, self](asio::error_code ec, std::size_t) {
|
[this, self](asio::error_code ec, std::size_t) {
|
||||||
if (!NetHelper::isExpectedDisconnect(ec)) {
|
if (!ec) {
|
||||||
std::string payload(mBody.begin(), mBody.end());
|
std::string payload(mBody.begin(), mBody.end());
|
||||||
|
|
||||||
// Dispatch based on message type
|
// Dispatch based on message type
|
||||||
@@ -71,8 +77,10 @@ namespace ColumnLynx::Net::TCP {
|
|||||||
|
|
||||||
mReadHeader(); // Keep listening
|
mReadHeader(); // Keep listening
|
||||||
} else {
|
} else {
|
||||||
Utils::error("Body read failed: " + ec.message());
|
if (!NetHelper::isExpectedDisconnect(ec)) {
|
||||||
|
Utils::error("Body read failed: " + ec.message());
|
||||||
|
}
|
||||||
|
// Connection closed, trigger disconnect handler
|
||||||
if (mOnDisconnect) {
|
if (mOnDisconnect) {
|
||||||
mOnDisconnect(ec);
|
mOnDisconnect(ec);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ namespace ColumnLynx::Utils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string getVersion() {
|
std::string getVersion() {
|
||||||
return "b0.1";
|
return "b0.3";
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned short serverPort() {
|
unsigned short serverPort() {
|
||||||
|
|||||||
@@ -6,6 +6,55 @@
|
|||||||
|
|
||||||
// This is all fucking voodoo dark magic.
|
// This is all fucking voodoo dark magic.
|
||||||
|
|
||||||
|
#if defined(_WIN32)
|
||||||
|
|
||||||
|
static HMODULE gWintun = nullptr;
|
||||||
|
|
||||||
|
static WINTUN_OPEN_ADAPTER_FUNC* pWintunOpenAdapter;
|
||||||
|
static WINTUN_START_SESSION_FUNC* pWintunStartSession;
|
||||||
|
static WINTUN_END_SESSION_FUNC* pWintunEndSession;
|
||||||
|
static WINTUN_GET_READ_WAIT_EVENT_FUNC* pWintunGetReadWaitEvent;
|
||||||
|
static WINTUN_RECEIVE_PACKET_FUNC* pWintunReceivePacket;
|
||||||
|
static WINTUN_RELEASE_RECEIVE_PACKET_FUNC* pWintunReleaseReceivePacket;
|
||||||
|
static WINTUN_ALLOCATE_SEND_PACKET_FUNC* pWintunAllocateSendPacket;
|
||||||
|
static WINTUN_SEND_PACKET_FUNC* pWintunSendPacket;
|
||||||
|
static WINTUN_CREATE_ADAPTER_FUNC* pWintunCreateAdapter;
|
||||||
|
|
||||||
|
static void InitializeWintun()
|
||||||
|
{
|
||||||
|
if (gWintun)
|
||||||
|
return;
|
||||||
|
|
||||||
|
gWintun = LoadLibraryExW(
|
||||||
|
L"wintun.dll",
|
||||||
|
nullptr,
|
||||||
|
LOAD_LIBRARY_SEARCH_APPLICATION_DIR
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!gWintun)
|
||||||
|
throw std::runtime_error("Failed to load wintun.dll");
|
||||||
|
|
||||||
|
#define RESOLVE(name, type) \
|
||||||
|
p##name = reinterpret_cast<type*>( \
|
||||||
|
GetProcAddress(gWintun, #name)); \
|
||||||
|
if (!p##name) \
|
||||||
|
throw std::runtime_error("Missing Wintun symbol: " #name);
|
||||||
|
|
||||||
|
RESOLVE(WintunOpenAdapter, WINTUN_OPEN_ADAPTER_FUNC)
|
||||||
|
RESOLVE(WintunStartSession, WINTUN_START_SESSION_FUNC)
|
||||||
|
RESOLVE(WintunEndSession, WINTUN_END_SESSION_FUNC)
|
||||||
|
RESOLVE(WintunGetReadWaitEvent, WINTUN_GET_READ_WAIT_EVENT_FUNC)
|
||||||
|
RESOLVE(WintunReceivePacket, WINTUN_RECEIVE_PACKET_FUNC)
|
||||||
|
RESOLVE(WintunReleaseReceivePacket, WINTUN_RELEASE_RECEIVE_PACKET_FUNC)
|
||||||
|
RESOLVE(WintunAllocateSendPacket, WINTUN_ALLOCATE_SEND_PACKET_FUNC)
|
||||||
|
RESOLVE(WintunSendPacket, WINTUN_SEND_PACKET_FUNC)
|
||||||
|
RESOLVE(WintunCreateAdapter, WINTUN_CREATE_ADAPTER_FUNC)
|
||||||
|
|
||||||
|
#undef RESOLVE
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // _WIN32
|
||||||
|
|
||||||
namespace ColumnLynx::Net {
|
namespace ColumnLynx::Net {
|
||||||
// ------------------------------ Constructor ------------------------------
|
// ------------------------------ Constructor ------------------------------
|
||||||
VirtualInterface::VirtualInterface(const std::string& ifName)
|
VirtualInterface::VirtualInterface(const std::string& ifName)
|
||||||
@@ -63,20 +112,33 @@ namespace ColumnLynx::Net {
|
|||||||
Utils::log("VirtualInterface: opened macOS UTUN: " + mIfName);
|
Utils::log("VirtualInterface: opened macOS UTUN: " + mIfName);
|
||||||
|
|
||||||
#elif defined(_WIN32)
|
#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 =
|
// Convert to Windows' wchar_t* thingy
|
||||||
WintunStartSession(adapter, 0x200000); // ring buffer size
|
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
|
||||||
if (!session)
|
std::wstring wide_string = converter.from_bytes(mIfName);
|
||||||
|
const wchar_t* wide_c_str = wide_string.c_str();
|
||||||
|
|
||||||
|
InitializeWintun();
|
||||||
|
|
||||||
|
mAdapter = pWintunOpenAdapter(wide_c_str);
|
||||||
|
|
||||||
|
if (!mAdapter) {
|
||||||
|
mAdapter = pWintunCreateAdapter(
|
||||||
|
wide_c_str,
|
||||||
|
L"ColumnLynx",
|
||||||
|
nullptr
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!mAdapter)
|
||||||
|
throw std::runtime_error("Failed to open or create Wintun adapter (run running as admin)");
|
||||||
|
|
||||||
|
mSession = pWintunStartSession(mAdapter, 0x200000);
|
||||||
|
if (!mSession)
|
||||||
throw std::runtime_error("Failed to start Wintun session");
|
throw std::runtime_error("Failed to start Wintun session");
|
||||||
|
|
||||||
mHandle = WintunGetReadWaitEvent(session);
|
mHandle = pWintunGetReadWaitEvent(mSession);
|
||||||
mFd = -1; // not used on Windows
|
mFd = -1;
|
||||||
mIfName = ifName;
|
|
||||||
|
|
||||||
#else
|
#else
|
||||||
throw std::runtime_error("Unsupported platform");
|
throw std::runtime_error("Unsupported platform");
|
||||||
@@ -89,9 +151,8 @@ namespace ColumnLynx::Net {
|
|||||||
if (mFd >= 0)
|
if (mFd >= 0)
|
||||||
close(mFd);
|
close(mFd);
|
||||||
#elif defined(_WIN32)
|
#elif defined(_WIN32)
|
||||||
// Wintun sessions need explicit stop
|
if (mSession)
|
||||||
// (assuming you stored the session handle as member)
|
pWintunEndSession(mSession);
|
||||||
// WintunEndSession(mSession);
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,11 +218,13 @@ namespace ColumnLynx::Net {
|
|||||||
|
|
||||||
#elif defined(_WIN32)
|
#elif defined(_WIN32)
|
||||||
|
|
||||||
WINTUN_PACKET* packet = WintunReceivePacket(mSession, nullptr);
|
DWORD size = 0;
|
||||||
if (!packet) return {};
|
BYTE* packet = pWintunReceivePacket(mSession, &size);
|
||||||
|
if (!packet)
|
||||||
|
return {};
|
||||||
|
|
||||||
std::vector<uint8_t> buf(packet->Data, packet->Data + packet->Length);
|
std::vector<uint8_t> buf(packet, packet + size);
|
||||||
WintunReleaseReceivePacket(mSession, packet);
|
pWintunReleaseReceivePacket(mSession, packet);
|
||||||
return buf;
|
return buf;
|
||||||
|
|
||||||
#else
|
#else
|
||||||
@@ -206,12 +269,16 @@ namespace ColumnLynx::Net {
|
|||||||
|
|
||||||
#elif defined(_WIN32)
|
#elif defined(_WIN32)
|
||||||
|
|
||||||
WINTUN_PACKET* tx = WintunAllocateSendPacket(mSession, (DWORD)packet.size());
|
BYTE* tx = pWintunAllocateSendPacket(
|
||||||
|
mSession,
|
||||||
|
static_cast<DWORD>(packet.size())
|
||||||
|
);
|
||||||
|
|
||||||
if (!tx)
|
if (!tx)
|
||||||
throw std::runtime_error("WintunAllocateSendPacket failed");
|
throw std::runtime_error("WintunAllocateSendPacket failed");
|
||||||
|
|
||||||
memcpy(tx->Data, packet.data(), packet.size());
|
memcpy(tx, packet.data(), packet.size());
|
||||||
WintunSendPacket(mSession, tx);
|
pWintunSendPacket(mSession, tx);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@@ -237,7 +304,46 @@ namespace ColumnLynx::Net {
|
|||||||
return false;
|
return false;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void VirtualInterface::resetIP() {
|
||||||
|
#if defined(__linux__)
|
||||||
|
char cmd[512];
|
||||||
|
snprintf(cmd, sizeof(cmd),
|
||||||
|
"ip addr flush dev %s",
|
||||||
|
mIfName.c_str()
|
||||||
|
);
|
||||||
|
system(cmd);
|
||||||
|
#elif defined(__APPLE__)
|
||||||
|
char cmd[512];
|
||||||
|
snprintf(cmd, sizeof(cmd),
|
||||||
|
"ifconfig %s inet 0.0.0.0 delete",
|
||||||
|
mIfName.c_str()
|
||||||
|
);
|
||||||
|
system(cmd);
|
||||||
|
|
||||||
|
snprintf(cmd, sizeof(cmd),
|
||||||
|
"ifconfig %s inet6 :: delete",
|
||||||
|
mIfName.c_str()
|
||||||
|
);
|
||||||
|
system(cmd);
|
||||||
|
#elif defined(_WIN32)
|
||||||
|
char cmd[512];
|
||||||
|
// Remove any persistent routes associated with this interface
|
||||||
|
snprintf(cmd, sizeof(cmd),
|
||||||
|
"netsh routing ip delete persistentroute all name=\"%s\"",
|
||||||
|
mIfName.c_str()
|
||||||
|
);
|
||||||
|
system(cmd);
|
||||||
|
|
||||||
|
// Reset to DHCP
|
||||||
|
snprintf(cmd, sizeof(cmd),
|
||||||
|
"netsh interface ip set address name=\"%s\" dhcp",
|
||||||
|
mIfName.c_str()
|
||||||
|
);
|
||||||
|
system(cmd);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------
|
// ------------------------------------------------------------
|
||||||
// Linux
|
// Linux
|
||||||
// ------------------------------------------------------------
|
// ------------------------------------------------------------
|
||||||
@@ -322,7 +428,11 @@ namespace ColumnLynx::Net {
|
|||||||
uint32_t maskInt = (prefixLen == 0) ? 0 : (0xFFFFFFFF << (32 - prefixLen));
|
uint32_t maskInt = (prefixLen == 0) ? 0 : (0xFFFFFFFF << (32 - prefixLen));
|
||||||
mask = ipv4ToString(maskInt);
|
mask = ipv4ToString(maskInt);
|
||||||
|
|
||||||
char cmd[256];
|
// Calculate network address from IP and mask
|
||||||
|
uint32_t networkInt = (clientIP & maskInt);
|
||||||
|
std::string network = ipv4ToString(networkInt);
|
||||||
|
|
||||||
|
char cmd[512];
|
||||||
|
|
||||||
// 1. Set the static IP + mask + gateway
|
// 1. Set the static IP + mask + gateway
|
||||||
snprintf(cmd, sizeof(cmd),
|
snprintf(cmd, sizeof(cmd),
|
||||||
@@ -338,6 +448,14 @@ namespace ColumnLynx::Net {
|
|||||||
);
|
);
|
||||||
system(cmd);
|
system(cmd);
|
||||||
|
|
||||||
|
// 3. Add route for the VPN network to go through the TUN interface
|
||||||
|
// This is critical: tells Windows to send packets destined for the server/network through the TUN interface
|
||||||
|
snprintf(cmd, sizeof(cmd),
|
||||||
|
"netsh routing ip add persistentroute dest=%s/%d name=\"%s\" nexthopcfg=%s",
|
||||||
|
network.c_str(), prefixLen, mIfName.c_str(), gw.c_str()
|
||||||
|
);
|
||||||
|
system(cmd);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
#else
|
#else
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -4,6 +4,8 @@
|
|||||||
|
|
||||||
#include <asio.hpp>
|
#include <asio.hpp>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <thread>
|
||||||
|
#include <chrono>
|
||||||
#include <columnlynx/common/utils.hpp>
|
#include <columnlynx/common/utils.hpp>
|
||||||
#include <columnlynx/common/panic_handler.hpp>
|
#include <columnlynx/common/panic_handler.hpp>
|
||||||
#include <columnlynx/server/net/tcp/tcp_server.hpp>
|
#include <columnlynx/server/net/tcp/tcp_server.hpp>
|
||||||
@@ -23,20 +25,7 @@ using namespace ColumnLynx;
|
|||||||
|
|
||||||
volatile sig_atomic_t done = 0;
|
volatile sig_atomic_t done = 0;
|
||||||
|
|
||||||
void signalHandler(int signum) {
|
|
||||||
if (signum == SIGINT || signum == SIGTERM) {
|
|
||||||
log("Received termination signal. Shutting down server gracefully.");
|
|
||||||
done = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
// Capture SIGINT and SIGTERM for graceful shutdown
|
|
||||||
struct sigaction action;
|
|
||||||
memset(&action, 0, sizeof(struct sigaction));
|
|
||||||
action.sa_handler = signalHandler;
|
|
||||||
sigaction(SIGINT, &action, nullptr);
|
|
||||||
sigaction(SIGTERM, &action, nullptr);
|
|
||||||
|
|
||||||
cxxopts::Options options("columnlynx_server", "ColumnLynx Server Application");
|
cxxopts::Options options("columnlynx_server", "ColumnLynx Server Application");
|
||||||
|
|
||||||
@@ -68,7 +57,7 @@ int main(int argc, char** argv) {
|
|||||||
log("This software is licensed under the GPLv2 only OR the GPLv3. See LICENSES/ for details.");
|
log("This software is licensed under the GPLv2 only OR the GPLv3. See LICENSES/ for details.");
|
||||||
|
|
||||||
#if defined(__WIN32__)
|
#if defined(__WIN32__)
|
||||||
WintunInitialize();
|
//WintunInitialize();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
std::unordered_map<std::string, std::string> config = Utils::getConfigMap(optionsObj["config"].as<std::string>());
|
std::unordered_map<std::string, std::string> config = Utils::getConfigMap(optionsObj["config"].as<std::string>());
|
||||||
@@ -128,6 +117,8 @@ int main(int argc, char** argv) {
|
|||||||
while (!done) {
|
while (!done) {
|
||||||
auto packet = tun->readPacket();
|
auto packet = tun->readPacket();
|
||||||
if (packet.empty()) {
|
if (packet.empty()) {
|
||||||
|
// Small sleep to avoid busy-waiting and to allow signal processing
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,20 +6,39 @@
|
|||||||
|
|
||||||
namespace ColumnLynx::Net::TCP {
|
namespace ColumnLynx::Net::TCP {
|
||||||
void TCPConnection::start() {
|
void TCPConnection::start() {
|
||||||
|
try {
|
||||||
|
// Cache the remote IP early to avoid calling remote_endpoint() on closed sockets later
|
||||||
|
mRemoteIP = mHandler->socket().remote_endpoint().address().to_string();
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
mRemoteIP = "unknown";
|
||||||
|
Utils::warn("Failed to get remote endpoint: " + std::string(e.what()));
|
||||||
|
}
|
||||||
|
|
||||||
mHandler->onMessage([this](AnyMessageType type, const std::string& data) {
|
mHandler->onMessage([this](AnyMessageType type, const std::string& data) {
|
||||||
mHandleMessage(static_cast<ClientMessageType>(MessageHandler::toUint8(type)), data);
|
mHandleMessage(static_cast<ClientMessageType>(MessageHandler::toUint8(type)), data);
|
||||||
});
|
});
|
||||||
|
|
||||||
mHandler->onDisconnect([this](const asio::error_code& ec) {
|
mHandler->onDisconnect([this](const asio::error_code& ec) {
|
||||||
Utils::log("Client disconnected: " + mHandler->socket().remote_endpoint().address().to_string() + " - " + ec.message());
|
// Peer has closed; finalize locally without sending RST
|
||||||
disconnect();
|
Utils::log("Client disconnected: " + mRemoteIP + " - " + ec.message());
|
||||||
|
asio::error_code ec2;
|
||||||
|
mHandler->socket().close(ec2);
|
||||||
|
|
||||||
|
SessionRegistry::getInstance().erase(mConnectionSessionID);
|
||||||
|
SessionRegistry::getInstance().deallocIP(mConnectionSessionID);
|
||||||
|
|
||||||
|
Utils::log("Closed connection to " + mRemoteIP);
|
||||||
|
|
||||||
|
if (mOnDisconnect) {
|
||||||
|
mOnDisconnect(shared_from_this());
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
mHandler->start();
|
mHandler->start();
|
||||||
mStartHeartbeat();
|
mStartHeartbeat();
|
||||||
|
|
||||||
// Placeholder for message handling setup
|
// Placeholder for message handling setup
|
||||||
Utils::log("Client connected: " + mHandler->socket().remote_endpoint().address().to_string());
|
Utils::log("Client connected: " + mRemoteIP);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TCPConnection::sendMessage(ServerMessageType type, const std::string& data) {
|
void TCPConnection::sendMessage(ServerMessageType type, const std::string& data) {
|
||||||
@@ -32,23 +51,19 @@ namespace ColumnLynx::Net::TCP {
|
|||||||
mOnDisconnect = std::move(cb);
|
mOnDisconnect = std::move(cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TCPConnection::disconnect() {
|
void TCPConnection::disconnect(bool echo) {
|
||||||
std::string ip = mHandler->socket().remote_endpoint().address().to_string();
|
if (echo) {
|
||||||
|
mHandler->sendMessage(ServerMessageType::GRACEFUL_DISCONNECT, "Server initiated disconnect.");
|
||||||
mHandler->sendMessage(ServerMessageType::GRACEFUL_DISCONNECT, "Server initiated disconnect.");
|
}
|
||||||
mHeartbeatTimer.cancel();
|
mHeartbeatTimer.cancel();
|
||||||
asio::error_code ec;
|
asio::error_code ec;
|
||||||
mHandler->socket().shutdown(asio::ip::tcp::socket::shutdown_both, ec);
|
// Half-close: stop sending, keep reading until peer FIN
|
||||||
mHandler->socket().close(ec);
|
mHandler->socket().shutdown(asio::ip::tcp::socket::shutdown_send, ec);
|
||||||
|
if (ec) {
|
||||||
SessionRegistry::getInstance().erase(mConnectionSessionID);
|
Utils::error("Error during socket shutdown: " + ec.message());
|
||||||
SessionRegistry::getInstance().deallocIP(mConnectionSessionID);
|
|
||||||
|
|
||||||
Utils::log("Closed connection to " + ip);
|
|
||||||
|
|
||||||
if (mOnDisconnect) {
|
|
||||||
mOnDisconnect(shared_from_this());
|
|
||||||
}
|
}
|
||||||
|
// Do not close immediately; final cleanup happens in onDisconnect
|
||||||
|
Utils::log("Initiated graceful disconnect (half-close) to " + mRemoteIP);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t TCPConnection::getSessionID() const {
|
uint64_t TCPConnection::getSessionID() const {
|
||||||
@@ -92,7 +107,7 @@ namespace ColumnLynx::Net::TCP {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void TCPConnection::mHandleMessage(ClientMessageType type, const std::string& data) {
|
void TCPConnection::mHandleMessage(ClientMessageType type, const std::string& data) {
|
||||||
std::string reqAddr = mHandler->socket().remote_endpoint().address().to_string();
|
std::string& reqAddr = mRemoteIP;
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case ClientMessageType::HANDSHAKE_INIT: {
|
case ClientMessageType::HANDSHAKE_INIT: {
|
||||||
@@ -272,6 +287,11 @@ namespace ColumnLynx::Net::TCP {
|
|||||||
disconnect();
|
disconnect();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case ClientMessageType::KILL_CONNECTION: {
|
||||||
|
Utils::warn("Received KILL_CONNECTION from " + reqAddr + ": " + data);
|
||||||
|
disconnect();
|
||||||
|
break;
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
Utils::warn("Unhandled message type from " + reqAddr);
|
Utils::warn("Unhandled message type from " + reqAddr);
|
||||||
break;
|
break;
|
||||||
|
|||||||
Reference in New Issue
Block a user