From 7f86ff01f1744e9339d3f55c7507a74a17e7f7e4 Mon Sep 17 00:00:00 2001 From: adz Date: Fri, 25 Aug 2023 17:17:06 +0200 Subject: [PATCH 1/3] Add bind-address configuration --- aquadoggo/src/config.rs | 10 ++++++++++ aquadoggo/src/http/service.rs | 7 ++++--- aquadoggo/src/network/service.rs | 17 ++++++++--------- aquadoggo_cli/config.toml | 17 ++++++++++++++--- aquadoggo_cli/src/config.rs | 15 ++++++++++++++- 5 files changed, 50 insertions(+), 16 deletions(-) diff --git a/aquadoggo/src/config.rs b/aquadoggo/src/config.rs index eba85da0a..673d3f5ec 100644 --- a/aquadoggo/src/config.rs +++ b/aquadoggo/src/config.rs @@ -1,6 +1,7 @@ // SPDX-License-Identifier: AGPL-3.0-or-later use std::path::PathBuf; +use std::net::Ipv4Addr; use p2panda_rs::schema::SchemaId; @@ -33,6 +34,14 @@ pub struct Configuration { /// application in high-availability deployments). pub database_max_connections: u32, + /// IPv4 address this node listens to for networking with other nodes. + /// + /// Depending on this bind address other nodes will or will not be able to reach out to you. + /// + /// Set address to 0.0.0.0 (catch-all) if you want your node to listen on all networking + /// interfaces. This might expose your node to the internet. + pub bind_address: Ipv4Addr, + /// HTTP port, serving the GraphQL API (for example hosted under /// http://localhost:2020/graphql). This API is used for client-node communication. Defaults to /// 2020. @@ -61,6 +70,7 @@ impl Default for Configuration { allow_schema_ids: AllowList::Wildcard, database_url: "sqlite::memory:".into(), database_max_connections: 32, + bind_address: Ipv4Addr::LOCALHOST, http_port: 2020, blobs_base_path: PathBuf::new(), worker_pool_size: 16, diff --git a/aquadoggo/src/http/service.rs b/aquadoggo/src/http/service.rs index 855d61193..91ab271c2 100644 --- a/aquadoggo/src/http/service.rs +++ b/aquadoggo/src/http/service.rs @@ -1,6 +1,6 @@ // SPDX-License-Identifier: AGPL-3.0-or-later -use std::net::{IpAddr, Ipv4Addr, SocketAddr}; +use std::net::{IpAddr, SocketAddr}; use anyhow::Result; use axum::extract::Extension; @@ -55,7 +55,8 @@ pub async fn http_service( tx_ready: ServiceReadySender, ) -> Result<()> { let http_port = context.config.http_port; - let http_address = SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), http_port); + let bind_address = context.config.bind_address; + let http_address = SocketAddr::new(IpAddr::V4(bind_address), http_port); // Prepare GraphQL manager executing incoming GraphQL queries via HTTP let graphql_schema_manager = @@ -75,7 +76,7 @@ pub async fn http_service( builder } else { println!("HTTP port {http_port} was already taken, try random port instead .."); - axum::Server::try_bind(&SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), 0))? + axum::Server::try_bind(&SocketAddr::new(IpAddr::V4(bind_address), 0))? }; let builder = builder.serve(build_server(http_context).into_make_service()); diff --git a/aquadoggo/src/network/service.rs b/aquadoggo/src/network/service.rs index 720d8e36f..5fe978a3f 100644 --- a/aquadoggo/src/network/service.rs +++ b/aquadoggo/src/network/service.rs @@ -1,7 +1,6 @@ // SPDX-License-Identifier: AGPL-3.0-or-later use std::collections::HashMap; -use std::net::Ipv4Addr; use std::num::NonZeroU8; use std::time::Duration; @@ -53,7 +52,7 @@ pub async fn network_service( // Start listening on tcp address. let listen_addr_tcp = Multiaddr::empty() - .with(Protocol::from(Ipv4Addr::UNSPECIFIED)) + .with(Protocol::from(context.config.bind_address)) .with(Protocol::Tcp(0)); swarm.listen_on(listen_addr_tcp)?; @@ -65,12 +64,12 @@ pub async fn network_service( // Start listening on QUIC address. Pick a random one if the given is taken already. let listen_addr_quic = Multiaddr::empty() - .with(Protocol::from(Ipv4Addr::UNSPECIFIED)) + .with(Protocol::from(context.config.bind_address)) .with(Protocol::Udp(network_config.quic_port)) .with(Protocol::QuicV1); if swarm.listen_on(listen_addr_quic).is_err() { let random_port_addr = Multiaddr::empty() - .with(Protocol::from(Ipv4Addr::UNSPECIFIED)) + .with(Protocol::from(context.config.bind_address)) .with(Protocol::Udp(0)) .with(Protocol::QuicV1); println!( @@ -318,7 +317,7 @@ struct EventLoop { rx: BroadcastStream, relay_addresses: HashMap, shutdown_handler: ShutdownHandler, - learned_port: bool, + learned_listen_address: bool, } impl EventLoop { @@ -336,7 +335,7 @@ impl EventLoop { tx, relay_addresses, shutdown_handler, - learned_port: false, + learned_listen_address: false, } } @@ -367,15 +366,15 @@ impl EventLoop { let event = event.expect("Swarm stream to be infinite"); match event { SwarmEvent::NewListenAddr { address, .. } => { - if self.learned_port { + if self.learned_listen_address { continue; } // Show only one QUIC address during the runtime of the node, otherwise // it might get too spammy if let Some(address) = utils::to_quic_address(&address) { - println!("Node is listening on 0.0.0.0:{}", address.port()); - self.learned_port = true; + println!("Node is listening on {}:{}", address.ip(), address.port()); + self.learned_listen_address = true; } } SwarmEvent::Behaviour(Event::Identify(event)) => self.handle_identify_events(&event).await, diff --git a/aquadoggo_cli/config.toml b/aquadoggo_cli/config.toml index 26ce05b03..12fc1edd0 100644 --- a/aquadoggo_cli/config.toml +++ b/aquadoggo_cli/config.toml @@ -70,9 +70,20 @@ allow_schema_ids = "*" # database_max_connections = 32 -# ゚・。+☆ -# PORTS -# ゚・。+☆ +# ゚・。+☆+。・゚・。+☆+ +# ADDRESS & PORT +# ゚・。+☆+。・゚・。+☆+ + +# IPv4 address this node listens to for networking with other nodes. Defaults +# to localhost or 127.0.0.1. +# +# Depending on this bind address other nodes will or will not be able to reach +# out to you. +# +# Set address to 0.0.0.0 (catch-all) if you want your node to listen on all +# networking interfaces. This might expose your node to the internet. +# +bind_address = "127.0.0.1" # HTTP port, serving the GraphQL API (for example hosted under # http://localhost:2020/graphql). This API is used for client-node diff --git a/aquadoggo_cli/src/config.rs b/aquadoggo_cli/src/config.rs index 375d16ee6..5661170e3 100644 --- a/aquadoggo_cli/src/config.rs +++ b/aquadoggo_cli/src/config.rs @@ -1,7 +1,7 @@ // SPDX-License-Identifier: AGPL-3.0-or-later use std::convert::TryFrom; -use std::net::{IpAddr, SocketAddr}; +use std::net::{IpAddr, Ipv4Addr, SocketAddr}; use std::path::PathBuf; use std::str::FromStr; use std::sync::OnceLock; @@ -115,6 +115,16 @@ struct Cli { #[serde(skip_serializing_if = "Option::is_none")] database_url: Option, + /// IPv4 address this node listens to for networking with other nodes. + /// + /// Depending on this bind address other nodes will or will not be able to reach out to you. + /// + /// Set address to 0.0.0.0 (catch-all) if you want your node to listen on all networking + /// interfaces. This might expose your node to the internet. + #[arg(short = 'i', long, value_name = "IP")] + #[serde(skip_serializing_if = "Option::is_none")] + bind_address: Option, + /// HTTP port for client-node communication, serving the GraphQL API. Defaults to 2020. #[arg(short = 'p', long, value_name = "PORT")] #[serde(skip_serializing_if = "Option::is_none")] @@ -278,6 +288,7 @@ pub struct Configuration { pub allow_schema_ids: UncheckedAllowList, pub database_url: String, pub database_max_connections: u32, + pub bind_address: Ipv4Addr, pub http_port: u16, pub quic_port: u16, pub blobs_base_path: Option, @@ -298,6 +309,7 @@ impl Default for Configuration { allow_schema_ids: UncheckedAllowList::Wildcard, database_url: "sqlite::memory:".into(), database_max_connections: 32, + bind_address: Ipv4Addr::LOCALHOST, http_port: 2020, quic_port: 2022, blobs_base_path: None, @@ -371,6 +383,7 @@ impl TryFrom for NodeConfiguration { allow_schema_ids, database_url: value.database_url, database_max_connections: value.database_max_connections, + bind_address: value.bind_address, http_port: value.http_port, blobs_base_path, worker_pool_size: value.worker_pool_size, From b28eacec13cc7aa8f3522df22eba583156049fe8 Mon Sep 17 00:00:00 2001 From: adz Date: Fri, 25 Aug 2023 17:18:05 +0200 Subject: [PATCH 2/3] Add entry to CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 16a912807..7b135650b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -54,6 +54,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fix relayed connections, add DCUtR Holepunching and reduce CLI args [#502](https://github.com/p2panda/aquadoggo/pull/502) - Announce supported schema ids in network before replication [#515](https://github.com/p2panda/aquadoggo/pull/515) - Allow & block lists, direct dial known peers, connect to multiple relays [#542](https://github.com/p2panda/aquadoggo/pull/524) +- Add configuration to bind to Ipv4 address [#530](https://github.com/p2panda/aquadoggo/pull/530) ### Changed From 3c05f5787a1632ab8130a057d75af32cf1e7e988 Mon Sep 17 00:00:00 2001 From: adz Date: Tue, 19 Sep 2023 16:47:37 +0200 Subject: [PATCH 3/3] Run cargo fmt --- aquadoggo/src/config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aquadoggo/src/config.rs b/aquadoggo/src/config.rs index 673d3f5ec..e58a92fc3 100644 --- a/aquadoggo/src/config.rs +++ b/aquadoggo/src/config.rs @@ -1,7 +1,7 @@ // SPDX-License-Identifier: AGPL-3.0-or-later -use std::path::PathBuf; use std::net::Ipv4Addr; +use std::path::PathBuf; use p2panda_rs::schema::SchemaId;