Skip to content

Commit

Permalink
Use SocketAddr for all addresses in network config (#576)
Browse files Browse the repository at this point in the history
* Change all addresses on NetworkConfig to SocketAddr

* Clippy

* Update CHANGELOG
  • Loading branch information
sandreae committed Oct 11, 2023
1 parent 94bc8e9 commit a24b2d8
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 39 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Updates for new hash serialization in p2panda-rs [#569](https://github.com/p2panda/aquadoggo/pull/569)
- Use `libp2p` `0.5.3` [#570](https://github.com/p2panda/aquadoggo/pull/570)
- Optimize test data generation methods [#572](https://github.com/p2panda/aquadoggo/pull/572)
- Use `SocketAddr` in network config instead of `MultiAddr` [#576](https://github.com/p2panda/aquadoggo/pull/576)

## Fixed

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

use libp2p::Multiaddr;
use libp2p::{connection_limits::ConnectionLimits, PeerId};
use std::net::SocketAddr;

use libp2p::connection_limits::ConnectionLimits;
use libp2p::PeerId;

use crate::AllowList;

Expand All @@ -23,7 +25,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<Multiaddr>,
pub direct_node_addresses: Vec<SocketAddr>,

/// List of peers which are allowed to connect to your node.
///
Expand Down Expand Up @@ -62,7 +64,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<Multiaddr>,
pub relay_addresses: Vec<SocketAddr>,

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

for mut relay_address in network_config.relay_addresses.clone() {
if let Some(address) = utils::to_quic_address(&relay_address) {
info!("Connecting to relay node {}", address);
}
for relay_address in network_config.relay_addresses.clone() {
let mut address = utils::to_multiaddress(&relay_address);
info!("Connecting to relay node {}", 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 relay_address),
connect_to_relay(&mut swarm, &mut address),
)
.await
{
Expand Down Expand Up @@ -162,13 +161,10 @@ pub async fn network_service(

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

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

match swarm.dial(opts) {
Ok(_) => (),
Expand Down
13 changes: 12 additions & 1 deletion 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::SocketAddr;
use std::net::{IpAddr, SocketAddr};

use libp2p::multiaddr::Protocol;
use libp2p::Multiaddr;
use regex::Regex;

Expand All @@ -22,3 +23,13 @@ pub fn to_quic_address(address: &Multiaddr) -> Option<SocketAddr> {
}
}
}

pub fn to_multiaddress(socket_address: &SocketAddr) -> Multiaddr {
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
}
27 changes: 4 additions & 23 deletions 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::SocketAddr;
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::OnceLock;
Expand All @@ -13,8 +13,7 @@ use colored::Colorize;
use directories::ProjectDirs;
use figment::providers::{Env, Format, Serialized, Toml};
use figment::Figment;
use libp2p::multiaddr::Protocol;
use libp2p::{Multiaddr, PeerId};
use libp2p::PeerId;
use p2panda_rs::schema::SchemaId;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use tempfile::TempDir;
Expand Down Expand Up @@ -377,35 +376,17 @@ impl TryFrom<Configuration> for NodeConfiguration {
network: NetworkConfiguration {
quic_port: value.quic_port,
mdns: value.mdns,
direct_node_addresses: value
.direct_node_addresses
.into_iter()
.map(to_multiaddress)
.collect(),
direct_node_addresses: value.direct_node_addresses,
allow_peer_ids,
block_peer_ids: value.block_peer_ids,
relay_addresses: value
.relay_addresses
.into_iter()
.map(to_multiaddress)
.collect(),
relay_addresses: value.relay_addresses,
relay_mode: value.relay_mode,
..Default::default()
},
})
}
}

fn to_multiaddress(socket_address: SocketAddr) -> Multiaddr {
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
}

fn try_determine_config_file_path() -> Option<PathBuf> {
// Find config file in current folder
let mut current_dir = std::env::current_dir().expect("Could not determine current directory");
Expand Down

0 comments on commit a24b2d8

Please sign in to comment.