Skip to main content

tor_dircommon/
authority.rs

1//! Information about directory authorities
2//!
3//! From a client's point of view, an authority's role is to sign the
4//! consensus directory.
5
6use std::net::SocketAddr;
7
8use derive_builder::Builder;
9use getset::Getters;
10use serde::{Deserialize, Serialize};
11use tor_config::{ConfigBuildError, define_list_builder_accessors, impl_standard_builder};
12use tor_llcrypto::pk::rsa::RsaIdentity;
13
14/// The contact information for all directory authorities this implementation is
15/// aware of.
16///
17/// This data structure makes use of proposal 330 in order to distinguish
18/// authorities by their responsibilities, hence why the fields are divided.
19#[derive(Debug, Clone, Builder, Eq, PartialEq, Getters)]
20#[builder(build_fn(error = "ConfigBuildError"))]
21#[builder(derive(Debug, Serialize, Deserialize))]
22pub struct AuthorityContacts {
23    /// The [`RsaIdentity`] keys that may be used to sign valid consensus documents.
24    #[builder(setter(custom), default = "default_v3idents()")]
25    #[getset(get = "pub")]
26    v3idents: Vec<RsaIdentity>,
27    /// The endpoints of authorities where upload of router descriptors and other
28    /// documents is possible.
29    ///
30    /// This section is primarily of interest for relays.
31    ///
32    /// The use of nested a [`Vec`] serves the purpose to assign multiple IPs to
33    /// a single logical authority, such as having an IPv4 and IPv6 address.
34    #[builder(setter(custom), default = "default_uploads()")]
35    #[getset(get = "pub")]
36    uploads: Vec<Vec<SocketAddr>>,
37    #[builder(setter(custom), default = "default_downloads()")]
38    /// The endpoints of authorities where download of network documents is possible.
39    ///
40    /// This section is primarily of interest for directory mirrors.
41    ///
42    /// The use of nested a [`Vec`] serves the purpose to assign multiple IPs to
43    /// a single logical authority, such as having an IPv4 and IPv6 address.
44    #[getset(get = "pub")]
45    downloads: Vec<Vec<SocketAddr>>,
46    #[builder(setter(custom), default = "default_votes()")]
47    #[getset(get = "pub")]
48    /// The endpoints of authorities where voting for consensus documents is possible.
49    ///
50    /// This section is primarily of interest for other directory authorities.
51    ///
52    /// The use of nested a [`Vec`] serves the purpose to assign multiple IPs to
53    /// a single logical authority, such as having an IPv4 and IPv6 address.
54    votes: Vec<Vec<SocketAddr>>,
55}
56
57impl_standard_builder! { AuthorityContacts }
58
59define_list_builder_accessors! {
60    struct AuthorityContactsBuilder {
61        pub v3idents: [RsaIdentity],
62        pub uploads: [Vec<SocketAddr>],
63        pub downloads: [Vec<SocketAddr>],
64        pub votes: [Vec<SocketAddr>],
65    }
66}
67
68/// Returns a list of the default [`RsaIdentity`]s for the directory authorities.
69fn default_v3idents() -> Vec<RsaIdentity> {
70    fn rsa(hex: &str) -> RsaIdentity {
71        RsaIdentity::from_hex(hex).expect("invalid hex?!?")
72    }
73
74    vec![
75        rsa("27102BC123E7AF1D4741AE047E160C91ADC76B21"), // bastet
76        rsa("0232AF901C31A04EE9848595AF9BB7620D4C5B2E"), // dannenberg
77        rsa("E8A9C45EDE6D711294FADF8E7951F4DE6CA56B58"), // dizum
78        rsa("70849B868D606BAECFB6128C5E3D782029AA394F"), // faravahar
79        rsa("ED03BB616EB2F60BEC80151114BB25CEF515B226"), // gabelmoo
80        rsa("23D15D965BC35114467363C165C4F724B64B4F66"), // longclaw
81        rsa("49015F787433103580E3B66A1707A00E60F2D15B"), // maatuska
82        rsa("F533C81CEF0BC0267857C99B2F471ADF249FA232"), // moria1
83        rsa("2F3DF9CA0E5D36F2685A2DA67184EB8DCB8CBA8C"), // tor26
84    ]
85}
86
87/// Returns a list of the [`SocketAddr`] for the directory authorities.
88///
89/// The nested [`Vec`] serves dual-stack purposes
90/// (i.e. many IPs mapping to one logical authority).
91fn default_uploads() -> Vec<Vec<SocketAddr>> {
92    /// Converts a [`str`] to a [`SocketAddr`].
93    fn sa(s: &str) -> SocketAddr {
94        s.parse().expect("invalid socket address?!?")
95    }
96
97    vec![
98        // bastet
99        vec![
100            sa("204.13.164.118:80"),
101            sa("[2620:13:4000:6000::1000:118]:80"),
102        ],
103        // dannenberg
104        vec![sa("193.23.244.244:80"), sa("[2001:678:558:1000::244]:80")],
105        // dizum
106        vec![sa("45.66.35.11:80"), sa("[2a09:61c0::1337]:80")],
107        // faravahar
108        vec![sa("216.218.219.41:80"), sa("[2001:470:164:2::2]:80")],
109        // gabelmoo
110        vec![
111            sa("131.188.40.189:80"),
112            sa("[2001:638:a000:4140::ffff:189]:80"),
113        ],
114        // longclaw
115        vec![sa("199.58.81.140:80")],
116        // maatuska
117        vec![sa("171.25.193.9:443"), sa("[2001:67c:289c::9]:443")],
118        // moria1
119        vec![sa("128.31.0.39:9231")],
120        // tor26
121        vec![sa("217.196.147.77:80"), sa("[2a02:16a8:662:2203::1]:80")],
122    ]
123}
124
125/// For now, an alias to [`default_uploads()`].
126fn default_downloads() -> Vec<Vec<SocketAddr>> {
127    default_uploads()
128}
129
130/// For now, an alias to [`default_uploads()`].
131fn default_votes() -> Vec<Vec<SocketAddr>> {
132    default_uploads()
133}
134
135#[cfg(test)]
136mod test {
137    // @@ begin test lint list maintained by maint/add_warning @@
138    #![allow(clippy::bool_assert_comparison)]
139    #![allow(clippy::clone_on_copy)]
140    #![allow(clippy::dbg_macro)]
141    #![allow(clippy::mixed_attributes_style)]
142    #![allow(clippy::print_stderr)]
143    #![allow(clippy::print_stdout)]
144    #![allow(clippy::single_char_pattern)]
145    #![allow(clippy::unwrap_used)]
146    #![allow(clippy::unchecked_time_subtraction)]
147    #![allow(clippy::useless_vec)]
148    #![allow(clippy::needless_pass_by_value)]
149    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
150    #![allow(clippy::unnecessary_wraps)]
151
152    use super::*;
153
154    #[test]
155    fn default_auths() {
156        let dflt = AuthorityContacts::builder().build().unwrap();
157        assert_eq!(dflt.v3idents, default_v3idents());
158        assert_eq!(dflt.uploads, default_uploads());
159        assert_eq!(dflt.downloads, default_downloads());
160        assert_eq!(dflt.votes, default_votes());
161
162        assert_eq!(
163            dflt.v3idents[8],
164            RsaIdentity::from_hex("2F3DF9CA0E5D36F2685A2DA67184EB8DCB8CBA8C").unwrap()
165        );
166        assert_eq!(
167            dflt.uploads[8],
168            vec![
169                "217.196.147.77:80".parse().unwrap(),
170                "[2a02:16a8:662:2203::1]:80".parse().unwrap()
171            ]
172        );
173        assert_eq!(dflt.uploads[8], dflt.downloads[8]);
174        assert_eq!(dflt.uploads[8], dflt.votes[8]);
175    }
176}