From 5b1c778a250173471fc3df6c88dafb3d90559fae Mon Sep 17 00:00:00 2001 From: Niko PLP Date: Sun, 25 Jun 2023 03:02:57 +0300 Subject: [PATCH] fix IPv6 listeners --- p2p-broker/src/server_ws.rs | 27 +++++++++++++++------------ p2p-net/src/types.rs | 5 ++++- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/p2p-broker/src/server_ws.rs b/p2p-broker/src/server_ws.rs index c5df72e..fa5bffd 100644 --- a/p2p-broker/src/server_ws.rs +++ b/p2p-broker/src/server_ws.rs @@ -256,8 +256,8 @@ fn upgrade_ws_or_serve_app( Err(make_error(StatusCode::FORBIDDEN)) } -const LOCAL_HOSTS: [&str; 3] = ["localhost", "127.0.0.1", "::1"]; -const LOCAL_URLS: [&str; 3] = ["http://localhost", "http://127.0.0.1", "http://::1"]; +const LOCAL_HOSTS: [&str; 3] = ["localhost", "127.0.0.1", "[::1]"]; +const LOCAL_URLS: [&str; 3] = ["http://localhost", "http://127.0.0.1", "http://[::1]"]; const APP_NG_ONE_URL: &str = "https://app.nextgraph.one"; impl Callback for SecurityCallback { @@ -743,17 +743,20 @@ pub async fn run_server_v0( .map(SocketAddr::to_string) .collect::>() .join(", "); - let tcp_listener = TcpListener::bind(addrs.0.as_slice()).await.map_err(|e| { - log_err!( - "cannot bind to {} with addresses {} : {}", - addrs.1, - addrs_string, - e.to_string() - ) - })?; - log_info!("Listening on {} {}", addrs.1, addrs_string); - listeners.push(tcp_listener); + for addr in addrs.0 { + let tcp_listener = TcpListener::bind(addr).await.map_err(|e| { + log_err!( + "cannot bind to {} with addresses {} : {}", + addrs.1, + addrs_string, + e.to_string() + ) + })?; + listeners.push(tcp_listener); + } + + log_info!("Listening on {} {}", addrs.1, addrs_string); } // select on all listeners diff --git a/p2p-net/src/types.rs b/p2p-net/src/types.rs index 7dcd238..751f463 100644 --- a/p2p-net/src/types.rs +++ b/p2p-net/src/types.rs @@ -459,7 +459,10 @@ impl IP { impl fmt::Display for IP { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let t: IpAddr = self.try_into().unwrap(); - write!(f, "{}", t) + match self { + IP::IPv4(_) => write!(f, "{}", t), + IP::IPv6(_) => write!(f, "[{}]", t), + } } }