fix(client): bind SOCKS5 UDP socket to IPv6 properly, and fix 100% CPU spin in Android TUN reader

This commit is contained in:
ospab 2026-05-30 02:01:31 +03:00
parent 02de5456aa
commit 6fa6170c75
2 changed files with 7 additions and 4 deletions

View File

@ -329,8 +329,8 @@ pub async fn run_native_tunnel_from_fd(
}) { }) {
Ok(Ok(n)) if n > 0 => n as usize, Ok(Ok(n)) if n > 0 => n as usize,
Ok(Ok(n)) if n == 0 => break, // EOF Ok(Ok(n)) if n == 0 => break, // EOF
Ok(Ok(_n)) => continue, // Error reading packet, skip to next Ok(Ok(_n)) => break, // Fatal error reading TUN, must break to prevent 100% CPU spin
Ok(Err(_)) => continue, // Should not happen with try_io Ok(Err(_)) => break,
Err(_would_block) => continue, Err(_would_block) => continue,
}; };

View File

@ -96,7 +96,10 @@ async fn start_udp_session(
} }
} }
let udp = UdpSocket::bind("127.0.0.1:0").await?; let udp = match relay_addr {
SocketAddr::V4(_) => UdpSocket::bind("127.0.0.1:0").await?,
SocketAddr::V6(_) => UdpSocket::bind("[::1]:0").await.or_else(|_| UdpSocket::bind("[::]:0").await)?,
};
let mut buf = vec![0u8; 65536]; let mut buf = vec![0u8; 65536];
@ -115,7 +118,7 @@ async fn start_udp_session(
} }
packet.extend_from_slice(&dst.port().to_be_bytes()); packet.extend_from_slice(&dst.port().to_be_bytes());
packet.extend_from_slice(&payload); packet.extend_from_slice(&payload);
udp.send_to(&packet, relay_addr).await?; let _ = udp.send_to(&packet, relay_addr).await;
} }
Ok(None) => break, Ok(None) => break,
Err(_) => break, // timeout Err(_) => break, // timeout