1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
use tor_circmgr::TargetPort;
use tor_dirmgr::DirMgrConfig;
use tor_proto::circuit::IpVersionPreference;
use tor_proto::stream::DataStream;
use tor_rtcompat::{Runtime, SleepProviderExt};
use std::sync::Arc;
use std::time::Duration;
use anyhow::{anyhow, Context, Result};
use log::info;
#[derive(Clone)]
pub struct TorClient<R: Runtime> {
runtime: R,
circmgr: Arc<tor_circmgr::CircMgr<R>>,
dirmgr: Arc<tor_dirmgr::DirMgr<R>>,
}
#[derive(Debug, Default, Clone)]
pub struct ConnectPrefs {
ip_ver_pref: IpVersionPreference,
}
impl ConnectPrefs {
pub fn new() -> Self {
Self::default()
}
pub fn ipv6_preferred(&mut self) -> &mut Self {
self.ip_ver_pref = IpVersionPreference::Ipv6Preferred;
self
}
pub fn ipv6_only(&mut self) -> &mut Self {
self.ip_ver_pref = IpVersionPreference::Ipv6Only;
self
}
pub fn ipv4_preferred(&mut self) -> &mut Self {
self.ip_ver_pref = IpVersionPreference::Ipv4Preferred;
self
}
pub fn ipv4_only(&mut self) -> &mut Self {
self.ip_ver_pref = IpVersionPreference::Ipv4Only;
self
}
fn begin_flags(&self) -> IpVersionPreference {
self.ip_ver_pref
}
fn wrap_target_port(&self, port: u16) -> TargetPort {
match self.ip_ver_pref {
IpVersionPreference::Ipv6Only => TargetPort::ipv6(port),
_ => TargetPort::ipv4(port),
}
}
}
impl<R: Runtime> TorClient<R> {
pub async fn bootstrap(runtime: R, dircfg: DirMgrConfig) -> Result<TorClient<R>> {
let chanmgr = Arc::new(tor_chanmgr::ChanMgr::new(runtime.clone()));
let circmgr = Arc::new(tor_circmgr::CircMgr::new(
runtime.clone(),
Arc::clone(&chanmgr),
));
let dirmgr = tor_dirmgr::DirMgr::bootstrap_from_config(
dircfg,
runtime.clone(),
Arc::clone(&circmgr),
)
.await?;
Ok(TorClient {
runtime,
circmgr,
dirmgr,
})
}
pub async fn connect(
&self,
addr: &str,
port: u16,
flags: Option<ConnectPrefs>,
) -> Result<DataStream> {
if addr.to_lowercase().ends_with(".onion") {
return Err(anyhow!("Rejecting .onion address as unsupported."));
}
let flags = flags.unwrap_or_default();
let exit_ports = [flags.wrap_target_port(port)];
let dir = self.dirmgr.netdir();
let circ = self
.circmgr
.get_or_launch_exit(dir.as_ref().into(), &exit_ports)
.await
.context("Unable to launch circuit")?;
info!("Got a circuit for {}:{}", addr, port);
drop(dir);
let stream_timeout = Duration::new(10, 0);
let stream_future = circ.begin_stream(addr, port, Some(flags.begin_flags()));
let stream = self
.runtime
.timeout(stream_timeout, stream_future)
.await??;
Ok(stream)
}
#[cfg(feature = "experimental-api")]
pub fn dirmgr(&self) -> Arc<tor_dirmgr::DirMgr<R>> {
Arc::clone(&self.dirmgr)
}
#[cfg(feature = "experimental-api")]
pub fn circmgr(&self) -> Arc<tor_circmgr::CircMgr<R>> {
Arc::clone(&self.circmgr)
}
}