Bind IPv4 and IPv6 on the same port

This commit is contained in:
James 2018-08-04 01:01:20 +10:00
parent 55239ec4aa
commit 88ac38defb
2 changed files with 20 additions and 3 deletions

View File

@ -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();

View File

@ -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<RwLock<InstallerFramework>>, addr: SocketAddr) -> Result<Self, HyperError> {
pub fn with_addr(
framework: Arc<RwLock<InstallerFramework>>,
addr: SocketAddr,
) -> Result<Self, HyperError> {
let (sender, receiver) = channel();
let handle = thread::spawn(move || {