Skip to content

Commit

Permalink
feat(igd): expose a feature to completely disable IGD support, downgr…
Browse files Browse the repository at this point in the history
…ading igd crate to v0.11.1
  • Loading branch information
bochaco committed Feb 18, 2021
1 parent 1a9b748 commit e995730
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 18 deletions.
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ webpki = "~0.21.3"
features = [ "serde" ]

[dependencies.igd]
version = "~0.12.0"
version = "~0.11.1"
features = [ "aio" ]
optional = true

[dependencies.quinn]
version = "~0.6.1"
Expand All @@ -54,3 +55,7 @@ rand = "~0.7.3"

[target."cfg(any(all(unix, not(any(target_os = \"android\", target_os = \"androideabi\", target_os = \"ios\"))), windows))".dependencies]
dirs-next = "2.0.0"

[features]
default = [ "igd" ]
no-igd = []
47 changes: 32 additions & 15 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ 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};
#[cfg(not(feature = "no-igd"))]
use log::warn;
use log::{debug, error, info, trace};
use std::net::{IpAddr, Ipv4Addr, SocketAddr, UdpSocket};
use std::path::PathBuf;
use std::{collections::HashSet, 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.
Expand Down Expand Up @@ -315,25 +317,40 @@ fn bind(

fn unwrap_config_or_default(cfg: Option<Config>) -> Result<Config> {
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;
}
#[cfg(feature = "no-igd")]
{
cfg.local_ip = realize_ip_from_contacts(&cfg.hard_coded_contacts)?;
}

#[cfg(not(feature = "no-igd"))]
{
cfg.local_ip = match crate::igd::get_local_ip() {
Ok(addr) => Some(addr),
Err(err) => {
warn!("Error realizing local IP using IGD gateway: {}", err);
realize_ip_from_contacts(&cfg.hard_coded_contacts)?
}
local_ip
}
}
};
}

if cfg.clean {
Config::clear_config_from_disk(None)?;
}

Ok(cfg)
}

fn realize_ip_from_contacts(contacts: &HashSet<SocketAddr>) -> Result<Option<IpAddr>> {
debug!("Realizing local IP by connecting to contacts");
let socket = UdpSocket::bind("0.0.0.0:0")?;
for addr in contacts.iter() {
if let Ok(Ok(local_addr)) = socket.connect(addr).map(|()| socket.local_addr()) {
return Ok(Some(local_addr.ip()));
}
}

Ok(None)
}
11 changes: 9 additions & 2 deletions src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
// Software.

use super::error::Error;
use super::igd::forward_port;
use super::wire_msg::WireMsg;
#[cfg(not(feature = "no-igd"))]
use super::{api::DEFAULT_UPNP_LEASE_DURATION_SEC, igd::forward_port};
use super::{
api::DEFAULT_UPNP_LEASE_DURATION_SEC,
connection_deduplicator::ConnectionDeduplicator,
connection_pool::ConnectionPool,
connections::{
Expand All @@ -32,6 +32,7 @@ use tokio::time::timeout;
const CERT_SERVER_NAME: &str = "MaidSAFE.net";

// Number of seconds before timing out the IGD request to forward a port.
#[cfg(not(feature = "no-igd"))]
const PORT_FORWARD_TIMEOUT: u64 = 30;

/// Channel on which incoming messages can be listened to
Expand Down Expand Up @@ -210,6 +211,12 @@ impl Endpoint {

let mut addr = None;

#[cfg(feature = "no-igd")]
if self.qp2p_config.forward_port {
warn!("Ignoring 'forward_port' flag from config since IGD has been disabled (feature 'no-igd' has been set)");
}

#[cfg(not(feature = "no-igd"))]
if self.qp2p_config.forward_port {
// Attempt to use IGD for port forwarding
match timeout(
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ mod connection_pool;
mod connections;
mod endpoint;
mod error;
#[cfg(not(feature = "no-igd"))]
mod igd;
mod peer_config;
#[cfg(test)]
Expand Down

0 comments on commit e995730

Please sign in to comment.