Skip to content

Commit

Permalink
fix: check local addr is not loopback when no public is known
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuef committed Apr 1, 2021
1 parent bbf0a31 commit 521c288
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ impl Endpoint {
IncomingMessages,
DisconnectionEvents,
)> {
debug!("Making a new endpoint...");
let local_addr = quic_endpoint.local_addr()?;
debug!("Local addr is:{:?}.", local_addr);

let public_addr = match (qp2p_config.external_ip, qp2p_config.external_port) {
(Some(ip), Some(port)) => Some(SocketAddr::new(ip, port)),
_ => None,
Expand All @@ -131,6 +134,7 @@ impl Endpoint {
connection_pool: connection_pool.clone(),
connection_deduplicator: ConnectionDeduplicator::new(),
};
debug!("Endpoint public addrress: {:?}", endpoint.public_addr);

if let Some(addr) = endpoint.public_addr {
// External IP and port number is provided
Expand Down Expand Up @@ -166,7 +170,12 @@ impl Endpoint {
warn!("Public IP address not verified since bootstrap contacts are empty");
}
} else {
endpoint.public_addr = Some(endpoint.fetch_public_address().await?);
if !local_addr.ip().is_loopback() {
info!("local addr is not loopback: {:?}", local_addr);
endpoint.public_addr = Some(local_addr);
} else {
endpoint.public_addr = Some(endpoint.fetch_public_address().await?);
}
}

listen_for_incoming_connections(
Expand Down Expand Up @@ -203,6 +212,11 @@ impl Endpoint {
/// Note that if such an obtained address is of unspecified category we will ignore that as
/// such an address cannot be reached and hence not useful.
async fn fetch_public_address(&mut self) -> Result<SocketAddr> {
debug!(
"Fetching public addr as none was found, localaddr is: {:?}",
self.local_addr
);

// Skip port forwarding
if self.local_addr.ip().is_loopback() {
return Ok(self.local_addr);
Expand All @@ -221,6 +235,7 @@ impl Endpoint {

#[cfg(not(feature = "no-igd"))]
if self.qp2p_config.forward_port {
debug!("Should be attempting to port forward...");
// Attempt to use IGD for port forwarding
match timeout(
Duration::from_secs(PORT_FORWARD_TIMEOUT),
Expand Down Expand Up @@ -263,7 +278,6 @@ impl Endpoint {
Err(err) => info!("Query to echo service timed out: {:?}", err),
}
}

addr.map_or(Err(Error::NoEchoServiceResponse), |socket_addr| {
self.public_addr = Some(socket_addr);
Ok(socket_addr)
Expand Down
1 change: 1 addition & 0 deletions src/igd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use tokio::time::{self, Instant};

/// Automatically forwards a port and setups a tokio task to renew it periodically.
pub async fn forward_port(local_addr: SocketAddr, lease_duration: u32) -> Result<SocketAddrV4> {
debug!("local addre in forwrd port: {:?}", local_addr);
let igd_res = add_port(local_addr, lease_duration).await;

if let Ok(ref ext_sock_addr) = igd_res {
Expand Down

0 comments on commit 521c288

Please sign in to comment.