From eb2d40339bd177a3d8d91bfcfd6a9b365b9055e7 Mon Sep 17 00:00:00 2001 From: lanthora Date: Thu, 27 Jun 2024 16:06:26 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=B8=85=E9=99=A4=20Windows?= =?UTF-8?q?=20=E7=B3=BB=E7=BB=9F=E8=B7=AF=E7=94=B1=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/client.cc | 4 ++++ src/tun/linux.cc | 3 --- src/tun/windows.cc | 12 +++++++++++- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/core/client.cc b/src/core/client.cc index fc030d2..839781e 100644 --- a/src/core/client.cc +++ b/src/core/client.cc @@ -582,6 +582,10 @@ void Client::handleSysRtMessage(WebSocketMessage &message) { entry.mask = Address::netToHost(rt[idx].mask); entry.next = Address::netToHost(rt[idx].nexthop); + if (entry.next == this->tun.getIP()) { + continue; + } + std::string dstStr = Address::ipToStr(entry.dst); std::string maskStr = Address::ipToStr(entry.mask); std::string nextStr = Address::ipToStr(entry.next); diff --git a/src/tun/linux.cc b/src/tun/linux.cc index 84476e7..81fb287 100644 --- a/src/tun/linux.cc +++ b/src/tun/linux.cc @@ -179,9 +179,6 @@ class LinuxTun { } int setSysRtTable(uint32_t dst, uint32_t mask, uint32_t nexthop) { - if (nexthop == this->ip) { - return 0; - } int sockfd = socket(AF_INET, SOCK_DGRAM, 0); if (sockfd == -1) { spdlog::error("set route failed: create socket failed"); diff --git a/src/tun/windows.cc b/src/tun/windows.cc index 04133af..543a6be 100644 --- a/src/tun/windows.cc +++ b/src/tun/windows.cc @@ -8,6 +8,7 @@ #include #include #include +#include #include // clang-format off #include @@ -174,6 +175,11 @@ class WindowsTun { } int down() { + while (!routes.empty()) { + DeleteIpForwardEntry(&routes.top()); + routes.pop(); + } + if (this->session) { WintunEndSession(this->session); this->session = NULL; @@ -234,9 +240,12 @@ class WindowsTun { route.dwForwardMetric5 = MIB_IPROUTE_METRIC_UNUSED; DWORD result = CreateIpForwardEntry(&route); - if (result != NO_ERROR) { + if (result == NO_ERROR) { + routes.push(route); + } else { spdlog::error("add route failed: {}", result); } + return 0; } @@ -247,6 +256,7 @@ class WindowsTun { int mtu; int timeout; NET_IFINDEX ifindex; + std::stack routes; WINTUN_ADAPTER_HANDLE adapter = NULL; WINTUN_SESSION_HANDLE session = NULL;