Skip to content

Commit 72384ce

Browse files
FrandoramfoxArqurklaehn
authored
feat: node discovery via DNS (#2045)
## Description This enables global node discovery over DNS, i.e. dialing nodes by just their node id. Current setup is as follows: * When dialing a node only by its NodeId, the new `DnsDiscovery` service is invoked. It will lookup a TXT record at (by default) `_iroh_node.b32encodednodeid.testdns.iroh.link` over regular DNS or DNS-over-http. Right now the Cloudflare DNS servers are configured. At `testdns.iroh.link` we run a custom [DNS server](https://github.com/n0-computer/iroh-dns-server/tree/main) * Nodes publish their Derp address to this DNS server through Pkarr signed packets. This is an intermediate step, we decided that the publishing by default should not happen by the nodes directly but mediated through the Derp servers. Work for the latter happens in #2052 This PR thus allows for the following: ```sh # terminal/computer 1 $ iroh console --start Iroh is running Node ID: qp2znfedwdij4llc5noizwfemfgba7bzxozvr4bp7hfsdmwqbpua $ blob add ./myfile ... Blob: o5uanh5s2zwn2sucy47puqidsfx2advxos7kajq3ajwitcwobhba ... # terminal/computer 2 iroh console --start blob get o5uanh5s2zwn2sucy47puqidsfx2advxos7kajq3ajwitcwobhba --node qp2znfedwdij4llc5noizwfemfgba7bzxozvr4bp7hfsdmwqbpua ``` <!-- A summary of what this pull request achieves and a rough list of changes. --> ## Notes & open questions * Misses node configuration in the CLI for the node origin domain (right now hardcoded to `testdns.iroh.link`). How do we want to expose this - CLI flag? Or in the config file? I'd say the latter. * Offload publishing to the Derpers - see #2052 * Right now the records published via pkarr have a TTL of 30s - the iroh-dns-server will use that TTL as-is when serving the records over DNS. both can/should change? * We can also *very* easily allow to lookup nodes not only by NodeId, but by any domain name. In the `iroh-dns` crate I included an example `resolve` that does just that. By setting a `CNAME` record you can even use any domain and simply point to the record hosted at the `testdns.iroh.link` server. So if, on your custom domain, you added a record like this ``` _iroh_node.frando.n0.computer CNAME _iroh_node.qp2znfedwdij4llc5noizwfemfgba7bzxozvr4bp7hfsdmwqbpua.iroh.link. ``` You can use this with the example to resolve to the node id and derp addresses: ``` cargo run --example resolve -- domain frando.n0.computer ``` <!-- Any notes, remarks or open questions you have to make about the PR. --> ## Change checklist - [x] Self-review. - [x] Documentation updates if relevant. - [x] Tests if relevant. Closes #1248 --------- Co-authored-by: Kasey <[email protected]> Co-authored-by: Asmir Avdicevic <[email protected]> Co-authored-by: Ruediger Klaehn <[email protected]>
1 parent d22c1cd commit 72384ce

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+4354
-235
lines changed

Cargo.lock

Lines changed: 403 additions & 217 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ members = [
33
"iroh",
44
"iroh-bytes",
55
"iroh-base",
6+
"iroh-dns-server",
67
"iroh-gossip",
78
"iroh-metrics",
89
"iroh-net",

iroh-base/src/node_addr.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use anyhow::Context;
44
use serde::{Deserialize, Serialize};
55
use url::Url;
66

7-
use crate::key::PublicKey;
7+
use crate::key::{NodeId, PublicKey};
88

99
/// A peer and it's addressing information.
1010
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
@@ -63,6 +63,12 @@ impl From<(PublicKey, Option<RelayUrl>, &[SocketAddr])> for NodeAddr {
6363
}
6464
}
6565

66+
impl From<NodeId> for NodeAddr {
67+
fn from(node_id: NodeId) -> Self {
68+
NodeAddr::new(node_id)
69+
}
70+
}
71+
6672
/// Addressing information to connect to a peer.
6773
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
6874
pub struct AddrInfo {
@@ -142,6 +148,12 @@ impl FromStr for RelayUrl {
142148
}
143149
}
144150

151+
impl From<RelayUrl> for Url {
152+
fn from(value: RelayUrl) -> Self {
153+
value.0
154+
}
155+
}
156+
145157
/// Dereference to the wrapped [`Url`].
146158
///
147159
/// Note that [`DerefMut`] is not implemented on purpose, so this type has more flexibility

iroh-cli/src/commands/blob.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,6 @@ impl BlobCommands {
236236
return Err(anyhow::anyhow!("The input arguments refer to a collection of blobs and output is set to STDOUT. Only single blobs may be passed in this case."));
237237
}
238238

239-
if node_addr.info.is_empty() {
240-
return Err(anyhow::anyhow!(
241-
"no relay url provided and no direct addresses provided"
242-
));
243-
}
244239
let tag = match tag {
245240
Some(tag) => SetTagOption::Named(Tag::from(tag)),
246241
None => SetTagOption::Auto,

iroh-dns-server/Cargo.toml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
[package]
2+
name = "iroh-dns-server"
3+
version = "0.13.0"
4+
edition = "2021"
5+
description = "A pkarr relay and DNS server"
6+
license = "MIT OR Apache-2.0"
7+
authors = ["Frando <[email protected]>", "n0 team"]
8+
repository = "https://github.com/n0-computer/iroh"
9+
keywords = ["networking", "pkarr", "dns", "dns-server", "iroh"]
10+
readme = "README.md"
11+
12+
[dependencies]
13+
anyhow = "1.0.80"
14+
async-trait = "0.1.77"
15+
axum = { version = "0.7.4", features = ["macros"] }
16+
axum-server = { version = "0.6.0", features = ["tls-rustls"] }
17+
base64-url = "2.0.2"
18+
bytes = "1.5.0"
19+
clap = { version = "4.5.1", features = ["derive"] }
20+
derive_more = { version = "1.0.0-beta.1", features = ["debug", "display", "into", "from"] }
21+
dirs-next = "2.0.0"
22+
futures = "0.3.30"
23+
governor = "0.6.3"
24+
hickory-proto = "0.24.0"
25+
hickory-server = { version = "0.24.0", features = ["dns-over-rustls"] }
26+
http = "1.0.0"
27+
iroh-metrics = { version = "0.13.0", path = "../iroh-metrics" }
28+
lru = "0.12.3"
29+
parking_lot = "0.12.1"
30+
pkarr = { version = "1.1.2", features = [ "async", "relay"], default_features = false }
31+
rcgen = "0.12.1"
32+
redb = "2.0.0"
33+
regex = "1.10.3"
34+
rustls = "0.21"
35+
rustls-pemfile = "1"
36+
serde = { version = "1.0.197", features = ["derive"] }
37+
struct_iterable = "0.1.1"
38+
strum = { version = "0.26.1", features = ["derive"] }
39+
tokio = { version = "1.36.0", features = ["full"] }
40+
tokio-rustls = "0.24"
41+
tokio-rustls-acme = { version = "0.3", features = ["axum"] }
42+
tokio-stream = "0.1.14"
43+
tokio-util = "0.7.10"
44+
toml = "0.8.10"
45+
tower-http = { version = "0.5.2", features = ["cors", "trace"] }
46+
tower_governor = "0.3.2"
47+
tracing = "0.1.40"
48+
tracing-subscriber = "0.3.18"
49+
url = "2.5.0"
50+
z32 = "1.1.1"
51+
52+
[dev-dependencies]
53+
hickory-resolver = "0.24.0"
54+
iroh-net = { version = "0.13.0", path = "../iroh-net" }

iroh-dns-server/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# iroh-dns-server
2+
3+
A server that functions as a [pkarr](https://github.com/Nuhvi/pkarr/) relay and
4+
[DNS](https://de.wikipedia.org/wiki/Domain_Name_System) server.
5+
6+
This server compiles to a binary `iroh-dns-server`. It needs a config file, of
7+
which there are two examples included:
8+
9+
- [`config.dev.toml`](./config.dev.toml) - suitable for local development
10+
- [`config.prod.toml`](./config.dev.toml) - suitable for production, after
11+
adjusting the domain names and IP addresses
12+
13+
The server will expose the following services:
14+
15+
- A DNS server listening on UDP and TCP for DNS queries
16+
- A HTTP and/or HTTPS server which provides the following routes:
17+
- `/pkarr`: `GET` and `PUT` for pkarr signed packets
18+
- `/dns-query`: Answer DNS queries over
19+
[DNS-over-HTTPS](https://datatracker.ietf.org/doc/html/rfc8484)
20+
21+
All received and valid pkarr signed packets will be served over DNS. The pkarr
22+
packet origin will be appended with the origin as configured by this server.
23+
24+
# License
25+
26+
This project is licensed under either of
27+
28+
- Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
29+
http://www.apache.org/licenses/LICENSE-2.0)
30+
- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
31+
32+
at your option.
33+
34+
### Contribution
35+
36+
Unless you explicitly state otherwise, any contribution intentionally submitted
37+
for inclusion in this project by you, as defined in the Apache-2.0 license,
38+
shall be dual licensed as above, without any additional terms or conditions.

iroh-dns-server/config.dev.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[http]
2+
port = 8080
3+
bind_addr = "127.0.0.1"
4+
5+
[https]
6+
port = 8443
7+
bind_addr = "127.0.0.1"
8+
domains = ["localhost"]
9+
cert_mode = "self_signed"
10+
11+
[dns]
12+
port = 5300
13+
bind_addr = "127.0.0.1"
14+
default_soa = "dns1.irohdns.example hostmaster.irohdns.example 0 10800 3600 604800 3600"
15+
default_ttl = 900
16+
origins = ["irohdns.example.", "."]
17+
rr_a = "127.0.0.1"
18+
rr_ns = "ns1.irohdns.example."

iroh-dns-server/config.prod.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[https]
2+
port = 443
3+
domains = ["irohdns.example.org"]
4+
cert_mode = "lets_encrypt"
5+
letsencrypt_prod = true
6+
7+
[dns]
8+
port = 53
9+
default_soa = "dns1.irohdns.example.org hostmaster.irohdns.example.org 0 10800 3600 604800 3600"
10+
default_ttl = 30
11+
origins = ["irohdns.example.org", "."]
12+
rr_a = "203.0.10.10"
13+
rr_ns = "ns1.irohdns.example.org."

iroh-dns-server/examples/convert.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use std::str::FromStr;
2+
3+
use clap::Parser;
4+
use iroh_net::NodeId;
5+
6+
#[derive(Debug, Parser)]
7+
struct Cli {
8+
#[clap(subcommand)]
9+
command: Command,
10+
}
11+
12+
#[derive(Debug, Parser)]
13+
enum Command {
14+
NodeToPkarr { node_id: String },
15+
PkarrToNode { z32_pubkey: String },
16+
}
17+
18+
fn main() -> anyhow::Result<()> {
19+
let args = Cli::parse();
20+
match args.command {
21+
Command::NodeToPkarr { node_id } => {
22+
let node_id = NodeId::from_str(&node_id)?;
23+
let public_key = pkarr::PublicKey::try_from(*node_id.as_bytes())?;
24+
println!("{}", public_key.to_z32())
25+
}
26+
Command::PkarrToNode { z32_pubkey } => {
27+
let public_key = pkarr::PublicKey::try_from(z32_pubkey.as_str())?;
28+
let node_id = NodeId::from_bytes(public_key.as_bytes())?;
29+
println!("{}", node_id)
30+
}
31+
}
32+
Ok(())
33+
}

iroh-dns-server/examples/publish.rs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
use std::str::FromStr;
2+
3+
use anyhow::{bail, Result};
4+
use clap::{Parser, ValueEnum};
5+
use iroh_net::{
6+
discovery::{
7+
dns::N0_DNS_NODE_ORIGIN,
8+
pkarr_publish::{PkarrRelayClient, N0_DNS_PKARR_RELAY},
9+
},
10+
dns::node_info::{to_z32, NodeInfo, IROH_TXT_NAME},
11+
key::SecretKey,
12+
NodeId,
13+
};
14+
use url::Url;
15+
16+
const LOCALHOST_PKARR: &str = "http://localhost:8080/pkarr";
17+
const EXAMPLE_ORIGIN: &str = "irohdns.example";
18+
19+
#[derive(ValueEnum, Clone, Debug, Default, Copy, strum::Display)]
20+
#[strum(serialize_all = "kebab-case")]
21+
pub enum Env {
22+
/// Use the pkarr relay run by number0.
23+
#[default]
24+
Default,
25+
/// Use a relay listening at http://localhost:8080
26+
Dev,
27+
}
28+
29+
/// Publish a record to an irohdns server.
30+
///
31+
/// You have to set the IROH_SECRET environment variable to the node secret for which to publish.
32+
#[derive(Parser, Debug)]
33+
struct Cli {
34+
/// Environment to publish to.
35+
#[clap(value_enum, short, long, default_value_t = Env::Default)]
36+
env: Env,
37+
/// Pkarr Relay URL. If set, the --env option will be ignored.
38+
#[clap(long, conflicts_with = "env")]
39+
pkarr_relay: Option<Url>,
40+
/// Home relay server to publish for this node
41+
relay_url: Url,
42+
/// Create a new node secret if IROH_SECRET is unset. Only for development / debugging.
43+
#[clap(short, long)]
44+
create: bool,
45+
}
46+
47+
#[tokio::main]
48+
async fn main() -> Result<()> {
49+
tracing_subscriber::fmt::init();
50+
let args = Cli::parse();
51+
52+
let secret_key = match std::env::var("IROH_SECRET") {
53+
Ok(s) => SecretKey::from_str(&s)?,
54+
Err(_) if args.create => {
55+
let s = SecretKey::generate();
56+
println!("Generated a new node secret. To reuse, set");
57+
println!("IROH_SECRET={s}");
58+
s
59+
}
60+
Err(_) => {
61+
bail!("Environtment variable IROH_SECRET is not set. To create a new secret, use the --create option.")
62+
}
63+
};
64+
65+
let node_id = secret_key.public();
66+
let pkarr_relay = match (args.pkarr_relay, args.env) {
67+
(Some(pkarr_relay), _) => pkarr_relay,
68+
(None, Env::Default) => N0_DNS_PKARR_RELAY.parse().expect("valid url"),
69+
(None, Env::Dev) => LOCALHOST_PKARR.parse().expect("valid url"),
70+
};
71+
72+
println!("announce {node_id}:");
73+
println!(" relay={}", args.relay_url);
74+
println!();
75+
println!("publish to {pkarr_relay} ...");
76+
77+
let pkarr = PkarrRelayClient::new(pkarr_relay);
78+
let node_info = NodeInfo::new(node_id, Some(args.relay_url));
79+
let signed_packet = node_info.to_pkarr_signed_packet(&secret_key, 30)?;
80+
pkarr.publish(&signed_packet).await?;
81+
82+
println!("signed packet published.");
83+
println!("resolve with:");
84+
85+
match args.env {
86+
Env::Default => {
87+
println!(" cargo run --example resolve -- node {}", node_id);
88+
println!(" dig {} TXT", fmt_domain(&node_id, N0_DNS_NODE_ORIGIN))
89+
}
90+
Env::Dev => {
91+
println!(
92+
" cargo run --example resolve -- --env dev node {}",
93+
node_id
94+
);
95+
println!(
96+
" dig @localhost -p 5300 {} TXT",
97+
fmt_domain(&node_id, EXAMPLE_ORIGIN)
98+
)
99+
}
100+
}
101+
Ok(())
102+
}
103+
104+
fn fmt_domain(node_id: &NodeId, origin: &str) -> String {
105+
format!("{}.{}.{}", IROH_TXT_NAME, to_z32(node_id), origin)
106+
}

0 commit comments

Comments
 (0)