Skip to content

Commit

Permalink
Merge pull request #8 from umgefahren/dependabot/cargo/libp2p-0.51
Browse files Browse the repository at this point in the history
  • Loading branch information
dependabot[bot] committed Mar 30, 2023
2 parents 0c5d7cb + d62d271 commit da1c03f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 19 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

- Updated dependencies: See [PR 6].
- [`arti-client` to `v0.8`]
- [`libp2p-core` to `v0.8`]

- Updated dependencies: See [PR 8].
- [`libp2p-core` to `v0.39`]
- [`libp2p` to `0.51`]

[PR 6]: https://github.com/umgefahren/libp2p-tor/pull/6
[PR 8]: https://github.com/umgefahren/libp2p-tor/pull/8

# 0.2.0-alpha

Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "libp2p-community-tor"
version = "0.2.0-alpha"
version = "0.3.0-alpha"
edition = "2021"
license = "MIT"
resolver = "2"
Expand All @@ -12,13 +12,13 @@ authors = ["umgefahren <hannes@umgefahren.xyz>"]
arti-client = { version = "0.8", default-features = false }
async-std-crate = { package = "async-std", version = "1", optional = true, default-features = false }
futures = "0.3"
libp2p-core = { version = "0.38" }
libp2p-core = { version = "0.39" }
thiserror = "1"
tokio-crate = { package = "tokio", version = "1", optional = true, default-features = false }
tor-rtcompat = "0.8"

[dev-dependencies]
libp2p = { version = "0.50", features = ["mplex", "noise", "ping", "yamux", "macros", "async-std"] }
libp2p = { version = "0.51", features = ["mplex", "noise", "ping", "yamux", "macros", "async-std"] }
tokio-crate = { package = "tokio", version = "1", features = ["rt", "macros"] }
async-std-crate = { package = "async-std", version = "1", features = ["attributes"] }

Expand Down
6 changes: 4 additions & 2 deletions examples/ping-onion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

