mirror of
https://mirror.suhoan.cn/https://github.com/EasyTier/EasyTier.git
synced 2025-12-12 12:47:25 +08:00
avoid packets sending to non-exist peer causing route loop (#1378)
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
use std::collections::hash_map::DefaultHasher;
|
||||
use std::net::IpAddr;
|
||||
use std::{
|
||||
hash::Hasher,
|
||||
sync::{Arc, Mutex},
|
||||
@@ -239,6 +240,13 @@ impl GlobalCtx {
|
||||
self.config.get_id()
|
||||
}
|
||||
|
||||
pub fn is_ip_in_same_network(&self, ip: &IpAddr) -> bool {
|
||||
match ip {
|
||||
IpAddr::V4(v4) => self.get_ipv4().map(|x| x.contains(v4)).unwrap_or(false),
|
||||
IpAddr::V6(v6) => self.get_ipv6().map(|x| x.contains(v6)).unwrap_or(false),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_network_identity(&self) -> NetworkIdentity {
|
||||
self.config.get_network_identity()
|
||||
}
|
||||
|
||||
@@ -1055,10 +1055,19 @@ impl PeerManager {
|
||||
|| ipv4_addr.is_multicast()
|
||||
|| *ipv4_addr == ipv4_inet.last_address()
|
||||
{
|
||||
dst_peers.extend(self.peers.list_routes().await.iter().map(|x| *x.key()));
|
||||
dst_peers.extend(self.peers.list_routes().await.iter().filter_map(|x| {
|
||||
if *x.key() != self.my_peer_id {
|
||||
Some(*x.key())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}));
|
||||
} else if let Some(peer_id) = self.peers.get_peer_id_by_ipv4(ipv4_addr).await {
|
||||
dst_peers.push(peer_id);
|
||||
} else {
|
||||
} else if !self
|
||||
.global_ctx
|
||||
.is_ip_in_same_network(&std::net::IpAddr::V4(*ipv4_addr))
|
||||
{
|
||||
for exit_node in &self.exit_nodes {
|
||||
let IpAddr::V4(exit_node) = exit_node else {
|
||||
continue;
|
||||
@@ -1072,8 +1081,12 @@ impl PeerManager {
|
||||
}
|
||||
#[cfg(target_env = "ohos")]
|
||||
{
|
||||
if dst_peers.is_empty() {
|
||||
tracing::info!("no peer id for ipv4: {}, set exit_node for ohos", ipv4_addr);
|
||||
if dst_peers.is_empty()
|
||||
&& !self
|
||||
.global_ctx
|
||||
.is_ip_in_same_network(&std::net::IpAddr::V4(*ipv4_addr))
|
||||
{
|
||||
tracing::trace!("no peer id for ipv4: {}, set exit_node for ohos", ipv4_addr);
|
||||
dst_peers.push(self.my_peer_id.clone());
|
||||
is_exit_node = true;
|
||||
}
|
||||
|
||||
@@ -2396,6 +2396,15 @@ impl Route for PeerRoute {
|
||||
return Some(p.peer_id);
|
||||
}
|
||||
|
||||
// only get peer id for proxy when the dst ipv4 is not in same network with us
|
||||
if self
|
||||
.global_ctx
|
||||
.is_ip_in_same_network(&std::net::IpAddr::V4(*ipv4_addr))
|
||||
{
|
||||
tracing::trace!(?ipv4_addr, "ipv4 addr is in same network with us");
|
||||
return None;
|
||||
}
|
||||
|
||||
if let Some(peer_id) = route_table.get_peer_id_for_proxy(ipv4_addr) {
|
||||
return Some(peer_id);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user