Skip to content

Commit

Permalink
feat: Add Ipfs::{add,remove}_external_address.
Browse files Browse the repository at this point in the history
  • Loading branch information
dariusc93 committed Jun 6, 2024
1 parent 5036f1d commit d23210d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 0.11.20
- feat: Add Ipfs::{add,remove}_external_address.

# 0.11.19
- chore: Pin getrandom to 0.2.14 due to libc 0.2.154 being yanked.

Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ name = "rust-ipfs"
readme = "README.md"
repository = "https://github.com/dariusc93/rust-ipfs"
description = "IPFS node implementation"
version = "0.11.19"
version = "0.11.20"

[features]

Expand Down
35 changes: 35 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,8 @@ enum IpfsEvent {
PubsubSubscribed(Channel<Vec<String>>),
AddListeningAddress(Multiaddr, Channel<Multiaddr>),
RemoveListeningAddress(Multiaddr, Channel<()>),
AddExternalAddress(Multiaddr, Channel<()>),
RemoveExternalAddress(Multiaddr, Channel<()>),
Bootstrap(Channel<ReceiverChannel<KadResult>>),
AddPeer(PeerId, Multiaddr, Channel<()>),
RemovePeer(PeerId, Option<Multiaddr>, Channel<bool>),
Expand Down Expand Up @@ -1786,6 +1788,39 @@ impl Ipfs {
.await
}

/// Add a given multiaddr as a external address to indenticate how our node can be reached.
/// Note: We will not perform checks
pub async fn add_external_address(&self, addr: Multiaddr) -> Result<(), Error> {
async move {
let (tx, rx) = oneshot_channel();

self.to_task
.clone()
.send(IpfsEvent::AddExternalAddress(addr, tx))
.await?;

rx.await?
}
.instrument(self.span.clone())
.await
}

/// Removes a previously added external address.
pub async fn remove_external_address(&self, addr: Multiaddr) -> Result<(), Error> {
async move {
let (tx, rx) = oneshot_channel();

self.to_task
.clone()
.send(IpfsEvent::RemoveExternalAddress(addr, tx))
.await?;

rx.await?
}
.instrument(self.span.clone())
.await
}

/// Obtain the addresses associated with the given `PeerId`; they are first searched for locally
/// and the DHT is used as a fallback: a `Kademlia::get_closest_peers(peer_id)` query is run and
/// when it's finished, the newly added DHT records are checked for the existence of the desired
Expand Down
8 changes: 8 additions & 0 deletions src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,14 @@ impl<C: NetworkBehaviour<ToSwarm = void::Void>> IpfsTask<C> {
}
}
}
IpfsEvent::AddExternalAddress(addr, ret) => {
self.swarm.add_external_address(addr);
_ = ret.send(Ok(()))
}
IpfsEvent::RemoveExternalAddress(addr, ret) => {
self.swarm.remove_external_address(&addr);
_ = ret.send(Ok(()))
}
IpfsEvent::Bootstrap(ret) => {
let Some(kad) = self.swarm.behaviour_mut().kademlia.as_mut() else {
let _ = ret.send(Err(anyhow!("kad protocol is disabled")));
Expand Down

0 comments on commit d23210d

Please sign in to comment.