Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
node: parse ipv6 addresses in square brackets correctly
Defelo committed 2 months ago
commit 7a334edd0f0193298c0f4f6acc6bc4ec948c5840
parent e9245b630d728672d2b3d6ff9265fa4f1a86f13a
1 file changed +25 -2
modified crates/radicle/src/node.rs
@@ -19,6 +19,7 @@ pub mod timestamp;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque};
use std::io::{BufRead, BufReader};
use std::marker::PhantomData;
+
use std::net::Ipv6Addr;
use std::ops::{ControlFlow, Deref};
use std::path::{Path, PathBuf};
use std::str::FromStr;
@@ -30,7 +31,7 @@ use std::os::unix::net::UnixStream;
use uds_windows::UnixStream;

use amplify::WrapperMut;
-
use cyphernet::addr::NetAddr;
+
use cyphernet::addr::{AddrParseError, NetAddr};
use localtime::{LocalDuration, LocalTime};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
@@ -422,7 +423,7 @@ impl TryFrom<&sqlite::Value> for Alias {

/// Peer public protocol address.
#[derive(Clone, Eq, PartialEq, Debug, Hash, From, Wrapper, WrapperMut, Serialize, Deserialize)]
-
#[wrapper(Deref, Display, FromStr)]
+
#[wrapper(Deref, Display)]
#[wrapper_mut(DerefMut)]
#[cfg_attr(
    feature = "schemars",
@@ -490,6 +491,28 @@ impl Address {
    }
}

+
impl FromStr for Address {
+
    type Err = AddrParseError;
+

+
    fn from_str(s: &str) -> Result<Self, Self::Err> {
+
        let (host, port) = s.rsplit_once(':').ok_or(AddrParseError::PortAbsent)?;
+

+
        let host = if let Some(addr) = host
+
            .strip_prefix('[')
+
            .and_then(|host| host.strip_suffix(']'))
+
            .and_then(|host| host.parse::<Ipv6Addr>().ok())
+
        {
+
            HostName::Ip(addr.into())
+
        } else {
+
            host.parse()?
+
        };
+

+
        let port = port.parse().map_err(|_| AddrParseError::InvalidPort)?;
+

+
        Ok(Self(NetAddr::new(host, port)))
+
    }
+
}
+

impl cyphernet::addr::Host for Address {
    fn requires_proxy(&self) -> bool {
        self.0.requires_proxy()