From 8d76214193eceb0147896e226c1d0909b2717f0e Mon Sep 17 00:00:00 2001 From: lubeilin <1791778603@qq.com> Date: Mon, 20 Mar 2023 21:13:38 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BC=A0=E9=80=92=E6=9C=AC=E5=9C=B0ip?= =?UTF-8?q?=E3=80=81=E4=BF=AE=E5=A4=8D=E5=BB=B6=E8=BF=9F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- switch/src/handle/heartbeat_handler.rs | 13 +++++++++++-- switch/src/handle/recv_handler.rs | 22 ++++++++++++++++------ 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/switch/src/handle/heartbeat_handler.rs b/switch/src/handle/heartbeat_handler.rs index 5a4df8a..b65f2d9 100644 --- a/switch/src/handle/heartbeat_handler.rs +++ b/switch/src/handle/heartbeat_handler.rs @@ -42,6 +42,13 @@ pub fn start_heartbeat(sender: Sender, device_list: Arc) -> io::Result<()> { + let current_time = Local::now().timestamp_millis() as u16; + let mut ping = PingPacket::new(packet.payload_mut())?; + ping.set_time(current_time); + Ok(()) +} + fn start_heartbeat_(sender: Sender, device_list: Arc)>>, current_device: Arc>) -> io::Result<()> { let mut net_packet = NetPacket::new([0u8; 16])?; net_packet.set_version(Version::V1); @@ -53,9 +60,7 @@ fn start_heartbeat_(sender: Sender, device_list: Arc, device_list: Arc> = None; let peer_list = device_list.lock().1.clone(); for peer in peer_list { + set_now_time(&mut net_packet)?; net_packet.first_set_ttl(MAX_TTL); net_packet.set_destination(peer.virtual_ip); if sender.send_to_id(net_packet.buffer(), &peer.virtual_ip).is_err() { @@ -78,6 +84,7 @@ fn start_heartbeat_(sender: Sender, device_list: Arc, device_list: Arc, route_key: &RouteKey) -> crate::Result<()> { match ControlPacket::new(net_packet.transport_protocol(), net_packet.payload())? { ControlPacket::PingPacket(_) => { + let metric = net_packet.source_ttl() - net_packet.ttl() + 1; net_packet.set_transport_protocol(control_packet::Protocol::Pong.into()); net_packet.set_source(current_device.virtual_ip()); net_packet.set_destination(source); net_packet.first_set_ttl(MAX_TTL); self.channel.send_to_route(net_packet.buffer(), route_key)?; + if metric == 1 { + if let Some(current_route) = self.channel.route(&source) { + if current_route.metric > 1 { + let route = Route::from(*route_key, 1, -1); + self.channel.add_route(source, route); + } + } + } } ControlPacket::PongPacket(pong_packet) => { let current_time = Local::now().timestamp_millis() as u16; @@ -330,18 +339,19 @@ impl RecvHandler { let nat_info = self.nat_test.nat_info(); punch_reply.public_ip_list = nat_info.public_ips.iter().map(|i| { match i { - IpAddr::V4(ip) => { - u32::from_be_bytes(ip.octets()) - } - IpAddr::V6(_) => { - panic!() - } + IpAddr::V4(ip) => u32::from_be_bytes(ip.octets()), + IpAddr::V6(_) => 0 } }).collect(); punch_reply.public_port = nat_info.public_port as u32; punch_reply.public_port_range = nat_info.public_port_range as u32; punch_reply.nat_type = protobuf::EnumOrUnknown::new(PunchNatType::from(nat_info.nat_type)); + punch_reply.local_ip = match nat_info.local_ip { + IpAddr::V4(ip) => u32::from_be_bytes(ip.octets()), + IpAddr::V6(_) => 0 + }; + punch_reply.local_port = nat_info.local_port as u32; let bytes = punch_reply.write_to_bytes()?; let mut net_packet = NetPacket::new(vec![0u8; 12 + bytes.len()])?; From 5dcda4d088fd8ddebd9b868d7337e05cccb14a24 Mon Sep 17 00:00:00 2001 From: lubeilin <1791778603@qq.com> Date: Thu, 30 Mar 2023 12:09:12 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- switch-desktop/src/unix/mod.rs | 4 ++-- switch/src/handle/heartbeat_handler.rs | 8 ++++---- switch/src/handle/punch_handler.rs | 12 ++++++------ switch/src/handle/recv_handler.rs | 4 ++-- switch/src/handle/tun_handler.rs | 4 ++-- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/switch-desktop/src/unix/mod.rs b/switch-desktop/src/unix/mod.rs index f94a764..62c8dbd 100644 --- a/switch-desktop/src/unix/mod.rs +++ b/switch-desktop/src/unix/mod.rs @@ -76,11 +76,11 @@ pub fn main0(base_args: BaseArgs) { log::error!("{:?}", e); } let switch1 = switch.clone(); - let handle = std::thread::spawn(move || { + let handle = std::thread::Builder::new().name("cmd-server".into()).spawn(move || { if let Err(e) = command_server.start(switch1) { log::error!("{:?}", e); } - }); + }).unwrap(); crate::console_listen(&switch); if let Err(e) = handle.join() { log::error!("后台任务异常{:?}",e); diff --git a/switch/src/handle/heartbeat_handler.rs b/switch/src/handle/heartbeat_handler.rs index b65f2d9..6134e31 100644 --- a/switch/src/handle/heartbeat_handler.rs +++ b/switch/src/handle/heartbeat_handler.rs @@ -17,11 +17,11 @@ use crate::protocol::{control_packet, MAX_TTL, NetPacket, Protocol, Version}; use crate::protocol::control_packet::PingPacket; pub fn start_idle(idle: Idle, sender: Sender) { - thread::spawn(move || { + thread::Builder::new().name("idle".into()).spawn(move || { if let Err(e) = start_idle_(idle, sender) { log::info!("空闲检测线程停止:{:?}",e); } - }); + }).unwrap(); } fn start_idle_(idle: Idle, sender: Sender) -> io::Result<()> { @@ -35,11 +35,11 @@ fn start_idle_(idle: Idle, sender: Sender) -> io::Result<()> } pub fn start_heartbeat(sender: Sender, device_list: Arc)>>, current_device: Arc>) { - thread::spawn(move || { + thread::Builder::new().name("heartbeat".into()).spawn(move || { if let Err(e) = start_heartbeat_(sender, device_list, current_device) { log::info!("空闲检测线程停止:{:?}",e); } - }); + }).unwrap(); } fn set_now_time(packet: &mut NetPacket<[u8; 16]>) -> io::Result<()> { diff --git a/switch/src/handle/punch_handler.rs b/switch/src/handle/punch_handler.rs index f060249..2901af2 100644 --- a/switch/src/handle/punch_handler.rs +++ b/switch/src/handle/punch_handler.rs @@ -14,19 +14,19 @@ use crate::proto::message::{PunchInfo, PunchNatType}; use crate::protocol::{control_packet, MAX_TTL, NetPacket, Protocol, turn_packet, Version}; pub fn start_cone(punch: Punch, current_device: Arc>) { - thread::spawn(move || { + thread::Builder::new().name("punch-cone".into()).spawn(move || { if let Err(e) = start_(true, punch, current_device) { log::warn!("锥形网络打洞处理线程停止 {:?}",e); } - }); + }).unwrap(); } pub fn start_symmetric(punch: Punch, current_device: Arc>) { - thread::spawn(move || { + thread::Builder::new().name("punch-symmetric".into()).spawn(move || { if let Err(e) = start_(false, punch, current_device) { log::warn!("对称网络打洞处理线程停止 {:?}",e); } - }); + }).unwrap(); } fn start_(is_cone: bool, mut punch: Punch, current_device: Arc>) -> io::Result<()> { @@ -57,11 +57,11 @@ fn start_(is_cone: bool, mut punch: Punch, current_device: Arc)>>, sender: Sender, current_device: Arc>) { - thread::spawn(move || { + thread::Builder::new().name("punch-send-request".into()).spawn(move || { if let Err(e) = start_punch_(nat_test, device_list, sender, current_device) { log::warn!("对称网络打洞处理线程停止 {:?}",e); } - }); + }).unwrap(); } fn start_punch_(nat_test: NatTest, device_list: Arc)>>, sender: Sender, current_device: Arc>) -> crate::Result<()> { diff --git a/switch/src/handle/recv_handler.rs b/switch/src/handle/recv_handler.rs index 2741750..0f3961e 100644 --- a/switch/src/handle/recv_handler.rs +++ b/switch/src/handle/recv_handler.rs @@ -26,7 +26,7 @@ use crate::protocol::error_packet::InErrorPacket; use crate::tun_device::TunWriter; pub fn start(mut handler: RecvHandler) { - thread::spawn(move || { + thread::Builder::new().name("udp-recv-handler".into()).spawn(move || { let mut buf = [0; 4096]; loop { match handler.channel.recv_from(&mut buf, None) { @@ -48,7 +48,7 @@ pub fn start(mut handler: RecvHandler) { } } } - }); + }).unwrap(); } pub struct RecvHandler { diff --git a/switch/src/handle/tun_handler.rs b/switch/src/handle/tun_handler.rs index ca2ae07..da837f1 100644 --- a/switch/src/handle/tun_handler.rs +++ b/switch/src/handle/tun_handler.rs @@ -69,11 +69,11 @@ pub fn start(sender: Sender, tun_reader: TunReader, tun_writer: TunWriter, current_device: Arc>, ) { - thread::spawn(move || { + thread::Builder::new().name("tun-handler".into()).spawn(move || { if let Err(e) = start_(sender, tun_reader, tun_writer, current_device) { log::warn!("{:?}",e); } - }); + }).unwrap(); } #[cfg(target_os = "windows")] From 8a032f86d88c40162aa82aff3733d1342c91884a Mon Sep 17 00:00:00 2001 From: lubeilin <1791778603@qq.com> Date: Sat, 8 Apr 2023 21:44:31 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dudp=E5=B9=BF=E6=92=AD?= =?UTF-8?q?=E4=B8=A2=E5=A4=B1=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- switch/src/handle/recv_handler.rs | 3 ++- switch/src/handle/tun_handler.rs | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/switch/src/handle/recv_handler.rs b/switch/src/handle/recv_handler.rs index 0f3961e..a9e3f80 100644 --- a/switch/src/handle/recv_handler.rs +++ b/switch/src/handle/recv_handler.rs @@ -109,7 +109,8 @@ impl RecvHandler { return Ok(()); } let destination = net_packet.destination(); - if current_device.virtual_ip() != destination && self.connect_status.load() == ConnectStatus::Connected { + if !destination.is_broadcast() && destination != current_device.broadcast_address + && current_device.virtual_ip() != destination && self.connect_status.load() == ConnectStatus::Connected { if !check_dest(source, current_device.virtual_netmask, current_device.virtual_network) { log::warn!("转发数据,源地址错误:{:?},当前网络:{:?},route_key:{:?}",source,current_device.virtual_network,route_key); return Ok(()); diff --git a/switch/src/handle/tun_handler.rs b/switch/src/handle/tun_handler.rs index da837f1..022d5ee 100644 --- a/switch/src/handle/tun_handler.rs +++ b/switch/src/handle/tun_handler.rs @@ -49,7 +49,7 @@ fn handle(sender: &Sender, data: &mut [u8], tun_writer: &TunWriter, cu // // 137端口是在局域网中提供计算机的名字或IP地址查询服务 // return Ok(()); // } - if src_ip != current_device.virtual_ip() || !check_dest(dest_ip, current_device.virtual_netmask, current_device.virtual_network) { + if src_ip != current_device.virtual_ip() || (!check_dest(dest_ip, current_device.virtual_netmask, current_device.virtual_network) && !dest_ip.is_broadcast()) { return Ok(()); } if src_ip == dest_ip { @@ -97,7 +97,7 @@ fn start_(sender: Sender, } } -#[cfg(any(target_os = "linux",target_os = "macos"))] +#[cfg(any(target_os = "linux", target_os = "macos"))] fn start_(sender: Sender, tun_reader: TunReader, tun_writer: TunWriter,