Skip to main content

arti_client/
lib.rs

1#![cfg_attr(docsrs, feature(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_time_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#![allow(mismatched_lifetime_syntaxes)] // temporary workaround for arti#2060
45#![allow(clippy::collapsible_if)] // See arti#2342
46#![deny(clippy::unused_async)]
47#![deny(clippy::string_slice)] // See arti#2571
48//! <!-- @@ end lint list maintained by maint/add_warning @@ -->
49
50// TODO #1645 (either remove this, or decide to have it everywhere)
51#![cfg_attr(not(all(feature = "full", feature = "experimental")), allow(unused))]
52
53mod address;
54mod builder;
55mod client;
56mod protostatus;
57mod release_date;
58#[cfg(feature = "rpc")]
59pub mod rpc;
60mod util;
61
62pub mod config;
63pub mod status;
64
65pub use address::{DangerouslyIntoTorAddr, IntoTorAddr, TorAddr, TorAddrError};
66pub use builder::{MAX_LOCAL_RESOURCE_TIMEOUT, TorClientBuilder};
67pub use client::{BootstrapBehavior, DormantMode, InertTorClient, StreamPrefs, TorClient};
68pub use config::TorClientConfig;
69
70pub use tor_circmgr::IsolationToken;
71pub use tor_circmgr::isolation;
72pub use tor_error::{ErrorKind, HasKind};
73pub use tor_proto::client::stream::{DataReader, DataStream, DataWriter};
74
75mod err;
76pub use err::{Error, ErrorHint, HintableError};
77
78#[cfg(feature = "error_detail")]
79pub use err::ErrorDetail;
80
81/// Alias for the [`Result`] type corresponding to the high-level [`Error`].
82pub type Result<T> = std::result::Result<T, Error>;
83
84#[cfg(feature = "experimental-api")]
85pub use builder::DirProviderBuilder;
86
87#[cfg(all(feature = "onion-service-client", feature = "experimental-api"))]
88pub use {
89    tor_hscrypto::pk::{HsClientDescEncKey, HsId},
90    tor_keymgr::KeystoreSelector,
91};
92
93#[cfg(feature = "geoip")]
94pub use tor_geoip::CountryCode;
95
96/// Return a list of the protocols [supported](tor_protover::doc_supported) by this crate.
97///
98/// (This is a crate-private method so as not to expose tor_protover in our public API.)
99///
100/// *WARNING*: REMOVING ELEMENTS FROM THIS LIST CAN BE DANGEROUS!
101/// SEE [`tor_protover::doc_changing`]
102pub(crate) fn supported_protocols() -> tor_protover::Protocols {
103    let protocols = tor_proto::supported_client_protocols()
104        .union(&tor_netdoc::supported_protocols())
105        .union(&tor_dirmgr::supported_client_protocols());
106
107    // TODO: the behavior for here seems most questionable!
108    // We will warn if any hs protocol happens to be recommended and we do not support onion
109    // services.
110    // We will also fail to warn if any hs protocol is required, and we support it only as a client
111    // or only as a service.
112    // We ought to determine the right behavior here.
113    // See torspec#319 at https://gitlab.torproject.org/tpo/core/torspec/-/issues/319.
114    #[cfg(feature = "onion-service-service")]
115    let protocols = protocols.union(&tor_hsservice::supported_hsservice_protocols());
116    #[cfg(feature = "onion-service-client")]
117    let protocols = protocols.union(&tor_hsclient::supported_hsclient_protocols());
118
119    let hs_protocols = {
120        // As a temporary workaround (again see torspec#319) we are unconditionally adding the
121        // conditionally supported HSService protocols.
122        use tor_protover::named::*;
123        [
124            //
125            HSINTRO_V3,
126            HSINTRO_RATELIM,
127            HSREND_V3,
128            HSDIR_V3,
129        ]
130        .into_iter()
131        .collect()
132    };
133
134    protocols.union(&hs_protocols)
135}
136
137/// Return the approximate release date of this version of arti client.
138///
139/// See[`release_date::ARTI_CLIENT_RELEASE_DATE`] for rationale.
140pub(crate) fn software_release_date() -> std::time::SystemTime {
141    use time::OffsetDateTime;
142
143    let format = time::macros::format_description!("[year]-[month]-[day]");
144    let date = time::Date::parse(release_date::ARTI_CLIENT_RELEASE_DATE, &format)
145        .expect("Invalid hard-coded release date!?");
146    OffsetDateTime::new_utc(date, time::Time::MIDNIGHT).into()
147}
148
149#[cfg(test)]
150mod test {
151    // @@ begin test lint list maintained by maint/add_warning @@
152    #![allow(clippy::bool_assert_comparison)]
153    #![allow(clippy::clone_on_copy)]
154    #![allow(clippy::dbg_macro)]
155    #![allow(clippy::mixed_attributes_style)]
156    #![allow(clippy::print_stderr)]
157    #![allow(clippy::print_stdout)]
158    #![allow(clippy::single_char_pattern)]
159    #![allow(clippy::unwrap_used)]
160    #![allow(clippy::unchecked_time_subtraction)]
161    #![allow(clippy::useless_vec)]
162    #![allow(clippy::needless_pass_by_value)]
163    #![allow(clippy::string_slice)] // See arti#2571
164    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
165
166    use cfg_if::cfg_if;
167
168    use super::*;
169
170    #[test]
171    fn protocols_enforced() {
172        let pr = supported_protocols();
173
174        for recommendation in [
175            // Required in consensus as of 2024-04-02
176            "Cons=2 Desc=2 Link=4 Microdesc=2 Relay=2",
177            // Recommended in consensus as of 2024-04-02
178            "Cons=2 Desc=2 DirCache=2 HSDir=2 HSIntro=4 HSRend=2 Link=4-5 Microdesc=2 Relay=2",
179            // Required by c-tor main-branch authorities as of 2024-04-02
180            "Cons=2 Desc=2 FlowCtrl=1 Link=4 Microdesc=2 Relay=2",
181            // // Recommended by c-tor main-branch authorities as of 2024-04-02
182            // TODO: (Cannot deploy yet, see below.)
183            // "Cons=2 Desc=2 DirCache=2 FlowCtrl=1-2 HSDir=2 HSIntro=4 HSRend=2 Link=4-5 Microdesc=2 Relay=2-4",
184        ] {
185            let rec: tor_protover::Protocols = recommendation.parse().unwrap();
186
187            let unsupported = rec.difference(&pr);
188
189            assert!(unsupported.is_empty(), "{} not supported", unsupported);
190        }
191
192        // TODO: Revise this once congestion control is fully implemented and always-on.
193        {
194            // Recommended by c-tor main-branch authorities as of 2024-04-02
195            let rec: tor_protover::Protocols =
196                "Cons=2 Desc=2 DirCache=2 FlowCtrl=1-2 HSDir=2 HSIntro=4 \
197                 HSRend=2 Link=4-5 Microdesc=2 Relay=2-4"
198                    .parse()
199                    .unwrap();
200
201            // Although this is recommended, it isn't always-on in Arti yet.
202            cfg_if! {
203                if #[cfg(feature="flowctl-cc")] {
204                     let permitted_missing: tor_protover::Protocols =
205                        [].into_iter().collect();
206                } else {
207                    let permitted_missing: tor_protover::Protocols =
208                        [tor_protover::named::FLOWCTRL_CC].into_iter().collect();
209                }
210            }
211            let unsupported = rec.difference(&pr);
212            assert!(unsupported.difference(&permitted_missing).is_empty());
213        }
214    }
215
216    #[test]
217    fn release_date_format() {
218        // Make sure we can parse the release date.
219        let _d: std::time::SystemTime = software_release_date();
220    }
221}