Skip to content

Commit

Permalink
Add russh::server::run_on_socket to facilitate dropping privileges …
Browse files Browse the repository at this point in the history
…immediately after socket binding
  • Loading branch information
samuela authored and Eugeny committed Jan 26, 2024
1 parent 6824b44 commit 273fd88
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions russh/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,15 +630,13 @@ pub trait Server {
fn handle_session_error(&mut self, _error: <Self::Handler as Handler>::Error) {}
}

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

/// Run a server.
/// Create a new `Connection` from the server's configuration, a
/// stream and a [`Handler`](trait.Handler.html).
pub async fn run<H: Server + Send + 'static, A: ToSocketAddrs>(
config: Arc<Config>,
addrs: A,
server: H,
) -> Result<(), std::io::Error> {
let socket = TcpListener::bind(addrs).await?;
run_on_socket(config, &socket, server).await
}

use std::cell::RefCell;
thread_local! {
static B1: RefCell<CryptoVec> = RefCell::new(CryptoVec::new());
Expand Down

0 comments on commit 273fd88

Please sign in to comment.