iroh/endpoint/
presets.rs

1//! Presets allow configuring an endpoint quickly with a chosen set of defaults.
2//!
3//! # Example
4//!
5//! ```no_run
6//! # #[cfg(with_crypto_provider)]
7//! # {
8//! # async fn wrapper() -> n0_error::Result {
9//! use iroh::{Endpoint, RelayMode, Watcher, endpoint::presets};
10//!
11//! let endpoint = Endpoint::builder(presets::N0).bind().await?;
12//! # let _ = endpoint;
13//! # Ok(())
14//! # }
15//! # }
16//! ```
17
18use crate::endpoint::Builder;
19
20/// A reusable bundle of endpoint [`Builder`] configuration.
21pub trait Preset {
22    /// Applies the configuration to the passed in [`Builder`].
23    fn apply(self, builder: Builder) -> Builder;
24}
25
26/// An empty preset that doesn't set anything on the builder.
27///
28/// This doesn't set mandatory builder options, so using this in
29/// `Endpoint::bind(presets::Empty)` will always fail.
30///
31/// However, it can be useful, if you want control over all mandatory options
32/// yourself, by using `Endpoint::builder(presets::Empty)`.
33///
34/// If you prefer a minimal version that is guaranteed to work, see the
35/// [`Minimal`] preset.
36#[derive(Debug, Copy, Clone, Default)]
37pub struct Empty;
38
39impl Preset for Empty {
40    fn apply(self, builder: Builder) -> Builder {
41        builder
42    }
43}
44
45/// A preset that is almost empty, besides setting mandatory options.
46///
47/// At the moment the only mandatory option to set on the endpoint builder is
48/// [`Builder::crypto_provider`]. This preset makes a choice for that based on
49/// the current set of enabled features in iroh, which is why it's only available
50/// with the `tls-ring` or `tls-aws-lc-rs` feature flag.
51///
52/// It uses either [ring] or [aws-lc-rs], depending on which feature is enabled
53/// on iroh (preferring ring if both are enabled).
54///
55/// [ring]: rustls::crypto::ring::default_provider
56/// [aws-lc-rs]: rustls::crypto::aws_lc_rs::default_provider
57#[cfg(with_crypto_provider)]
58#[derive(Debug, Copy, Clone, Default)]
59pub struct Minimal;
60
61#[cfg(with_crypto_provider)]
62impl Preset for Minimal {
63    fn apply(self, mut builder: Builder) -> Builder {
64        use std::sync::Arc;
65
66        #[cfg(feature = "tls-ring")]
67        {
68            builder = builder.crypto_provider(Arc::new(rustls::crypto::ring::default_provider()));
69        }
70
71        #[cfg(all(feature = "tls-aws-lc-rs", not(feature = "tls-ring")))]
72        {
73            builder =
74                builder.crypto_provider(Arc::new(rustls::crypto::aws_lc_rs::default_provider()));
75        }
76
77        builder
78    }
79}
80
81/// Configures the endpoint to use the n0 defaults
82///
83/// Currently this consists of
84/// - the DNS Address Lookup service.
85/// - the default relay servers provided by Number 0.
86/// - setting the [`rustls::crypto::CryptoProvider`] to [ring] or [aws-lc-rs], depending
87///   on which feature is enabled in iroh (preferring ring if both are enabled).
88///
89/// Due to the last point, this preset is only available with the `tls-ring` or
90/// `tls-aws-lc-rs` feature enabled.
91/// If you want to set your own crypto provider, we recommend copying the
92/// implementation of this preset into your own and setting the appropriate crypto
93/// provider there.
94///
95/// The default address lookup service publishes to and resolves from the
96/// n0.computer dns server `iroh.link`.
97///
98/// This is equivalent to adding a [`crate::address_lookup::PkarrPublisher`],
99/// a [`crate::address_lookup::PkarrResolver`], and (outside browsers) a
100/// [`crate::address_lookup::DnsAddressLookup`], all configured to use the
101/// n0.computer dns server.
102///
103/// This will by default use [`N0_DNS_PKARR_RELAY_PROD`].
104/// When in tests, or when the `test-utils` feature is enabled, this will use the
105/// [`N0_DNS_PKARR_RELAY_STAGING`].
106///
107/// [ring]: rustls::crypto::ring::default_provider
108/// [aws-lc-rs]: rustls::crypto::aws_lc_rs::default_provider
109/// [`N0_DNS_PKARR_RELAY_PROD`]: crate::address_lookup::N0_DNS_PKARR_RELAY_PROD
110/// [`N0_DNS_PKARR_RELAY_STAGING`]: crate::address_lookup::N0_DNS_PKARR_RELAY_STAGING
111#[cfg(with_crypto_provider)]
112#[derive(Debug, Copy, Clone, Default)]
113pub struct N0;
114
115#[cfg(with_crypto_provider)]
116impl Preset for N0 {
117    fn apply(self, mut builder: Builder) -> Builder {
118        use crate::{
119            address_lookup::{PkarrPublisher, PkarrResolver},
120            endpoint::default_relay_mode,
121        };
122
123        builder = Minimal.apply(builder);
124
125        builder = builder.address_lookup(PkarrPublisher::n0_dns());
126
127        // Resolve using HTTPS requests to our DNS server's /pkarr path.
128        builder = builder.address_lookup(PkarrResolver::n0_dns());
129
130        // Additionally resolve using DNS queries outside browsers.
131        #[cfg(not(wasm_browser))]
132        {
133            builder = builder.address_lookup(crate::address_lookup::DnsAddressLookup::n0_dns());
134        }
135
136        builder = builder.relay_mode(default_relay_mode());
137
138        builder
139    }
140}
141
142/// Configures the endpoint to use the n0 defaults, but with relay mode disabled.
143///
144/// Currently this consists of
145/// - setting `RelayMode::Disabled`
146/// - the DNS Address Lookup service, that publishes IP addresses rather than
147///   relay urls.
148/// - setting the [`rustls::crypto::CryptoProvider`] to [ring] or [aws-lc-rs], depending
149///   on which feature is enabled in iroh (preferring ring if both are enabled).
150///
151/// Due to the last point, this preset is only available with the `tls-ring` or
152/// `tls-aws-lc-rs` feature enabled.
153/// If you want to set your own crypto provider, we recommend copying the
154/// implementation of this preset into your own and setting the appropriate crypto
155/// provider there.
156///
157/// The default address lookup service publishes to and resolves from the
158/// n0.computer dns server `iroh.link`.
159///
160/// This is equivalent to adding a [`crate::address_lookup::PkarrPublisher`],
161/// a [`crate::address_lookup::PkarrResolver`], and (outside browsers) a
162/// [`crate::address_lookup::DnsAddressLookup`], all configured to use the
163/// n0.computer dns server.
164///
165/// This will by default use [`N0_DNS_PKARR_RELAY_PROD`].
166/// When in tests, or when the `test-utils` feature is enabled, this will use the
167/// [`N0_DNS_PKARR_RELAY_STAGING`].
168///
169/// [ring]: rustls::crypto::ring::default_provider
170/// [aws-lc-rs]: rustls::crypto::aws_lc_rs::default_provider
171/// [`N0_DNS_PKARR_RELAY_PROD`]: crate::address_lookup::N0_DNS_PKARR_RELAY_PROD
172/// [`N0_DNS_PKARR_RELAY_STAGING`]: crate::address_lookup::N0_DNS_PKARR_RELAY_STAGING
173#[cfg(with_crypto_provider)]
174#[derive(Debug, Copy, Clone, Default)]
175pub struct N0DisableRelay;
176
177#[cfg(with_crypto_provider)]
178impl Preset for N0DisableRelay {
179    fn apply(self, builder: Builder) -> Builder {
180        use crate::RelayMode;
181
182        N0.apply(builder).relay_mode(RelayMode::Disabled)
183    }
184}