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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
use crate::misc::{Message, Response};
use crate::AsyncEventKind;
use log::{debug, warn};
use std::collections::HashMap;
use std::io::{Error, ErrorKind};
use std::str::FromStr;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpStream;
use tokio::task::JoinHandle;
// Handling the async events, send and receive return value of commands
pub(crate) struct Connection {
sender: tokio::sync::mpsc::Sender<Message>,
event_handler: JoinHandle<()>,
}
impl Drop for Connection {
fn drop(&mut self) {
self.event_handler.abort();
}
}
impl Connection {
// Read until end of line (as specified in Tor protocol)
fn line_end(buffer: &[u8]) -> Option<usize> {
buffer
.windows(2)
.position(|w| w.eq(b"\r\n"))
.map(|end| end + 2)
}
// Read until end of multi-line (as specified in Tor protocol)
fn line_multi_end(buffer: &[u8]) -> Option<usize> {
buffer
.windows(5)
.position(|w| w.eq(b"\r\n.\r\n"))
.map(|end| end + 5)
}
// Split buffer at specific index and remove prefix and suffix as specified by byte len
fn split_off_string(buffer: &mut Vec<u8>, at: usize, prefix: usize, suffix: usize) -> String {
let mut new = buffer.split_off(at);
core::mem::swap(buffer, &mut new);
String::from_utf8_lossy(&new[prefix..at - suffix]).into_owned()
}
// parse buffer and split off a string if the information is valid
fn parse(buffer: &mut Vec<u8>) -> Option<(bool, u16, String)> {
// if buffer is smaller than 8, we wait for more information
// -> minimum reply message might have at least 8 bytes:
// 0-2: return code
// 3: space
// 4-5: 'OK'
// 6-7: '\r\n'
if buffer.len() < 8 {
return None;
}
// read the 3 digit response code
let raw = match std::str::from_utf8(&buffer[..3]) {
Ok(raw) => raw,
Err(err) => {
// if we already have a line in the buffer - we remove it
// otherwise we keep it and might remove it the next iteration
if let Some(end) = Self::line_end(buffer) {
let line = Self::split_off_string(buffer, end, 3, 2);
warn!(
"Could not parse valid utf8 from response code (raw: {:?}): {err} [{line}]",
&buffer[..3]
);
}
return None;
}
};
let response_code = match u16::from_str(raw) {
Ok(response) => response,
Err(err) => {
// if we already have a line in the buffer - we remove it
// otherwise we keep it and might remove it the next iteration
if let Some(end) = Self::line_end(buffer) {
let line = Self::split_off_string(buffer, end, 3, 2);
warn!(
"Could not parse valid response code (raw: {:?}): {err} [{line}]",
&buffer[..3]
);
}
return None;
}
};
// get next character to determine further process
match buffer[3] {
b' ' => {
// ' ': last line - read until newline
let line = if let Some(end) = Self::line_end(buffer) {
Self::split_off_string(buffer, end, 4, 2)
} else {
// no line end found - wait for more data
debug!("no end of end-line found yet - continue to read");
return None;
};
Some((true, response_code, line))
}
b'-' => {
// '-': one of multiple - read line and continue
let line = if let Some(end) = Self::line_end(buffer) {
Self::split_off_string(buffer, end, 4, 2)
} else {
// no line end found - wait for more data
debug!("no end of mid-line found yet - continue to read");
return None;
};
Some((false, response_code, line))
}
b'+' => {
// multiline mode
let multiline = if let Some(end) = Self::line_multi_end(buffer) {
Self::split_off_string(buffer, end, 4, 5)
} else {
// no multi-line end found - wait for more data
debug!("no end of data-line found yet - continue to read");
return None;
};
Some((true, response_code, multiline))
}
mode => {
if let Some(end) = Self::line_multi_end(buffer) {
let line = Self::split_off_string(buffer, end, 3, 2);
warn!("invalid code: {mode}: forget line: {line}");
}
None
}
}
}
// Parse incoming messages from the buffer
fn parse_message(buffer: &mut Vec<u8>, lines: &mut Vec<String>) -> Option<Response> {
// while we get valid lines from buffer
while let Some((complete, code, line)) = Self::parse(buffer) {
// put data into vec
lines.push(line);
if !complete {
// this was part of a multi-line message, there is still data missing
continue;
};
// swap buffer
let mut data = Vec::new();
core::mem::swap(lines, &mut data);
return Some(Response { code, data });
}
None
}
// Create the control command to set all currently active async event kinds
fn active_async_event_kinds(
handlers: &HashMap<AsyncEventKind, Vec<tokio::sync::mpsc::Sender<Response>>>,
) -> Vec<u8> {
let event_kinds: Vec<&AsyncEventKind> = handlers.keys().collect();
let mut req = String::from("SETEVENTS");
for e in event_kinds {
req.push_str(&format!(" {e}"));
}
req.push_str("\r\n");
req.as_bytes().to_owned()
}
}
impl Connection {
// The event loop that is handling async events as well as responses of commands sent
async fn event_loop(
mut stream: TcpStream,
mut command_receiver: tokio::sync::mpsc::Receiver<Message>,
) {
let mut lines = Vec::new();
let mut buffer = Vec::new();
let mut handlers = HashMap::new();
loop {
// handle either command or receive from stream
tokio::select! {
// receive command
message = command_receiver.recv() => {
// write a command to the control stream
let sender = Self::handle_command(&mut stream, message.expect("Cannot receive commands anymore: channel closed"), &mut handlers).await;
// parse messages until we get sync message
let message = Self::parse_until_sync_message(&mut buffer, &mut lines, &mut handlers, &mut stream).await;
// return message
sender
.send(message)
.await
.expect("Could not return sync response message");
}
// receive events
_len = Self::read_to_buffer(&mut stream, &mut buffer) => {
while let Some(response) = Self::parse_message(&mut buffer,&mut lines) {
// this should always be async messages
if response.code == 650 {
// handle async messages
if Self::send_async_messages(&mut handlers, response).await {
// clean up event subscriptions
let cmd = Self::active_async_event_kinds(&handlers);
// send message to controller
stream
.write_all(&cmd)
.await
.expect("Cannot write to controller");
// parse messages until we get sync message
let response: Response = Self::parse_until_sync_message(&mut buffer, &mut lines, &mut handlers, &mut stream)
.await
.try_into()
.expect("Could not convert into Response");
if response.code != 250 {
warn!("Could not adjust async event registrations: {response:?}");
}
}
}else{
// got a sync message
warn!("Received a sync message but did not expect one: {response:?}");
}
}
}
}
}
}
// Parse all messages until we get a sync message
// Because async messages can arrive at any time, we have to handle them as well
async fn parse_until_sync_message(
buffer: &mut Vec<u8>,
lines: &mut Vec<String>,
handlers: &mut HashMap<AsyncEventKind, Vec<tokio::sync::mpsc::Sender<Response>>>,
stream: &mut TcpStream,
) -> Message {
// parse messages until we get sync message
loop {
let mut message = None;
while let Some(response) = Self::parse_message(buffer, lines) {
// check if we got a async message
if response.code == 650 {
// ignore clean ups here - if events are come in regulary we can
// handle them in the async section
let _ = Self::send_async_messages(handlers, response).await;
} else {
// got sync message
message = Some(Message::Response(response.code, response.data));
}
}
// after finishing all messages form the buffer check if we are done
if let Some(message) = message {
return message;
}
// there is no data left in the buffer and we did not receive sync message yet
// read into buffer
let _ = Self::read_to_buffer(stream, buffer).await;
}
}
// Send async messages to the registered event listeners
// This will return true if its needed to re-register the current event listeners
async fn send_async_messages(
handlers: &mut HashMap<AsyncEventKind, Vec<tokio::sync::mpsc::Sender<Response>>>,
mut response: Response,
) -> bool {
// try to get async-event-kind
let kind = response.data.first_mut().and_then(|line| {
line.find(' ').map(|idx| {
let raw_kind = line.drain(..=idx).collect::<String>();
AsyncEventKind::from(raw_kind.trim_end())
})
});
if let Some(kind) = kind {
// get listeners
let remove = if let Some(senders) = handlers.get_mut(&kind) {
// this should be fine as we checked previously that data is Ok
let mut failed = Vec::new();
// skip first to avoid clone
for (idx, sender) in senders.iter_mut().enumerate().skip(1) {
if let Err(err) = sender.send(response.clone()).await {
debug!("Handler error: {err}");
failed.push(idx);
}
}
// send it to first listener
if let Some(sender) = senders.iter_mut().next() {
if let Err(err) = sender.send(response).await {
debug!("Handler error: {err}");
failed.push(0);
}
}
// clean closed handlers
while let Some(idx) = failed.pop() {
debug!("Remove handler {kind}:{idx}");
let _ = senders.remove(idx);
}
// check if we need to refresh our event subscriptions
senders.is_empty()
} else {
// got an event where we do not have a subscription - need to reset events
warn!("Got unhandled event");
true
};
// clean up
if remove {
let _ = handlers.remove(&kind);
return true;
}
} else {
warn!("Cannot parse async-event-kind from {response:?}");
}
false
}
// Read new data to the buffer
// This might panic - but we cannot recover if the control connection is lost!
async fn read_to_buffer(stream: &mut TcpStream, buffer: &mut Vec<u8>) -> usize {
match stream.read_buf(buffer).await {
Ok(0) => panic!("Control connection closed - EOF"),
Ok(len) => len,
Err(err) => panic!("Control connection was closed: {err}"),
}
}
// Handle our internal commands we are supporting
async fn handle_command(
stream: &mut TcpStream,
message: Message,
handlers: &mut HashMap<AsyncEventKind, Vec<tokio::sync::mpsc::Sender<Response>>>,
) -> tokio::sync::mpsc::Sender<Message> {
match message {
Message::Authenticate(cmd, sender)
| Message::Raw(cmd, sender)
| Message::AddOnionService(cmd, sender)
| Message::DeleteOnionService(cmd, sender) => {
// send message to controller
stream
.write_all(&cmd)
.await
.expect("Cannot write to controller");
sender
}
Message::AddEventHandler(kind, sender, event_handler) => {
// add event handler
match handlers.get_mut(&kind) {
Some(senders) => senders.push(event_handler),
None => {
handlers.insert(kind, vec![event_handler]);
}
}
// register events
let cmd = Self::active_async_event_kinds(handlers);
// send message to controller
stream
.write_all(&cmd)
.await
.expect("Cannot write to controller");
sender
}
msg => panic!("Got invalid command message: {msg:?}"),
}
}
// Send a command to the Tor controller
pub(crate) async fn send(&mut self, command: Message) -> Result<(), Error> {
self.sender
.send(command)
.await
.map_err(|err| Error::new(ErrorKind::Other, err))
}
// Set a new event handler
pub(crate) async fn add_event_handler(
&mut self,
event_kind: AsyncEventKind,
response_handler: tokio::sync::mpsc::Sender<Message>,
event_handler: tokio::sync::mpsc::Sender<Response>,
) -> Result<(), Error> {
self.send(Message::AddEventHandler(
event_kind,
response_handler,
event_handler,
))
.await
}
}
impl Connection {
// Create a new connection to the Tor control socket
pub(crate) async fn new(port: u16) -> Result<Connection, Error> {
let stream = TcpStream::connect(&format!("127.0.0.1:{port}")).await?;
// create command loop channels
let (sender, receiver) = tokio::sync::mpsc::channel(1024);
// spawn event-handler
let event_handler = tokio::spawn(Connection::event_loop(stream, receiver));
Ok(Self {
sender,
event_handler,
})
}
// Authenticate with the simplest authentication command
pub(crate) async fn authenticate(&mut self) -> Result<(), Error> {
let (sender, mut receiver) = tokio::sync::mpsc::channel(1024);
self.send(Message::Authenticate(b"AUTHENTICATE\r\n".to_vec(), sender))
.await?;
let response: Response = match receiver.recv().await {
Some(result) => result
.try_into()
.map_err(|err| Error::new(ErrorKind::Other, err)),
None => Err(Error::new(ErrorKind::Other, "Event-channel was closed")),
}?;
if response.code != 250 {
return Err(Error::new(
ErrorKind::Unsupported,
format!(
"Invalid response code {}: {:?}",
response.code, response.data
),
));
}
Ok(())
}
}