Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,24 @@ edition = "2021"
license = "MIT"
resolver = "2"
description = "Tor transport for libp2p."
repository = "https://github.com/umgefahren/libp2p-tor"
authors = ["umgefahren <[email protected]>"]
repository = "https://github.com/UnstoppableSwap/libp2p-tor"
authors = ["umgefahren <[email protected]>", "наб <[email protected]>"]

[dependencies]
thiserror = "1.0"
anyhow = "1.0.93"
tokio = "1.41.1"
futures = "0.3"

arti-client = { version = "0.30", default-features = false, features = ["tokio", "rustls", "onion-service-client", "static-sqlite"] }
arti-client = { version = "^0.25", default-features = false, features = ["tokio", "rustls", "onion-service-client", "static-sqlite"] }
tor-interface = { git = "https://github.com/nabijaczleweli/gosling", rev = "32988e5770c12f1b48b865c158509473123eae90", optional = true }
libp2p = { version = "^0.53", default-features = false, features = ["tokio", "tcp", "tls"] }

tor-rtcompat = { version = "0.30", features = ["tokio", "rustls"] }
tor-rtcompat = { version = "^0.25", features = ["tokio", "rustls"] }
tracing = "0.1.40"
tor-hsservice = { version = "0.30", optional = true }
tor-cell = { version = "0.30", optional = true }
tor-proto = { version = "0.30", optional = true }
tor-hsservice = { version = "^0.25", optional = true }
tor-cell = { version = "^0.25", optional = true }
tor-proto = { version = "^0.25", optional = true }
data-encoding = { version = "2.6.0" }

[dev-dependencies]
Expand All @@ -37,11 +38,18 @@ listen-onion-service = [
"dep:tor-cell",
"dep:tor-proto"
]
arti-client-tor-provider = ["tor-interface", "tor-interface/arti-client-tor-provider"]
legacy-tor-provider = ["tor-interface", "tor-interface/legacy-tor-provider"]
mock-tor-provider = ["tor-interface", "tor-interface/mock-tor-provider"]

[[example]]
name = "ping-onion"
required-features = ["listen-onion-service"]

[[example]]
name = "ping-onion-tor-interface"
required-features = ["tor-interface"]

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ let mut transport = libp2p_community_tor::TorTransport::bootstrapped().await?;
// we have achieved tor connection
let _conn = transport.dial(address)?.await?;
```
```rust
let address = "/dns/www.torproject.org/tcp/1000".parse()?;
let mut provider = libp2p_community_tor_interface::tor_interface::/* whichever one you want */;
let mut transport = libp2p_community_tor_interface::TorInterfaceTransport::from_provider(Default::default(), Arc::new(Mutex::new(provider)), None);
// we have achieved tor connection
let _conn = transport.dial(address)?.await?;
```

### About

Expand Down
146 changes: 146 additions & 0 deletions examples/ping-onion-tor-interface.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
// Copyright 2022 Hannes Furmans
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

//! Ping-Onion example
//!
//! See ../src/tutorial.rs for a step-by-step guide building the example below.
//!
//! This example requires two seperate computers, one of which has to be reachable from the
//! internet.
//!
//! On the first computer run:
//! ```sh
//! cargo run --example ping
//! ```
//!
//! It will print the PeerId and the listening addresses, e.g. `Listening on
//! "/ip4/0.0.0.0/tcp/24915"`
//!
//! Make sure that the first computer is reachable under one of these ip addresses and port.
//!
//! On the second computer run:
//! ```sh
//! cargo run --example ping-onion -- /ip4/123.45.67.89/tcp/24915
//! ```
//!
//! The two nodes establish a connection, negotiate the ping protocol
//! and begin pinging each other over Tor.

use futures::StreamExt;
use libp2p::core::upgrade::Version;
use libp2p::Transport;
use libp2p::{
core::muxing::StreamMuxerBox,
identity, noise,
swarm::{NetworkBehaviour, SwarmEvent},
yamux, Multiaddr, PeerId, SwarmBuilder,
};
use std::error::Error;
use std::sync::{Arc, Mutex};
use tor_interface::tor_crypto::Ed25519PrivateKey;

/// Create a transport
/// Returns a tuple of the transport and the onion address we can instruct it to listen on
async fn onion_transport(
keypair: identity::Keypair,
) -> Result<
(
libp2p::core::transport::Boxed<(PeerId, libp2p::core::muxing::StreamMuxerBox)>,
Multiaddr,
),
Box<dyn Error>,
> {
let provider = libp2p_community_tor::tor_interface::legacy_tor_client::LegacyTorClient::new(
libp2p_community_tor::tor_interface::legacy_tor_client::LegacyTorClientConfig::system_from_environment().expect("Configure $TOR_... to talk to"))?;

let mut transport = libp2p_community_tor::TorInterfaceTransport::from_provider(
libp2p_community_tor::AddressConversion::IpAndDns, Arc::new(Mutex::new(provider)), None)?;

let onion_listen_address = transport.add_onion_service(&Ed25519PrivateKey::generate(), 999, None, None).unwrap();

let auth_upgrade = noise::Config::new(&keypair)?;
let multiplex_upgrade = yamux::Config::default();

let transport = transport
.boxed()
.upgrade(Version::V1)
.authenticate(auth_upgrade)
.multiplex(multiplex_upgrade)
.map(|(peer, muxer), _| (peer, StreamMuxerBox::new(muxer)))
.boxed();

Ok((transport, onion_listen_address))
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
tracing_subscriber::fmt::init();

let local_key = identity::Keypair::generate_ed25519();
let local_peer_id = PeerId::from(local_key.public());

println!("Local peer id: {local_peer_id}");

let (transport, onion_listen_address) = onion_transport(local_key).await?;

let mut swarm = SwarmBuilder::with_new_identity()
.with_tokio()
.with_other_transport(|_| transport)
.unwrap()
.with_behaviour(|_| Behaviour {
ping: libp2p::ping::Behaviour::default(),
})
.unwrap()
.build();

// Dial the peer identified by the multi-address given as the second
// command-line argument, if any.
if let Some(addr) = std::env::args().nth(1) {
let remote: Multiaddr = addr.parse()?;
swarm.dial(remote)?;
println!("Dialed {addr}")
} else {
// If we are not dialing, we need to listen
// Tell the swarm to listen on a specific onion address
swarm.listen_on(onion_listen_address).unwrap();
}

loop {
match swarm.select_next_some().await {
SwarmEvent::ConnectionEstablished {
endpoint, peer_id, ..
} => {
println!("Connection established with {peer_id} on {endpoint:?}");
}
SwarmEvent::OutgoingConnectionError { peer_id, error, .. } => {
println!("Outgoing connection error with {peer_id:?}: {error:?}");
}
SwarmEvent::NewListenAddr { address, .. } => println!("Listening on {address:?}"),
SwarmEvent::Behaviour(event) => println!("{event:?}"),
_ => {}
}
}
}

/// Our network behaviour.
#[derive(NetworkBehaviour)]
struct Behaviour {
ping: libp2p::ping::Behaviour,
}
File renamed without changes.
Loading