Added caching for i2p b32 addresses

This commit is contained in:
Philip (a-0) 2024-02-24 16:11:16 +01:00
parent 91142f7bfb
commit 29ff183c08
5 changed files with 32 additions and 25 deletions

View file

@ -4,24 +4,32 @@ use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PeerId {
i2p_addr: I2pSocketAddr,
i2p_dest: I2pSocketAddr,
i2p_b32: Option<I2pAddr>,
}
impl PeerId {
pub fn addr(&self) -> I2pSocketAddr {
self.i2p_addr.to_owned()
self.i2p_dest.to_owned()
}
pub fn addr_ref(&self) -> &I2pSocketAddr {
&self.i2p_addr
&self.i2p_dest
}
pub fn b32_addr(&self) -> anyhow::Result<I2pAddr> {
I2pAddr::from_b64(&self.i2p_addr.dest().string()).map_err(|e| anyhow!(e))
pub fn b32_addr(&mut self) -> anyhow::Result<I2pAddr> {
let result = I2pAddr::from_b64(&self.i2p_dest.dest().string());
if let Ok(addr) = &result {
self.i2p_b32 = Some(addr.to_owned());
}
result.map_err(|e| anyhow!(e))
}
pub fn b32_addr_nocache(&self) -> anyhow::Result<I2pAddr> {
I2pAddr::from_b64(&self.i2p_dest.dest().string()).map_err(|e| anyhow!(e))
}
}
impl ToString for PeerId {
fn to_string(&self) -> String {
self.i2p_addr.to_string()
self.i2p_dest.to_string()
}
}
@ -32,7 +40,7 @@ impl TryFrom<&str> for PeerId {
match ToI2pSocketAddrs::to_socket_addrs(&value) {
Ok(addr_iter) => {
for addr in addr_iter {
return Ok(PeerId { i2p_addr: addr });
return Ok(PeerId { i2p_dest: addr, i2p_b32: None });
}
return Err(anyhow::Error::msg("No valid I2P address found"));
}
@ -51,20 +59,21 @@ impl TryFrom<String> for PeerId {
impl From<I2pSocketAddr> for PeerId {
fn from(value: I2pSocketAddr) -> Self {
PeerId { i2p_addr: value }
PeerId { i2p_dest: value, i2p_b32: None }
}
}
impl From<PeerId> for I2pSocketAddr {
fn from(value: PeerId) -> Self {
value.i2p_addr
value.i2p_dest
}
}
impl Default for PeerId {
fn default() -> Self {
PeerId {
i2p_addr: I2pSocketAddr::new(I2pAddr::new(""), 0),
i2p_dest: I2pSocketAddr::new(I2pAddr::new(""), 0),
i2p_b32: None
}
}
}