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/p2p_channel b/switch/p2p_channel index a9c49f7..9d2e02f 160000 --- a/switch/p2p_channel +++ b/switch/p2p_channel @@ -1 +1 @@ -Subproject commit a9c49f79c66ecbe6a4286b9b6862c3488f730b13 +Subproject commit 9d2e02f6290bde873584f457cd1236a594b1bb42 diff --git a/switch/src/handle/heartbeat_handler.rs b/switch/src/handle/heartbeat_handler.rs index 5a4df8a..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,18 @@ 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<()> { + 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<()> { @@ -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, 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 9388ce8..a9e3f80 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 { @@ -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(()); @@ -254,11 +255,20 @@ impl RecvHandler { fn control(&self, current_device: CurrentDeviceInfo, source: Ipv4Addr, mut net_packet: NetPacket<&mut [u8]>, 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 +340,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()])?; diff --git a/switch/src/handle/tun_handler.rs b/switch/src/handle/tun_handler.rs index ca2ae07..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 { @@ -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")] @@ -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,