过滤非vnt包
This commit is contained in:
@@ -24,7 +24,7 @@ use crate::handle::{BaseConfigInfo, CurrentDeviceInfo, PeerDeviceInfo, SELF_IP};
|
||||
#[cfg(feature = "ip_proxy")]
|
||||
use crate::ip_proxy::IpProxyMap;
|
||||
use crate::nat::NatTest;
|
||||
use crate::protocol::NetPacket;
|
||||
use crate::protocol::{NetPacket, HEAD_LEN};
|
||||
use crate::tun_tap_device::tun_create_helper::DeviceAdapter;
|
||||
use crate::util::U64Adder;
|
||||
|
||||
@@ -50,6 +50,9 @@ impl<Call: VntCallback> RecvChannelHandler for RecvDataHandler<Call> {
|
||||
route_key: RouteKey,
|
||||
context: &ChannelContext,
|
||||
) {
|
||||
if buf.len() < HEAD_LEN {
|
||||
return;
|
||||
}
|
||||
//判断stun响应包
|
||||
if !route_key.is_tcp() {
|
||||
if let Ok(rs) = self
|
||||
@@ -136,7 +139,7 @@ impl<Call: VntCallback> RecvDataHandler<Call> {
|
||||
let net_packet = NetPacket::new(buf)?;
|
||||
let extend = NetPacket::unchecked(extend);
|
||||
if net_packet.ttl() == 0 || net_packet.source_ttl() < net_packet.ttl() {
|
||||
log::warn!("丢弃过时包:{:?}", net_packet.head());
|
||||
log::warn!("丢弃过时包:{:?} {}", net_packet.head(), route_key.addr);
|
||||
return Ok(());
|
||||
}
|
||||
let current_device = self.current_device.load();
|
||||
|
||||
+40
-27
@@ -224,34 +224,48 @@ impl NatTest {
|
||||
source_addr: SocketAddr,
|
||||
buf: &[u8],
|
||||
) -> anyhow::Result<bool> {
|
||||
if let Some(addr) = stun::recv_stun_response(buf) {
|
||||
if let SocketAddr::V4(addr) = addr {
|
||||
let mut check_fail = true;
|
||||
let source_ip = match source_addr.ip() {
|
||||
IpAddr::V4(ip) => ip,
|
||||
IpAddr::V6(ip) => {
|
||||
if let Some(ip) = ip.to_ipv4_mapped() {
|
||||
ip
|
||||
} else {
|
||||
return Ok(false);
|
||||
}
|
||||
}
|
||||
};
|
||||
'a: for stun_server in &self.stun_server {
|
||||
for x in stun_server.to_socket_addrs()? {
|
||||
if source_addr.port() == x.port() {
|
||||
if let IpAddr::V4(ip) = x.ip() {
|
||||
if ip == source_ip {
|
||||
check_fail = false;
|
||||
break 'a;
|
||||
}
|
||||
};
|
||||
}
|
||||
if buf[0] == 0x01 && buf[1] == 0x01 {
|
||||
if let Some(addr) = stun::recv_stun_response(buf) {
|
||||
if let Err(e) = self.recv_data_(index, source_addr, addr) {
|
||||
log::warn!("{:?}", e);
|
||||
}
|
||||
}
|
||||
Ok(true)
|
||||
} else {
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
fn recv_data_(
|
||||
&self,
|
||||
index: usize,
|
||||
source_addr: SocketAddr,
|
||||
addr: SocketAddr,
|
||||
) -> anyhow::Result<()> {
|
||||
if let SocketAddr::V4(addr) = addr {
|
||||
let mut check_fail = true;
|
||||
let source_ip = match source_addr.ip() {
|
||||
IpAddr::V4(ip) => ip,
|
||||
IpAddr::V6(ip) => {
|
||||
if let Some(ip) = ip.to_ipv4_mapped() {
|
||||
ip
|
||||
} else {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
if check_fail {
|
||||
return Ok(false);
|
||||
};
|
||||
'a: for stun_server in &self.stun_server {
|
||||
for x in stun_server.to_socket_addrs()? {
|
||||
if source_addr.port() == x.port() {
|
||||
if let IpAddr::V4(ip) = x.ip() {
|
||||
if ip == source_ip {
|
||||
check_fail = false;
|
||||
break 'a;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
if !check_fail {
|
||||
let ip = addr.ip();
|
||||
if !ip.is_multicast()
|
||||
&& !ip.is_broadcast()
|
||||
@@ -260,10 +274,9 @@ impl NatTest {
|
||||
&& !ip.is_private()
|
||||
{
|
||||
self.update_addr(index, *addr.ip(), addr.port());
|
||||
return Ok(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
return Ok(false);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -198,9 +198,6 @@ pub fn send_stun_request() -> Vec<u8> {
|
||||
}
|
||||
|
||||
pub fn recv_stun_response(buf: &[u8]) -> Option<SocketAddr> {
|
||||
if buf[0] != 0x01 && buf[1] != 0x01 {
|
||||
return None;
|
||||
}
|
||||
let msg = stun_format::Msg::from(buf);
|
||||
if let Some(tid) = msg.tid() {
|
||||
if tid & TAG != TAG {
|
||||
|
||||
Reference in New Issue
Block a user