Skip to content

Commit

Permalink
Accept domain name and ip addresses for peers (#612)
Browse files Browse the repository at this point in the history
* Accept String for relay and direct peer addresses in config

* Use ToSocketAddress to handle ip and domain name addresses

* Clippy

* fmt

* Update CHANGELOG

* Update example config.toml
  • Loading branch information
sandreae authored Jun 3, 2024
1 parent b162439 commit e75ac97
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Accept domain name and ip addresses for peers in configuration file and cli [#612](https://github.com/p2panda/aquadoggo/pull/612)
- Allow setting config file path via environment variables [#611](https://github.com/p2panda/aquadoggo/pull/611)

## [0.7.1]
Expand Down
24 changes: 18 additions & 6 deletions aquadoggo/src/api/config_file.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

use std::convert::TryFrom;
use std::net::SocketAddr;
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::OnceLock;

use anyhow::{anyhow, Result};
use libp2p::PeerId;
use libp2p::{Multiaddr, PeerId};
use p2panda_rs::schema::SchemaId;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use tempfile::TempDir;

use crate::network::utils::to_multiaddress;
use crate::{AllowList, Configuration, NetworkConfiguration};

const WILDCARD: &str = "*";
Expand Down Expand Up @@ -154,7 +154,7 @@ pub struct ConfigFile {
/// addresses or even with nodes behind a firewall or NAT, do not use this field but use at
/// least one relay.
#[serde(default)]
pub direct_node_addresses: Vec<SocketAddr>,
pub direct_node_addresses: Vec<String>,

/// List of peers which are allowed to connect to your node.
///
Expand Down Expand Up @@ -196,7 +196,7 @@ pub struct ConfigFile {
/// trusted relays or make sure your IP address is hidden via a VPN or proxy if you're
/// concerned about leaking your IP.
#[serde(default)]
pub relay_addresses: Vec<SocketAddr>,
pub relay_addresses: Vec<String>,

/// Enable if node should also function as a relay. Disabled by default.
///
Expand Down Expand Up @@ -287,6 +287,18 @@ impl TryFrom<ConfigFile> for Configuration {
.to_path_buf(),
};

let direct_node_addresses = value
.direct_node_addresses
.iter()
.map(to_multiaddress)
.collect::<Result<Vec<Multiaddr>, _>>()?;

let relay_addresses = value
.relay_addresses
.iter()
.map(to_multiaddress)
.collect::<Result<Vec<Multiaddr>, _>>()?;

Ok(Configuration {
allow_schema_ids,
database_url: value.database_url,
Expand All @@ -297,10 +309,10 @@ impl TryFrom<ConfigFile> for Configuration {
network: NetworkConfiguration {
quic_port: value.quic_port,
mdns: value.mdns,
direct_node_addresses: value.direct_node_addresses,
direct_node_addresses,
allow_peer_ids,
block_peer_ids: value.block_peer_ids,
relay_addresses: value.relay_addresses,
relay_addresses,
relay_mode: value.relay_mode,
..Default::default()
},
Expand Down
8 changes: 3 additions & 5 deletions aquadoggo/src/network/config.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

use std::net::SocketAddr;

use libp2p::connection_limits::ConnectionLimits;
use libp2p::PeerId;
use libp2p::{Multiaddr, PeerId};

use crate::AllowList;

Expand All @@ -25,7 +23,7 @@ pub struct NetworkConfiguration {
/// with a static IP Address). If you need to connect to nodes with changing, dynamic IP
/// addresses or even with nodes behind a firewall or NAT, do not use this field but use at
/// least one relay.
pub direct_node_addresses: Vec<SocketAddr>,
pub direct_node_addresses: Vec<Multiaddr>,

/// List of peers which are allowed to connect to your node.
///
Expand Down Expand Up @@ -64,7 +62,7 @@ pub struct NetworkConfiguration {
/// WARNING: This will potentially expose your IP address on the network. Do only connect to
/// trusted relays or make sure your IP address is hidden via a VPN or proxy if you're
/// concerned about leaking your IP.
pub relay_addresses: Vec<SocketAddr>,
pub relay_addresses: Vec<Multiaddr>,

/// Enable if node should also function as a relay.
///
Expand Down
14 changes: 7 additions & 7 deletions aquadoggo/src/network/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,14 @@ pub async fn network_service(
// replication sessions, which could leave the node in a strange state.
swarm.behaviour_mut().peers.disable();

for relay_address in network_config.relay_addresses.clone() {
let mut address = utils::to_multiaddress(&relay_address);
info!("Connecting to relay node {}", address);
for mut relay_address in network_config.relay_addresses.clone() {
info!("Connecting to relay node {}", relay_address);

// Attempt to connect to the relay node, we give this a 5 second timeout so as not to
// get stuck if one relay is unreachable.
if let Ok(result) = tokio::time::timeout(
RELAY_CONNECT_TIMEOUT,
connect_to_relay(&mut swarm, &mut address),
connect_to_relay(&mut swarm, &mut relay_address),
)
.await
{
Expand Down Expand Up @@ -161,10 +160,11 @@ pub async fn network_service(

// Dial all nodes we want to directly connect to.
for direct_node_address in &network_config.direct_node_addresses {
let address = utils::to_multiaddress(direct_node_address);
info!("Connecting to node @ {}", address);
info!("Connecting to node @ {}", direct_node_address);

let opts = DialOpts::unknown_peer_id().address(address.clone()).build();
let opts = DialOpts::unknown_peer_id()
.address(direct_node_address.clone())
.build();

match swarm.dial(opts) {
Ok(_) => (),
Expand Down
12 changes: 9 additions & 3 deletions aquadoggo/src/network/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

use std::net::{IpAddr, SocketAddr};
use std::net::{IpAddr, SocketAddr, ToSocketAddrs};

use anyhow::Result;
use libp2p::multiaddr::Protocol;
use libp2p::Multiaddr;
use regex::Regex;
Expand All @@ -24,12 +25,17 @@ pub fn to_quic_address(address: &Multiaddr) -> Option<SocketAddr> {
}
}

pub fn to_multiaddress(socket_address: &SocketAddr) -> Multiaddr {
pub fn to_multiaddress(address: &String) -> Result<Multiaddr> {
let socket_address = address
.to_socket_addrs()?
.next()
.unwrap_or_else(|| panic!("Could not resolve socket address for: {}", address));

let mut multiaddr = match socket_address.ip() {
IpAddr::V4(ip) => Multiaddr::from(Protocol::Ip4(ip)),
IpAddr::V6(ip) => Multiaddr::from(Protocol::Ip6(ip)),
};
multiaddr.push(Protocol::Udp(socket_address.port()));
multiaddr.push(Protocol::QuicV1);
multiaddr
Ok(multiaddr)
}
10 changes: 6 additions & 4 deletions aquadoggo_cli/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ mdns = true
# NODES
# ゚・。+☆

# List of known node addresses (IP + port) we want to connect to directly.
# List of known node addresses we want to connect to direct. Addresses can be
# domain names or IP addresses and must include a port number.
#
# NOTE: Make sure that nodes mentioned in this list are directly reachable
# (they need to be hosted with a static IP Address). If you need to connect to
Expand All @@ -151,7 +152,7 @@ mdns = true
#
direct_node_addresses = [
# "192.0.2.0:2022",
# "192.0.2.2:3000",
# "my.domain.name:2022",
]

# List of peers which are allowed to connect to your node.
Expand Down Expand Up @@ -189,7 +190,8 @@ block_peer_ids = []
# RELAYS
# ゚・。+☆+

# List of relay addresses.
# List of relay addresses. Addresses can be domain names or IP addresses
# and must include a port number.
#
# A relay helps discover other nodes on the internet (also known as
# "rendesvouz" or "bootstrap" server) and helps establishing direct p2p
Expand All @@ -210,7 +212,7 @@ block_peer_ids = []
#
relay_addresses = [
# "192.0.2.16:2022",
# "192.0.2.17:2022",
# "my.domain.me:2022",
]

# Set to true if node should also function as a relay. Defaults to false.
Expand Down
5 changes: 2 additions & 3 deletions aquadoggo_cli/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

use std::net::SocketAddr;
use std::path::PathBuf;

use anyhow::{bail, Result};
Expand Down Expand Up @@ -161,7 +160,7 @@ struct Cli {
/// least one relay.
#[arg(short = 'n', long, value_name = "IP:PORT", num_args = 0..)]
#[serde(skip_serializing_if = "Option::is_none")]
direct_node_addresses: Option<Vec<SocketAddr>>,
direct_node_addresses: Option<Vec<String>>,

/// List of peers which are allowed to connect to your node.
///
Expand Down Expand Up @@ -209,7 +208,7 @@ struct Cli {
/// concerned about leaking your IP.
#[arg(short = 'r', long, value_name = "IP:PORT", num_args = 0..)]
#[serde(skip_serializing_if = "Option::is_none")]
relay_addresses: Option<Vec<SocketAddr>>,
relay_addresses: Option<Vec<String>>,

/// Enable if node should also function as a relay. Disabled by default.
///
Expand Down

0 comments on commit e75ac97

Please sign in to comment.