Skip to content

Commit

Permalink
fixes(all): multiple fixes
Browse files Browse the repository at this point in the history
- always use random port unless otherwise specified
- use echoservice first and then attempt IGD
- don't bind to an unspecified IP address
  • Loading branch information
lionel-faber authored and Yoga07 committed Apr 5, 2021
1 parent 4d650bd commit e436503
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
6 changes: 3 additions & 3 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ use super::{
};
use futures::{future, TryFutureExt};
use log::{debug, error, info, trace};
use std::net::{IpAddr, Ipv4Addr, SocketAddr, UdpSocket};
use std::net::{SocketAddr, UdpSocket};
use std::path::PathBuf;

/// In the absence of a port supplied by the user via the config we will first try using this
/// before using a random port.
pub const DEFAULT_PORT_TO_TRY: u16 = 12000;
pub const DEFAULT_PORT_TO_TRY: u16 = 0;

/// Default duration of a UPnP lease, in seconds.
pub const DEFAULT_UPNP_LEASE_DURATION_SEC: u32 = 120;
Expand Down Expand Up @@ -77,7 +77,7 @@ impl QuicP2p {
.map(|p| (p, false))
.unwrap_or((DEFAULT_PORT_TO_TRY, true));

let ip = cfg.local_ip.unwrap_or(IpAddr::V4(Ipv4Addr::UNSPECIFIED));
let ip = cfg.local_ip.ok_or(Error::UnspecifiedLocalIp)?;

let idle_timeout_msec = cfg.idle_timeout_msec.unwrap_or(DEFAULT_IDLE_TIMEOUT_MSEC);

Expand Down
34 changes: 16 additions & 18 deletions src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ impl Endpoint {
/// such an address cannot be reached and hence not useful.
async fn fetch_public_address(&mut self) -> Result<SocketAddr> {
// Skip port forwarding
if self.local_addr.ip().is_loopback() {
return Ok(self.local_addr);
if self.local_addr.ip().is_loopback() || !self.qp2p_config.forward_port {
self.public_addr = Some(self.local_addr);
}

if let Some(socket_addr) = self.public_addr {
Expand All @@ -220,8 +220,20 @@ impl Endpoint {
warn!("Ignoring 'forward_port' flag from config since IGD has been disabled (feature 'no-igd' has been set)");
}

// Try to contact an echo service
match timeout(
Duration::from_secs(ECHO_SERVICE_QUERY_TIMEOUT),
self.query_ip_echo_service(),
)
.await
{
Ok(Ok(echo_res)) => addr = Some(echo_res),
Ok(Err(err)) => info!("Could not contact echo service: {} - {:?}", err, err),
Err(err) => info!("Query to echo service timed out: {:?}", err),
}

#[cfg(not(feature = "no-igd"))]
if self.qp2p_config.forward_port {
if addr.is_none() && self.qp2p_config.forward_port {
// Attempt to use IGD for port forwarding
match timeout(
Duration::from_secs(PORT_FORWARD_TIMEOUT),
Expand Down Expand Up @@ -251,21 +263,7 @@ impl Endpoint {
}
}

if addr.is_none() {
// Try to contact an echo service
match timeout(
Duration::from_secs(ECHO_SERVICE_QUERY_TIMEOUT),
self.query_ip_echo_service(),
)
.await
{
Ok(Ok(echo_res)) => addr = Some(echo_res),
Ok(Err(err)) => info!("Could not contact echo service: {} - {:?}", err, err),
Err(err) => info!("Query to echo service timed out: {:?}", err),
}
}

addr.map_or(Err(Error::NoEchoServiceResponse), |socket_addr| {
addr.map_or(Err(Error::UnresolvedPublicIp), |socket_addr| {
self.public_addr = Some(socket_addr);
Ok(socket_addr)
})
Expand Down
6 changes: 6 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,10 @@ pub enum Error {
/// A supposedly impossible internal error occurred
#[error("Unexpected internal error: {0}")]
UnexpectedError(String),
/// Unspecified local IP address
#[error("Unspecified Local IP address")]
UnspecifiedLocalIp,
/// Couldn't resolve Public IP address
#[error("Unresolved Public IP address")]
UnresolvedPublicIp,
}

0 comments on commit e436503

Please sign in to comment.