mirror of
https://github.com/n0-computer/iroh.git
synced 2026-11-29 08:01:13 +00:00
fix: bind iroh-dns-server to both IPv4 and IPv6 (#4432)
## Description This makes iroh-dns-server bind to both IPv4 and IPv6 by default for HTTP, HTTPS, and DNS. The IPv6 wildcard (`::`) on Linux binds to both IPv4 and IPv6, which is what we want for the default. People can still override it via `bind_addr` in `config.toml`. ## Breaking Changes <!-- Optional, if there are any breaking changes document them, including how to migrate older code. --> ## Notes & open questions <!-- Any notes, remarks or open questions you have to make about the PR. --> ## Change checklist <!-- Remove any that are not relevant. --> - [x] Self-review. - [x] Documentation updates following the [style guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text), if relevant. - [x] Tests if relevant. - [x] All breaking changes documented. - [x] This PR was created by a human that thought critically about the proposed change and wrote an as clear and concise description as they could. - [x] This PR isn't slop, and is carefully crafted to do have the intented effect.
This commit is contained in:
Generated
+3
@@ -2355,6 +2355,7 @@ dependencies = [
|
||||
"bytes",
|
||||
"clap",
|
||||
"criterion",
|
||||
"ctor",
|
||||
"data-encoding",
|
||||
"derive_more",
|
||||
"dirs-next",
|
||||
@@ -2373,6 +2374,7 @@ dependencies = [
|
||||
"n0-error",
|
||||
"n0-future",
|
||||
"n0-tracing-test",
|
||||
"patchbay",
|
||||
"rand 0.10.1",
|
||||
"rand_chacha 0.10.0",
|
||||
"rcgen",
|
||||
@@ -2385,6 +2387,7 @@ dependencies = [
|
||||
"simple-dns",
|
||||
"strum",
|
||||
"tempfile",
|
||||
"testdir",
|
||||
"tokio",
|
||||
"tokio-rustls",
|
||||
"tokio-rustls-acme",
|
||||
|
||||
+1
-1
@@ -39,7 +39,7 @@ args = [
|
||||
[tasks.patchbay]
|
||||
workspace = false
|
||||
command = "cargo"
|
||||
args = ["nextest", "run", "-p", "iroh", "--features", "qlog", "--test", "patchbay", "--profile", "patchbay", "${@}"]
|
||||
args = ["nextest", "run", "-p", "iroh", "-p", "iroh-dns-server", "--features", "iroh/qlog", "--test", "patchbay", "--profile", "patchbay", "${@}"]
|
||||
|
||||
[tasks.check-external-types]
|
||||
description = "Run cargo check-external-types on workspace crates"
|
||||
|
||||
@@ -62,6 +62,7 @@ criterion = "0.8.0"
|
||||
data-encoding = "2.3.3"
|
||||
hickory-resolver = { version = "0.26.0", features = ["tls-ring", "https-ring"] }
|
||||
iroh = { path = "../iroh" }
|
||||
n0-error = { version = "1.0.0", features = ["anyhow"] }
|
||||
n0-tracing-test = "0.3"
|
||||
rand = "0.10"
|
||||
simple-dns = "0.11"
|
||||
@@ -72,6 +73,12 @@ reqwest = { version = "0.13", default-features = false, features = [
|
||||
serde_json = "1.0.145"
|
||||
tempfile = "3.23.0"
|
||||
|
||||
# patchbay netsim test dependencies (linux only)
|
||||
[target.'cfg(target_os = "linux")'.dev-dependencies]
|
||||
ctor = "1.0"
|
||||
patchbay = "0.6"
|
||||
testdir = "0.10"
|
||||
|
||||
[features]
|
||||
default = ["ring"]
|
||||
ring = ["rustls/ring", "tokio-rustls/ring"]
|
||||
|
||||
@@ -46,7 +46,7 @@ const DEFAULT_A_TTL: u32 = 60 * 60; // 1h
|
||||
pub struct DnsConfig {
|
||||
/// Port to bind the DNS listener to, for both UDP and TCP.
|
||||
pub port: u16,
|
||||
/// Address to bind the DNS listener to (defaults to `0.0.0.0`).
|
||||
/// Address to bind the DNS listener to (defaults to `::`, i.e. IPv6 wildcard which also covers IPv4).
|
||||
pub bind_addr: Option<IpAddr>,
|
||||
/// SOA record data served for authoritative zones, in zone-file format.
|
||||
pub default_soa: String,
|
||||
@@ -100,7 +100,7 @@ impl DnsServer {
|
||||
let mut server = hickory_server::Server::new(dns_handler);
|
||||
|
||||
let bind_addr = SocketAddr::new(
|
||||
config.bind_addr.unwrap_or(Ipv4Addr::UNSPECIFIED.into()),
|
||||
config.bind_addr.unwrap_or(Ipv6Addr::UNSPECIFIED.into()),
|
||||
config.port,
|
||||
);
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! HTTP server part of iroh-dns-server
|
||||
|
||||
use std::{
|
||||
net::{IpAddr, Ipv4Addr, SocketAddr},
|
||||
net::{IpAddr, Ipv6Addr, SocketAddr},
|
||||
path::PathBuf,
|
||||
time::Instant,
|
||||
};
|
||||
@@ -39,7 +39,7 @@ use crate::state::AppState;
|
||||
pub struct HttpConfig {
|
||||
/// Port to bind the HTTP listener to.
|
||||
pub port: u16,
|
||||
/// Address to bind the HTTP listener to (defaults to `0.0.0.0`).
|
||||
/// Address to bind the HTTP listener to (defaults to `::`, i.e. IPv6 wildcard which also covers IPv4).
|
||||
pub bind_addr: Option<IpAddr>,
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ pub struct HttpConfig {
|
||||
pub struct HttpsConfig {
|
||||
/// Port to bind the HTTPS listener to.
|
||||
pub port: u16,
|
||||
/// Address to bind the HTTPS listener to (defaults to `0.0.0.0`).
|
||||
/// Address to bind the HTTPS listener to (defaults to `::`, i.e. IPv6 wildcard which also covers IPv4).
|
||||
pub bind_addr: Option<IpAddr>,
|
||||
/// Domains for which TLS certificates are issued or loaded.
|
||||
pub domains: Vec<String>,
|
||||
@@ -97,7 +97,7 @@ impl HttpServer {
|
||||
// launch http
|
||||
let http_addr = if let Some(config) = http_config {
|
||||
let bind_addr = SocketAddr::new(
|
||||
config.bind_addr.unwrap_or(Ipv4Addr::UNSPECIFIED.into()),
|
||||
config.bind_addr.unwrap_or(Ipv6Addr::UNSPECIFIED.into()),
|
||||
config.port,
|
||||
);
|
||||
let app = app.clone();
|
||||
@@ -119,7 +119,7 @@ impl HttpServer {
|
||||
// launch https
|
||||
let https_addr = if let Some(config) = https_config {
|
||||
let bind_addr = SocketAddr::new(
|
||||
config.bind_addr.unwrap_or(Ipv4Addr::UNSPECIFIED.into()),
|
||||
config.bind_addr.unwrap_or(Ipv6Addr::UNSPECIFIED.into()),
|
||||
config.port,
|
||||
);
|
||||
let acceptor = {
|
||||
|
||||
@@ -0,0 +1,274 @@
|
||||
//! Patchbay network simulation tests for iroh-dns-server.
|
||||
//!
|
||||
//! These tests use the [`patchbay`] crate to create virtual network topologies
|
||||
//! in Linux user namespaces. They require Linux with user namespace support;
|
||||
//! on other systems, use the `patchbay` CLI to get a suitable container or VM.
|
||||
//!
|
||||
//! To run:
|
||||
//!
|
||||
//! ```sh
|
||||
//! cargo nextest run -p iroh-dns-server --test patchbay --profile patchbay
|
||||
//! # or via the `cargo make` alias (also runs iroh's patchbay tests):
|
||||
//! cargo make patchbay
|
||||
//! ```
|
||||
|
||||
// patchbay only runs on linux, and is skipped in cross-compile environments
|
||||
// via a cfg directive
|
||||
#![cfg(all(target_os = "linux", not(skip_patchbay)))]
|
||||
|
||||
use std::{
|
||||
net::{IpAddr, SocketAddr},
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use iroh_base::SecretKey;
|
||||
use iroh_dns::{
|
||||
dns::{DnsProtocol, DnsResolver},
|
||||
pkarr::SignedPacket,
|
||||
};
|
||||
use iroh_dns_server::{
|
||||
Server,
|
||||
config::{Config, MetricsConfig},
|
||||
};
|
||||
use n0_error::{Result, StackResultExt, StdResultExt, anyerr, ensure_any};
|
||||
use n0_tracing_test::traced_test;
|
||||
use patchbay::{IpSupport, Lab, OutDir};
|
||||
use testdir::testdir;
|
||||
use tokio::sync::oneshot;
|
||||
use tracing::info;
|
||||
|
||||
/// Init the user namespace before any threads are spawned.
|
||||
///
|
||||
/// This gives us all permissions we need for the patchbay tests.
|
||||
#[ctor::ctor(unsafe)]
|
||||
fn userns_ctor() {
|
||||
patchbay::init_userns().expect("failed to init userns");
|
||||
}
|
||||
|
||||
/// Ports the server task reports back after binding.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
struct Ports {
|
||||
dns: u16,
|
||||
http: u16,
|
||||
https: u16,
|
||||
}
|
||||
|
||||
/// The default bind addresses accept both IPv4 and IPv6 clients.
|
||||
///
|
||||
/// Runs the server with the default listener config (all `bind_addr`s unset,
|
||||
/// so every listener binds the `::` wildcard; only the data dir and metrics
|
||||
/// are overridden) on a dual-stack device. A client device then exercises
|
||||
/// all three listeners once per address family: it publishes a pkarr packet
|
||||
/// over HTTP, resolves it back over DNS (UDP and TCP), and fetches
|
||||
/// `/healthz` over HTTPS.
|
||||
#[tokio::test]
|
||||
#[traced_test]
|
||||
async fn default_binds_reachable_v4_and_v6() -> Result {
|
||||
let dir = testdir!();
|
||||
let mut builder = Lab::builder().outdir(OutDir::Exact(dir.clone()));
|
||||
if let Some(name) = std::thread::current().name() {
|
||||
builder = builder.label(name);
|
||||
}
|
||||
let lab = builder.build().await?;
|
||||
let guard = lab.test_guard();
|
||||
|
||||
let net = lab
|
||||
.add_router("net")
|
||||
.ip_support(IpSupport::DualStack)
|
||||
.build()
|
||||
.await?;
|
||||
let dev_server = lab.add_device("dns").uplink(net.id()).build().await?;
|
||||
let dev_client = lab.add_device("client").uplink(net.id()).build().await?;
|
||||
|
||||
let server_v4: IpAddr = dev_server.ip().expect("server has IPv4").into();
|
||||
let server_v6: IpAddr = dev_server.ip6().expect("server has IPv6").into();
|
||||
|
||||
let data_dir = dir.join("dns-server-data");
|
||||
let (ports_tx, ports_rx) = oneshot::channel();
|
||||
let mut server_task = dev_server.spawn(move |_dev| async move {
|
||||
tokio::fs::create_dir_all(&data_dir)
|
||||
.await
|
||||
.expect("create data dir");
|
||||
let mut config = Config::default();
|
||||
config.data_dir = Some(data_dir);
|
||||
config.metrics = Some(MetricsConfig::disabled());
|
||||
let server = Server::bind(config).await.expect("server bind");
|
||||
let ports = Ports {
|
||||
dns: server.dns_addr().port(),
|
||||
http: server.http_addr().expect("http is bound").port(),
|
||||
https: server.https_addr().expect("https is bound").port(),
|
||||
};
|
||||
ports_tx.send(ports).expect("test task alive");
|
||||
server.join().await.expect("server stopped unexpectedly");
|
||||
})?;
|
||||
let ports = ports_rx
|
||||
.await
|
||||
.std_context("server task died before binding")?;
|
||||
info!(?ports, %server_v4, %server_v6, "server bound with default addresses");
|
||||
|
||||
let client_task = dev_client.spawn(move |_dev| async move {
|
||||
for ip in [server_v4, server_v6] {
|
||||
check_reachable(ip, ports)
|
||||
.await
|
||||
.with_context(|_| format!("server not reachable via {ip}"))?;
|
||||
info!(%ip, "all listeners reachable");
|
||||
}
|
||||
n0_error::Ok(())
|
||||
})?;
|
||||
// Race the client against the server task: the server task runs forever,
|
||||
// so it finishing means it panicked. Failing here directly beats letting
|
||||
// the client run into timeouts that blame the network.
|
||||
tokio::select! {
|
||||
res = client_task => res.std_context("client task")??,
|
||||
res = &mut server_task => {
|
||||
return Err(anyerr!("dns server task exited: {res:?}"));
|
||||
}
|
||||
}
|
||||
|
||||
guard.ok();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Exercises all three listeners on the given server IP.
|
||||
///
|
||||
/// Publishes a fresh pkarr packet over HTTP, resolves its TXT record over DNS
|
||||
/// (UDP), and fetches `/healthz` over HTTPS (accepting the self-signed cert).
|
||||
/// A fresh key per call keeps the publishes from clashing across families.
|
||||
async fn check_reachable(ip: IpAddr, ports: Ports) -> Result {
|
||||
let host = match ip {
|
||||
IpAddr::V4(ip) => ip.to_string(),
|
||||
IpAddr::V6(ip) => format!("[{ip}]"),
|
||||
};
|
||||
let secret_key = SecretKey::from_bytes(&rand::random());
|
||||
let z32 = secret_key.public().to_z32();
|
||||
let txt_value = format!("bind-test={ip}");
|
||||
let packet = SignedPacket::from_txt_strings(&secret_key, "_iroh", [&txt_value], 30)
|
||||
.context("build signed packet")?;
|
||||
|
||||
let client = reqwest::Client::builder()
|
||||
.use_preconfigured_tls(tls::insecure_tls_config())
|
||||
.timeout(Duration::from_secs(10))
|
||||
.build()
|
||||
.anyerr()?;
|
||||
|
||||
// HTTP: publish the packet via the pkarr endpoint.
|
||||
let url = format!("http://{host}:{}/pkarr/{z32}", ports.http);
|
||||
let res = client
|
||||
.put(&url)
|
||||
.body(packet.to_relay_payload())
|
||||
.send()
|
||||
.await
|
||||
.anyerr()?;
|
||||
ensure_any!(
|
||||
res.status().is_success(),
|
||||
"pkarr publish over HTTP at {url} failed: {}",
|
||||
res.status()
|
||||
);
|
||||
|
||||
// DNS: resolve the published TXT record, once over UDP and once over TCP
|
||||
// (the DNS server binds both on the same address).
|
||||
let name = format!("_iroh.{z32}.irohdns.example.");
|
||||
for protocol in [DnsProtocol::Udp, DnsProtocol::Tcp] {
|
||||
let resolver = DnsResolver::builder()
|
||||
.with_nameserver(SocketAddr::new(ip, ports.dns), protocol)
|
||||
.build();
|
||||
let records: Vec<String> = resolver
|
||||
.lookup_txt(&name, Duration::from_secs(5))
|
||||
.await
|
||||
.with_context(|_| format!("TXT lookup over {protocol:?}"))?
|
||||
.map(|record| record.to_string())
|
||||
.collect();
|
||||
ensure_any!(
|
||||
records.contains(&txt_value),
|
||||
"TXT lookup over {protocol:?} for {name} returned {records:?}, expected {txt_value:?}"
|
||||
);
|
||||
}
|
||||
|
||||
// HTTPS: fetch the health endpoint.
|
||||
let url = format!("https://{host}:{}/healthz", ports.https);
|
||||
let res = client.get(&url).send().await.anyerr()?;
|
||||
ensure_any!(
|
||||
res.status().is_success(),
|
||||
"healthz over HTTPS at {url} failed: {}",
|
||||
res.status()
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
mod tls {
|
||||
use std::sync::Arc;
|
||||
|
||||
use rustls::{
|
||||
DigitallySignedStruct, RootCertStore,
|
||||
client::{
|
||||
ClientConfig,
|
||||
danger::{HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier},
|
||||
},
|
||||
crypto::{
|
||||
CryptoProvider, ring::default_provider, verify_tls12_signature, verify_tls13_signature,
|
||||
},
|
||||
pki_types::{CertificateDer, ServerName, UnixTime},
|
||||
};
|
||||
|
||||
/// Accepts any server certificate; the server uses a self-signed cert.
|
||||
#[derive(Debug)]
|
||||
struct NoCertificateVerification(CryptoProvider);
|
||||
|
||||
impl ServerCertVerifier for NoCertificateVerification {
|
||||
fn verify_server_cert(
|
||||
&self,
|
||||
_end_entity: &CertificateDer<'_>,
|
||||
_intermediates: &[CertificateDer<'_>],
|
||||
_server_name: &ServerName<'_>,
|
||||
_ocsp: &[u8],
|
||||
_now: UnixTime,
|
||||
) -> Result<ServerCertVerified, rustls::Error> {
|
||||
Ok(ServerCertVerified::assertion())
|
||||
}
|
||||
|
||||
fn verify_tls12_signature(
|
||||
&self,
|
||||
message: &[u8],
|
||||
cert: &CertificateDer<'_>,
|
||||
dss: &DigitallySignedStruct,
|
||||
) -> Result<HandshakeSignatureValid, rustls::Error> {
|
||||
verify_tls12_signature(
|
||||
message,
|
||||
cert,
|
||||
dss,
|
||||
&self.0.signature_verification_algorithms,
|
||||
)
|
||||
}
|
||||
|
||||
fn verify_tls13_signature(
|
||||
&self,
|
||||
message: &[u8],
|
||||
cert: &CertificateDer<'_>,
|
||||
dss: &DigitallySignedStruct,
|
||||
) -> Result<HandshakeSignatureValid, rustls::Error> {
|
||||
verify_tls13_signature(
|
||||
message,
|
||||
cert,
|
||||
dss,
|
||||
&self.0.signature_verification_algorithms,
|
||||
)
|
||||
}
|
||||
|
||||
fn supported_verify_schemes(&self) -> Vec<rustls::SignatureScheme> {
|
||||
self.0.signature_verification_algorithms.supported_schemes()
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn insecure_tls_config() -> ClientConfig {
|
||||
let provider = default_provider();
|
||||
let mut cfg = ClientConfig::builder_with_provider(Arc::new(provider.clone()))
|
||||
.with_safe_default_protocol_versions()
|
||||
.expect("valid protocol versions")
|
||||
.with_root_certificates(RootCertStore::empty())
|
||||
.with_no_client_auth();
|
||||
cfg.dangerous()
|
||||
.set_certificate_verifier(Arc::new(NoCertificateVerification(provider)));
|
||||
cfg
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user