@@ -2,6 +2,7 @@ use arti_client::TorClient;
22use libp2p:: core:: multiaddr:: Protocol ;
33use libp2p:: core:: transport:: { ListenerId , TransportEvent } ;
44use libp2p:: { Multiaddr , Transport , TransportError } ;
5+ use std:: fs;
56use std:: future:: Future ;
67use std:: net:: SocketAddr ;
78use std:: pin:: Pin ;
@@ -160,3 +161,46 @@ impl std::fmt::Debug for TorBackend {
160161 } )
161162 }
162163}
164+
165+ #[ derive( Copy , Clone , Debug ) ]
166+ pub enum SpecialTorEnvironment {
167+ /// Torsocksed userland, Tor control and SOCKS5 in `$TOR_...`, well-known SOCKS5 at `127.0.0.1:9050`
168+ ///
169+ /// The `$TOR_...` configuration uses unix-domain sockets which we'd have to wrap ourselves
170+ ///
171+ /// We can't support a hypothetical `TorsocksTransport` that'd substitute
172+ /// /onion3/cebulka7uxchnbpvmqapg5pfos4ngaxglsktzvha7a5rigndghvadeyd:13
173+ /// with
174+ /// /dns/cebulka7uxchnbpvmqapg5pfos4ngaxglsktzvha7a5rigndghvadeyd.onion/tcp/13
175+ /// then forward to TcpTransport,
176+ /// because [hickory-resolver refuses to resolve `.onion` addresses](https://github.com/hickory-dns/hickory-dns/issues/3331).
177+ Whonix ,
178+ /// Well-known SOCKS5 at `127.0.0.1:9050`, cf. `/usr/local/bin/curl`
179+ ///
180+ /// Userland pretends it's torsocksed but dialling actually doesn't work at all; *all* network traffic must go through SOCKS5.
181+ Tails ,
182+ }
183+
184+ pub static TOR_ENVIRONMENT : once_cell:: sync:: Lazy < Option < SpecialTorEnvironment > > =
185+ once_cell:: sync:: Lazy :: new ( || {
186+ if fs:: exists ( "/usr/share/whonix/marker" ) . unwrap_or ( false ) {
187+ Some ( SpecialTorEnvironment :: Whonix )
188+ } else if fs:: read_to_string ( "/etc/os-release" )
189+ . unwrap_or ( String :: new ( ) )
190+ . contains ( r#"ID="tails""# )
191+ {
192+ Some ( SpecialTorEnvironment :: Tails )
193+ } else {
194+ None
195+ }
196+ } ) ;
197+
198+ impl SpecialTorEnvironment {
199+ pub fn backend ( self ) -> TorBackend {
200+ match self {
201+ Self :: Whonix | Self :: Tails => TorBackend :: Socks ( SocksServerAddress (
202+ ( std:: net:: Ipv4Addr :: LOCALHOST , 9050 ) . into ( ) ,
203+ ) ) ,
204+ }
205+ }
206+ }
0 commit comments