use async_std_crate as async_std;
use futures::prelude::*;
use libp2p::swarm::{keep_alive, NetworkBehaviour, Swarm, SwarmEvent};
use libp2p::swarm::{keep_alive, NetworkBehaviour, SwarmBuilder, SwarmEvent};
use libp2p::{core::upgrade, identity, mplex, noise, ping, yamux, Multiaddr, PeerId, Transport};
use libp2p_community_tor::{AddressConversion, AsyncStdRustlsTorTransport};
use std::error::Error;
Expand Down Expand Up @@ -84,7 +84,9 @@ async fn main() -> Result<(), Box<dyn Error>> {

let transport = onion_transport(local_key).await?;

let mut swarm = Swarm::with_async_std_executor(transport, Behaviour::default(), local_peer_id);
let mut swarm =
SwarmBuilder::with_async_std_executor(transport, Behaviour::default(), local_peer_id)
.build();

// Dial the peer identified by the multi-address given as the second
// command-line argument, if any.
Expand Down
17 changes: 7 additions & 10 deletions src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ use std::net::SocketAddr;
/// "Dangerously" extract a Tor address from the provided [`Multiaddr`].
///
/// See [`DangerouslyIntoTorAddr`] for details around the safety / privacy considerations.
pub fn dangerous_extract_tor_address(multiaddr: &Multiaddr) -> Option<TorAddr> {
if let Some(tor_addr) = safe_extract_tor_address(multiaddr) {
pub fn dangerous_extract(multiaddr: &Multiaddr) -> Option<TorAddr> {
if let Some(tor_addr) = safe_extract(multiaddr) {
return Some(tor_addr);
}

Expand All @@ -42,7 +42,7 @@ pub fn dangerous_extract_tor_address(multiaddr: &Multiaddr) -> Option<TorAddr> {
/// "Safely" extract a Tor address from the provided [`Multiaddr`].
///
/// See [`IntoTorAddr`] for details around the safety / privacy considerations.
pub fn safe_extract_tor_address(multiaddr: &Multiaddr) -> Option<TorAddr> {
pub fn safe_extract(multiaddr: &Multiaddr) -> Option<TorAddr> {
let mut protocols = multiaddr.into_iter();

let tor_addr = try_to_domain_and_port(&protocols.next()?, &protocols.next()?)?
Expand Down Expand Up @@ -89,7 +89,7 @@ mod tests {

let actual = addresses
.iter()
.filter_map(safe_extract_tor_address)
.filter_map(safe_extract)
.collect::<Vec<_>>();

assert_eq!(
Expand All @@ -111,7 +111,7 @@ mod tests {

let actual = addresses
.iter()
.filter_map(dangerous_extract_tor_address)
.filter_map(dangerous_extract)
.collect::<Vec<_>>();

assert_eq!(
Expand All @@ -133,7 +133,7 @@ mod tests {

let actual = addresses
.iter()
.filter_map(dangerous_extract_tor_address)
.filter_map(dangerous_extract)
.collect::<Vec<_>>();

assert_eq!(
Expand All @@ -154,10 +154,7 @@ mod tests {
"/tcp/10/ip4/1.1.1.1".parse().unwrap(),
];

let all_correct = addresses
.iter()
.map(safe_extract_tor_address)
.all(|res| res.is_none());
let all_correct = addresses.iter().map(safe_extract).all(|res| res.is_none());

assert!(
all_correct,
Expand Down
29 changes: 26 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
// DEALINGS IN THE SOFTWARE.

#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
#![warn(clippy::pedantic)]
#![deny(unsafe_code)]
//! Tor based transport for libp2p. Connect through the Tor network to TCP listeners.
//!
//! # ⚠️ Misuse warning ⚠️ - read carefully before using
Expand Down Expand Up @@ -67,7 +69,7 @@
//! # async_std::task::block_on(async { test_func().await.unwrap() });
//! ```

use address::{dangerous_extract_tor_address, safe_extract_tor_address};
use address::{dangerous_extract, safe_extract};
use arti_client::{TorClient, TorClientBuilder};
use futures::{future::BoxFuture, FutureExt};
use libp2p_core::{transport::TransportError, Multiaddr, Transport};
Expand Down Expand Up @@ -115,6 +117,11 @@ pub enum AddressConversion {
}

impl<R: Runtime, S> TorTransport<R, S> {
/// Builds a `TorTransport` from an Arti `TorClientBuilder`.
///
/// # Errors
///
/// Could return errors emitted from Arti.
pub fn from_builder(
builder: TorBuilder<R>,
conversion_mode: AddressConversion,
Expand All @@ -127,18 +134,34 @@ impl<R: Runtime, S> TorTransport<R, S> {
})
}

/// Bootstraps the `TorTransport` into the Tor network.
///
/// # Errors
///
/// Could return error emitted during bootstrap by Arti.
pub async fn bootstrap(&self) -> Result<(), TorError> {
self.client.bootstrap().await
}

/// Set the address conversion mode
#[must_use]
pub fn with_address_conversion(mut self, conversion_mode: AddressConversion) -> Self {
self.conversion_mode = conversion_mode;
self
}
}

#[cfg(all(
any(feature = "native-tls", feature = "rustls"),
any(feature = "async-std", feature = "tokio")
))]
macro_rules! default_constructor {
() => {
/// Creates a bootstrapped `TorTransport`
///
/// # Errors
///
/// Could return error emitted during Tor bootstrap by Arti.
pub async fn bootstrapped() -> Result<Self, TorError> {
let builder = Self::builder();
let ret = Self::from_builder(builder, AddressConversion::DnsOnly)?;
Expand Down Expand Up @@ -242,8 +265,8 @@ where

fn dial(&mut self, addr: Multiaddr) -> Result<Self::Dial, TransportError<Self::Error>> {
let maybe_tor_addr = match self.conversion_mode {
AddressConversion::DnsOnly => safe_extract_tor_address(&addr),
AddressConversion::IpAndDns => dangerous_extract_tor_address(&addr),
AddressConversion::DnsOnly => safe_extract(&addr),
AddressConversion::IpAndDns => dangerous_extract(&addr),
};

let tor_address = maybe_tor_addr.ok_or(TransportError::MultiaddrNotSupported(addr))?;
Expand Down

0 comments on commit da1c03f

Please sign in to comment.