From 62dbcc4d9eda960adabb76c13c10e98ab4842735 Mon Sep 17 00:00:00 2001 From: Lionel Faber Date: Fri, 19 Feb 2021 19:53:04 +0530 Subject: [PATCH] fix(config): do not use the IGD gateway to realize local IP address --- src/api.rs | 37 +++++++++---------------------------- src/igd.rs | 12 +----------- 2 files changed, 10 insertions(+), 39 deletions(-) diff --git a/src/api.rs b/src/api.rs index b3544ceb..6737d1ef 100644 --- a/src/api.rs +++ b/src/api.rs @@ -15,7 +15,7 @@ use super::{ peer_config::{self, DEFAULT_IDLE_TIMEOUT_MSEC, DEFAULT_KEEP_ALIVE_INTERVAL_MSEC}, }; use futures::{future, TryFutureExt}; -use log::{debug, error, info, trace, warn}; +use log::{debug, error, info, trace}; use std::net::{IpAddr, Ipv4Addr, SocketAddr, UdpSocket}; use std::path::PathBuf; @@ -77,20 +77,7 @@ impl QuicP2p { .map(|p| (p, false)) .unwrap_or((DEFAULT_PORT_TO_TRY, true)); - let mut ip = cfg.local_ip.unwrap_or(IpAddr::V4(Ipv4Addr::UNSPECIFIED)); - - // check hard coded contacts for being local (aka loopback) - if let Some(contact) = cfg.hard_coded_contacts.iter().next() { - let potential_ip = contact.ip(); - - if potential_ip.is_loopback() { - trace!( - "IP from hardcoded contact is loopback, setting our IP to: {:?}", - potential_ip - ); - ip = potential_ip; - } - } + let ip = cfg.local_ip.unwrap_or(IpAddr::V4(Ipv4Addr::UNSPECIFIED)); let idle_timeout_msec = cfg.idle_timeout_msec.unwrap_or(DEFAULT_IDLE_TIMEOUT_MSEC); @@ -316,21 +303,15 @@ fn bind( fn unwrap_config_or_default(cfg: Option) -> Result { let mut cfg = cfg.map_or(Config::read_or_construct_default(None)?, |cfg| cfg); if cfg.local_ip.is_none() { - cfg.local_ip = match crate::igd::get_local_ip() { - Ok(addr) => Some(addr), - Err(err) => { - warn!("Error realizing local IP using IGD gateway: {}", err); - let socket = UdpSocket::bind("0.0.0.0:0")?; - let mut local_ip = None; - for addr in cfg.hard_coded_contacts.iter() { - if let Ok(Ok(local_addr)) = socket.connect(addr).map(|()| socket.local_addr()) { - local_ip = Some(local_addr.ip()); - break; - } - } - local_ip + let socket = UdpSocket::bind("0.0.0.0:0")?; + let mut local_ip = None; + for addr in cfg.hard_coded_contacts.iter() { + if let Ok(Ok(local_addr)) = socket.connect(addr).map(|()| socket.local_addr()) { + local_ip = Some(local_addr.ip()); + break; } } + cfg.local_ip = local_ip; }; if cfg.clean { Config::clear_config_from_disk(None)?; diff --git a/src/igd.rs b/src/igd.rs index 5b5cf45c..db22e74c 100644 --- a/src/igd.rs +++ b/src/igd.rs @@ -10,7 +10,7 @@ use crate::error::{Error, Result}; use igd::SearchOptions; use log::{debug, info, warn}; -use std::net::{IpAddr, SocketAddr, SocketAddrV4}; +use std::net::{SocketAddr, SocketAddrV4}; use std::time::Duration; use tokio::time::{self, Instant}; @@ -104,13 +104,3 @@ pub(crate) async fn renew_port( Err(Error::IgdNotSupported) } } - -// Find our local IP address by connecting to the gateway and querying local socket address. -pub(crate) fn get_local_ip() -> Result { - debug!("Attempting to realise local IP address with IGD..."); - let gateway = igd::search_gateway(SearchOptions::default())?; - let gateway_conn = std::net::TcpStream::connect(gateway.addr)?; - let local_sa = gateway_conn.local_addr()?; - info!("Fetched IP address from IGD gateway: {:?}", &local_sa.ip()); - Ok(local_sa.ip()) -}