From c577e6381f0311cfa3b5d8c6ec8f183ad3352286 Mon Sep 17 00:00:00 2001 From: lubeilin <1791778603@qq.com> Date: Mon, 25 Dec 2023 23:06:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86ipv4=E8=BD=AC=E6=8D=A2=E6=88=90ipv6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnt/src/core/mod.rs | 40 +++++++++++++++++------------ vnt/src/handle/heartbeat_handler.rs | 20 +++++++++------ 2 files changed, 35 insertions(+), 25 deletions(-) diff --git a/vnt/src/core/mod.rs b/vnt/src/core/mod.rs index 78abfe5..c05e630 100644 --- a/vnt/src/core/mod.rs +++ b/vnt/src/core/mod.rs @@ -1,8 +1,8 @@ use std::collections::HashMap; use std::io; -use std::net::TcpStream; -use std::net::UdpSocket; +use std::net::{SocketAddrV6, TcpStream}; use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4}; +use std::net::UdpSocket; use std::sync::Arc; use std::time::Duration; @@ -11,25 +11,25 @@ use parking_lot::{Mutex, RwLock}; use rand::Rng; use tokio::sync::mpsc::channel; +use crate::channel::{Route, RouteKey}; use crate::channel::channel::{Channel, Context}; use crate::channel::idle::Idle; use crate::channel::punch::{NatInfo, Punch, PunchModel}; use crate::channel::sender::ChannelSender; -use crate::channel::{Route, RouteKey}; use crate::cipher::{Cipher, CipherModel, RsaCipher}; use crate::core::status::VntStatusManger; use crate::error::Error; use crate::external_route::{AllowExternalRoute, ExternalRoute}; +use crate::handle::{ + ConnectStatus, CurrentDeviceInfo, handshake_handler, heartbeat_handler, PeerDeviceInfo, + punch_handler, registration_handler, +}; use crate::handle::handshake_handler::HandshakeEnum; use crate::handle::recv_handler::ChannelDataHandler; use crate::handle::registration_handler::{RegResponse, ReqEnum}; #[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))] use crate::handle::tun_tap::tap_handler; use crate::handle::tun_tap::tun_handler; -use crate::handle::{ - handshake_handler, heartbeat_handler, punch_handler, registration_handler, ConnectStatus, - CurrentDeviceInfo, PeerDeviceInfo, -}; use crate::igmp_server::IgmpServer; use crate::nat::NatTest; use crate::tun_tap_device; @@ -287,18 +287,18 @@ impl VntUtil { Some(ExternalRoute::new(config.in_ips)) }; #[cfg(feature = "ip_proxy")] - let (tcp_proxy, udp_proxy, ip_proxy_map) = if config.out_ips.is_empty() || config.no_proxy { + let (tcp_proxy, udp_proxy, ip_proxy_map) = if config.out_ips.is_empty() || config.no_proxy { (None, None, None) } else { let (tcp_proxy, udp_proxy, ip_proxy_map) = crate::ip_proxy::init_proxy( #[cfg(not(target_os = "android"))] - channel_sender.clone(), + channel_sender.clone(), #[cfg(not(target_os = "android"))] - current_device.clone(), + current_device.clone(), #[cfg(not(target_os = "android"))] - client_cipher.clone(), + client_cipher.clone(), ) - .await?; + .await?; (Some(tcp_proxy), Some(udp_proxy), Some(ip_proxy_map)) }; let out_external_route = AllowExternalRoute::new(config.out_ips); @@ -319,7 +319,7 @@ impl VntUtil { current_device.clone(), in_external_route, #[cfg(feature = "ip_proxy")] - ip_proxy_map.clone(), + ip_proxy_map.clone(), client_cipher.clone(), self.server_cipher.clone(), config.parallel, @@ -334,7 +334,7 @@ impl VntUtil { current_device.clone(), in_external_route, #[cfg(feature = "ip_proxy")] - ip_proxy_map.clone(), + ip_proxy_map.clone(), client_cipher.clone(), self.server_cipher.clone(), config.parallel, @@ -350,7 +350,7 @@ impl VntUtil { current_device.clone(), in_external_route, #[cfg(feature = "ip_proxy")] - ip_proxy_map.clone(), + ip_proxy_map.clone(), client_cipher.clone(), self.server_cipher.clone(), config.parallel, @@ -367,7 +367,7 @@ impl VntUtil { connect_status.clone(), peer_nat_info_map.clone(), #[cfg(feature = "ip_proxy")] - ip_proxy_map, + ip_proxy_map, out_external_route, cone_sender, symmetric_sender, @@ -581,7 +581,7 @@ impl Config { token: String, device_id: String, name: String, - server_address: SocketAddr, + mut server_address: SocketAddr, server_address_str: String, mut stun_server: Vec, in_ips: Vec<(u32, u32, Ipv4Addr)>, @@ -606,6 +606,12 @@ impl Config { x.push_str(":3478"); } } + match server_address { + SocketAddr::V4(ipv4) => { + server_address = SocketAddr::V6(SocketAddrV6::new(ipv4.ip().to_ipv6_mapped(), ipv4.port(), 0, 0)) + } + SocketAddr::V6(_) => {} + } Self { tap, token, diff --git a/vnt/src/handle/heartbeat_handler.rs b/vnt/src/handle/heartbeat_handler.rs index d521c3b..9e5db7f 100644 --- a/vnt/src/handle/heartbeat_handler.rs +++ b/vnt/src/handle/heartbeat_handler.rs @@ -1,21 +1,21 @@ use std::io; -use std::net::{Ipv4Addr, ToSocketAddrs}; +use std::net::{Ipv4Addr, SocketAddr, SocketAddrV6, ToSocketAddrs}; use std::sync::Arc; use std::time::Duration; -use crate::channel::idle::Idle; -use crate::channel::sender::ChannelSender; -use crate::channel::Route; -use crate::cipher::Cipher; -use crate::core::status::VntWorker; use crossbeam_utils::atomic::AtomicCell; use parking_lot::Mutex; use rand::prelude::SliceRandom; +use crate::channel::idle::Idle; +use crate::channel::Route; +use crate::channel::sender::ChannelSender; +use crate::cipher::Cipher; +use crate::core::status::VntWorker; use crate::handle::{CurrentDeviceInfo, PeerDeviceInfo}; +use crate::protocol::{control_packet, MAX_TTL, NetPacket, Protocol, Version}; use crate::protocol::body::ENCRYPTION_RESERVED; use crate::protocol::control_packet::PingPacket; -use crate::protocol::{control_packet, NetPacket, Protocol, Version, MAX_TTL}; pub fn start_idle(mut worker: VntWorker, idle: Idle, sender: ChannelSender) { tokio::spawn(async move { @@ -64,6 +64,7 @@ pub fn start_heartbeat( worker.stop_all(); }); } + pub fn start_heartbeat_main( mut worker: VntWorker, sender: ChannelSender, @@ -137,9 +138,12 @@ async fn start_heartbeat_main_( let src = current_dev.virtual_ip(); if count % 40 == 19 { if let Ok(mut addr) = server_address_str.to_socket_addrs() { - if let Some(addr) = addr.next() { + if let Some(mut addr) = addr.next() { if addr != current_dev.connect_server { let mut tmp = current_dev.clone(); + if let SocketAddr::V4(ipv4) = addr { + addr = SocketAddr::V6(SocketAddrV6::new(ipv4.ip().to_ipv6_mapped(), ipv4.port(), 0, 0)) + } tmp.connect_server = addr; log::info!( "服务端地址变化,旧地址:{},新地址:{}",