Skip to content

Commit 31e4304

Browse files
committed
Added an arg to control port fallback behaviour
1 parent b23beb9 commit 31e4304

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/main.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ enum Command {
2929
#[clap(short, long, default_value_t = 0)]
3030
port: u16,
3131

32+
/// Optional if you fallback to different port if selected port is occupied
33+
#[clap(long, default_value_t = false)]
34+
port_fallback: bool,
35+
3236
/// Optional secret for authentication.
3337
#[clap(short, long, env = "BORE_SECRET", hide_env_values = true)]
3438
secret: Option<String>,
@@ -58,9 +62,24 @@ async fn run(command: Command) -> Result<()> {
5862
local_port,
5963
to,
6064
port,
65+
port_fallback,
6166
secret,
6267
} => {
63-
let client = Client::new(&local_host, local_port, &to, port, secret.as_deref()).await?;
68+
let client =
69+
match Client::new(&local_host, local_port, &to, port, secret.as_deref()).await {
70+
Ok(client) => client,
71+
Err(e) => {
72+
if e.to_string().eq("server error: port already in use")
73+
&& port != 0
74+
&& port_fallback
75+
{
76+
Client::new(&local_host, local_port, &to, 0, secret.as_deref()).await?
77+
} else {
78+
return Err(e);
79+
}
80+
}
81+
};
82+
6483
client.listen().await?;
6584
}
6685
Command::Server {

0 commit comments

Comments
 (0)