From 8bee92864d7d324891146595ebf746a0b4798bf2 Mon Sep 17 00:00:00 2001 From: Samir Talwar Date: Tue, 2 Apr 2024 12:16:45 +0200 Subject: [PATCH] Listen on any IPv4 or any IPv6 address. (#428) ## Description Listening on `0.0.0.0` only binds to IPv4 addresses. We can listen on IPv4 and IPv6 by using `::`. I have opted to use the constants for this rather than parsing a string, both for clarity and to avoid errors. ## Changelog - Add a changelog entry (in the "Changelog entry" section below) if the changes in this PR have any user-facing impact. See [changelog guide](https://github.com/hasura/graphql-engine-mono/wiki/Changelog-Guide). - If no changelog is required ignore/remove this section and add a `no-changelog-required` label to the PR. ### Product _(Select all products this will be available in)_ - [x] community-edition - [ ] cloud ### Type _(Select only one. In case of multiple, choose the most appropriate)_ - [ ] highlight - [x] enhancement - [ ] bugfix - [ ] behaviour-change - [ ] performance-enhancement - [ ] security-fix ### Changelog entry The v3 engine now binds to all IPv4 and IPv6 addresses. Previously it only used IPv4. V3_GIT_ORIGIN_REV_ID: d63b5544bfbf2ad067113980fa61dd74213c7b78 --- v3/crates/engine/bin/engine/main.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/v3/crates/engine/bin/engine/main.rs b/v3/crates/engine/bin/engine/main.rs index 90c2a8ed08a..1bc086da52b 100644 --- a/v3/crates/engine/bin/engine/main.rs +++ b/v3/crates/engine/bin/engine/main.rs @@ -1,4 +1,5 @@ use std::fmt::Display; +use std::net; use std::path::PathBuf; use std::sync::Arc; @@ -26,6 +27,8 @@ use hasura_authn_jwt::jwt; use hasura_authn_webhook::webhook; use lang_graphql as gql; +const DEFAULT_PORT: u16 = 3000; + #[derive(Parser)] #[command(version = VERSION)] struct ServerOptions { @@ -36,7 +39,7 @@ struct ServerOptions { #[arg(long, value_name = "AUTHN_CONFIG_FILE", env = "AUTHN_CONFIG_PATH")] authn_config_path: PathBuf, #[arg(long, value_name = "SERVER_PORT", env = "PORT")] - port: Option, + port: Option, } struct EngineState { @@ -172,14 +175,16 @@ async fn start_engine(server: &ServerOptions) -> Result<(), StartupError> { .merge(explain_route) .merge(health_route); - let addr = format!("0.0.0.0:{}", server.port.unwrap_or(3000)); - - let log = format!("starting server on {addr}"); + // The "unspecified" IPv6 address will match any IPv4 or IPv6 address. + let host = net::IpAddr::V6(net::Ipv6Addr::UNSPECIFIED); + let port = server.port.unwrap_or(DEFAULT_PORT); + let address = net::SocketAddr::new(host, port); + let log = format!("starting server on {address}"); println!("{log}"); add_event_on_active_span(log); // run it with hyper on `addr` - axum::Server::bind(&addr.as_str().parse().unwrap()) + axum::Server::bind(&address) .serve(app.into_make_service()) .with_graceful_shutdown(shutdown_signal()) .await