Skip to content

Commit 273fd88

Browse files
samuelaEugeny
authored andcommitted
Add russh::server::run_on_socket to facilitate dropping privileges immediately after socket binding
1 parent 6824b44 commit 273fd88

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

russh/src/server/mod.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -630,15 +630,13 @@ pub trait Server {
630630
fn handle_session_error(&mut self, _error: <Self::Handler as Handler>::Error) {}
631631
}
632632

633-
/// Run a server.
634-
/// Create a new `Connection` from the server's configuration, a
635-
/// stream and a [`Handler`](trait.Handler.html).
636-
pub async fn run<H: Server + Send + 'static, A: ToSocketAddrs>(
633+
/// Run a server on a specified `tokio::net::TcpListener`. Useful when dropping
634+
/// privileges immediately after socket binding, for example.
635+
pub async fn run_on_socket<H: Server + Send + 'static>(
637636
config: Arc<Config>,
638-
addrs: A,
637+
socket: &TcpListener,
639638
mut server: H,
640639
) -> Result<(), std::io::Error> {
641-
let socket = TcpListener::bind(addrs).await?;
642640
if config.maximum_packet_size > 65535 {
643641
error!(
644642
"Maximum packet size ({:?}) should not larger than a TCP packet (65535)",
@@ -686,6 +684,18 @@ pub async fn run<H: Server + Send + 'static, A: ToSocketAddrs>(
686684
Ok(())
687685
}
688686

687+
/// Run a server.
688+
/// Create a new `Connection` from the server's configuration, a
689+
/// stream and a [`Handler`](trait.Handler.html).
690+
pub async fn run<H: Server + Send + 'static, A: ToSocketAddrs>(
691+
config: Arc<Config>,
692+
addrs: A,
693+
server: H,
694+
) -> Result<(), std::io::Error> {
695+
let socket = TcpListener::bind(addrs).await?;
696+
run_on_socket(config, &socket, server).await
697+
}
698+
689699
use std::cell::RefCell;
690700
thread_local! {
691701
static B1: RefCell<CryptoVec> = RefCell::new(CryptoVec::new());

0 commit comments

Comments
 (0)