Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transports/onion: Add dial-only implementation of Transport #2899

Closed
wants to merge 66 commits into from
Closed
Show file tree
Hide file tree
Changes from 47 commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
ecd352b
Initial commit on Onion transport for libp2p
umgefahren Sep 13, 2022
ca954bf
Added tests, comments and mate implementation somewhat usesable
umgefahren Sep 13, 2022
59e4792
simplified, streamlined and added an example
umgefahren Sep 13, 2022
f69a566
simplified, streamlined and added an example
umgefahren Sep 13, 2022
6cd8ce0
Added some traits
umgefahren Sep 13, 2022
ab0197e
Added cargo fmt changes
umgefahren Sep 13, 2022
0c31c37
fixed typo
umgefahren Sep 13, 2022
9380b18
implemented suggestions
umgefahren Sep 14, 2022
3dabad1
implemented suggestions
umgefahren Sep 14, 2022
cd9ff90
Merge branch 'master' into add-onion
umgefahren Sep 19, 2022
0c7298c
Addressed latest issues
umgefahren Sep 19, 2022
e342500
Merge branch 'add-onion' of github.com:umgefahren/rust-libp2p into ad…
umgefahren Sep 19, 2022
d88af2e
fixed test failure
umgefahren Sep 19, 2022
0680655
Ran cargo fmt
umgefahren Sep 19, 2022
e04869a
Implemented suggested changes
umgefahren Sep 20, 2022
e7db75f
Small module doc
umgefahren Sep 20, 2022
c6335b5
compiles again on wasm32
umgefahren Sep 20, 2022
8c20c30
Refactor multiaddress parsing to avoid clones
thomaseizinger Sep 21, 2022
b6b2aac
Apply suggestions from code review
umgefahren Sep 21, 2022
9d2d94c
renamed stuff
umgefahren Sep 21, 2022
4076338
corrected issues of previous commit an implemented all recently reque…
umgefahren Sep 21, 2022
bf9df0a
formatted
umgefahren Sep 21, 2022
548a815
Apply suggestions from code review
umgefahren Sep 28, 2022
307f19c
Added license headers and added to README.md
umgefahren Sep 28, 2022
6f4bca5
Merge branch 'master' into add-onion
umgefahren Sep 28, 2022
96d0bf9
Merge branch 'master' into add-onion
umgefahren Oct 3, 2022
03ba6e7
addressed todos by @thomaseizinger
umgefahren Oct 3, 2022
e6a51c0
Remove `cargo-deny` exception
thomaseizinger Oct 4, 2022
8de49ca
Remove unnecessary empty lines
thomaseizinger Oct 4, 2022
1492557
Merge `use` statements into one block
thomaseizinger Oct 4, 2022
402835d
Make use of link in changelog
thomaseizinger Oct 4, 2022
b292cd0
Consistently reference `Pin` with FQP to avoid import
thomaseizinger Oct 4, 2022
85f6eb6
Update `arti` to latest version
thomaseizinger Oct 4, 2022
38ba200
Sort dependencies alphabetically
thomaseizinger Oct 4, 2022
077ab94
Use link reference in CHANGELOG.md
thomaseizinger Oct 4, 2022
d57fe41
Fix compile errors
thomaseizinger Oct 4, 2022
389ff09
Merge branch 'master' into add-onion
thomaseizinger Oct 4, 2022
ac6b9a8
Merge branch 'master' into add-onion
umgefahren Oct 4, 2022
0a89bef
Merge branch 'master' into add-onion
umgefahren Oct 4, 2022
bd7f8ac
Really enable all features in full
umgefahren Oct 4, 2022
86a511c
Merge branch 'master' into add-onion
umgefahren Oct 5, 2022
49f0c79
Merge branch 'master' into add-onion
umgefahren Oct 10, 2022
f834d9e
Adjusted example to match the ping example
umgefahren Oct 10, 2022
1a5668b
Fix formatting
thomaseizinger Oct 11, 2022
2fa5162
Merge branch 'master' into add-onion
umgefahren Oct 14, 2022
1d1a219
Merge branch 'master' into add-onion
umgefahren Oct 18, 2022
9bcd2be
Changed CHANGELOG.md entry
umgefahren Oct 18, 2022
65bc267
Update CHANGELOG.md
umgefahren Oct 19, 2022
40765c6
Updated Cargo.toml responding to the requested changes
umgefahren Oct 19, 2022
1f692de
Corrected error in the feature flag
umgefahren Oct 19, 2022
76ed916
Fixed issues with featues
umgefahren Oct 19, 2022
9332ee3
Alphabetically sort feature list
thomaseizinger Oct 19, 2022
c09fa80
Alphabetically sort dependency list
thomaseizinger Oct 19, 2022
285bdc0
Update transports/onion/Cargo.toml
umgefahren Oct 20, 2022
5c908a6
Merge branch 'master' into add-onion
umgefahren Oct 20, 2022
d2c6256
Changed to alpha version.
umgefahren Oct 20, 2022
9b4fce9
Merge branch 'master' into add-onion
umgefahren Oct 21, 2022
62d093b
Merge branch 'master' into add-onion
mxinden Oct 21, 2022
25ea701
Merge branch 'master' into add-onion
thomaseizinger Oct 24, 2022
7865af7
Fixed issue with Cargo.toml
umgefahren Oct 25, 2022
37f170b
Merge master into add-onion
umgefahren Oct 25, 2022
dd5b8b7
Added type alias for DataStream and removed custom doc(cfg( in order …
umgefahren Oct 25, 2022
d4c6ff4
Merge branch 'master' into add-onion
umgefahren Oct 31, 2022
6771e93
Merge branch 'master' into add-onion
umgefahren Nov 9, 2022
d236989
Merge branch 'master' into add-onion
umgefahren Nov 18, 2022
4f72d3c
Made compileable again
umgefahren Nov 22, 2022
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
- [`libp2p-uds` CHANGELOG](transports/uds/CHANGELOG.md)
- [`libp2p-wasm-ext` CHANGELOG](transports/wasm-ext/CHANGELOG.md)
- [`libp2p-websocket` CHANGELOG](transports/websocket/CHANGELOG.md)
- [`libp2p-onion` CHANGELOG](transports/onion/CHANGELOG.md)

## Multiplexers

Expand All @@ -43,11 +44,18 @@

# `libp2p` facade crate

# 0.50.0 [unreleased]

- New `libp2p-onion` crate. See [PR 2899].

[PR 2899]: https://github.com/libp2p/rust-libp2p/pull/2899

# 0.49.0

- Remove default features. You need to enable required features explicitly now. As a quick workaround, you may want to use the
new `full` feature which activates all features. See [PR 2918].


umgefahren marked this conversation as resolved.
Show resolved Hide resolved
- Introduce `tokio` and `async-std` features and deprecate the following ones:
- `tcp-tokio` in favor of `tcp` + `tokio`
- `mdns-tokio` in favor of `mdns` + `tokio`
Expand Down
19 changes: 17 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ full = [
"wasm-ext-websocket",
"websocket",
"yamux",
"onion-async-std",
"onion-tokio",
"onion-native-tls",
"onion-rustls",
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
]
autonat = ["dep:libp2p-autonat"]
dcutr = ["dep:libp2p-dcutr", "libp2p-metrics?/dcutr"]
Expand Down Expand Up @@ -85,7 +89,11 @@ secp256k1 = ["libp2p-core/secp256k1"]
rsa = ["libp2p-core/rsa"]
ecdsa = ["libp2p-core/ecdsa"]
serde = ["libp2p-core/serde", "libp2p-kad?/serde", "libp2p-gossipsub?/serde"]
tokio = ["libp2p-mdns?/tokio", "libp2p-tcp?/tokio", "libp2p-dns?/tokio"]
onion-async-std = ["dep:libp2p-onion", "libp2p-onion?/async-std"]
onion-tokio = ["dep:libp2p-onion", "libp2p-onion?/tokio"]
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
onion-native-tls = ["dep:libp2p-onion", "libp2p-onion?/native-tls"]
onion-rustls = ["dep:libp2p-onion", "libp2p-onion?/rustls"]
tokio = ["libp2p-mdns?/tokio", "libp2p-tcp?/tokio", "libp2p-dns?/tokio", "libp2p-onion?/tokio"]
async-std = ["libp2p-mdns?/async-io", "libp2p-tcp?/async-io", "libp2p-dns?/async-std"]
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved

[package.metadata.docs.rs]
Expand Down Expand Up @@ -130,6 +138,7 @@ libp2p-dns = { version = "0.37.0", path = "transports/dns", optional = true }
libp2p-mdns = { version = "0.41.0", path = "protocols/mdns", optional = true }
libp2p-tcp = { version = "0.37.0", path = "transports/tcp", optional = true }
libp2p-websocket = { version = "0.39.0", path = "transports/websocket", optional = true }
libp2p-onion = { version = "0.1.0", path = "transports/onion", default-features = false, optional = true }

[target.'cfg(not(target_os = "unknown"))'.dependencies]
libp2p-gossipsub = { version = "0.42.1", path = "protocols/gossipsub", optional = true }
Expand Down Expand Up @@ -174,7 +183,8 @@ members = [
"transports/tcp",
"transports/uds",
"transports/websocket",
"transports/wasm-ext"
"transports/wasm-ext",
"transports/onion"
]

[[example]]
Expand All @@ -197,6 +207,10 @@ required-features = ["full"]
name = "ipfs-private"
required-features = ["full"]

[[example]]
name = "ping-onion"
required-features = ["full"]

[[example]]
name = "ipfs-kad"
required-features = ["full"]
Expand All @@ -212,3 +226,4 @@ required-features = ["full"]
[[example]]
name = "distributed-key-value-store"
required-features = ["full"]

7 changes: 7 additions & 0 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ exceptions = [
{ allow = ["ISC", "MIT", "OpenSSL"], name = "ring" },
# libp2p is not re-distributing unicode tables data by itself
{ allow = ["MIT", "Apache-2.0", "Unicode-DFS-2016"], name = "unicode-ident" },
# libp2p is not re-distributing unicode tables data by iteself
{ allow = ["Unicode-DFS-2016"], name = "tinystr"},
]

# Some crates don't have (easily) machine readable licensing information,
Expand All @@ -88,6 +90,11 @@ name = "ring"
expression = "ISC AND MIT AND OpenSSL"
license-files = [{ path = "LICENSE", hash = 0xbd0eed23 }]

[[licenses.clarify]]
name = "bounded-vec-deque"
expression = "GPL-3.0 OR BSD-3-Clause"
license-files = [{ path = "LICENSE", hash = 0x40145f22 }]

[licenses.private]
# If true, ignores workspace crates that aren't published, or are only
# published to private registries
Expand Down
6 changes: 6 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ A set of examples showcasing how to use rust-libp2p.

Tutorial on how to overcome firewalls and NATs with libp2p’s hole punching mechanism.

## Special transports

- [Ping over Onion](ping-onion.rs)

Client counterpart to normal ping, that routes traffic over Tor.

## Integration into a larger application

- [File sharing application](./file-sharing.rs)
Expand Down
120 changes: 120 additions & 0 deletions examples/ping-onion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Copyright 2022 Hannes Furmans
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
//
// 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::prelude::*;
use libp2p::onion::AddressConversion;
use libp2p::swarm::{keep_alive, Swarm, SwarmEvent};
use libp2p::{
core::upgrade, identity, mplex, noise, onion, ping, yamux, Multiaddr, NetworkBehaviour, PeerId,
Transport,
};
use std::error::Error;

async fn onion_transport(
keypair: identity::Keypair,
) -> Result<
libp2p_core::transport::Boxed<(PeerId, libp2p_core::muxing::StreamMuxerBox)>,
Box<dyn Error>,
> {
use std::time::Duration;

let transport = onion::AsyncStdNativeTlsOnionTransport::bootstrapped()
.await?
.with_address_conversion(AddressConversion::IpAndDns);
Ok(transport
.upgrade(upgrade::Version::V1)
.authenticate(
noise::NoiseAuthenticated::xx(&keypair)
.expect("Signing libp2p-noise static DH keypair failed."),
)
.multiplex(upgrade::SelectUpgrade::new(
yamux::YamuxConfig::default(),
mplex::MplexConfig::default(),
))
.timeout(Duration::from_secs(20))
.boxed())
}

#[async_std::main]
async fn main() -> Result<(), Box<dyn Error>> {
let addr = std::env::args().nth(1).expect("no multiaddr given");
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_transport(local_key).await?;

let mut swarm = Swarm::new(transport, Behaviour::default(), local_peer_id);

// Dial the peer identified by the multi-address given as the second
// command-line argument, if any.
let remote: Multiaddr = addr.parse()?;
swarm.dial(remote)?;
println!("Dialed {}", addr);

loop {
match swarm.select_next_some().await {
SwarmEvent::ConnectionEstablished { endpoint, .. } => {
let endpoint_addr = endpoint.get_remote_address();
println!("Connection established to {:?}", endpoint_addr);
}
SwarmEvent::OutgoingConnectionError { error, .. } => {
println!("Error establishing outgoing connection: {:?}", error)
}
SwarmEvent::Behaviour(event) => println!("{:?}", event),
_ => {}
}
}
}

/// Our network behaviour.
///
/// For illustrative purposes, this includes the [`KeepAlive`](behaviour::KeepAlive) behaviour so a continuous sequence of
/// pings can be observed.
#[derive(NetworkBehaviour, Default)]
struct Behaviour {
keep_alive: keep_alive::Behaviour,
ping: ping::Behaviour,
}
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ pub use libp2p_mplex as mplex;
#[cfg_attr(docsrs, doc(cfg(feature = "noise")))]
#[doc(inline)]
pub use libp2p_noise as noise;
#[cfg(any(feature = "onion-async-std", feature = "onion-tokio"))]
#[cfg_attr(docsrs, doc(cfg(feature = "onion")))]
#[cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))]
#[doc(inline)]
pub use libp2p_onion as onion;
#[cfg(feature = "ping")]
#[cfg_attr(docsrs, doc(cfg(feature = "ping")))]
#[doc(inline)]
Expand Down
6 changes: 6 additions & 0 deletions transports/onion/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# 0.37.0 - [unreleased]

- Initial Implementation. See [PR 2899].

[PR 2899]: https://github.com/libp2p/rust-libp2p/pull/2899

30 changes: 30 additions & 0 deletions transports/onion/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
name = "libp2p-onion"
version = "0.1.0"
umgefahren marked this conversation as resolved.
Show resolved Hide resolved
edition = "2021"
license = "MIT"
resolver = "2"

[dependencies]
arti-client = { version = "0.7", default-features = false }
async-std-crate = { package = "async-std", version = "1", optional = true, default-features = false }
futures = "0.3"
libp2p-core = { version = "0.37", path = "../../core" }
thiserror = "1"
tokio-crate = { package = "tokio", version = "1", optional = true, default-features = false }
tor-rtcompat = "0.7"

[dev-dependencies]
libp2p = { version = "0.49", path = "../../" }
tokio-crate = { package = "tokio", version = "1", features = ["rt", "macros"] }
async-std-crate = { package = "async-std", version = "1", features = ["attributes"] }

[features]
tokio = ["arti-client/tokio", "dep:tokio-crate"]
async-std = ["arti-client/async-std", "dep:async-std-crate"]
native-tls = ["arti-client/native-tls"]
rustls = ["arti-client/rustls"]

[package.metadata.docs.rs]
rustdoc-args = ["--cfg", "docsrs"]
rustc-args = ["--cfg", "docsrs"]
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
Loading