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#![allow(clippy::cognitive_complexity)] // See arti#2556
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#![warn(clippy::or_fun_call)]
28#![deny(clippy::print_stderr)]
29#![deny(clippy::print_stdout)]
30#![warn(clippy::rc_buffer)]
31#![deny(clippy::ref_option_ref)]
32#![warn(clippy::semicolon_if_nothing_returned)]
33#![warn(clippy::trait_duplication_in_bounds)]
34#![deny(clippy::unchecked_time_subtraction)]
35#![deny(clippy::unnecessary_wraps)]
36#![warn(clippy::unseparated_literal_suffix)]
37#![deny(clippy::unwrap_used)]
38#![deny(clippy::mod_module_files)]
39#![allow(clippy::let_unit_value)] // This can reasonably be done for explicitness
40#![allow(clippy::uninlined_format_args)]
41#![allow(clippy::significant_drop_in_scrutinee)] // arti/-/merge_requests/588/#note_2812945
42#![allow(clippy::result_large_err)] // temporary workaround for arti#587
43#![allow(clippy::needless_raw_string_hashes)] // complained-about code is fine, often best
44#![allow(clippy::needless_lifetimes)] // See arti#1765
45#![allow(mismatched_lifetime_syntaxes)] // temporary workaround for arti#2060
46#![allow(clippy::collapsible_if)] // See arti#2342
47#![deny(clippy::unused_async)]
48#![deny(clippy::string_slice)] // See arti#2571
49#![allow(recursion_depth_exceeding_limit)] // arti#2715, rust/issues/159228
50//! <!-- @@ end lint list maintained by maint/add_warning @@ -->
51
52// TODO #1645 (either remove this, or decide to have it everywhere)
53#![cfg_attr(not(all(feature = "full", feature = "experimental")), allow(unused))]
54
55mod address;
56mod builder;
57mod client;
58mod protostatus;
59mod release_date;
60#[cfg(feature = "rpc")]
61pub mod rpc;
62mod util;
63
64pub mod config;
65pub mod status;
66
67pub use address::{DangerouslyIntoTorAddr, IntoTorAddr, TorAddr, TorAddrError};
68pub use builder::{MAX_LOCAL_RESOURCE_TIMEOUT, TorClientBuilder};
69pub use client::{BootstrapBehavior, DormantMode, InertTorClient, StreamPrefs, TorClient};
70pub use config::TorClientConfig;
71
72pub use tor_circmgr::IsolationToken;
73pub use tor_circmgr::isolation;
74pub use tor_error::{ErrorKind, HasKind};
75pub use tor_proto::client::stream::{DataReader, DataStream, DataWriter};
76
77mod err;
78pub use err::{Error, ErrorHint, HintableError};
79
80#[cfg(feature = "error_detail")]
81pub use err::ErrorDetail;
82
83/// Alias for the [`Result`] type corresponding to the high-level [`Error`].
84pub type Result<T> = std::result::Result<T, Error>;
85
86#[cfg(feature = "experimental-api")]
87pub use builder::DirProviderBuilder;
88
89#[cfg(all(feature = "onion-service-client", feature = "experimental-api"))]
90pub use {
91    tor_hscrypto::pk::{HsClientDescEncKey, HsId},
92    tor_keymgr::KeystoreSelector,
93};
94
95#[cfg(feature = "geoip")]
96pub use tor_geoip::CountryCode;
97
98/// Return a list of the protocols [supported](tor_protover::doc_supported) by this crate.
99///
100/// (This is a crate-private method so as not to expose tor_protover in our public API.)
101///
102/// *WARNING*: REMOVING ELEMENTS FROM THIS LIST CAN BE DANGEROUS!
103/// SEE [`tor_protover::doc_changing`]
104pub(crate) fn supported_protocols() -> tor_protover::Protocols {
105    let protocols = tor_proto::supported_client_protocols()
106        .union(&tor_netdoc::supported_protocols())
107        .union(&tor_dirmgr::supported_client_protocols());
108
109    // TODO: the behavior for here seems most questionable!
110    // We will warn if any hs protocol happens to be recommended and we do not support onion
111    // services.
112    // We will also fail to warn if any hs protocol is required, and we support it only as a client
113    // or only as a service.
114    // We ought to determine the right behavior here.
115    // See torspec#319 at https://gitlab.torproject.org/tpo/core/torspec/-/issues/319.
116    #[cfg(feature = "onion-service-service")]
117    let protocols = protocols.union(&tor_hsservice::supported_hsservice_protocols());
118    #[cfg(feature = "onion-service-client")]
119    let protocols = protocols.union(&tor_hsclient::supported_hsclient_protocols());
120
121    let hs_protocols = {
122        // As a temporary workaround (again see torspec#319) we are unconditionally adding the
123        // conditionally supported HSService protocols.
124        use tor_protover::named::*;
125        [
126            //
127            HSINTRO_V3,
128            HSINTRO_RATELIM,
129            HSREND_V3,
130            HSDIR_V3,
131        ]
132        .into_iter()
133        .collect()
134    };
135
136    protocols.union(&hs_protocols)
137}
138
139/// Return the approximate release date of this version of arti client.
140///
141/// See[`release_date::ARTI_CLIENT_RELEASE_DATE`] for rationale.
142pub(crate) fn software_release_date() -> std::time::SystemTime {
143    use time::OffsetDateTime;
144
145    let format = time::macros::format_description!("[year]-[month]-[day]");
146    let date = time::Date::parse(release_date::ARTI_CLIENT_RELEASE_DATE, &format)
147        .expect("Invalid hard-coded release date!?");
148    OffsetDateTime::new_utc(date, time::Time::MIDNIGHT).into()
149}
150
151#[cfg(test)]
152mod test {
153    // @@ begin test lint list maintained by maint/add_warning @@
154    #![allow(clippy::bool_assert_comparison)]
155    #![allow(clippy::clone_on_copy)]
156    #![allow(clippy::dbg_macro)]
157    #![allow(clippy::mixed_attributes_style)]
158    #![allow(clippy::print_stderr)]
159    #![allow(clippy::print_stdout)]
160    #![allow(clippy::single_char_pattern)]
161    #![allow(clippy::unwrap_used)]
162    #![allow(clippy::unchecked_time_subtraction)]
163    #![allow(clippy::useless_vec)]
164    #![allow(clippy::needless_pass_by_value)]
165    #![allow(clippy::string_slice)] // See arti#2571
166    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
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        {
193            // Recommended by c-tor main-branch authorities as of 2024-04-02
194            let rec: tor_protover::Protocols =
195                "Cons=2 Desc=2 DirCache=2 FlowCtrl=1-2 HSDir=2 HSIntro=4 \
196                 HSRend=2 Link=4-5 Microdesc=2 Relay=2-4"
197                    .parse()
198                    .unwrap();
199
200            // Here we list any recommended protocols that are not yet always-on.
201            let permitted_missing = tor_protover::Protocols::new();
202            let unsupported = rec.difference(&pr);
203            assert!(unsupported.difference(&permitted_missing).is_empty());
204        }
205    }
206
207    #[test]
208    fn release_date_format() {
209        // Make sure we can parse the release date.
210        let _d: std::time::SystemTime = software_release_date();
211    }
212}