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
}
}
}

View file

@ -10,7 +10,7 @@ use std::io::{Read, Write};
use std::ops::Deref;
use std::sync::Arc;
use anyhow::bail;
use anyhow::{anyhow, bail};
use i2p::net::{I2pAddr, I2pListener, I2pListenerBuilder, I2pSocketAddr, I2pStream};
use i2p::sam_options::SAMOptions;
use tokio::sync::RwLock;
@ -22,6 +22,7 @@ use crate::Config;
pub struct CommHandle {
state: Arc<CommState>,
i2p_server: Arc<I2pListener>,
peer_id: RwLock<PeerId>,
// Maps peer addresses to existing connections to them
clients: Arc<RwLock<HashMap<I2pSocketAddr, Arc<RwLock<I2pStream>>>>>,
thread: RwLock<Option<JoinHandle<()>>>,
@ -37,10 +38,12 @@ impl CommHandle {
}
let listener = listener_builder.build().unwrap();
let own_peer_id: PeerId = (&listener).local_addr().map_err(|e| anyhow!(e))?.into();
Ok(CommHandle {
state: Arc::new(state),
i2p_server: Arc::new(listener),
peer_id: RwLock::new(own_peer_id),
clients: Default::default(),
thread: RwLock::new(None),
})
@ -149,21 +152,16 @@ impl CommHandle {
))
}
pub fn i2p_address(&self) -> anyhow::Result<I2pSocketAddr> {
match self.i2p_server.local_addr() {
Ok(addr) => Ok(addr),
Err(e) => bail!(e),
}
pub fn i2p_address(&self) -> I2pSocketAddr {
self.peer_id.blocking_read().addr()
}
pub fn i2p_b32_address(&self) -> anyhow::Result<I2pSocketAddr> {
let mut i2p_dest = self.i2p_address()?;
i2p_dest.set_dest(I2pAddr::from_b64(&i2p_dest.dest().string()).unwrap());
Ok(i2p_dest)
pub fn i2p_b32_address(&self) -> anyhow::Result<I2pAddr> {
self.peer_id.blocking_write().b32_addr()
}
pub fn own_peer_id(&self) -> anyhow::Result<PeerId> {
Ok(self.i2p_address()?.into())
Ok(self.peer_id.blocking_read().to_owned())
}
fn read_connection(

View file

@ -84,7 +84,7 @@ impl Ubisync {
self.state_handle.get_peers().unwrap_or(vec![])
}
pub fn get_destination(&self) -> anyhow::Result<I2pSocketAddr> {
pub fn get_destination(&self) -> I2pSocketAddr {
self.comm_handle.i2p_address()
}

View file

@ -246,7 +246,7 @@ impl State {
{
debug!(
"Sending to peer '{:?}' returned an error: {}",
peer.b32_addr(),
peer.b32_addr_nocache(),
e
)
}

View file

@ -23,7 +23,7 @@ async fn two_nodes_element_creation() {
c2.api_config.port = Some(9982);
let ubi1 = Ubisync::new(&Config::default()).await.unwrap();
let ubi2 = Arc::new(Ubisync::new(&c2).await.unwrap());
ubi1.add_family_member_from_id(ubi2.get_destination().unwrap().into())
ubi1.add_family_member_from_id(ubi2.get_destination().into())
.unwrap();
let api_client1 =
@ -93,7 +93,7 @@ async fn two_nodes_api_event() {
c2.api_config.port = Some(9982);
let ubi1 = Arc::new(Ubisync::new(&Config::default()).await.unwrap());
let ubi2 = Ubisync::new(&c2).await.unwrap();
ubi2.add_family_member_from_id(ubi1.get_destination().unwrap().into())
ubi2.add_family_member_from_id(ubi1.get_destination().into())
.unwrap();
let api_client1 =