Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configuration to bind to Ipv4 address #530

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,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

Expand Down
10 changes: 10 additions & 0 deletions aquadoggo/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

use std::net::Ipv4Addr;
use std::path::PathBuf;

use p2panda_rs::schema::SchemaId;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions aquadoggo/src/http/service.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 =
Expand All @@ -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());
Expand Down
17 changes: 8 additions & 9 deletions aquadoggo/src/network/service.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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)?;

Expand All @@ -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!(
Expand Down Expand Up @@ -318,7 +317,7 @@ struct EventLoop {
rx: BroadcastStream<ServiceMessage>,
relay_addresses: HashMap<PeerId, Multiaddr>,
shutdown_handler: ShutdownHandler,
learned_port: bool,
learned_listen_address: bool,
}

impl EventLoop {
Expand All @@ -336,7 +335,7 @@ impl EventLoop {
tx,
relay_addresses,
shutdown_handler,
learned_port: false,
learned_listen_address: false,
}
}

Expand Down Expand Up @@ -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,
Expand Down
17 changes: 14 additions & 3 deletions aquadoggo_cli/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion aquadoggo_cli/src/config.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -115,6 +115,16 @@ struct Cli {
#[serde(skip_serializing_if = "Option::is_none")]
database_url: Option<String>,

/// 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<Ipv4Addr>,

/// 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")]
Expand Down Expand Up @@ -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<PathBuf>,
Expand All @@ -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,
Expand Down Expand Up @@ -371,6 +383,7 @@ impl TryFrom<Configuration> 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,
Expand Down