arti_client/
lib.rs

1#![cfg_attr(docsrs, feature(doc_auto_cfg, doc_cfg))]
2#![doc = include_str!("../README.md")]
3// @@ begin lint list maintained by maint/add_warning @@
4#![allow(renamed_and_removed_lints)] // @@REMOVE_WHEN(ci_arti_stable)
5#![allow(unknown_lints)] // @@REMOVE_WHEN(ci_arti_nightly)
6#![warn(missing_docs)]
7#![warn(noop_method_call)]
8#![warn(unreachable_pub)]
9#![warn(clippy::all)]
10#![deny(clippy::await_holding_lock)]
11#![deny(clippy::cargo_common_metadata)]
12#![deny(clippy::cast_lossless)]
13#![deny(clippy::checked_conversions)]
14#![warn(clippy::cognitive_complexity)]
15#![deny(clippy::debug_assert_with_mut_call)]
16#![deny(clippy::exhaustive_enums)]
17#![deny(clippy::exhaustive_structs)]
18#![deny(clippy::expl_impl_clone_on_copy)]
19#![deny(clippy::fallible_impl_from)]
20#![deny(clippy::implicit_clone)]
21#![deny(clippy::large_stack_arrays)]
22#![warn(clippy::manual_ok_or)]
23#![deny(clippy::missing_docs_in_private_items)]
24#![warn(clippy::needless_borrow)]
25#![warn(clippy::needless_pass_by_value)]
26#![warn(clippy::option_option)]
27#![deny(clippy::print_stderr)]
28#![deny(clippy::print_stdout)]
29#![warn(clippy::rc_buffer)]
30#![deny(clippy::ref_option_ref)]
31#![warn(clippy::semicolon_if_nothing_returned)]
32#![warn(clippy::trait_duplication_in_bounds)]
33#![deny(clippy::unchecked_duration_subtraction)]
34#![deny(clippy::unnecessary_wraps)]
35#![warn(clippy::unseparated_literal_suffix)]
36#![deny(clippy::unwrap_used)]
37#![deny(clippy::mod_module_files)]
38#![allow(clippy::let_unit_value)] // This can reasonably be done for explicitness
39#![allow(clippy::uninlined_format_args)]
40#![allow(clippy::significant_drop_in_scrutinee)] // arti/-/merge_requests/588/#note_2812945
41#![allow(clippy::result_large_err)] // temporary workaround for arti#587
42#![allow(clippy::needless_raw_string_hashes)] // complained-about code is fine, often best
43#![allow(clippy::needless_lifetimes)] // See arti#1765
44//! <!-- @@ end lint list maintained by maint/add_warning @@ -->
45
46// TODO #1645 (either remove this, or decide to have it everywhere)
47#![cfg_attr(not(all(feature = "full", feature = "experimental")), allow(unused))]
48
49mod address;
50mod builder;
51mod client;
52mod protostatus;
53mod release_date;
54#[cfg(feature = "rpc")]
55pub mod rpc;
56mod util;
57
58pub mod config;
59pub mod status;
60
61pub use address::{DangerouslyIntoTorAddr, IntoTorAddr, TorAddr, TorAddrError};
62pub use builder::{TorClientBuilder, MAX_LOCAL_RESOURCE_TIMEOUT};
63pub use client::{BootstrapBehavior, DormantMode, InertTorClient, StreamPrefs, TorClient};
64pub use config::TorClientConfig;
65
66pub use tor_circmgr::isolation;
67pub use tor_circmgr::IsolationToken;
68pub use tor_error::{ErrorKind, HasKind};
69pub use tor_proto::stream::{DataReader, DataStream, DataWriter};
70
71mod err;
72pub use err::{Error, ErrorHint, HintableError};
73
74#[cfg(feature = "error_detail")]
75pub use err::ErrorDetail;
76
77/// Alias for the [`Result`] type corresponding to the high-level [`Error`].
78pub type Result<T> = std::result::Result<T, Error>;
79
80#[cfg(feature = "experimental-api")]
81pub use builder::DirProviderBuilder;
82
83#[cfg(all(feature = "onion-service-client", feature = "experimental-api"))]
84#[cfg_attr(
85    docsrs,
86    doc(cfg(all(feature = "onion-service-client", feature = "experimental-api")))
87)]
88pub use {
89    tor_hscrypto::pk::{HsClientDescEncKey, HsId},
90    tor_keymgr::KeystoreSelector,
91};
92
93#[cfg(feature = "geoip")]
94#[cfg_attr(docsrs, doc(cfg(feature = "geoip")))]
95pub use tor_geoip::CountryCode;
96
97/// Return a list of the protocols [supported](tor_protover::doc_supported) by this crate.
98///
99/// (This is a crate-private method so as not to expose tor_protover in our public API.)
100///
101/// *WARNING*: REMOVING ELEMENTS FROM THIS LIST CAN BE DANGEROUS!
102/// SEE [`tor_protover::doc_changing`]
103pub(crate) fn supported_protocols() -> tor_protover::Protocols {
104    let protocols = tor_proto::supported_client_protocols()
105        .union(&tor_netdoc::supported_protocols())
106        .union(&tor_dirmgr::supported_client_protocols());
107
108    // TODO: the behavior for here seems most questionable!
109    // We will warn if any hs protocol happens to be recommended and we do not support onion
110    // services.
111    // We will also fail to warn if any hs protocol is required, and we support it only as a client
112    // or only as a service.
113    // We ought to determine the right behavior here.
114    // See torspec#319 at https://gitlab.torproject.org/tpo/core/torspec/-/issues/319.
115    #[cfg(feature = "onion-service-service")]
116    let protocols = protocols.union(&tor_hsservice::supported_hsservice_protocols());
117    #[cfg(feature = "onion-service-client")]
118    let protocols = protocols.union(&tor_hsclient::supported_hsclient_protocols());
119
120    let hs_protocols = {
121        // As a temporary workaround (again see torspec#319) we are unconditionally adding the
122        // conditionally supported HSService protocols.
123        use tor_protover::named::*;
124        [
125            //
126            HSINTRO_V3,
127            HSINTRO_RATELIM,
128            HSREND_V3,
129            HSDIR_V3,
130        ]
131        .into_iter()
132        .collect()
133    };
134
135    protocols.union(&hs_protocols)
136}
137
138/// Return the approximate release date of this version of arti client.
139///
140/// See[`release_date::ARTI_CLIENT_RELEASE_DATE`] for rationale.
141pub(crate) fn software_release_date() -> std::time::SystemTime {
142    use time::OffsetDateTime;
143
144    let format = time::macros::format_description!("[year]-[month]-[day]");
145    let date = time::Date::parse(release_date::ARTI_CLIENT_RELEASE_DATE, &format)
146        .expect("Invalid hard-coded release date!?");
147    OffsetDateTime::new_utc(date, time::Time::MIDNIGHT).into()
148}
149
150#[cfg(test)]
151mod test {
152    // @@ begin test lint list maintained by maint/add_warning @@
153    #![allow(clippy::bool_assert_comparison)]
154    #![allow(clippy::clone_on_copy)]
155    #![allow(clippy::dbg_macro)]
156    #![allow(clippy::mixed_attributes_style)]
157    #![allow(clippy::print_stderr)]
158    #![allow(clippy::print_stdout)]
159    #![allow(clippy::single_char_pattern)]
160    #![allow(clippy::unwrap_used)]
161    #![allow(clippy::unchecked_duration_subtraction)]
162    #![allow(clippy::useless_vec)]
163    #![allow(clippy::needless_pass_by_value)]
164    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
165
166    use super::*;
167
168    #[test]
169    fn protocols_enforced() {
170        let pr = supported_protocols();
171
172        for recommendation in [
173            // Required in consensus as of 2024-04-02
174            "Cons=2 Desc=2 Link=4 Microdesc=2 Relay=2",
175            // Recommended in consensus as of 2024-04-02
176            "Cons=2 Desc=2 DirCache=2 HSDir=2 HSIntro=4 HSRend=2 Link=4-5 Microdesc=2 Relay=2",
177            // Required by c-tor main-branch authorities as of 2024-04-02
178            "Cons=2 Desc=2 FlowCtrl=1 Link=4 Microdesc=2 Relay=2",
179            // // Recommended by c-tor main-branch authorities as of 2024-04-02
180            // TODO: (Cannot deploy yet, see below.)
181            // "Cons=2 Desc=2 DirCache=2 FlowCtrl=1-2 HSDir=2 HSIntro=4 HSRend=2 Link=4-5 Microdesc=2 Relay=2-4",
182        ] {
183            let rec: tor_protover::Protocols = recommendation.parse().unwrap();
184
185            let unsupported = rec.difference(&pr);
186
187            assert!(unsupported.is_empty(), "{} not supported", unsupported);
188        }
189
190        // TODO: Remove this once congestion control is fully implemented.
191        {
192            // Recommended by c-tor main-branch authorities as of 2024-04-02
193            let rec: tor_protover::Protocols =
194                "Cons=2 Desc=2 DirCache=2 FlowCtrl=1-2 HSDir=2 HSIntro=4 \
195                 HSRend=2 Link=4-5 Microdesc=2 Relay=2-4"
196                    .parse()
197                    .unwrap();
198
199            // Although this is recommended, Arti hasn't built it yet.
200            let expected_missing: tor_protover::Protocols =
201                [tor_protover::named::FLOWCTRL_CC].into_iter().collect();
202
203            let unsupported = rec.difference(&pr);
204            assert_eq!(unsupported, expected_missing);
205        }
206    }
207
208    #[test]
209    fn release_date_format() {
210        // Make sure we can parse the release date.
211        let _d: std::time::SystemTime = software_release_date();
212    }
213}