cuprated/config/
tor.rs

1use std::{
2    net::{IpAddr, Ipv4Addr, SocketAddr, SocketAddrV4},
3    path::PathBuf,
4};
5
6use serde::{Deserialize, Serialize};
7
8use cuprate_helper::fs::CUPRATE_DATA_DIR;
9
10use crate::{config::macros::config_struct, tor::TorMode};
11
12config_struct! {
13    /// Arti config
14    #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
15    #[serde(deny_unknown_fields, default)]
16    #[allow(rustdoc::broken_intra_doc_links)]
17    pub struct ArtiConfig {
18        /// Path to the arti state directory.
19        ///
20        /// The default directories for each OS:
21        ///
22        /// | OS      | Path                                                |
23        /// |---------|-----------------------------------------------------|
24        /// | Windows | "C:\Users\Alice\AppData\Roaming\Cuprate\"           |
25        /// | macOS   | "/Users/Alice/Library/Application Support/Cuprate/" |
26        /// | Linux   | "/home/alice/.local/share/cuprate/"                 |
27        pub directory_path: PathBuf,
28
29        /// Enable isolated circuits for Arti.
30        ///
31        /// If set, Arti will use different tor circuits for each connections. This can
32        /// cause stability issues if the connection count is important.
33        ///
34        /// Type         | boolean
35        /// Valid values | false, true
36        /// Examples     | false
37        pub isolated_circuit: bool,
38
39        /// Enable PoW security for Arti.
40        ///
41        /// If set, Arti will enforce an EquiX PoW to be resolved for
42        /// other nodes to complete a rendez-vous request when under
43        /// heavy load.
44        ///
45        /// Type         | boolean
46        /// Valid values | false, true
47        /// Examples     | false
48        pub onion_service_pow: bool,
49    }
50
51    /// Tor config
52    #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
53    #[serde(deny_unknown_fields, default)]
54    #[allow(rustdoc::broken_intra_doc_links)]
55    pub struct TorDaemonConfig {
56        /// The IP address and port of the external Tor daemon to use for outgoing connections.
57        ///
58        /// Type     | Socket address
59        /// Examples | "[::1]:9050", "127.0.0.1:9050"
60        pub address: SocketAddr,
61
62        #[comment_out = true]
63        /// Enable inbound connections for Daemon mode
64        ///
65        /// This string specify the onion address that should be advertized to the Tor network
66        /// and that your daemon should be expecting connections from.
67        ///
68        /// When this is set, `p2p.tor_net.p2p_port` is not used for host listening, but as the source
69        /// port of your hidden service in your torrc configuration file. For setting Cuprate's
70        /// listening port see `tor.listening_addr` field
71        ///
72        /// Type         | String
73        /// Valid values | "<56 character domain>.onion"
74        /// Examples     | "monerotoruzizulg5ttgat2emf4d6fbmiea25detrmmy7erypseyteyd.onion"
75        pub anonymous_inbound: String,
76
77        /// The IP address and port to bind and listen on for anonymous inbound connections from Tor Daemon.
78        ///
79        /// Type     | Socket address
80        /// Examples | "0.0.0.0:18083", "192.168.1.50:2000", "[::]:5000", "[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:18082"
81        pub listening_addr: SocketAddr,
82    }
83
84    /// Tor config
85    #[derive(Debug, Default, Clone, Deserialize, Serialize, PartialEq, Eq)]
86    #[serde(deny_unknown_fields, default)]
87    #[allow(rustdoc::broken_intra_doc_links)]
88    pub struct TorConfig {
89
90        #[comment_out = true]
91        /// Enable Tor network by specifying how to connect to it.
92        ///
93        /// When "Daemon" is set, the Tor daemon address to use can be
94        /// specified in `tor.daemon_addr`.
95        ///
96        /// Type         | String
97        /// Valid values | "Arti", "Daemon", "Off"
98        /// Examples     | "Arti"
99        pub mode: TorMode,
100
101        #[child = true]
102        /// Arti config
103        ///
104        /// Only relevant if `tor.mode` is set to "Arti"
105        pub arti: ArtiConfig,
106
107        #[child = true]
108        /// Tor Daemon config
109        ///
110        /// Only relevant if `tor.mode` is set to "Daemon"
111        pub daemon: TorDaemonConfig,
112    }
113}
114
115impl Default for TorDaemonConfig {
116    fn default() -> Self {
117        Self {
118            address: "127.0.0.1:9050".parse().unwrap(),
119            anonymous_inbound: String::new(),
120            listening_addr: SocketAddrV4::new(Ipv4Addr::LOCALHOST, 18083).into(),
121        }
122    }
123}
124
125impl Default for ArtiConfig {
126    fn default() -> Self {
127        Self {
128            directory_path: CUPRATE_DATA_DIR.join("arti"),
129            isolated_circuit: false,
130            onion_service_pow: false,
131        }
132    }
133}