Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the node to auto-forward its listnening port using UPnP #1575

Open
wants to merge 2 commits into
base: testnet2-2022
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ version = "2.0"
version = "0.23"
optional = true

[dependencies.igd]
version = "0.12"

[dependencies.num_cpus]
version = "1"

Expand Down
3 changes: 3 additions & 0 deletions snarkos/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ pub struct Node {
/// Specify the IP address and port for the node server.
#[clap(parse(try_from_str), default_value = "0.0.0.0:4132", long = "node")]
pub node: SocketAddr,
/// If the flag is set, the node will attempt to use UPnP to open its listening port.
#[structopt(long = "upnp")]
pub upnp: bool,
/// Specify the IP address and port for the RPC server.
#[clap(parse(try_from_str), default_value = "0.0.0.0:3032", long = "rpc")]
pub rpc: SocketAddr,
Expand Down
24 changes: 24 additions & 0 deletions snarkos/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use snarkos_metrics as metrics;
use tokio::sync::RwLock;

use anyhow::Result;
use igd::{AddPortError, PortMappingProtocol};
use std::{net::SocketAddr, sync::Arc, time::Duration};
use tokio::{net::TcpListener, sync::oneshot, task};

Expand All @@ -64,6 +65,29 @@ impl<N: Network, E: Environment> Server<N, E> {
///
#[inline]
pub async fn initialize(node: &Node, address: Option<Address<N>>, pool_ip: Option<SocketAddr>) -> Result<Self> {
// Use UPnP to detect the gateway, if the upnp option is enabled.
if node.upnp {
let gateway_search_opts = igd::SearchOptions {
timeout: Some(Duration::from_secs(1)),
..Default::default()
};
let gateway = igd::search_gateway(gateway_search_opts)
.map_err(|e| warn!("Can't obtain the node's gateway details: {}; perhaps it's not UPnP-enabled?", e))
.ok();

if let Some(ref gateway) = gateway {
if let SocketAddr::V4(internal_addr) = node.node {
let external_port = internal_addr.port();

match gateway.add_port(PortMappingProtocol::TCP, external_port, internal_addr, 0, "snarkOS") {
Err(AddPortError::PortInUse) => debug!("Port {} is already forwarded", external_port),
Err(e) => error!("Can't map external port {} to address {}: {}", external_port, internal_addr, e),
Ok(_) => info!("Enabled port forwarding via UPnP"),
}
}
}
}

// Initialize a new TCP listener at the given IP.
let (local_ip, listener) = match TcpListener::bind(node.node).await {
Ok(listener) => (listener.local_addr().expect("Failed to fetch the local IP"), listener),
Expand Down