From 1609c97574f48291a81536f0bbbbe2fa408b672e Mon Sep 17 00:00:00 2001 From: "Sijie.Sun" Date: Mon, 2 Sep 2024 09:37:34 +0800 Subject: [PATCH] fix panic when wireguard tunnel encounter udp recv error (#299) --- easytier/src/common/global_ctx.rs | 2 +- easytier/src/tunnel/wireguard.rs | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/easytier/src/common/global_ctx.rs b/easytier/src/common/global_ctx.rs index 086046f..d537e65 100644 --- a/easytier/src/common/global_ctx.rs +++ b/easytier/src/common/global_ctx.rs @@ -91,7 +91,7 @@ impl GlobalCtx { let net_ns = NetNS::new(config_fs.get_netns()); let hostname = config_fs.get_hostname(); - let (event_bus, _) = tokio::sync::broadcast::channel(100); + let (event_bus, _) = tokio::sync::broadcast::channel(1024); let stun_info_collection = Arc::new(StunInfoCollector::new_with_default_servers()); diff --git a/easytier/src/tunnel/wireguard.rs b/easytier/src/tunnel/wireguard.rs index 25d4c4f..4f3fc5e 100644 --- a/easytier/src/tunnel/wireguard.rs +++ b/easytier/src/tunnel/wireguard.rs @@ -634,7 +634,14 @@ impl WgTunnelConnector { let handshake = wg_peer.create_handshake_init().await; udp.send_to(&handshake, addr).await?; let mut buf = [0u8; MAX_PACKET]; - let (n, recv_addr) = udp.recv_from(&mut buf).await.unwrap(); + let (n, recv_addr) = match udp.recv_from(&mut buf).await { + Ok(ret) => ret, + Err(e) => { + tracing::error!("Failed to receive handshake response: {}", e); + return Err(TunnelError::IOError(e)); + } + }; + if recv_addr != addr { tracing::warn!(?recv_addr, "Received packet from changed address"); } @@ -646,7 +653,13 @@ impl WgTunnelConnector { data.handle_one_packet_from_peer(&mut sink, &buf[..n]).await; loop { let mut buf = vec![0u8; MAX_PACKET]; - let (n, _) = data.udp.recv_from(&mut buf).await.unwrap(); + let (n, _) = match udp.recv_from(&mut buf).await { + Ok(ret) => ret, + Err(e) => { + tracing::error!("Failed to receive wg packet: {}", e); + break; + } + }; data.handle_one_packet_from_peer(&mut sink, &buf[..n]).await; } });