Skip to content

Commit

Permalink
move the defaults to the defaults module
Browse files Browse the repository at this point in the history
There it's also available to other code
  • Loading branch information
flub committed Jun 7, 2024
1 parent 2ee232f commit f4bd06d
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 25 deletions.
4 changes: 2 additions & 2 deletions iroh-cli/src/commands/doctor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use iroh::{
},
docs::{Capability, DocTicket},
net::{
defaults::DEFAULT_RELAY_STUN_PORT,
defaults::DEFAULT_STUN_PORT,
discovery::{
dns::DnsDiscovery, pkarr_publish::PkarrPublisher, ConcurrentDiscovery, Discovery,
},
Expand Down Expand Up @@ -631,7 +631,7 @@ async fn passive_side(gui: Gui, connection: Connection) -> anyhow::Result<()> {
}

fn configure_local_relay_map() -> RelayMap {
let stun_port = DEFAULT_RELAY_STUN_PORT;
let stun_port = DEFAULT_STUN_PORT;
let url = "http://localhost:3340".parse().unwrap();
RelayMap::default_from_node(url, stun_port)
}
Expand Down
16 changes: 5 additions & 11 deletions iroh-net/src/bin/iroh-relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ use http::{response::Builder as ResponseBuilder, HeaderMap};
use hyper::body::Incoming;
use hyper::{Method, Request, Response, StatusCode};
use iroh_metrics::inc;
use iroh_net::defaults::{DEFAULT_RELAY_STUN_PORT, NA_RELAY_HOSTNAME};
use iroh_net::defaults::{
DEFAULT_HTTPS_PORT, DEFAULT_HTTP_PORT, DEFAULT_METRICS_PORT, DEFAULT_STUN_PORT,
NA_RELAY_HOSTNAME,
};
use iroh_net::key::SecretKey;
use iroh_net::relay::http::{
ServerBuilder as RelayServerBuilder, TlsAcceptor, TlsConfig as RelayTlsConfig,
Expand All @@ -42,15 +45,6 @@ type BytesBody = http_body_util::Full<hyper::body::Bytes>;
type HyperError = Box<dyn std::error::Error + Send + Sync>;
type HyperResult<T> = std::result::Result<T, HyperError>;

/// The default port for `http_bind_addr`.
const DEFAULT_HTTP_PORT: u16 = 80;

/// The default port for `https_bind_addr`.
const DEFAULT_HTTPS_PORT: u16 = 443;

/// The default port for the metrics server.
const DEFAULT_METRICS_PORT: u16 = 9090;

/// The default `http_bind_port` when using `--dev`.
const DEV_MODE_HTTP_PORT: u16 = 3340;

Expand Down Expand Up @@ -259,7 +253,7 @@ impl Config {

fn stun_bind_addr(&self) -> SocketAddr {
self.stun_bind_addr
.unwrap_or_else(|| SocketAddr::new(self.http_bind_addr().ip(), DEFAULT_RELAY_STUN_PORT))
.unwrap_or_else(|| SocketAddr::new(self.http_bind_addr().ip(), DEFAULT_STUN_PORT))
}

fn metrics_bind_addr(&self) -> SocketAddr {
Expand Down
20 changes: 16 additions & 4 deletions iroh-net/src/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,20 @@ pub const NA_RELAY_HOSTNAME: &str = "use1-1.relay.iroh.network.";
/// Hostname of the default EU relay.
pub const EU_RELAY_HOSTNAME: &str = "euw1-1.relay.iroh.network.";

/// STUN port as defined by [RFC 8489](<https://www.rfc-editor.org/rfc/rfc8489#section-18.6>)
pub const DEFAULT_RELAY_STUN_PORT: u16 = 3478;
/// The default STUN port used by the Relay server.
///
/// The STUN port as defined by [RFC
/// 8489](<https://www.rfc-editor.org/rfc/rfc8489#section-18.6>)
pub const DEFAULT_STUN_PORT: u16 = 3478;

/// The default HTTP port used by the Relay server.
pub const DEFAULT_HTTP_PORT: u16 = 80;

/// The default HTTPS port used by the Relay server.
pub const DEFAULT_HTTPS_PORT: u16 = 443;

/// The default metrics port used by the Relay server.
pub const DEFAULT_METRICS_PORT: u16 = 9090;

/// Get the default [`RelayMap`].
pub fn default_relay_map() -> RelayMap {
Expand All @@ -27,7 +39,7 @@ pub fn default_na_relay_node() -> RelayNode {
RelayNode {
url: url.into(),
stun_only: false,
stun_port: DEFAULT_RELAY_STUN_PORT,
stun_port: DEFAULT_STUN_PORT,
}
}

Expand All @@ -40,6 +52,6 @@ pub fn default_eu_relay_node() -> RelayNode {
RelayNode {
url: url.into(),
stun_only: false,
stun_port: DEFAULT_RELAY_STUN_PORT,
stun_port: DEFAULT_STUN_PORT,
}
}
15 changes: 11 additions & 4 deletions iroh-net/src/netcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,17 +785,24 @@ mod tests {
use tokio::time;
use tracing::info;

use crate::defaults::{DEFAULT_RELAY_STUN_PORT, EU_RELAY_HOSTNAME};
use crate::defaults::{DEFAULT_STUN_PORT, EU_RELAY_HOSTNAME};
use crate::ping::Pinger;
use crate::relay::iroh_relay;
use crate::relay::RelayNode;

use super::*;

#[tokio::test]
async fn test_basic() -> Result<()> {
let _guard = iroh_test::logging::setup();
let (stun_addr, stun_stats, _cleanup_guard) =
stun::test::serve("0.0.0.0".parse().unwrap()).await?;
let stun_server = iroh_relay::Server::new(iroh_relay::ServerConfig {
relay: None,
stun: iroh_relay::StunConfig {
bind_addr: (Ipv4Addr::LOCALHOST, DEFAULT_STUN_PORT).into(),
},
metrics_addr: None,
});
let stun_addr = stun_server.stun_addr();

let resolver = crate::dns::default_resolver();
let mut client = Client::new(None, resolver.clone())?;
Expand Down Expand Up @@ -842,7 +849,7 @@ mod tests {
let dm = RelayMap::from_nodes([RelayNode {
url: url.clone(),
stun_only: true,
stun_port: DEFAULT_RELAY_STUN_PORT,
stun_port: DEFAULT_STUN_PORT,
}])
.expect("hardcoded");

Expand Down
4 changes: 2 additions & 2 deletions iroh-net/src/netcheck/reportgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use tokio::time::{self, Instant};
use tracing::{debug, debug_span, error, info_span, trace, warn, Instrument, Span};

use super::NetcheckMetrics;
use crate::defaults::DEFAULT_RELAY_STUN_PORT;
use crate::defaults::DEFAULT_STUN_PORT;
use crate::dns::{DnsResolver, ResolverExt};
use crate::net::interfaces;
use crate::net::ip;
Expand Down Expand Up @@ -935,7 +935,7 @@ async fn get_relay_addr(
proto: ProbeProto,
) -> Result<SocketAddr> {
let port = if relay_node.stun_port == 0 {
DEFAULT_RELAY_STUN_PORT
DEFAULT_STUN_PORT
} else {
relay_node.stun_port
};
Expand Down
4 changes: 2 additions & 2 deletions iroh-net/src/relay/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{collections::BTreeMap, fmt, sync::Arc};
use anyhow::{ensure, Result};
use serde::{Deserialize, Serialize};

use crate::defaults::DEFAULT_RELAY_STUN_PORT;
use crate::defaults::DEFAULT_STUN_PORT;

use super::RelayUrl;

Expand Down Expand Up @@ -91,7 +91,7 @@ impl RelayMap {
/// This will use the default STUN port and IP addresses resolved from the URL's host name via DNS.
/// relay nodes are specified at <../../../docs/relay_nodes.md>
pub fn from_url(url: RelayUrl) -> Self {
Self::default_from_node(url, DEFAULT_RELAY_STUN_PORT)
Self::default_from_node(url, DEFAULT_STUN_PORT)
}

/// Constructs the [`RelayMap] from an iterator of [`RelayNode`]s.
Expand Down

0 comments on commit f4bd06d

Please sign in to comment.