From 18eb4af01666bdd8b7ae506ee0c57341a0c5faf5 Mon Sep 17 00:00:00 2001 From: lbl8603 <49143209+lbl8603@users.noreply.github.com> Date: Wed, 24 Apr 2024 23:03:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=BB=98=E8=AE=A4=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnt/src/core/conn.rs | 1 + vnt/src/core/mod.rs | 4 ---- vnt/src/handle/handshaker.rs | 6 +++--- vnt/src/handle/maintain/addr_request.rs | 4 ++-- vnt/src/handle/maintain/heartbeat.rs | 4 ++-- vnt/src/handle/maintain/punch.rs | 6 +++--- vnt/src/handle/maintain/up_status.rs | 4 ++-- vnt/src/handle/recv_data/mod.rs | 3 ++- vnt/src/handle/recv_data/server.rs | 4 ++-- vnt/src/handle/registrar.rs | 4 ++-- vnt/src/handle/tun_tap/mod.rs | 6 +++--- vnt/src/ip_proxy/icmp_proxy.rs | 4 ++-- vnt/src/protocol/mod.rs | 11 ++++++----- 13 files changed, 30 insertions(+), 31 deletions(-) diff --git a/vnt/src/core/conn.rs b/vnt/src/core/conn.rs index a2f8020..03f0da1 100644 --- a/vnt/src/core/conn.rs +++ b/vnt/src/core/conn.rs @@ -211,6 +211,7 @@ impl Vnt { config.punch_model, config.tcp, tcp_socket_sender.clone(), + external_route.clone(), ); #[cfg(not(target_os = "android"))] diff --git a/vnt/src/core/mod.rs b/vnt/src/core/mod.rs index 774c50e..d45e446 100644 --- a/vnt/src/core/mod.rs +++ b/vnt/src/core/mod.rs @@ -94,10 +94,6 @@ impl Config { if name.is_empty() || name.len() > 128 { return Err(anyhow!("name too long")); } - if name_servers.is_empty() { - name_servers.push("114.114.114.114:53".to_string()); - name_servers.push("8.8.8.8:53".to_string()); - } let server_address = address_choose(dns_query_all(&server_address_str, name_servers.clone())?)?; Ok(Self { diff --git a/vnt/src/handle/handshaker.rs b/vnt/src/handle/handshaker.rs index 0bb65be..ca90752 100644 --- a/vnt/src/handle/handshaker.rs +++ b/vnt/src/handle/handshaker.rs @@ -16,7 +16,7 @@ use crate::proto::message::HandshakeRequest; use crate::proto::message::SecretHandshakeRequest; #[cfg(feature = "server_encrypt")] use crate::protocol::body::RSA_ENCRYPTION_RESERVED; -use crate::protocol::{service_packet, NetPacket, Protocol, Version, MAX_TTL}; +use crate::protocol::{service_packet, NetPacket, Protocol, MAX_TTL}; pub enum HandshakeEnum { NotSecret, @@ -65,7 +65,7 @@ impl Handshake { })?; let buf = vec![0u8; 12 + bytes.len()]; let mut net_packet = NetPacket::new(buf)?; - net_packet.set_version(Version::V1); + net_packet.set_default_version(); net_packet.set_gateway_flag(true); net_packet.set_destination(GATEWAY_IP); net_packet.set_source(SELF_IP); @@ -97,7 +97,7 @@ pub fn secret_handshake_request_packet( 12 + bytes.len(), vec![0u8; 12 + bytes.len() + RSA_ENCRYPTION_RESERVED], )?; - net_packet.set_version(Version::V1); + net_packet.set_default_version(); net_packet.set_gateway_flag(true); net_packet.set_destination(GATEWAY_IP); net_packet.set_source(SELF_IP); diff --git a/vnt/src/handle/maintain/addr_request.rs b/vnt/src/handle/maintain/addr_request.rs index c51cf29..51bef9a 100644 --- a/vnt/src/handle/maintain/addr_request.rs +++ b/vnt/src/handle/maintain/addr_request.rs @@ -7,7 +7,7 @@ use crate::channel::context::Context; use crate::cipher::Cipher; use crate::handle::{BaseConfigInfo, CurrentDeviceInfo}; use crate::protocol::body::ENCRYPTION_RESERVED; -use crate::protocol::{control_packet, NetPacket, Protocol, Version, MAX_TTL}; +use crate::protocol::{control_packet, NetPacket, Protocol, MAX_TTL}; use crate::util::Scheduler; pub fn addr_request( @@ -51,7 +51,7 @@ pub fn addr_request0( let gateway_ip = current_dev.virtual_gateway; let src_ip = current_dev.virtual_ip; let mut packet = NetPacket::new_encrypt([0; 12 + ENCRYPTION_RESERVED]).unwrap(); - packet.set_version(Version::V1); + packet.set_default_version(); packet.set_gateway_flag(true); packet.set_protocol(Protocol::Control); packet.set_transport_protocol(control_packet::Protocol::AddrRequest.into()); diff --git a/vnt/src/handle/maintain/heartbeat.rs b/vnt/src/handle/maintain/heartbeat.rs index 9101791..d857a70 100644 --- a/vnt/src/handle/maintain/heartbeat.rs +++ b/vnt/src/handle/maintain/heartbeat.rs @@ -12,7 +12,7 @@ use crate::cipher::Cipher; use crate::handle::{CurrentDeviceInfo, PeerDeviceInfo}; use crate::protocol::body::ENCRYPTION_RESERVED; use crate::protocol::control_packet::PingPacket; -use crate::protocol::{control_packet, NetPacket, Protocol, Version}; +use crate::protocol::{control_packet, NetPacket, Protocol}; use crate::util::Scheduler; /// 定时发送心跳包 @@ -213,7 +213,7 @@ fn heartbeat_packet( dest: Ipv4Addr, ) -> io::Result> { let mut net_packet = NetPacket::new_encrypt([0u8; 12 + 4 + ENCRYPTION_RESERVED])?; - net_packet.set_version(Version::V1); + net_packet.set_default_version(); net_packet.set_protocol(Protocol::Control); net_packet.set_transport_protocol(control_packet::Protocol::Ping.into()); net_packet.first_set_ttl(5); diff --git a/vnt/src/handle/maintain/punch.rs b/vnt/src/handle/maintain/punch.rs index 95a7ff5..b1172b3 100644 --- a/vnt/src/handle/maintain/punch.rs +++ b/vnt/src/handle/maintain/punch.rs @@ -17,7 +17,7 @@ use crate::handle::{CurrentDeviceInfo, PeerDeviceInfo}; use crate::nat::NatTest; use crate::proto::message::{PunchInfo, PunchNatType}; use crate::protocol::body::ENCRYPTION_RESERVED; -use crate::protocol::{control_packet, other_turn_packet, NetPacket, Protocol, Version, MAX_TTL}; +use crate::protocol::{control_packet, other_turn_packet, NetPacket, Protocol, MAX_TTL}; use crate::util::Scheduler; #[derive(Clone)] @@ -135,7 +135,7 @@ fn punch_start( ) { while let Ok((peer_ip, nat_info)) = receiver.recv() { let mut packet = NetPacket::new_encrypt([0u8; 12 + ENCRYPTION_RESERVED]).unwrap(); - packet.set_version(Version::V1); + packet.set_default_version(); packet.first_set_ttl(1); packet.set_protocol(Protocol::Control); packet.set_transport_protocol(control_packet::Protocol::PunchRequest.into()); @@ -322,7 +322,7 @@ fn punch_packet( .write_to_bytes() .map_err(|e| io::Error::new(io::ErrorKind::Other, format!("punch_packet {:?}", e)))?; let mut net_packet = NetPacket::new_encrypt(vec![0u8; 12 + bytes.len() + ENCRYPTION_RESERVED])?; - net_packet.set_version(Version::V1); + net_packet.set_default_version(); net_packet.set_protocol(Protocol::OtherTurn); net_packet.set_transport_protocol(other_turn_packet::Protocol::Punch.into()); net_packet.first_set_ttl(MAX_TTL); diff --git a/vnt/src/handle/maintain/up_status.rs b/vnt/src/handle/maintain/up_status.rs index 54fd621..5d2fae2 100644 --- a/vnt/src/handle/maintain/up_status.rs +++ b/vnt/src/handle/maintain/up_status.rs @@ -2,7 +2,7 @@ use crate::channel::context::Context; use crate::handle::CurrentDeviceInfo; use crate::proto::message::{ClientStatusInfo, PunchNatType, RouteItem}; use crate::protocol::body::ENCRYPTION_RESERVED; -use crate::protocol::{service_packet, NetPacket, Protocol, Version, HEAD_LEN, MAX_TTL}; +use crate::protocol::{service_packet, NetPacket, Protocol, HEAD_LEN, MAX_TTL}; use crate::util::{Scheduler, WatchSingleU64Adder, WatchU64Adder}; use crossbeam_utils::atomic::AtomicCell; use protobuf::Message; @@ -91,7 +91,7 @@ fn send_up_status_packet( .map_err(|e| io::Error::new(io::ErrorKind::Other, format!("up_status_packet {:?}", e)))?; let mut net_packet = NetPacket::new_encrypt(vec![0; HEAD_LEN + buf.len() + ENCRYPTION_RESERVED])?; - net_packet.set_version(Version::V1); + net_packet.set_default_version(); net_packet.set_gateway_flag(true); net_packet.set_protocol(Protocol::Service); net_packet.set_transport_protocol_into(service_packet::Protocol::ClientStatusInfo); diff --git a/vnt/src/handle/recv_data/mod.rs b/vnt/src/handle/recv_data/mod.rs index d02997c..d29d4ca 100644 --- a/vnt/src/handle/recv_data/mod.rs +++ b/vnt/src/handle/recv_data/mod.rs @@ -78,7 +78,7 @@ impl RecvDataHandler { config_info, nat_test.clone(), callback, - external_route, + external_route.clone(), handshake, ); let client = ClientPacketHandler::new( @@ -88,6 +88,7 @@ impl RecvDataHandler { peer_nat_info_map, nat_test, route, + external_route.clone(), #[cfg(feature = "ip_proxy")] ip_proxy_map, ); diff --git a/vnt/src/handle/recv_data/server.rs b/vnt/src/handle/recv_data/server.rs index e5e564f..4dcb085 100644 --- a/vnt/src/handle/recv_data/server.rs +++ b/vnt/src/handle/recv_data/server.rs @@ -31,7 +31,7 @@ use crate::proto::message::{DeviceList, HandshakeResponse, RegistrationResponse} use crate::protocol::body::ENCRYPTION_RESERVED; use crate::protocol::control_packet::ControlPacket; use crate::protocol::error_packet::InErrorPacket; -use crate::protocol::{ip_turn_packet, service_packet, NetPacket, Protocol, Version, MAX_TTL}; +use crate::protocol::{ip_turn_packet, service_packet, NetPacket, Protocol, MAX_TTL}; use crate::tun_tap_device::tun_create_helper::DeviceAdapter; use crate::{proto, PeerClientInfo}; #[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))] @@ -539,7 +539,7 @@ impl ServerPacketHandler { let mut poll_device = NetPacket::new_encrypt([0; 12 + ENCRYPTION_RESERVED])?; poll_device.set_source(current_device.virtual_ip); poll_device.set_destination(GATEWAY_IP); - poll_device.set_version(Version::V1); + poll_device.set_default_version(); poll_device.set_gateway_flag(true); poll_device.first_set_ttl(MAX_TTL); poll_device.set_protocol(Protocol::Service); diff --git a/vnt/src/handle/registrar.rs b/vnt/src/handle/registrar.rs index 641dd26..160850d 100644 --- a/vnt/src/handle/registrar.rs +++ b/vnt/src/handle/registrar.rs @@ -7,7 +7,7 @@ use crate::cipher::Cipher; use crate::handle::{GATEWAY_IP, SELF_IP}; use crate::proto::message::RegistrationRequest; use crate::protocol::body::ENCRYPTION_RESERVED; -use crate::protocol::{service_packet, NetPacket, Protocol, Version, MAX_TTL}; +use crate::protocol::{service_packet, NetPacket, Protocol, MAX_TTL}; /// 注册数据 pub fn registration_request_packet( @@ -43,7 +43,7 @@ pub fn registration_request_packet( let mut net_packet = NetPacket::new_encrypt(buf)?; net_packet.set_destination(GATEWAY_IP); net_packet.set_source(SELF_IP); - net_packet.set_version(Version::V1); + net_packet.set_default_version(); net_packet.set_gateway_flag(true); net_packet.set_protocol(Protocol::Service); net_packet.set_transport_protocol(service_packet::Protocol::RegistrationRequest.into()); diff --git a/vnt/src/handle/tun_tap/mod.rs b/vnt/src/handle/tun_tap/mod.rs index e26f788..92243d6 100644 --- a/vnt/src/handle/tun_tap/mod.rs +++ b/vnt/src/handle/tun_tap/mod.rs @@ -15,7 +15,7 @@ use crate::ip_proxy::{IpProxyMap, ProxyHandler}; use crate::protocol; use crate::protocol::body::ENCRYPTION_RESERVED; use crate::protocol::ip_turn_packet::BroadcastPacket; -use crate::protocol::{ip_turn_packet, NetPacket, Version, MAX_TTL}; +use crate::protocol::{ip_turn_packet, NetPacket, MAX_TTL}; mod channel_group; pub mod tun_handler; @@ -87,7 +87,7 @@ fn broadcast( let buf = vec![0u8; 12 + 1 + p2p_ips.len() * 4 + net_packet.data_len() + ENCRYPTION_RESERVED]; //剩余的发送到服务端,需要告知哪些已发送过 let mut server_packet = NetPacket::new_encrypt(buf)?; - server_packet.set_version(Version::V1); + server_packet.set_default_version(); server_packet.set_gateway_flag(true); server_packet.first_set_ttl(MAX_TTL); server_packet.set_source(net_packet.source()); @@ -123,7 +123,7 @@ pub fn base_handle( let src_ip = ipv4_packet.source_ip(); let mut dest_ip = ipv4_packet.destination_ip(); let mut net_packet = NetPacket::new0(data_len, buf)?; - net_packet.set_version(Version::V1); + net_packet.set_default_version(); net_packet.set_protocol(protocol::Protocol::IpTurn); net_packet.set_transport_protocol(ip_turn_packet::Protocol::Ipv4.into()); net_packet.first_set_ttl(6); diff --git a/vnt/src/ip_proxy/icmp_proxy.rs b/vnt/src/ip_proxy/icmp_proxy.rs index 9f1c8fc..995b30a 100644 --- a/vnt/src/ip_proxy/icmp_proxy.rs +++ b/vnt/src/ip_proxy/icmp_proxy.rs @@ -17,7 +17,7 @@ use crate::cipher::Cipher; use crate::handle::CurrentDeviceInfo; use crate::ip_proxy::ProxyHandler; use crate::protocol; -use crate::protocol::{NetPacket, Version, MAX_TTL}; +use crate::protocol::{NetPacket, MAX_TTL}; use crate::util::StopManager; #[derive(Clone)] pub struct IcmpProxy { @@ -172,7 +172,7 @@ fn recv_handle( let virtual_ip = current_device.virtual_ip(); let mut net_packet = NetPacket::new0(data_len, buf).unwrap(); - net_packet.set_version(Version::V1); + net_packet.set_default_version(); net_packet.set_protocol(protocol::Protocol::IpTurn); net_packet.set_transport_protocol( protocol::ip_turn_packet::Protocol::Ipv4.into(), diff --git a/vnt/src/protocol/mod.rs b/vnt/src/protocol/mod.rs index 8e44afc..19bedca 100644 --- a/vnt/src/protocol/mod.rs +++ b/vnt/src/protocol/mod.rs @@ -27,14 +27,15 @@ pub mod service_packet; #[derive(Eq, PartialEq, Copy, Clone, Debug)] pub enum Version { - V1, + V2, Unknown(u8), } impl From for Version { fn from(value: u8) -> Self { match value { - 1 => Version::V1, + // 版本从2开始,用于和stun协议的binging响应区分开 + 2 => Version::V2, val => Version::Unknown(val), } } @@ -43,7 +44,7 @@ impl From for Version { impl Into for Version { fn into(self) -> u8 { match self { - Version::V1 => 1, + Version::V2 => 2, Version::Unknown(val) => val, } } @@ -207,8 +208,8 @@ impl + AsMut<[u8]>> NetPacket { self.buffer.as_mut()[0] = self.buffer.as_ref()[0] & 0xBF }; } - pub fn set_version(&mut self, version: Version) { - let v: u8 = version.into(); + pub fn set_default_version(&mut self) { + let v: u8 = Version::V2.into(); self.buffer.as_mut()[0] = (self.buffer.as_ref()[0] & 0xF0) | (0x0F & v); } pub fn set_protocol(&mut self, protocol: Protocol) {