iroh/net_report/
options.rs

1//! Options for creating a report gen client.
2
3pub(crate) use imp::Options;
4
5#[cfg(not(wasm_browser))]
6mod imp {
7    use std::collections::BTreeSet;
8
9    use url::Url;
10
11    use crate::net_report::{NetReportConfig, QuicConfig, probes::Probe};
12
13    /// Options for running probes
14    ///
15    /// By default, will run Https probes.
16    ///
17    /// Use [`Options::quic_config`] to enable  QUIC address discovery.
18    #[derive(Debug, Clone)]
19    pub(crate) struct Options {
20        /// The configuration needed to launch QUIC address discovery probes.
21        ///
22        /// If not provided, will not run QUIC address discovery.
23        pub(crate) quic_config: Option<QuicConfig>,
24        /// TLS config for HTTPS probes.
25        pub(crate) tls_config: rustls::ClientConfig,
26        /// Proxy to send the HTTP(S) based probes through.
27        ///
28        /// If not provided, the probes connect directly.
29        pub(crate) proxy_url: Option<Url>,
30        /// User-facing configuration.
31        pub(crate) user_config: NetReportConfig,
32    }
33
34    impl Options {
35        pub(crate) fn new(tls_config: rustls::ClientConfig) -> Self {
36            Self {
37                quic_config: None,
38                tls_config,
39                proxy_url: None,
40                user_config: NetReportConfig::default(),
41            }
42        }
43        /// Enable quic probes
44        pub(crate) fn quic_config(mut self, quic_config: Option<QuicConfig>) -> Self {
45            self.quic_config = quic_config;
46            self
47        }
48
49        /// Sets the proxy to send the HTTP(S) based probes through.
50        pub(crate) fn proxy_url(mut self, proxy_url: Option<Url>) -> Self {
51            self.proxy_url = proxy_url;
52            self
53        }
54
55        /// Set the net report configuration.
56        pub(crate) fn net_report_config(mut self, config: NetReportConfig) -> Self {
57            self.user_config = config;
58            self
59        }
60
61        /// Turn the options into set of valid protocols
62        pub(crate) fn as_protocols(&self) -> BTreeSet<Probe> {
63            let mut protocols = BTreeSet::new();
64            if let Some(ref quic) = self.quic_config {
65                if quic.ipv4 {
66                    protocols.insert(Probe::QadIpv4);
67                }
68                if quic.ipv6 {
69                    protocols.insert(Probe::QadIpv6);
70                }
71            }
72            if self.user_config.https_probes {
73                protocols.insert(Probe::Https);
74            }
75            protocols
76        }
77    }
78}
79
80#[cfg(wasm_browser)]
81mod imp {
82    use std::collections::BTreeSet;
83
84    use crate::net_report::{NetReportConfig, Probe};
85
86    /// Options for running probes (in browsers).
87    ///
88    /// Only HTTPS probes are supported in browsers.
89    /// These are run by default.
90    #[derive(Debug, Clone)]
91    pub(crate) struct Options {
92        /// User-facing configuration.
93        pub(crate) user_config: NetReportConfig,
94    }
95
96    impl Default for Options {
97        fn default() -> Self {
98            Self {
99                user_config: NetReportConfig::default(),
100            }
101        }
102    }
103
104    impl Options {
105        /// Set the net report configuration.
106        pub(crate) fn net_report_config(mut self, config: NetReportConfig) -> Self {
107            self.user_config = config;
108            self
109        }
110
111        /// Turn the options into set of valid protocols
112        pub(crate) fn as_protocols(&self) -> BTreeSet<Probe> {
113            let mut protocols = BTreeSet::new();
114            if self.user_config.https_probes {
115                protocols.insert(Probe::Https);
116            }
117            protocols
118        }
119    }
120}