tor_linkspec/
verbatim.rs

1//! Wrappers for using a CircTarget with a verbatim list of
2//! [link specifiers](EncodedLinkSpec).
3
4use crate::{ChanTarget, CircTarget, EncodedLinkSpec, HasAddrs, HasChanMethod, HasRelayIds};
5
6/// A wrapper around an underlying [`CircTarget`] that provides a user-specified
7/// list of [link specifiers](EncodedLinkSpec).
8///
9/// Onion services and their clients use this type of target when telling a
10/// relay to extend a circuit to a target relay (an introduction point or
11/// rendezvous point) chosen by some other party.
12#[derive(Clone, Debug)]
13pub struct VerbatimLinkSpecCircTarget<T> {
14    /// The underlying CircTarget
15    target: T,
16    /// The link specifiers to provide.
17    linkspecs: Vec<EncodedLinkSpec>,
18}
19
20impl<T> VerbatimLinkSpecCircTarget<T> {
21    /// Construct a new `VerbatimLinkSpecCircTarget` to wrap an underlying
22    /// `CircTarget` object, and provide it with a new set of encoded link
23    /// specifiers that will be used when telling a relay to extend to this
24    /// node.
25    ///
26    /// Note that nothing here will check that `linkspecs` is sufficient to
27    /// actually connect to the chosen target, or to any target at all. It is
28    /// the caller's responsibility to choose a valid set of link specifiers.
29    pub fn new(target: T, linkspecs: Vec<EncodedLinkSpec>) -> Self {
30        Self { target, linkspecs }
31    }
32}
33
34// Now, the delegation functions.  All of these are simple delegations to
35// self.target, except for `CircTarget::linkspecs` with returns self.linkspecs.
36
37impl<T: HasRelayIds> HasRelayIds for VerbatimLinkSpecCircTarget<T> {
38    fn identity(&self, key_type: crate::RelayIdType) -> Option<crate::RelayIdRef<'_>> {
39        self.target.identity(key_type)
40    }
41}
42impl<T: HasAddrs> HasAddrs for VerbatimLinkSpecCircTarget<T> {
43    fn addrs(&self) -> impl Iterator<Item = std::net::SocketAddr> {
44        self.target.addrs()
45    }
46}
47impl<T: HasChanMethod> HasChanMethod for VerbatimLinkSpecCircTarget<T> {
48    fn chan_method(&self) -> crate::ChannelMethod {
49        self.target.chan_method()
50    }
51}
52impl<T: ChanTarget> ChanTarget for VerbatimLinkSpecCircTarget<T> {}
53impl<T: CircTarget> CircTarget for VerbatimLinkSpecCircTarget<T> {
54    fn linkspecs(&self) -> tor_bytes::EncodeResult<Vec<EncodedLinkSpec>> {
55        Ok(self.linkspecs.clone())
56    }
57
58    fn ntor_onion_key(&self) -> &tor_llcrypto::pk::curve25519::PublicKey {
59        self.target.ntor_onion_key()
60    }
61
62    fn protovers(&self) -> &tor_protover::Protocols {
63        self.target.protovers()
64    }
65}
66
67#[cfg(test)]
68mod test {
69    // @@ begin test lint list maintained by maint/add_warning @@
70    #![allow(clippy::bool_assert_comparison)]
71    #![allow(clippy::clone_on_copy)]
72    #![allow(clippy::dbg_macro)]
73    #![allow(clippy::mixed_attributes_style)]
74    #![allow(clippy::print_stderr)]
75    #![allow(clippy::print_stdout)]
76    #![allow(clippy::single_char_pattern)]
77    #![allow(clippy::unwrap_used)]
78    #![allow(clippy::unchecked_duration_subtraction)]
79    #![allow(clippy::useless_vec)]
80    #![allow(clippy::needless_pass_by_value)]
81    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
82
83    use super::*;
84    use crate::OwnedCircTarget;
85    use itertools::Itertools;
86
87    #[test]
88    fn verbatim_linkspecs() {
89        let mut builder = OwnedCircTarget::builder();
90        builder
91            .chan_target()
92            .addrs(vec!["127.0.0.1:11".parse().unwrap()])
93            .ed_identity([42; 32].into())
94            .rsa_identity([45; 20].into());
95        let inner = builder
96            .ntor_onion_key([99; 32].into())
97            .protocols("FlowCtrl=7".parse().unwrap())
98            .build()
99            .unwrap();
100        let weird_linkspecs = vec![EncodedLinkSpec::new(
101            77.into(),
102            b"mysterious whisper".to_vec(),
103        )];
104        let wrapped = VerbatimLinkSpecCircTarget::new(inner.clone(), weird_linkspecs.clone());
105
106        assert_eq!(wrapped.addrs().collect_vec(), inner.addrs().collect_vec());
107        assert!(wrapped.same_relay_ids(&inner));
108        assert_eq!(wrapped.ntor_onion_key(), inner.ntor_onion_key());
109        assert_eq!(wrapped.protovers(), inner.protovers());
110
111        assert_ne!(inner.linkspecs().unwrap(), weird_linkspecs);
112        assert_eq!(wrapped.linkspecs().unwrap(), weird_linkspecs);
113    }
114}