Skip to content

Commit

Permalink
bind argument
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed Dec 17, 2023
1 parent bfbbd1d commit 65c379e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ fn main() {
.multiple(true)
.default_value("")
)
.arg(
Arg::with_name("bind")
.help("bind to the interface associated with the address <host>")
.takes_value(true)
.long("bind")
.short("B")
.value_name("host")
.required(true)
.multiple(true)
.default_value("0.0.0.0")
)
.arg(
Arg::with_name("debug")
.help("emit debug-level logging on stderr; default is info and above")
Expand Down
12 changes: 9 additions & 3 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

use std::error::Error;
use std::io;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, Shutdown, SocketAddr};
use std::net::{IpAddr, Ipv4Addr, Shutdown, SocketAddr};
use std::sync::atomic::{AtomicBool, AtomicU16, Ordering};
use std::sync::mpsc::channel;
use std::sync::{Arc, Mutex};
Expand Down Expand Up @@ -401,13 +401,19 @@ pub fn serve(args: ArgMatches) -> BoxResult<()> {
log::debug!("limiting service to {} concurrent clients", client_limit);
}

let addr = args
.value_of("bind")
.unwrap_or("0.0.0.0")
.parse::<IpAddr>()
.unwrap_or(IpAddr::V4(Ipv4Addr::UNSPECIFIED));

//start listening for connections
let port: u16 = args.value_of("port").unwrap().parse()?;
let listener: TcpListener = if args.is_present("version6") {
TcpListener::bind(&SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), port))
TcpListener::bind(&SocketAddr::new(addr, port))
.unwrap_or_else(|_| panic!("failed to bind TCP socket, port {}", port))
} else {
TcpListener::bind(&SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), port))
TcpListener::bind(&SocketAddr::new(addr, port))
.unwrap_or_else(|_| panic!("failed to bind TCP socket, port {}", port))
};
log::info!("server listening on {}", listener.local_addr()?);
Expand Down

0 comments on commit 65c379e

Please sign in to comment.