From 88ac38defb20ba432e1059dd95cb5dd9c091a7f6 Mon Sep 17 00:00:00 2001 From: James Date: Sat, 4 Aug 2018 01:01:20 +1000 Subject: [PATCH] Bind IPv4 and IPv6 on the same port --- src/main.rs | 16 +++++++++++++++- src/rest.rs | 7 +++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 32d476f..3048bcf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -49,6 +49,7 @@ use rest::WebServer; use std::net::ToSocketAddrs; +use std::net::TcpListener; use std::sync::Arc; use std::sync::RwLock; @@ -78,6 +79,17 @@ fn main() { InstallerFramework::new(config) }; + // Firstly, allocate us an epidermal port + let target_port = { + let listener = + TcpListener::bind("127.0.0.1:0").expect("At least one local address should be free"); + listener + .local_addr() + .expect("Should be able to pull address from listener") + .port() + }; + + // Now, iterate over all ports let addresses = "localhost:0" .to_socket_addrs() .expect("No localhost address found"); @@ -88,7 +100,9 @@ fn main() { let framework = Arc::new(RwLock::new(framework)); // Startup HTTP server for handling the web view - for address in addresses { + for mut address in addresses { + address.set_port(target_port); + let server = WebServer::with_addr(framework.clone(), address).unwrap(); let addr = server.get_addr(); diff --git a/src/rest.rs b/src/rest.rs index adc6d96..e87935c 100644 --- a/src/rest.rs +++ b/src/rest.rs @@ -17,7 +17,7 @@ use hyper::{self, Error as HyperError, Get, Post, StatusCode}; use url::form_urlencoded; use std::collections::HashMap; -use std::net::{IpAddr, Ipv4Addr, SocketAddr}; +use std::net::{SocketAddr}; use std::process::exit; use std::sync::mpsc::channel; use std::sync::Arc; @@ -48,7 +48,10 @@ impl WebServer { } /// Creates a new web server with the specified address. - pub fn with_addr(framework: Arc>, addr: SocketAddr) -> Result { + pub fn with_addr( + framework: Arc>, + addr: SocketAddr, + ) -> Result { let (sender, receiver) = channel(); let handle = thread::spawn(move || {