Skip to content

Commit

Permalink
自动清除 Windows 系统路由表
Browse files Browse the repository at this point in the history
  • Loading branch information
lanthora committed Jun 27, 2024
1 parent f1abf0b commit eb2d403
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/core/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 0 additions & 3 deletions src/tun/linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
12 changes: 11 additions & 1 deletion src/tun/windows.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <openssl/sha.h>
#include <spdlog/fmt/bin_to_hex.h>
#include <spdlog/spdlog.h>
#include <stack>
#include <string>
// clang-format off
#include <winsock2.h>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -247,6 +256,7 @@ class WindowsTun {
int mtu;
int timeout;
NET_IFINDEX ifindex;
std::stack<MIB_IPFORWARDROW> routes;

WINTUN_ADAPTER_HANDLE adapter = NULL;
WINTUN_SESSION_HANDLE session = NULL;
Expand Down

0 comments on commit eb2d403

Please sign in to comment.