|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use std::{error::Error, net::IpAddr}; |
|
|
|
|
|
use crate::behaviour::{GatewayEvent, GatewayRequest}; |
|
|
use futures::{ |
|
|
channel::{mpsc, oneshot}, |
|
|
SinkExt, StreamExt, |
|
|
}; |
|
|
use igd_next::SearchOptions; |
|
|
|
|
|
pub use crate::behaviour::Behaviour; |
|
|
|
|
|
|
|
|
pub(crate) fn is_addr_global(addr: IpAddr) -> bool { |
|
|
match addr { |
|
|
IpAddr::V4(ip) => { |
|
|
!(ip.octets()[0] == 0 |
|
|
|| ip.is_private() |
|
|
|
|
|
|| (ip.octets()[0] == 100 && (ip.octets()[1] & 0b1100_0000 == 0b0100_0000)) |
|
|
|| ip.is_loopback() |
|
|
|| ip.is_link_local() |
|
|
|
|
|
||(ip.octets()[0] == 192 && ip.octets()[1] == 0 && ip.octets()[2] == 0) |
|
|
|| ip.is_documentation() |
|
|
|
|
|
|| (ip.octets()[0] == 198 && (ip.octets()[1] & 0xfe) == 18) |
|
|
|
|
|
|| (ip.octets()[0] & 240 == 240 && !ip.is_broadcast()) |
|
|
|| ip.is_broadcast()) |
|
|
} |
|
|
IpAddr::V6(ip) => { |
|
|
!(ip.is_unspecified() |
|
|
|| ip.is_loopback() |
|
|
|
|
|
|| matches!(ip.segments(), [0, 0, 0, 0, 0, 0xffff, _, _]) |
|
|
|
|
|
|| matches!(ip.segments(), [0x64, 0xff9b, 1, _, _, _, _, _]) |
|
|
|
|
|
|| matches!(ip.segments(), [0x100, 0, 0, 0, _, _, _, _]) |
|
|
|
|
|
|| (matches!(ip.segments(), [0x2001, b, _, _, _, _, _, _] if b < 0x200) |
|
|
&& !( |
|
|
|
|
|
u128::from_be_bytes(ip.octets()) == 0x2001_0001_0000_0000_0000_0000_0000_0001 |
|
|
|
|
|
|| u128::from_be_bytes(ip.octets()) == 0x2001_0001_0000_0000_0000_0000_0000_0002 |
|
|
|
|
|
|| matches!(ip.segments(), [0x2001, 3, _, _, _, _, _, _]) |
|
|
|
|
|
|| matches!(ip.segments(), [0x2001, 4, 0x112, _, _, _, _, _]) |
|
|
|
|
|
|| matches!(ip.segments(), [0x2001, b, _, _, _, _, _, _] if (0x20..=0x2F).contains(&b)) |
|
|
)) |
|
|
|
|
|
|| (ip.segments()[0] == 0x2001) && (ip.segments()[1] == 0xdb8) |
|
|
|
|
|
|| (ip.segments()[0] & 0xfe00) == 0xfc00 |
|
|
|
|
|
|| (ip.segments()[0] & 0xffc0) == 0xfe80) |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)] |
|
|
pub(crate) struct Gateway { |
|
|
pub(crate) sender: mpsc::Sender<GatewayRequest>, |
|
|
pub(crate) receiver: mpsc::Receiver<GatewayEvent>, |
|
|
pub(crate) external_addr: IpAddr, |
|
|
} |
|
|
|
|
|
pub(crate) fn search_gateway() -> oneshot::Receiver<Result<Gateway, Box<dyn Error + Send + Sync>>> { |
|
|
let (search_result_sender, search_result_receiver) = oneshot::channel(); |
|
|
|
|
|
let (events_sender, mut task_receiver) = mpsc::channel(10); |
|
|
let (mut task_sender, events_queue) = mpsc::channel(0); |
|
|
|
|
|
tokio::spawn(async move { |
|
|
let gateway = match igd_next::aio::tokio::search_gateway(SearchOptions::default()).await { |
|
|
Ok(gateway) => gateway, |
|
|
Err(err) => { |
|
|
let _ = search_result_sender.send(Err(err.into())); |
|
|
return; |
|
|
} |
|
|
}; |
|
|
|
|
|
let external_addr = match gateway.get_external_ip().await { |
|
|
Ok(addr) => addr, |
|
|
Err(err) => { |
|
|
let _ = search_result_sender.send(Err(err.into())); |
|
|
return; |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
if search_result_sender |
|
|
.send(Ok(Gateway { |
|
|
sender: events_sender, |
|
|
receiver: events_queue, |
|
|
external_addr, |
|
|
})) |
|
|
.is_err() |
|
|
{ |
|
|
return; |
|
|
} |
|
|
|
|
|
loop { |
|
|
|
|
|
let Some(req) = task_receiver.next().await else { |
|
|
return; |
|
|
}; |
|
|
let event = match req { |
|
|
GatewayRequest::AddMapping { mapping, duration } => { |
|
|
let gateway = gateway.clone(); |
|
|
match gateway |
|
|
.add_port( |
|
|
mapping.protocol, |
|
|
mapping.internal_addr.port(), |
|
|
mapping.internal_addr, |
|
|
duration, |
|
|
"rust-libp2p mapping", |
|
|
) |
|
|
.await |
|
|
{ |
|
|
Ok(()) => GatewayEvent::Mapped(mapping), |
|
|
Err(err) => GatewayEvent::MapFailure(mapping, err.into()), |
|
|
} |
|
|
} |
|
|
GatewayRequest::RemoveMapping(mapping) => { |
|
|
let gateway = gateway.clone(); |
|
|
match gateway |
|
|
.remove_port(mapping.protocol, mapping.internal_addr.port()) |
|
|
.await |
|
|
{ |
|
|
Ok(()) => GatewayEvent::Removed(mapping), |
|
|
Err(err) => GatewayEvent::RemovalFailure(mapping, err.into()), |
|
|
} |
|
|
} |
|
|
}; |
|
|
|
|
|
if task_sender.send(event).await.is_err() { |
|
|
return; |
|
|
} |
|
|
} |
|
|
}); |
|
|
|
|
|
search_result_receiver |
|
|
} |
|
|
|