Skip to content

Commit

Permalink
修复路由功能导致的局域网对等连接失效问题
Browse files Browse the repository at this point in the history
  • Loading branch information
lanthora committed Jun 28, 2024
1 parent eb2d403 commit 63b77aa
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.18.4)

project(candy LANGUAGES C CXX VERSION 5.9.1)
project(candy LANGUAGES C CXX VERSION 5.9.2)

option(CANDY_NOEXE "Don't build executable")
option(CANDY_DEVEL "Build development library")
Expand Down
19 changes: 15 additions & 4 deletions src/core/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ int Client::setLocalhost(std::string ip) {
int Client::run() {
std::lock_guard lock(this->runningMutex);
this->running = true;
this->localP2PDisabled = false;

if (startWsThread()) {
spdlog::critical("start websocket client thread failed");
Expand Down Expand Up @@ -432,6 +433,10 @@ void Client::sendDiscoveryMessage(uint32_t dst) {
}

void Client::sendLocalPeerConnMessage(const PeerInfo &peer) {
if (this->localP2PDisabled) {
return;
}

LocalPeerConnMessage header;
header.ge.subtype = GeSubType::LOCAL_PEER_CONN;
header.ge.extra = 0;
Expand Down Expand Up @@ -572,6 +577,7 @@ void Client::handleSysRtMessage(WebSocketMessage &message) {
spdlog::warn("invalid system route message: {:n}", spdlog::to_hex(message.buffer));
return;
}
this->localP2PDisabled = true;
SysRouteMessage *header = (SysRouteMessage *)message.buffer.c_str();
SysRouteItem *rt = header->rtTable;
std::unique_lock lock(this->sysRtTableMutex);
Expand Down Expand Up @@ -610,6 +616,9 @@ void Client::handleGeneralMessage(WebSocketMessage &message) {
}

void Client::handleLocalPeerConnMessage(WebSocketMessage &message) {
if (this->localP2PDisabled) {
return;
}
if (message.buffer.size() < sizeof(LocalPeerConnMessage)) {
spdlog::warn("invalid local peer conn message: {:n}", spdlog::to_hex(message.buffer));
return;
Expand Down Expand Up @@ -1067,7 +1076,7 @@ int Client::sendHeartbeatMessage(const PeerInfo &peer) {

if ((peer.getState() == PeerState::PREPARING || peer.getState() == PeerState::SYNCHRONIZING ||
peer.getState() == PeerState::CONNECTING) &&
(peer.local.ip && peer.local.port)) {
(!this->localP2PDisabled && peer.local.ip && peer.local.port)) {
heartbeat.ip = Address::hostToNet(this->selfInfo.local.ip);
heartbeat.port = Address::hostToNet(this->selfInfo.local.port);
message.ip = peer.local.ip;
Expand Down Expand Up @@ -1211,12 +1220,14 @@ int Client::handleHeartbeatMessage(const UdpMessage &message) {
return -1;
}

if (isLocalIp(message.ip)) {
if (!isLocalIp(message.ip)) {
peer.wide.ip = message.ip;
peer.wide.port = message.port;
} else if (!this->localP2PDisabled) {
peer.local.ip = message.ip;
peer.local.port = message.port;
} else {
peer.wide.ip = message.ip;
peer.wide.port = message.port;
return 0;
}

if (isLocalIp(message.ip) || !isLocalIp(peer.real.ip)) {
Expand Down
2 changes: 2 additions & 0 deletions src/core/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "tun/tun.h"
#include "utility/random.h"
#include "websocket/client.h"
#include <atomic>
#include <functional>
#include <list>
#include <map>
Expand Down Expand Up @@ -153,6 +154,7 @@ class Client {
uint64_t tickTick = randomUint32();
uint32_t discoveryInterval;
std::mutex cryptMutex;
std::atomic<bool> localP2PDisabled;

// Route
void showCandyRtChange(const CandyRouteEntry &entry);
Expand Down

0 comments on commit 63b77aa

Please sign in to comment.