arti/
onion_proxy.rs

1//! Configure and implement onion service reverse-proxy feature.
2
3use std::{
4    collections::{btree_map::Entry, BTreeMap, HashSet},
5    sync::{Arc, Mutex},
6};
7
8use arti_client::config::onion_service::{OnionServiceConfig, OnionServiceConfigBuilder};
9use futures::{task::SpawnExt, StreamExt as _};
10use tor_config::{
11    define_list_builder_helper, impl_standard_builder, ConfigBuildError, Flatten, Reconfigure,
12    ReconfigureError,
13};
14use tor_error::warn_report;
15use tor_hsrproxy::{config::ProxyConfigBuilder, OnionServiceReverseProxy, ProxyConfig};
16use tor_hsservice::{HsNickname, RunningOnionService};
17use tor_rtcompat::Runtime;
18use tracing::debug;
19
20/// Configuration for running an onion service from `arti`.
21///
22/// This onion service will forward incoming connections to one or more local
23/// ports, depending on its configuration.  If you need it to do something else
24/// with incoming connections, or if you need finer-grained control over its
25/// behavior, consider using
26/// [`TorClient::launch_onion_service`](arti_client::TorClient::launch_onion_service).
27#[derive(Clone, Debug, Eq, PartialEq)]
28pub struct OnionServiceProxyConfig {
29    /// Configuration for the onion service itself.
30    pub(crate) svc_cfg: OnionServiceConfig,
31    /// Configuration for the reverse proxy that handles incoming connections
32    /// from the onion service.
33    pub(crate) proxy_cfg: ProxyConfig,
34}
35
36/// Builder object to construct an [`OnionServiceProxyConfig`].
37//
38// We cannot easily use derive_builder on this builder type, since we want it to be a
39// "Flatten<>" internally.  Fortunately, it's easy enough to implement the
40// pieces that we need.
41#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, Default)]
42#[serde(transparent)]
43pub struct OnionServiceProxyConfigBuilder(Flatten<OnionServiceConfigBuilder, ProxyConfigBuilder>);
44
45impl OnionServiceProxyConfigBuilder {
46    /// Try to construct an [`OnionServiceProxyConfig`].
47    ///
48    /// Returns an error if any part of this builder is invalid.
49    pub fn build(&self) -> Result<OnionServiceProxyConfig, ConfigBuildError> {
50        let svc_cfg = self.0 .0.build()?;
51        let proxy_cfg = self.0 .1.build()?;
52        Ok(OnionServiceProxyConfig { svc_cfg, proxy_cfg })
53    }
54
55    /// Return a mutable reference to an onion-service configuration sub-builder.
56    pub fn service(&mut self) -> &mut OnionServiceConfigBuilder {
57        &mut self.0 .0
58    }
59
60    /// Return a mutable reference to a proxy configuration sub-builder.
61    pub fn proxy(&mut self) -> &mut ProxyConfigBuilder {
62        &mut self.0 .1
63    }
64}
65
66impl_standard_builder! { OnionServiceProxyConfig: !Default }
67
68/// Alias for a `BTreeMap` of [`OnionServiceProxyConfig`]; used to make [`derive_builder`] happy.
69#[cfg(feature = "onion-service-service")]
70pub(crate) type OnionServiceProxyConfigMap = BTreeMap<HsNickname, OnionServiceProxyConfig>;
71
72/// The serialized format of an [`OnionServiceProxyConfigMapBuilder`]:
73/// a map from [`HsNickname`] to [`OnionServiceConfigBuilder`].
74type ProxyBuilderMap = BTreeMap<HsNickname, OnionServiceProxyConfigBuilder>;
75
76// TODO: Someday we might want to have an API for a MapBuilder that is distinct
77// from that of a ListBuilder.  It would have to enforce that everything has a
78// key, and that keys are distinct.
79#[cfg(feature = "onion-service-service")]
80define_list_builder_helper! {
81    pub struct OnionServiceProxyConfigMapBuilder {
82        services: [OnionServiceProxyConfigBuilder],
83    }
84    built: OnionServiceProxyConfigMap = build_list(services)?;
85    default = vec![];
86    #[serde(try_from="ProxyBuilderMap", into="ProxyBuilderMap")]
87}
88
89/// Construct a [`OnionServiceProxyConfigMap`] from a `Vec` of [`OnionServiceProxyConfig`];
90/// enforce that [`HsNickname`]s are unique.
91fn build_list(
92    services: Vec<OnionServiceProxyConfig>,
93) -> Result<OnionServiceProxyConfigMap, ConfigBuildError> {
94    // It *is* reachable from OnionServiceProxyConfigMapBuilder::build(), since
95    // that builder's API uses push() to add OnionServiceProxyConfigBuilders to
96    // an internal _list_.  Alternatively, we might want to have a distinct
97    // MapBuilder type.
98
99    let mut map = BTreeMap::new();
100    for service in services {
101        if let Some(previous_value) = map.insert(service.svc_cfg.nickname().clone(), service) {
102            return Err(ConfigBuildError::Inconsistent {
103                fields: vec!["nickname".into()],
104                problem: format!(
105                    "Multiple onion services with the nickname {}",
106                    previous_value.svc_cfg.nickname()
107                ),
108            });
109        };
110    }
111    Ok(map)
112}
113
114impl TryFrom<ProxyBuilderMap> for OnionServiceProxyConfigMapBuilder {
115    type Error = ConfigBuildError;
116
117    fn try_from(value: ProxyBuilderMap) -> Result<Self, Self::Error> {
118        let mut list_builder = OnionServiceProxyConfigMapBuilder::default();
119        for (nickname, mut cfg) in value {
120            match cfg.0 .0.peek_nickname() {
121                Some(n) if n == &nickname => (),
122                None => (),
123                Some(other) => {
124                    return Err(ConfigBuildError::Inconsistent {
125                        fields: vec![nickname.to_string(), format!("{nickname}.{other}")],
126                        problem: "mismatched nicknames on onion service.".into(),
127                    });
128                }
129            }
130            cfg.0 .0.nickname(nickname);
131            list_builder.access().push(cfg);
132        }
133        Ok(list_builder)
134    }
135}
136
137impl From<OnionServiceProxyConfigMapBuilder> for ProxyBuilderMap {
138    /// Convert our Builder representation of a set of onion services into the
139    /// format that serde will serialize.
140    ///
141    /// Note: This is a potentially lossy conversion, since the serialized format
142    /// can't represent partially-built services without a nickname, or
143    /// a collection of services with duplicate nicknames.
144    fn from(value: OnionServiceProxyConfigMapBuilder) -> Self {
145        let mut map = BTreeMap::new();
146        for cfg in value.services.into_iter().flatten() {
147            let nickname = cfg.0 .0.peek_nickname().cloned().unwrap_or_else(|| {
148                "Unnamed"
149                    .to_string()
150                    .try_into()
151                    .expect("'Unnamed' was not a valid nickname")
152            });
153            map.insert(nickname, cfg);
154        }
155        map
156    }
157}
158
159/// A running onion service and an associated reverse proxy.
160///
161/// This is what a user configures when they add an onion service to their
162/// configuration.
163#[must_use = "a hidden service Proxy object will terminate the service when dropped"]
164struct Proxy {
165    /// The onion service.
166    ///
167    /// This is launched and running.
168    svc: Arc<RunningOnionService>,
169    /// The reverse proxy that accepts connections from the onion service.
170    ///
171    /// This is also launched and running.
172    proxy: Arc<OnionServiceReverseProxy>,
173}
174
175impl Proxy {
176    /// Create and launch a new onion service proxy, using a given `client`,
177    /// to handle connections according to `config`.
178    pub(crate) fn launch_new<R: Runtime>(
179        client: &arti_client::TorClient<R>,
180        config: OnionServiceProxyConfig,
181    ) -> anyhow::Result<Self> {
182        let OnionServiceProxyConfig { svc_cfg, proxy_cfg } = config;
183        let nickname = svc_cfg.nickname().clone();
184        let (svc, request_stream) = client.launch_onion_service(svc_cfg)?;
185        let proxy = OnionServiceReverseProxy::new(proxy_cfg);
186
187        {
188            let proxy = proxy.clone();
189            let runtime_clone = client.runtime().clone();
190            let nickname_clone = nickname.clone();
191            client.runtime().spawn(async move {
192                match proxy
193                    .handle_requests(runtime_clone, nickname.clone(), request_stream)
194                    .await
195                {
196                    Ok(()) => {
197                        debug!("Onion service {} exited cleanly.", nickname);
198                    }
199                    Err(e) => {
200                        warn_report!(e, "Onion service {} exited with an error", nickname);
201                    }
202                }
203            })?;
204
205            let mut status_stream = svc.status_events();
206            client.runtime().spawn(async move {
207                while let Some(status) = status_stream.next().await {
208                    debug!(
209                        nickname=%nickname_clone,
210                        status=?status.state(),
211                        problem=?status.current_problem(),
212                        "Onion service status change",
213                    );
214                }
215            })?;
216        }
217
218        Ok(Proxy { svc, proxy })
219    }
220
221    /// Reconfigure this proxy, using the new configuration `config` and the
222    /// rules in `how`.
223    fn reconfigure(
224        &mut self,
225        config: OnionServiceProxyConfig,
226        how: Reconfigure,
227    ) -> Result<(), ReconfigureError> {
228        if matches!(how, Reconfigure::AllOrNothing) {
229            self.reconfigure_inner(config.clone(), Reconfigure::CheckAllOrNothing)?;
230        }
231
232        self.reconfigure_inner(config, how)
233    }
234
235    /// Helper for `reconfigure`: Run `reconfigure` on each part of this `Proxy`.
236    fn reconfigure_inner(
237        &mut self,
238        config: OnionServiceProxyConfig,
239        how: Reconfigure,
240    ) -> Result<(), ReconfigureError> {
241        let OnionServiceProxyConfig { svc_cfg, proxy_cfg } = config;
242
243        self.svc.reconfigure(svc_cfg, how)?;
244        self.proxy.reconfigure(proxy_cfg, how)?;
245
246        Ok(())
247    }
248}
249
250/// A set of configured onion service proxies.
251#[must_use = "a hidden service ProxySet object will terminate the services when dropped"]
252pub(crate) struct ProxySet<R: Runtime> {
253    /// The arti_client that we use to launch proxies.
254    client: arti_client::TorClient<R>,
255    /// The proxies themselves, indexed by nickname.
256    proxies: Mutex<BTreeMap<HsNickname, Proxy>>,
257}
258
259impl<R: Runtime> ProxySet<R> {
260    /// Create and launch a set of onion service proxies.
261    pub(crate) fn launch_new(
262        client: &arti_client::TorClient<R>,
263        config_list: OnionServiceProxyConfigMap,
264    ) -> anyhow::Result<Self> {
265        let proxies: BTreeMap<_, _> = config_list
266            .into_iter()
267            .map(|(nickname, cfg)| Ok((nickname, Proxy::launch_new(client, cfg)?)))
268            .collect::<anyhow::Result<BTreeMap<_, _>>>()?;
269
270        Ok(Self {
271            client: client.clone(),
272            proxies: Mutex::new(proxies),
273        })
274    }
275
276    /// Try to reconfigure the set of onion proxies according to the
277    /// configuration in `new_config`.
278    ///
279    /// Launches or closes proxies as necessary.  Does not close existing
280    /// connections.
281    pub(crate) fn reconfigure(
282        &self,
283        new_config: OnionServiceProxyConfigMap,
284        // TODO: this should probably take `how: Reconfigure` and implement an all-or-nothing mode.
285        // See #1156.
286    ) -> Result<(), anyhow::Error> {
287        let mut proxy_map = self.proxies.lock().expect("lock poisoned");
288
289        // Set of the nicknames of defunct proxies.
290        let mut defunct_nicknames: HashSet<_> = proxy_map.keys().map(Clone::clone).collect();
291
292        for cfg in new_config.into_values() {
293            let nickname = cfg.svc_cfg.nickname().clone();
294            // This proxy is still configured, so remove it from the list of
295            // defunct proxies.
296            defunct_nicknames.remove(&nickname);
297
298            match proxy_map.entry(nickname) {
299                Entry::Occupied(mut existing_proxy) => {
300                    // We already have a proxy by this name, so we try to
301                    // reconfigure it.
302                    existing_proxy
303                        .get_mut()
304                        .reconfigure(cfg, Reconfigure::WarnOnFailures)?;
305                }
306                Entry::Vacant(ent) => {
307                    // We do not have a proxy by this name, so we try to launch
308                    // one.
309                    match Proxy::launch_new(&self.client, cfg) {
310                        Ok(new_proxy) => {
311                            ent.insert(new_proxy);
312                        }
313                        Err(err) => {
314                            warn_report!(err, "Unable to launch onion service {}", ent.key());
315                        }
316                    }
317                }
318            }
319        }
320
321        for nickname in defunct_nicknames {
322            // We no longer have any configuration for this proxy, so we remove
323            // it from our map.
324            let defunct_proxy = proxy_map
325                .remove(&nickname)
326                .expect("Somehow a proxy disappeared from the map");
327            // This "drop" should shut down the proxy.
328            drop(defunct_proxy);
329        }
330
331        Ok(())
332    }
333
334    /// Whether this `ProxySet` is empty.
335    pub(crate) fn is_empty(&self) -> bool {
336        self.proxies.lock().expect("lock poisoned").is_empty()
337    }
338}
339
340impl<R: Runtime> crate::reload_cfg::ReconfigurableModule for ProxySet<R> {
341    fn reconfigure(&self, new: &crate::ArtiCombinedConfig) -> anyhow::Result<()> {
342        ProxySet::reconfigure(self, new.0.onion_services.clone())?;
343        Ok(())
344    }
345}
346
347#[cfg(test)]
348mod tests {
349    // @@ begin test lint list maintained by maint/add_warning @@
350    #![allow(clippy::bool_assert_comparison)]
351    #![allow(clippy::clone_on_copy)]
352    #![allow(clippy::dbg_macro)]
353    #![allow(clippy::mixed_attributes_style)]
354    #![allow(clippy::print_stderr)]
355    #![allow(clippy::print_stdout)]
356    #![allow(clippy::single_char_pattern)]
357    #![allow(clippy::unwrap_used)]
358    #![allow(clippy::unchecked_duration_subtraction)]
359    #![allow(clippy::useless_vec)]
360    #![allow(clippy::needless_pass_by_value)]
361    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
362    use super::*;
363
364    use tor_config::ConfigBuildError;
365    use tor_hsservice::HsNickname;
366
367    /// Get an [`OnionServiceProxyConfig`] with its `svc_cfg` field having the nickname `nick`.
368    fn get_onion_service_proxy_config(nick: &HsNickname) -> OnionServiceProxyConfig {
369        let mut builder = OnionServiceProxyConfigBuilder::default();
370        builder.service().nickname(nick.clone());
371        builder.build().unwrap()
372    }
373
374    /// Test `super::build_list` with unique and duplicate [`HsNickname`]s.
375    #[test]
376    fn fn_build_list() {
377        let nick_1 = HsNickname::new("nick_1".to_string()).unwrap();
378        let nick_2 = HsNickname::new("nick_2".to_string()).unwrap();
379
380        let proxy_configs: Vec<OnionServiceProxyConfig> = [&nick_1, &nick_2]
381            .into_iter()
382            .map(get_onion_service_proxy_config)
383            .collect();
384        let actual = build_list(proxy_configs.clone()).unwrap();
385
386        let expected =
387            OnionServiceProxyConfigMap::from_iter([nick_1, nick_2].into_iter().zip(proxy_configs));
388
389        assert_eq!(actual, expected);
390
391        let nick = HsNickname::new("nick".to_string()).unwrap();
392        let proxy_configs_dup: Vec<OnionServiceProxyConfig> = [&nick, &nick]
393            .into_iter()
394            .map(get_onion_service_proxy_config)
395            .collect();
396        let actual = build_list(proxy_configs_dup).unwrap_err();
397        let ConfigBuildError::Inconsistent { fields, problem } = actual else {
398            panic!("Unexpected error from `build_list`: {actual:?}");
399        };
400
401        assert_eq!(fields, vec!["nickname".to_string()]);
402        assert_eq!(
403            problem,
404            format!("Multiple onion services with the nickname {nick}")
405        );
406    }
407}