Skip to content

Commit 272c86f

Browse files
committed
Add configurable control port option
1 parent 00a735a commit 272c86f

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

src/client.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use tracing::{error, info, info_span, warn, Instrument};
88
use uuid::Uuid;
99

1010
use crate::auth::Authenticator;
11-
use crate::shared::{ClientMessage, Delimited, ServerMessage, CONTROL_PORT, NETWORK_TIMEOUT};
11+
use crate::shared::{ClientMessage, Delimited, ServerMessage, NETWORK_TIMEOUT};
1212

1313
/// State structure for the client.
1414
pub struct Client {
@@ -29,6 +29,9 @@ pub struct Client {
2929

3030
/// Optional secret used to authenticate clients.
3131
auth: Option<Authenticator>,
32+
33+
/// TCP port used for control connections with the server.
34+
control_port: u16,
3235
}
3336

3437
impl Client {
@@ -39,8 +42,9 @@ impl Client {
3942
to: &str,
4043
port: u16,
4144
secret: Option<&str>,
45+
control_port: u16,
4246
) -> Result<Self> {
43-
let mut stream = Delimited::new(connect_with_timeout(to, CONTROL_PORT).await?);
47+
let mut stream = Delimited::new(connect_with_timeout(to, control_port).await?);
4448
let auth = secret.map(Authenticator::new);
4549
if let Some(auth) = &auth {
4650
auth.client_handshake(&mut stream).await?;
@@ -66,6 +70,7 @@ impl Client {
6670
local_port,
6771
remote_port,
6872
auth,
73+
control_port,
6974
})
7075
}
7176

@@ -104,7 +109,7 @@ impl Client {
104109

105110
async fn handle_connection(&self, id: Uuid) -> Result<()> {
106111
let mut remote_conn =
107-
Delimited::new(connect_with_timeout(&self.to[..], CONTROL_PORT).await?);
112+
Delimited::new(connect_with_timeout(&self.to[..], self.control_port).await?);
108113
if let Some(auth) = &self.auth {
109114
auth.client_handshake(&mut remote_conn).await?;
110115
}

src/main.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ enum Command {
3434
/// Optional secret for authentication.
3535
#[clap(short, long, env = "BORE_SECRET", hide_env_values = true)]
3636
secret: Option<String>,
37+
38+
/// TCP port used for control connections with the server.
39+
#[clap(long, default_value_t = 7835, env = "BORE_CONTROL_PORT")]
40+
control_port: u16,
3741
},
3842

3943
/// Runs the remote proxy server.
@@ -57,6 +61,10 @@ enum Command {
5761
/// IP address where tunnels will listen on, defaults to --bind-addr.
5862
#[clap(long)]
5963
bind_tunnels: Option<IpAddr>,
64+
65+
/// TCP port used for control connections with clients.
66+
#[clap(long, default_value_t = 7835, env = "BORE_CONTROL_PORT")]
67+
control_port: u16,
6068
},
6169
}
6270

@@ -69,8 +77,9 @@ async fn run(command: Command) -> Result<()> {
6977
to,
7078
port,
7179
secret,
80+
control_port,
7281
} => {
73-
let client = Client::new(&local_host, local_port, &to, port, secret.as_deref()).await?;
82+
let client = Client::new(&local_host, local_port, &to, port, secret.as_deref(), control_port).await?;
7483
client.listen().await?;
7584
}
7685
Command::Server {
@@ -79,6 +88,7 @@ async fn run(command: Command) -> Result<()> {
7988
secret,
8089
bind_addr,
8190
bind_tunnels,
91+
control_port,
8292
} => {
8393
let port_range = min_port..=max_port;
8494
if port_range.is_empty() {
@@ -89,6 +99,7 @@ async fn run(command: Command) -> Result<()> {
8999
let mut server = Server::new(port_range, secret.as_deref());
90100
server.set_bind_addr(bind_addr);
91101
server.set_bind_tunnels(bind_tunnels.unwrap_or(bind_addr));
102+
server.set_control_port(control_port);
92103
server.listen().await?;
93104
}
94105
}

src/server.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use tracing::{info, info_span, warn, Instrument};
1212
use uuid::Uuid;
1313

1414
use crate::auth::Authenticator;
15-
use crate::shared::{ClientMessage, Delimited, ServerMessage, CONTROL_PORT};
15+
use crate::shared::{ClientMessage, Delimited, ServerMessage, DEFAULT_CONTROL_PORT};
1616

1717
/// State structure for the server.
1818
pub struct Server {
@@ -30,6 +30,9 @@ pub struct Server {
3030

3131
/// IP address where tunnels will listen on.
3232
bind_tunnels: IpAddr,
33+
34+
/// TCP port used for control connections with clients.
35+
control_port: u16,
3336
}
3437

3538
impl Server {
@@ -42,6 +45,7 @@ impl Server {
4245
auth: secret.map(Authenticator::new),
4346
bind_addr: IpAddr::V4(Ipv4Addr::UNSPECIFIED),
4447
bind_tunnels: IpAddr::V4(Ipv4Addr::UNSPECIFIED),
48+
control_port: DEFAULT_CONTROL_PORT,
4549
}
4650
}
4751

@@ -55,11 +59,16 @@ impl Server {
5559
self.bind_tunnels = bind_tunnels;
5660
}
5761

62+
/// Set the TCP port used for control connections with clients.
63+
pub fn set_control_port(&mut self, control_port: u16) {
64+
self.control_port = control_port;
65+
}
66+
5867
/// Start the server, listening for new connections.
5968
pub async fn listen(self) -> Result<()> {
6069
let this = Arc::new(self);
61-
let listener = TcpListener::bind((this.bind_addr, CONTROL_PORT)).await?;
62-
info!(addr = ?this.bind_addr, "server listening");
70+
let listener = TcpListener::bind((this.bind_addr, this.control_port)).await?;
71+
info!(addr = ?this.bind_addr, port = this.control_port, "server listening");
6372

6473
loop {
6574
let (stream, addr) = listener.accept().await?;

src/shared.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use tokio_util::codec::{AnyDelimiterCodec, Framed, FramedParts};
1111
use tracing::trace;
1212
use uuid::Uuid;
1313

14-
/// TCP port used for control connections with the server.
15-
pub const CONTROL_PORT: u16 = 7835;
14+
/// Default TCP port used for control connections with the server.
15+
pub const DEFAULT_CONTROL_PORT: u16 = 7835;
1616

1717
/// Maximum byte length for a JSON frame in the stream.
1818
pub const MAX_FRAME_LENGTH: usize = 256;

0 commit comments

Comments
 (0)