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
//! Ratrod
//!
//! A TCP tunneler that uses public / private key authentication with encryption.
//! Basically, it's `ssh -L`. This is useful for tunneling through a machine that doesn't support SSH.
#![feature(coverage_attribute)]
#![feature(const_type_name)]
use anyhow::anyhow;
use clap::{Parser, Subcommand};
use ratrodlib::base::Void;
use ratrodlib::security::ensure_security_files;
use tracing::error;
#[coverage(off)]
#[tokio::main]
async fn main() {
let args = Args::parse();
let key_path = args.key_path;
let level = if args.verbose { tracing::Level::DEBUG } else { tracing::Level::INFO };
tracing_subscriber::fmt()
.with_ansi(true)
.with_level(true)
.with_file(false)
.with_target(false)
.with_thread_ids(false)
.with_thread_names(false)
.with_max_level(level)
.init();
let result = execute_command(key_path, args.command).await;
if let Err(err) = result {
error!("❌ {}", err);
std::process::exit(1);
}
}
async fn execute_command(key_path: Option<String>, command: Option<Command>) -> Void {
ensure_security_files(key_path.clone())?;
match command {
Some(Command::Serve { bind, remote_regex }) => {
ratrodlib::serve::Instance::prepare(key_path, remote_regex, bind)?.start().await?;
}
Some(Command::Connect {
server,
tunnel,
accept_all_hosts,
encrypt,
}) => {
ratrodlib::connect::Instance::prepare(key_path, server, &tunnel, accept_all_hosts, encrypt)?.start().await?;
}
None => {
return Err(anyhow!("No command specified."));
}
};
Ok(())
}
/// Tunnels a local port to a remote server, which then redirects
/// traffic to a specified remote host.
///
/// A TCP / UDP tunneler that uses public / private key authentication with encryption.
/// Basically, it's `ssh -L`. This is useful for tunneling through a machine that doesn't support SSH.
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
#[command(subcommand)]
command: Option<Command>,
/// Specifies the path to the key store (`key`, `key.pub`, `authorized_keys`, and `known_hosts`).
///
/// The default value is `$HOME/.ratrod`.
#[arg(short, long)]
key_path: Option<String>,
/// Flag that specifies verbose logging.
#[arg(short, long)]
verbose: bool,
}
#[derive(Subcommand, PartialEq, Debug)]
enum Command {
/// Start a server on this machine that listens for incoming
/// connections and forwards them to a remote server (as
/// specified by the client).
Serve {
/// Specifies the local `host:port` to bind to.
///
/// E.g., you may want to bind to `0.0.0.0:3000` to
/// listen for connections from other machines on any interface,
/// or `192.168.1.100:3000` to listen for connections from other
/// machines on a specific interface.
bind: String,
/// Specifies an optional regex restriction on the remote hostnames that can be connected to.
/// This is used to prevent clients from connecting to arbitrary through the server.
///
/// The regex is matched against the entire hostname, so `^` and `$` are not needed.
#[arg(short, long, default_value = ".*")]
remote_regex: String,
},
/// Connects to a server and forwards traffic from a local port to a remote `host:port`
/// "through" the server.
Connect {
/// Specifies the server's `host:port` to connect to.
///
/// This is the destination of the server, and is not
/// the "routing destination" of the traffic.
///
/// This would usually take the form of the server's address, e.g., `192.168.1.100:3000`
server: String,
/// Specifies the remote(s) (e.g., `client_port:host:remote_port`) that the client wishes the server to route
/// the traffic to.
///
/// This is the destination of the traffic, and is not
/// necessarily the same as the server's `host:port`.
///
/// This can also be reduced to `client_port:remote_port` if the client wishes to connect to the server's
/// own port. Or, if the client wishes to connect to the server's same port,
/// it can be reduced to `remote_port`.
///
/// Some examples:
///
/// - `3000:127.0.0.1:3000`: Requests to the client port 3000 route to `127.0.0.1:3000` on the server (
/// same as `3000:3000` or `3000`).
///
/// - `3000:127.0.0.1:80`: Requests to the client port 3000 route to `127.0.0.1:80` on the server (
/// same as `3000:80`).
///
/// - `3000:example.com:80`: Requests to the client port 3000 route to `example.com:80` on the server.
/// This is for use cases where the client can contact the server, but not the remote host, so the server
/// must act as a TCP proxy.
tunnel: Vec<String>,
/// Specifies that all server public keys should be accepted.
#[arg(short, long, default_value_t = false)]
accept_all_hosts: bool,
/// Specifies whether to encrypt the traffic between the client and server.
///
/// Both the client and server must specify this flag for it to take effect properly.
#[arg(short, long, default_value_t = false)]
encrypt: bool,
},
}
#[cfg(test)]
mod tests {
use std::time::Duration;
use super::*;
use pretty_assertions::assert_eq;
use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
net::{TcpListener, TcpStream, UdpSocket},
};
pub struct EchoServer;
impl EchoServer {
pub fn start(bind_address: String) {
let bind_address2 = bind_address.clone();
tokio::spawn(async move {
let listener = TcpListener::bind(bind_address).await.unwrap();
loop {
let (client, _) = listener.accept().await.unwrap();
tokio::spawn(async move {
let (mut read, mut write) = client.into_split();
let _ = tokio::io::copy(&mut read, &mut write).await;
});
}
});
tokio::spawn(async move {
let socket = UdpSocket::bind(bind_address2).await.unwrap();
loop {
let mut buffer = vec![0; 1024];
let (size, peer) = socket.recv_from(&mut buffer).await.unwrap();
socket.send_to(&buffer[..size], peer).await.unwrap();
}
});
}
}
async fn bootstrap_e2e(server_key_path: String, client_key_path: String, remote_regex: String, port: u16, should_encrypt: bool) -> [String; 2] {
let remote_address = format!("127.0.0.1:{}", port);
let server_address = format!("127.0.0.1:{}", port + 1);
let client_tunnels = [format!("{}:{}", port + 2, port), format!("{}:{}", port + 3, port)];
let client_addresses = [format!("127.0.0.1:{}", port + 2), format!("127.0.0.1:{}", port + 3)];
// Start a "remote" echo server.
EchoServer::start(remote_address);
// Start a "server".
tokio::spawn(ratrodlib::serve::Instance::prepare(server_key_path, remote_regex, server_address.clone()).unwrap().start());
// Start a "client".
tokio::spawn(
ratrodlib::connect::Instance::prepare(client_key_path, server_address, &client_tunnels, false, should_encrypt)
.unwrap()
.start(),
);
// Do a "healthcheck" to ensure that the server is up and running.
while TcpStream::connect(&client_addresses[0]).await.is_err() {
tokio::time::sleep(Duration::from_millis(10)).await;
}
client_addresses
}
#[test]
fn test_args() {
let args = Args::parse_from(["", "serve", "127.0.0.1:3000", "--remote-regex", ".*"]);
assert_eq!(
args.command,
Some(Command::Serve {
bind: "127.0.0.1:3000".to_string(),
remote_regex: ".*".to_string(),
})
);
let args = Args::parse_from(["", "connect", "127.0.0.1:3000", "3000:127.0.0.1:3000", "4000", "-e"]);
assert_eq!(
args.command,
Some(Command::Connect {
server: "127.0.0.1:3000".to_string(),
tunnel: vec!["3000:127.0.0.1:3000".to_string(), "4000".to_string()],
accept_all_hosts: false,
encrypt: true
})
);
}
#[tokio::test]
async fn test_e2e() {
let remote_regex = ".*".to_string();
let port = 3000;
let client_addresses = bootstrap_e2e("./test/server".to_owned(), "./test/client".to_owned(), remote_regex, port, false).await;
// Open a client connection.
let mut client = TcpStream::connect(&client_addresses[0]).await.unwrap();
// Send a message to the server.
let message = b"Hello, world!";
client.write_all(message).await.unwrap();
client.flush().await.unwrap();
// Read the message back from the server.
let mut buffer = vec![0; message.len()];
client.read_exact(&mut buffer).await.unwrap();
assert_eq!(buffer, message);
// Close the client connection.
client.shutdown().await.unwrap();
}
#[tokio::test]
async fn test_e2e_encrypt() {
let remote_regex = ".*".to_string();
let port = 3100;
let client_addresses = bootstrap_e2e("./test/server".to_owned(), "./test/client".to_owned(), remote_regex, port, true).await;
// Open a client connection.
let mut client = TcpStream::connect(&client_addresses[1]).await.unwrap();
// Send a message to the server.
let message = b"Hello, world!";
client.write_all(message).await.unwrap();
client.flush().await.unwrap();
// Read the message back from the server.
let mut buffer = vec![0; message.len()];
client.read_exact(&mut buffer).await.unwrap();
assert_eq!(buffer, message);
// Close the client connection.
client.shutdown().await.unwrap();
}
#[tokio::test]
async fn test_e2e_bad_client_key() {
let remote_regex = ".*".to_string();
let port = 3200;
let client_addresses = bootstrap_e2e("./test/server".to_owned(), "./test/bad".to_owned(), remote_regex, port, false).await;
// Open a client connection.
let mut client = TcpStream::connect(&client_addresses[0]).await.unwrap();
// Send a message to the server.
let message = b"Hello, world!";
client.write_all(message).await.unwrap();
client.flush().await.unwrap();
// Read the message back from the server.
let mut buffer = vec![0; message.len()];
let result = client.read_exact(&mut buffer).await;
// intermediary disconnected because the key is bad.
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "Connection reset by peer (os error 104)");
}
#[tokio::test]
async fn test_e2e_bad_server_key() {
let remote_regex = ".*".to_string();
let port = 3300;
let client_addresses = bootstrap_e2e("./test/bad".to_owned(), "./test/client".to_owned(), remote_regex, port, false).await;
// Open a client connection.
let mut client = TcpStream::connect(&client_addresses[0]).await.unwrap();
// Send a message to the server.
let message = b"Hello, world!";
client.write_all(message).await.unwrap();
client.flush().await.unwrap();
// Read the message back from the server.
let mut buffer = vec![0; message.len()];
let result = client.read_exact(&mut buffer).await;
// intermediary disconnected because the key is bad.
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "Connection reset by peer (os error 104)");
}
#[tokio::test]
async fn test_e2e_bad_host() {
let remote_regex = "not_127.0.0.1".to_string();
let port = 3300;
let client_addresses = bootstrap_e2e("./test/server".to_owned(), "./test/client".to_owned(), remote_regex, port, false).await;
// Open a client connection.
let mut client = TcpStream::connect(&client_addresses[0]).await.unwrap();
// Send a message to the server.
let message = b"Hello, world!";
client.write_all(message).await.unwrap();
client.flush().await.unwrap();
// Read the message back from the server.
let mut buffer = vec![0; message.len()];
let result = client.read_exact(&mut buffer).await;
// intermediary disconnected because the host is bad.
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "Connection reset by peer (os error 104)");
}
#[tokio::test]
async fn test_e2e_udp() {
let remote_regex = ".*".to_string();
let port = 3400;
let client_addresses = bootstrap_e2e("./test/server".to_owned(), "./test/client".to_owned(), remote_regex, port, false).await;
// Open a client connection.
let client = UdpSocket::bind("0.0.0.0:0").await.unwrap();
client.connect(&client_addresses[0]).await.unwrap();
// Send a message to the server.
let message = b"Hello, world!";
client.send(message).await.unwrap();
// Read the message back from the server.
let mut buffer = vec![0; message.len()];
client.recv(&mut buffer).await.unwrap();
assert_eq!(buffer, message);
// Send another message to the server.
let message = b"bello, world!";
client.send(message).await.unwrap();
// Read the message back from the server.
let mut buffer = vec![0; message.len()];
client.recv(&mut buffer).await.unwrap();
assert_eq!(buffer, message);
}
#[tokio::test]
async fn test_e2e_udp_encrypted() {
let remote_regex = ".*".to_string();
let port = 3500;
let client_addresses = bootstrap_e2e("./test/server".to_owned(), "./test/client".to_owned(), remote_regex, port, true).await;
// Open a client connection.
let client = UdpSocket::bind("0.0.0.0:0").await.unwrap();
client.connect(&client_addresses[0]).await.unwrap();
// Send a message to the server.
let message = b"Hello, world!";
client.send(message).await.unwrap();
// Read the message back from the server.
let mut buffer = vec![0; message.len()];
client.recv(&mut buffer).await.unwrap();
assert_eq!(buffer, message);
// Send another message to the server.
let message = b"bello, world!";
client.send(message).await.unwrap();
// Read the message back from the server.
let mut buffer = vec![0; message.len()];
client.recv(&mut buffer).await.unwrap();
assert_eq!(buffer, message);
}
}