From 9e249862dc77657ed074d479716efbbd5b90de76 Mon Sep 17 00:00:00 2001 From: lbl8603 <49143209+lbl8603@users.noreply.github.com> Date: Wed, 26 Jun 2024 21:53:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=92=E9=99=A4=E8=99=9A=E6=8B=9F=E7=BD=91?= =?UTF-8?q?=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnt/src/channel/punch.rs | 19 +++++++++++++------ vnt/src/channel/sender.rs | 9 ++++----- vnt/src/core/conn.rs | 1 + vnt/src/handle/mod.rs | 5 +++++ vnt/src/handle/tun_tap/tun_handler.rs | 13 ++----------- 5 files changed, 25 insertions(+), 22 deletions(-) diff --git a/vnt/src/channel/punch.rs b/vnt/src/channel/punch.rs index 4270f9a..25e778d 100644 --- a/vnt/src/channel/punch.rs +++ b/vnt/src/channel/punch.rs @@ -1,7 +1,9 @@ +use crossbeam_utils::atomic::AtomicCell; use std::collections::HashMap; use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6}; use std::ops::{Div, Mul}; use std::str::FromStr; +use std::sync::Arc; use std::time::Duration; use std::{io, thread}; @@ -12,6 +14,7 @@ use rand::Rng; use crate::channel::context::ChannelContext; use crate::channel::sender::AcceptSocketSender; use crate::external_route::ExternalRoute; +use crate::handle::CurrentDeviceInfo; use crate::nat::NatTest; #[derive(Copy, Clone, Eq, PartialEq, Debug)] @@ -194,6 +197,7 @@ pub struct Punch { tcp_socket_sender: AcceptSocketSender<(TcpStream, SocketAddr, Option>)>, external_route: ExternalRoute, nat_test: NatTest, + current_device: Arc>, } impl Punch { @@ -204,6 +208,7 @@ impl Punch { tcp_socket_sender: AcceptSocketSender<(TcpStream, SocketAddr, Option>)>, external_route: ExternalRoute, nat_test: NatTest, + current_device: Arc>, ) -> Self { let mut port_vec: Vec = (1..65535).collect(); port_vec.push(65535); @@ -218,6 +223,7 @@ impl Punch { tcp_socket_sender, external_route, nat_test, + current_device, } } } @@ -256,12 +262,13 @@ impl Punch { log::info!("已打洞成功,无需打洞:{:?}", id); return Ok(()); } - nat_info - .public_ips - .retain(|ip| self.external_route.route(&ip).is_none()); - nat_info - .local_ipv4 - .filter(|ip| self.external_route.route(&ip).is_none()); + let device_info = self.current_device.load(); + nat_info.public_ips.retain(|ip| { + self.external_route.route(ip).is_none() && device_info.not_in_network(*ip) + }); + nat_info.local_ipv4.filter(|ip| { + self.external_route.route(ip).is_none() && device_info.not_in_network(*ip) + }); nat_info.ipv6.filter(|ip| { if let Some(ip) = ip.to_ipv4_mapped() { self.external_route.route(&ip).is_none() diff --git a/vnt/src/channel/sender.rs b/vnt/src/channel/sender.rs index 725445e..9574083 100644 --- a/vnt/src/channel/sender.rs +++ b/vnt/src/channel/sender.rs @@ -91,11 +91,10 @@ impl IpPacketSender { return Ok(()); } - // if u32::from_be_bytes(dest_ip.octets()) & u32::from_be_bytes(device_info.virtual_netmask.octets()) - // != u32::from_be_bytes(device_info.virtual_network.octets()) { - // //不是一个网段的直接忽略 - // return Ok(()); - // } + if device_info.not_in_network(dest_ip) { + //不是一个网段的直接忽略 + return Ok(()); + } self.context.send_ipv4_by_id( net_packet.buffer(), &dest_ip, diff --git a/vnt/src/core/conn.rs b/vnt/src/core/conn.rs index 3ecc368..8dc6b5b 100644 --- a/vnt/src/core/conn.rs +++ b/vnt/src/core/conn.rs @@ -236,6 +236,7 @@ impl Vnt { tcp_socket_sender.clone(), external_route.clone(), nat_test.clone(), + current_device.clone(), ); // #[cfg(not(target_os = "android"))] diff --git a/vnt/src/handle/mod.rs b/vnt/src/handle/mod.rs index 3bcc193..5ba0312 100644 --- a/vnt/src/handle/mod.rs +++ b/vnt/src/handle/mod.rs @@ -229,9 +229,14 @@ impl CurrentDeviceInfo { pub fn virtual_gateway(&self) -> Ipv4Addr { self.virtual_gateway } + #[inline] pub fn is_gateway(&self, ip: &Ipv4Addr) -> bool { &self.virtual_gateway == ip || ip == &GATEWAY_IP } + #[inline] + pub fn not_in_network(&self, ip: Ipv4Addr) -> bool { + u32::from(ip) & u32::from(self.virtual_netmask) != u32::from(self.virtual_network) + } } pub fn change_status( current_device: &AtomicCell, diff --git a/vnt/src/handle/tun_tap/tun_handler.rs b/vnt/src/handle/tun_tap/tun_handler.rs index 5318427..d8cacfe 100644 --- a/vnt/src/handle/tun_tap/tun_handler.rs +++ b/vnt/src/handle/tun_tap/tun_handler.rs @@ -27,12 +27,7 @@ use crate::protocol::body::ENCRYPTION_RESERVED; use crate::protocol::ip_turn_packet::BroadcastPacket; use crate::protocol::{ip_turn_packet, NetPacket, MAX_TTL}; use crate::util::{SingleU64Adder, StopManager}; -/// 是否在一个网段 -#[inline] -fn check_dest(dest: Ipv4Addr, virtual_netmask: Ipv4Addr, virtual_network: Ipv4Addr) -> bool { - u32::from_be_bytes(dest.octets()) & u32::from_be_bytes(virtual_netmask.octets()) - == u32::from_be_bytes(virtual_network.octets()) -} + fn icmp(device_writer: &Device, mut ipv4_packet: IpV4Packet<&mut [u8]>) -> anyhow::Result<()> { if ipv4_packet.protocol() == Protocol::Icmp { let mut icmp = IcmpPacket::new(ipv4_packet.payload_mut())?; @@ -222,11 +217,7 @@ pub(crate) fn handle( } if !dest_ip.is_multicast() && !dest_ip.is_broadcast() && current_device.broadcast_ip != dest_ip { - if !check_dest( - dest_ip, - current_device.virtual_netmask, - current_device.virtual_network, - ) { + if current_device.not_in_network(dest_ip) { if let Some(r_dest_ip) = ip_route.route(&dest_ip) { //路由的目标不能是自己 if r_dest_ip == src_ip {