Skip to content

Commit

Permalink
fix local peer detection issues
Browse files Browse the repository at this point in the history
  • Loading branch information
returntoreality committed Apr 3, 2022
1 parent bcdb7c2 commit c917228
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wirespider"
version = "0.4.0"
version = "0.4.1"
authors = ["Linus Karl <[email protected]>"]
license = "GPL-3"
edition = "2021"
Expand Down
1 change: 1 addition & 0 deletions src/client/interface/command_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use tempfile::NamedTempFile;
use tracing::debug;
use wirespider::WireguardKey;

#[derive(Clone, Debug)]
pub struct WireguardCommandLineInterface {
device_name: String,
addresses: Vec<IpNet>,
Expand Down
21 changes: 14 additions & 7 deletions src/client/local_ip_detection.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::net::{IpAddr, SocketAddr};
use std::{net::{IpAddr, SocketAddr}, time::Duration};

use tokio::net::UdpSocket;
use tokio_graceful_shutdown::SubsystemHandle;
Expand All @@ -7,6 +7,7 @@ use tokio::io::Error;
use tracing::instrument;
use tracing_unwrap::ResultExt;
use wirespider::WireguardKey;
use tokio::time::timeout;

const MESSAGE : &str = "wirespider";

Expand All @@ -31,12 +32,17 @@ pub async fn local_ip_detection_service(subsys: SubsystemHandle, key: WireguardK

#[instrument]
pub async fn check_local_ips(ips: &[IpAddr], key: WireguardKey) -> Result<Option<IpAddr>,Error> {
let results = join_all(ips.iter().map(|x| check_ip(*x, key))).await;
for result in results {
match result? {
Some(ip) => return Ok(Some(ip)),
None => continue,
};
let results = timeout(Duration::from_millis(100), join_all(ips.iter().map(|x| check_ip(*x, key)))).await;
match results {
Ok(results) => {
for result in results {
match result? {
Some(ip) => return Ok(Some(ip)),
None => continue,
};
}
},
Err(_) => return Ok(None)
}
Ok(None)
}
Expand All @@ -50,6 +56,7 @@ async fn check_ip(ip: IpAddr, key: WireguardKey) -> Result<Option<IpAddr>,Error>
socket.connect(SocketAddr::from((ip, 27212))).await?;
socket.send(MESSAGE.as_bytes()).await?;
let mut buffer : WireguardKey = [0; 32];

match socket.recv(&mut buffer).await {
Ok(size) if size == 32 && buffer == key => Ok(Some(ip)),
_ => Ok(None)
Expand Down
12 changes: 11 additions & 1 deletion src/server/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ impl WirespiderServerState {
let local_ips = result
.get::<&str, &str>("local_ips")
.split(',')
.filter(|x| !x.is_empty())
.map(IpAddr::from_str)
.collect::<Result<Vec<_>, _>>()
.map_err(|_| Status::internal("local IP error"))?;
Expand Down Expand Up @@ -508,6 +509,7 @@ impl Wirespider for WirespiderServerState {
let local_port : u16 = request.get_ref().local_port.try_into().map_err(|_| Status::invalid_argument("Invalid local port"))?;

// TODO: do not allow updating key to be the same as an existing entry
debug!("getting peer data");
let peer_query =
sqlx::query(r#"SELECT pubkey, current_endpoint, local_ips, local_port FROM peers WHERE peerid=?"#)
.bind(auth_peer.peerid)
Expand All @@ -516,6 +518,7 @@ impl Wirespider for WirespiderServerState {
.into_status()?;
let old_pubkey = peer_query.get::<Option<&[u8]>, &str>("pubkey");
if old_pubkey != Some(&publickey[0..32]) {
debug!("updating peer data");
updated = true;
eventtype = EventType::New;
if old_pubkey.is_some() {
Expand All @@ -539,6 +542,8 @@ impl Wirespider for WirespiderServerState {
.await
.into_status()?;
}

debug!("checking local_port");
let old_local_port = peer_query.try_get::<u16,&str>("local_port").into_status()?;
if old_local_port != local_port {
// no need to send update to peers, so do not set updated flag
Expand Down Expand Up @@ -574,6 +579,7 @@ impl Wirespider for WirespiderServerState {
}

// local ips
debug!("checking local_ips");
let old_local_ips = peer_query
.get::<&str, &str>("local_ips")
.split(',')
Expand Down Expand Up @@ -632,11 +638,14 @@ impl Wirespider for WirespiderServerState {
result.get::<&str, &str>("network")
);
}
debug!("comparing local_ips to networks");
new_local_ips.retain(|x| !match x {
IpAddr::V4(net) => ip4range.contains(net),
IpAddr::V6(net) => ip6range.contains(net),
});
debug!("final local ips: {:?}", new_local_ips);
if new_local_ips != old_local_ips {
debug!("updating local ips");
sqlx::query(r#"UPDATE peers SET local_ips=? WHERE peerid=?"#)
.bind(new_local_ips.iter().map(IpAddr::to_string).join(","))
.bind(auth_peer.peerid)
Expand All @@ -650,8 +659,9 @@ impl Wirespider for WirespiderServerState {
.await
.map_err(|_| Status::internal("allowed IP error"))?;
let reply = AddressReply::new(&final_addresses, &overlay_ips);

if updated {
debug!("update triggered");
let peer = self
.get_peer_from_peerid(auth_peer.peerid)
.await?
Expand Down

0 comments on commit c917228

Please sign in to comment.