From 31e43047543c8f61880052da38a3a4744fa62aea Mon Sep 17 00:00:00 2001 From: Yuval Saraf Date: Mon, 17 Feb 2025 12:23:03 +0200 Subject: [PATCH] Added an arg to control port fallback behaviour --- src/main.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 3432c08..4640143 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,6 +29,10 @@ enum Command { #[clap(short, long, default_value_t = 0)] port: u16, + /// Optional if you fallback to different port if selected port is occupied + #[clap(long, default_value_t = false)] + port_fallback: bool, + /// Optional secret for authentication. #[clap(short, long, env = "BORE_SECRET", hide_env_values = true)] secret: Option, @@ -58,9 +62,24 @@ async fn run(command: Command) -> Result<()> { local_port, to, port, + port_fallback, secret, } => { - let client = Client::new(&local_host, local_port, &to, port, secret.as_deref()).await?; + let client = + match Client::new(&local_host, local_port, &to, port, secret.as_deref()).await { + Ok(client) => client, + Err(e) => { + if e.to_string().eq("server error: port already in use") + && port != 0 + && port_fallback + { + Client::new(&local_host, local_port, &to, 0, secret.as_deref()).await? + } else { + return Err(e); + } + } + }; + client.listen().await?; } Command::Server {