Skip to content

Commit c548084

Browse files
fix blocking fix for local peer discovery
1 parent 3ffecf1 commit c548084

4 files changed

Lines changed: 35 additions & 31 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Version 0.4.2
2+
* another fix for local peer discovery
3+
14
# Version 0.4.1
25
* fix for blocking on local peer discovery
36

Cargo.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "wirespider"
3-
version = "0.4.1"
3+
version = "0.4.2"
44
authors = ["Linus Karl <linus@lotz.li>"]
55
license = "GPL-3"
66
edition = "2021"

src/client/local_ip_detection.rs

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
1-
use std::{net::{IpAddr, SocketAddr}, time::Duration};
1+
use std::{
2+
net::{IpAddr, SocketAddr},
3+
time::Duration,
4+
};
25

3-
use tokio::net::UdpSocket;
4-
use tokio_graceful_shutdown::SubsystemHandle;
56
use futures::future::join_all;
67
use tokio::io::Error;
8+
use tokio::net::UdpSocket;
9+
use tokio::time::timeout;
10+
use tokio_graceful_shutdown::SubsystemHandle;
711
use tracing::instrument;
812
use tracing_unwrap::ResultExt;
913
use wirespider::WireguardKey;
10-
use tokio::time::timeout;
1114

12-
const MESSAGE : &str = "wirespider";
15+
const MESSAGE: &str = "wirespider";
1316

14-
pub async fn local_ip_detection_service(subsys: SubsystemHandle, key: WireguardKey) -> Result<(),Error> {
17+
pub async fn local_ip_detection_service(
18+
subsys: SubsystemHandle,
19+
key: WireguardKey,
20+
) -> Result<(), Error> {
1521
let socket = UdpSocket::bind("0.0.0.0:27212").await.unwrap_or_log();
1622
let mut buf = [0u8; MESSAGE.len()];
17-
loop{
23+
loop {
1824
tokio::select! {
1925
recv = socket.recv_from(&mut buf) => {
2026
let (length, from) = recv.unwrap_or_log();
@@ -31,34 +37,29 @@ pub async fn local_ip_detection_service(subsys: SubsystemHandle, key: WireguardK
3137
}
3238

3339
#[instrument]
34-
pub async fn check_local_ips(ips: &[IpAddr], key: WireguardKey) -> Result<Option<IpAddr>,Error> {
35-
let results = timeout(Duration::from_millis(100), join_all(ips.iter().map(|x| check_ip(*x, key)))).await;
36-
match results {
37-
Ok(results) => {
38-
for result in results {
39-
match result? {
40-
Some(ip) => return Ok(Some(ip)),
41-
None => continue,
42-
};
43-
}
44-
},
45-
Err(_) => return Ok(None)
40+
pub async fn check_local_ips(ips: &[IpAddr], key: WireguardKey) -> Result<Option<IpAddr>, Error> {
41+
let results = join_all(ips.iter().map(|x| check_ip(*x, key))).await;
42+
for result in results {
43+
match result? {
44+
Some(ip) => return Ok(Some(ip)),
45+
None => continue,
46+
};
4647
}
4748
Ok(None)
4849
}
4950

5051
#[instrument]
51-
async fn check_ip(ip: IpAddr, key: WireguardKey) -> Result<Option<IpAddr>,Error> {
52+
async fn check_ip(ip: IpAddr, key: WireguardKey) -> Result<Option<IpAddr>, Error> {
5253
if ip.is_ipv6() {
5354
return Ok(None); // not supported for now
5455
}
5556
let socket = UdpSocket::bind("0.0.0.0:0").await?;
5657
socket.connect(SocketAddr::from((ip, 27212))).await?;
5758
socket.send(MESSAGE.as_bytes()).await?;
58-
let mut buffer : WireguardKey = [0; 32];
59+
let mut buffer: WireguardKey = [0; 32];
5960

60-
match socket.recv(&mut buffer).await {
61-
Ok(size) if size == 32 && buffer == key => Ok(Some(ip)),
62-
_ => Ok(None)
61+
match timeout(Duration::from_millis(100), socket.recv(&mut buffer)).await {
62+
Ok(Ok(size)) if size == 32 && buffer == key => Ok(Some(ip)),
63+
_ => Ok(None),
6364
}
64-
}
65+
}

0 commit comments

Comments
 (0)