Skip to content

Commit 9339195

Browse files
authored
feat: implement --recv-only
This option causes the sending side to not send any data and instead immeditally send an EOF. This can be useful when you are only interested in sending data in one direction. Closes #10
1 parent aa0d368 commit 9339195

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/main.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ fn parse_alpn(alpn: &str) -> Result<Vec<u8>> {
150150

151151
#[derive(Parser, Debug)]
152152
pub struct ListenArgs {
153+
/// Immediately close our sending side, indicating that we will not transmit any data
154+
#[clap(long)]
155+
pub recv_only: bool,
156+
153157
#[clap(flatten)]
154158
pub common: CommonArgs,
155159
}
@@ -183,6 +187,10 @@ pub struct ConnectArgs {
183187
/// The node to connect to
184188
pub ticket: NodeTicket,
185189

190+
/// Immediately close our sending side, indicating that we will not transmit any data
191+
#[clap(long)]
192+
pub recv_only: bool,
193+
186194
#[clap(flatten)]
187195
pub common: CommonArgs,
188196
}
@@ -381,8 +389,13 @@ async fn listen_stdio(args: ListenArgs) -> Result<()> {
381389
r.read_exact(&mut buf).await.e()?;
382390
snafu::ensure_whatever!(buf == dumbpipe::HANDSHAKE, "invalid handshake");
383391
}
384-
tracing::info!("forwarding stdin/stdout to {}", remote_node_id);
385-
forward_bidi(tokio::io::stdin(), tokio::io::stdout(), r, s).await?;
392+
if args.recv_only {
393+
tracing::info!("forwarding stdout to {} (ignoring stdin)", remote_node_id);
394+
forward_bidi(tokio::io::empty(), tokio::io::stdout(), r, s).await?;
395+
} else {
396+
tracing::info!("forwarding stdin/stdout to {}", remote_node_id);
397+
forward_bidi(tokio::io::stdin(), tokio::io::stdout(), r, s).await?;
398+
}
386399
// stop accepting connections after the first successful one
387400
break;
388401
}
@@ -407,8 +420,13 @@ async fn connect_stdio(args: ConnectArgs) -> Result<()> {
407420
// on stdin, so just write a handshake.
408421
s.write_all(&dumbpipe::HANDSHAKE).await.e()?;
409422
}
410-
tracing::info!("forwarding stdin/stdout to {}", remote_node_id);
411-
forward_bidi(tokio::io::stdin(), tokio::io::stdout(), r, s).await?;
423+
if args.recv_only {
424+
tracing::info!("forwarding stdout to {} (ignoring stdin)", remote_node_id);
425+
forward_bidi(tokio::io::empty(), tokio::io::stdout(), r, s).await?;
426+
} else {
427+
tracing::info!("forwarding stdin/stdout to {}", remote_node_id);
428+
forward_bidi(tokio::io::stdin(), tokio::io::stdout(), r, s).await?;
429+
}
412430
tokio::io::stdout().flush().await.e()?;
413431
Ok(())
414432
}

0 commit comments

Comments
 (0)