Skip to content

Commit

Permalink
hosts: when binding to unspecified address allow requests from localh…
Browse files Browse the repository at this point in the history
…ost (#549)
  • Loading branch information
andresilva authored May 22, 2020
1 parent 5f1e693 commit 0d31c50
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions server-utils/src/hosts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,20 @@ pub fn is_host_valid(host: Option<&str>, allowed_hosts: &Option<Vec<Host>>) -> b

/// Updates given list of hosts with the address.
pub fn update(hosts: Option<Vec<Host>>, address: &SocketAddr) -> Option<Vec<Host>> {
use std::net::{IpAddr, Ipv4Addr};

hosts.map(|current_hosts| {
let mut new_hosts = current_hosts.into_iter().collect::<HashSet<_>>();
let address = address.to_string();
new_hosts.insert(address.clone().into());
new_hosts.insert(address.replace("127.0.0.1", "localhost").into());
let address_string = address.to_string();

if address.ip() == IpAddr::V4(Ipv4Addr::UNSPECIFIED) {
new_hosts.insert(address_string.replace("0.0.0.0", "127.0.0.1").into());
new_hosts.insert(address_string.replace("0.0.0.0", "localhost").into());
} else if address.ip() == IpAddr::V4(Ipv4Addr::LOCALHOST) {
new_hosts.insert(address_string.replace("127.0.0.1", "localhost").into());
}

new_hosts.insert(address_string.into());
new_hosts.into_iter().collect()
})
}
Expand Down

0 comments on commit 0d31c50

Please sign in to comment.