排除虚拟网段

This commit is contained in:
lbl8603
2024-06-26 21:53:59 +08:00
parent 3773c09f57
commit 9e249862dc
5 changed files with 25 additions and 22 deletions
+13 -6
View File
@@ -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<Vec<u8>>)>,
external_route: ExternalRoute,
nat_test: NatTest,
current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
}
impl Punch {
@@ -204,6 +208,7 @@ impl Punch {
tcp_socket_sender: AcceptSocketSender<(TcpStream, SocketAddr, Option<Vec<u8>>)>,
external_route: ExternalRoute,
nat_test: NatTest,
current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
) -> Self {
let mut port_vec: Vec<u16> = (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()
+4 -5
View File
@@ -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,
+1
View File
@@ -236,6 +236,7 @@ impl Vnt {
tcp_socket_sender.clone(),
external_route.clone(),
nat_test.clone(),
current_device.clone(),
);
// #[cfg(not(target_os = "android"))]
+5
View File
@@ -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<CurrentDeviceInfo>,
+2 -11
View File
@@ -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 {