-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathmain.rs
More file actions
546 lines (511 loc) · 18.7 KB
/
main.rs
File metadata and controls
546 lines (511 loc) · 18.7 KB
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
//! Command line arguments.
use anyhow::Context;
use clap::{Parser, Subcommand};
use dumbpipe::NodeTicket;
use iroh_net::{
discovery::{dns::DnsDiscovery, pkarr_publish::PkarrPublisher, ConcurrentDiscovery, Discovery},
key::SecretKey,
magic_endpoint::get_remote_node_id,
MagicEndpoint, NodeAddr,
};
use std::{
io,
net::{SocketAddr, ToSocketAddrs},
str::FromStr,
};
use tokio::{
io::{AsyncRead, AsyncWrite, AsyncWriteExt},
select,
};
use tokio_util::sync::CancellationToken;
/// Create a dumb pipe between two machines, using an iroh magicsocket.
///
/// One side listens, the other side connects. Both sides are identified by a
/// 32 byte node id.
///
/// Connecting to a node id is independent of its IP address. Dumbpipe will try
/// to establish a direct connection even through NATs and firewalls. If that
/// fails, it will fall back to using a relay server.
///
/// For all subcommands, you can specify a secret key using the IROH_SECRET
/// environment variable. If you don't, a random one will be generated.
///
/// You can also specify a port for the magicsocket. If you don't, a random one
/// will be chosen.
#[derive(Parser, Debug)]
pub struct Args {
#[clap(subcommand)]
pub command: Commands,
}
#[derive(Subcommand, Debug)]
pub enum Commands {
/// Listen on a magicsocket and forward stdin/stdout to the first incoming
/// bidi stream.
///
/// Will print a node ticket on stderr that can be used to connect.
Listen(ListenArgs),
/// Listen on a magicsocket and forward incoming connections to the specified
/// host and port. Every incoming bidi stream is forwarded to a new connection.
///
/// Will print a node ticket on stderr that can be used to connect.
///
/// As far as the magic socket is concerned, this is listening. But it is
/// connecting to a TCP socket for which you have to specify the host and port.
ListenTcp(ListenTcpArgs),
/// Connect to a magicsocket, open a bidi stream, and forward stdin/stdout.
///
/// A node ticket is required to connect.
Connect(ConnectArgs),
/// Connect to a magicsocket, open a bidi stream, and forward stdin/stdout
/// to it.
///
/// A node ticket is required to connect.
///
/// As far as the magic socket is concerned, this is connecting. But it is
/// listening on a TCP socket for which you have to specify the interface and port.
ConnectTcp(ConnectTcpArgs),
}
#[derive(Parser, Debug)]
pub struct CommonArgs {
/// The port to use for the magicsocket. Random by default.
#[clap(long, default_value_t = 0)]
pub magic_port: u16,
/// A custom ALPN to use for the magicsocket.
///
/// This is an expert feature that allows dumbpipe to be used to interact
/// with existing iroh protocols.
///
/// When using this option, the connect side must also specify the same ALPN.
/// The listen side will not expect a handshake, and the connect side will
/// not send one.
///
/// Alpns are byte strings. To specify an utf8 string, prefix it with `utf8:`.
/// Otherwise, it will be parsed as a hex string.
#[clap(long)]
pub custom_alpn: Option<String>,
/// The verbosity level. Repeat to increase verbosity.
#[clap(short = 'v', long, action = clap::ArgAction::Count)]
pub verbose: u8,
}
impl CommonArgs {
fn alpn(&self) -> anyhow::Result<Vec<u8>> {
Ok(match &self.custom_alpn {
Some(alpn) => parse_alpn(alpn)?,
None => dumbpipe::ALPN.to_vec(),
})
}
fn is_custom_alpn(&self) -> bool {
self.custom_alpn.is_some()
}
}
fn parse_alpn(alpn: &str) -> anyhow::Result<Vec<u8>> {
Ok(if let Some(text) = alpn.strip_prefix("utf8:") {
text.as_bytes().to_vec()
} else {
hex::decode(alpn)?
})
}
#[derive(Parser, Debug)]
pub struct ListenArgs {
#[clap(flatten)]
pub common: CommonArgs,
}
#[derive(Parser, Debug)]
pub struct ListenTcpArgs {
#[clap(long)]
pub host: String,
#[clap(flatten)]
pub common: CommonArgs,
}
#[derive(Parser, Debug)]
pub struct ConnectTcpArgs {
/// The addresses to listen on for incoming tcp connections.
///
/// To listen on all network interfaces, use 0.0.0.0:12345
#[clap(long)]
pub addr: String,
/// The node to connect to
pub ticket: NodeTicket,
#[clap(flatten)]
pub common: CommonArgs,
}
#[derive(Parser, Debug)]
pub struct ConnectArgs {
/// The node to connect to
pub ticket: NodeTicket,
#[clap(flatten)]
pub common: CommonArgs,
}
/// Copy from a reader to a quinn stream.
///
/// Will send a reset to the other side if the operation is cancelled, and fail
/// with an error.
///
/// Returns the number of bytes copied in case of success.
async fn copy_to_quinn(
mut from: impl AsyncRead + Unpin,
mut send: quinn::SendStream,
token: CancellationToken,
) -> io::Result<u64> {
tracing::trace!("copying to quinn");
tokio::select! {
res = tokio::io::copy(&mut from, &mut send) => {
let size = res?;
send.finish().await?;
Ok(size)
}
_ = token.cancelled() => {
// send a reset to the other side immediately
send.reset(0u8.into()).ok();
Err(io::Error::new(io::ErrorKind::Other, "cancelled"))
}
}
}
/// Copy from a quinn stream to a writer.
///
/// Will send stop to the other side if the operation is cancelled, and fail
/// with an error.
///
/// Returns the number of bytes copied in case of success.
async fn copy_from_quinn(
mut recv: quinn::RecvStream,
mut to: impl AsyncWrite + Unpin,
token: CancellationToken,
) -> io::Result<u64> {
tokio::select! {
res = tokio::io::copy(&mut recv, &mut to) => {
Ok(res?)
},
_ = token.cancelled() => {
recv.stop(0u8.into()).ok();
Err(io::Error::new(io::ErrorKind::Other, "cancelled"))
}
}
}
/// Get the secret key or generate a new one.
///
/// Print the secret key to stderr if it was generated, so the user can save it.
fn get_or_create_secret() -> anyhow::Result<SecretKey> {
match std::env::var("IROH_SECRET") {
Ok(secret) => SecretKey::from_str(&secret).context("invalid secret"),
Err(_) => {
let key = SecretKey::generate();
eprintln!("using secret key {}", key);
Ok(key)
}
}
}
fn cancel_token<T>(token: CancellationToken) -> impl Fn(T) -> T {
move |x| {
token.cancel();
x
}
}
/// Bidirectionally forward data from a quinn stream and an arbitrary tokio
/// reader/writer pair, aborting both sides when either one forwarder is done,
/// or when control-c is pressed.
async fn forward_bidi(
from1: impl AsyncRead + Send + Sync + Unpin + 'static,
to1: impl AsyncWrite + Send + Sync + Unpin + 'static,
from2: quinn::RecvStream,
to2: quinn::SendStream,
) -> anyhow::Result<()> {
let token1 = CancellationToken::new();
let token2 = token1.clone();
let token3 = token1.clone();
let forward_from_stdin = tokio::spawn(async move {
copy_to_quinn(from1, to2, token1.clone())
.await
.map_err(cancel_token(token1))
});
let forward_to_stdout = tokio::spawn(async move {
copy_from_quinn(from2, to1, token2.clone())
.await
.map_err(cancel_token(token2))
});
let _control_c = tokio::spawn(async move {
tokio::signal::ctrl_c().await?;
token3.cancel();
io::Result::Ok(())
});
forward_to_stdout.await??;
forward_from_stdin.await??;
Ok(())
}
async fn listen_stdio(args: ListenArgs) -> anyhow::Result<()> {
let secret_key = get_or_create_secret()?;
let discovery = n0_discovery(secret_key.clone());
let endpoint = MagicEndpoint::builder()
.discovery(discovery)
.alpns(vec![args.common.alpn()?])
.secret_key(secret_key)
.bind(args.common.magic_port)
.await?;
// wait for the endpoint to figure out its address before making a ticket
while endpoint.my_relay().is_none() {
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
}
let node = endpoint.my_addr().await?;
let mut short = node.clone();
let ticket = NodeTicket::new(node)?;
short.info.direct_addresses.clear();
let short = NodeTicket::new(short)?;
// print the ticket on stderr so it doesn't interfere with the data itself
//
// note that the tests rely on the ticket being the last thing printed
eprintln!("Listening. To connect, use:\ndumbpipe connect {}", ticket);
if args.common.verbose > 0 {
eprintln!("or:\ndumbpipe connect {}", short);
}
loop {
let Some(connecting) = endpoint.accept().await else {
break;
};
let connection = match connecting.await {
Ok(connection) => connection,
Err(cause) => {
tracing::warn!("error accepting connection: {}", cause);
// if accept fails, we want to continue accepting connections
continue;
}
};
let remote_node_id = get_remote_node_id(&connection)?;
tracing::info!("got connection from {}", remote_node_id);
let (s, mut r) = match connection.accept_bi().await {
Ok(x) => x,
Err(cause) => {
tracing::warn!("error accepting stream: {}", cause);
// if accept_bi fails, we want to continue accepting connections
continue;
}
};
tracing::info!("accepted bidi stream from {}", remote_node_id);
if !args.common.is_custom_alpn() {
// read the handshake and verify it
let mut buf = [0u8; dumbpipe::HANDSHAKE.len()];
r.read_exact(&mut buf).await?;
anyhow::ensure!(buf == dumbpipe::HANDSHAKE, "invalid handshake");
}
tracing::info!("forwarding stdin/stdout to {}", remote_node_id);
forward_bidi(tokio::io::stdin(), tokio::io::stdout(), r, s).await?;
// stop accepting connections after the first successful one
break;
}
Ok(())
}
async fn connect_stdio(args: ConnectArgs) -> anyhow::Result<()> {
let secret_key = get_or_create_secret()?;
let discovery = Box::new(DnsDiscovery::n0_dns());
let endpoint = MagicEndpoint::builder()
.secret_key(secret_key)
.discovery(discovery)
.alpns(vec![])
.bind(args.common.magic_port)
.await?;
let addr = args.ticket.node_addr();
let remote_node_id = addr.node_id;
// connect to the node, try only once
let connection = endpoint.connect(addr.clone(), &args.common.alpn()?).await?;
tracing::info!("connected to {}", remote_node_id);
// open a bidi stream, try only once
let (mut s, r) = connection.open_bi().await?;
tracing::info!("opened bidi stream to {}", remote_node_id);
// send the handshake unless we are using a custom alpn
// when using a custom alpn, evertyhing is up to the user
if !args.common.is_custom_alpn() {
// the connecting side must write first. we don't know if there will be something
// on stdin, so just write a handshake.
s.write_all(&dumbpipe::HANDSHAKE).await?;
}
tracing::info!("forwarding stdin/stdout to {}", remote_node_id);
forward_bidi(tokio::io::stdin(), tokio::io::stdout(), r, s).await?;
tokio::io::stdout().flush().await?;
Ok(())
}
/// Listen on a tcp port and forward incoming connections to a magicsocket.
async fn connect_tcp(args: ConnectTcpArgs) -> anyhow::Result<()> {
let addrs = args
.addr
.to_socket_addrs()
.context(format!("invalid host string {}", args.addr))?;
let secret_key = get_or_create_secret()?;
let discovery = Box::new(DnsDiscovery::n0_dns());
let endpoint = MagicEndpoint::builder()
.alpns(vec![])
.secret_key(secret_key)
.discovery(discovery)
.bind(args.common.magic_port)
.await
.context("unable to bind magicsock")?;
tracing::info!("tcp listening on {:?}", addrs);
let tcp_listener = match tokio::net::TcpListener::bind(addrs.as_slice()).await {
Ok(tcp_listener) => tcp_listener,
Err(cause) => {
tracing::error!("error binding tcp socket to {:?}: {}", addrs, cause);
return Ok(());
}
};
async fn handle_tcp_accept(
next: io::Result<(tokio::net::TcpStream, SocketAddr)>,
addr: NodeAddr,
endpoint: MagicEndpoint,
handshake: bool,
alpn: &[u8],
) -> anyhow::Result<()> {
let (tcp_stream, tcp_addr) = next.context("error accepting tcp connection")?;
let (tcp_recv, tcp_send) = tcp_stream.into_split();
tracing::info!("got tcp connection from {}", tcp_addr);
let remote_node_id = addr.node_id;
let connection = endpoint
.connect(addr, alpn)
.await
.context(format!("error connecting to {}", remote_node_id))?;
let (mut magic_send, magic_recv) = connection
.open_bi()
.await
.context(format!("error opening bidi stream to {}", remote_node_id))?;
// send the handshake unless we are using a custom alpn
// when using a custom alpn, evertyhing is up to the user
if handshake {
// the connecting side must write first. we don't know if there will be something
// on stdin, so just write a handshake.
magic_send.write_all(&dumbpipe::HANDSHAKE).await?;
}
forward_bidi(tcp_recv, tcp_send, magic_recv, magic_send).await?;
anyhow::Ok(())
}
let addr = args.ticket.node_addr();
loop {
// also wait for ctrl-c here so we can use it before accepting a connection
let next = tokio::select! {
stream = tcp_listener.accept() => stream,
_ = tokio::signal::ctrl_c() => {
eprintln!("got ctrl-c, exiting");
break;
}
};
let endpoint = endpoint.clone();
let addr = addr.clone();
let handshake = !args.common.is_custom_alpn();
let alpn = args.common.alpn()?;
tokio::spawn(async move {
if let Err(cause) = handle_tcp_accept(next, addr, endpoint, handshake, &alpn).await {
// log error at warn level
//
// we should know about it, but it's not fatal
tracing::warn!("error handling connection: {}", cause);
}
});
}
Ok(())
}
/// Listen on a magicsocket and forward incoming connections to a tcp socket.
async fn listen_tcp(args: ListenTcpArgs) -> anyhow::Result<()> {
let addrs = match args.host.to_socket_addrs() {
Ok(addrs) => addrs.collect::<Vec<_>>(),
Err(e) => anyhow::bail!("invalid host string {}: {}", args.host, e),
};
let secret_key = get_or_create_secret()?;
let discovery = n0_discovery(secret_key.clone());
let endpoint = MagicEndpoint::builder()
.alpns(vec![args.common.alpn()?])
.discovery(discovery)
.secret_key(secret_key)
.bind(args.common.magic_port)
.await?;
// wait for the endpoint to figure out its address before making a ticket
while endpoint.my_relay().is_none() {
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
}
let node_addr = endpoint.my_addr().await?;
let mut short = node_addr.clone();
let ticket = NodeTicket::new(node_addr)?;
short.info.direct_addresses.clear();
let short = NodeTicket::new(short)?;
// print the ticket on stderr so it doesn't interfere with the data itself
//
// note that the tests rely on the ticket being the last thing printed
eprintln!("Forwarding incoming requests to '{}'.", args.host);
eprintln!("To connect, use e.g.:");
eprintln!("dumbpipe connect {ticket}");
if args.common.verbose > 0 {
eprintln!("or:\ndumbpipe connect {}", short);
}
tracing::info!("node id is {}", ticket.node_addr().node_id);
tracing::info!("derp url is {:?}", ticket.node_addr().info.relay_url);
// handle a new incoming connection on the magic endpoint
async fn handle_magic_accept(
connecting: quinn::Connecting,
addrs: Vec<std::net::SocketAddr>,
handshake: bool,
) -> anyhow::Result<()> {
let connection = connecting.await.context("error accepting connection")?;
let remote_node_id = get_remote_node_id(&connection)?;
tracing::info!("got connection from {}", remote_node_id);
let (s, mut r) = connection
.accept_bi()
.await
.context("error accepting stream")?;
tracing::info!("accepted bidi stream from {}", remote_node_id);
if handshake {
// read the handshake and verify it
let mut buf = [0u8; dumbpipe::HANDSHAKE.len()];
r.read_exact(&mut buf).await?;
anyhow::ensure!(buf == dumbpipe::HANDSHAKE, "invalid handshake");
}
let connection = tokio::net::TcpStream::connect(addrs.as_slice())
.await
.context(format!("error connecting to {:?}", addrs))?;
let (read, write) = connection.into_split();
forward_bidi(read, write, r, s).await?;
Ok(())
}
loop {
let connecting = select! {
connecting = endpoint.accept() => connecting,
_ = tokio::signal::ctrl_c() => {
eprintln!("got ctrl-c, exiting");
break;
}
};
let Some(connecting) = connecting else {
break;
};
let addrs = addrs.clone();
let handshake = !args.common.is_custom_alpn();
tokio::spawn(async move {
if let Err(cause) = handle_magic_accept(connecting, addrs, handshake).await {
// log error at warn level
//
// we should know about it, but it's not fatal
tracing::warn!("error handling connection: {}", cause);
}
});
}
Ok(())
}
/// Create a discovery service that resolves and publishes via iroh DNS.
pub fn n0_discovery(secret_key: SecretKey) -> Box<dyn Discovery> {
Box::new(ConcurrentDiscovery::from_services(vec![
Box::new(DnsDiscovery::n0_dns()),
Box::new(PkarrPublisher::n0_dns(secret_key)),
]))
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt::init();
let args = Args::parse();
let res = match args.command {
Commands::Listen(args) => listen_stdio(args).await,
Commands::ListenTcp(args) => listen_tcp(args).await,
Commands::Connect(args) => connect_stdio(args).await,
Commands::ConnectTcp(args) => connect_tcp(args).await,
};
match res {
Ok(()) => std::process::exit(0),
Err(e) => {
eprintln!("error: {}", e);
std::process::exit(1)
}
}
}