-
Notifications
You must be signed in to change notification settings - Fork 1.2k
transports/onion: Add dial-only implementation of Transport
#2899
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
Closed
+716
−9
Closed
Changes from 14 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 ca954bf
Added tests, comments and mate implementation somewhat usesable
umgefahren 59e4792
simplified, streamlined and added an example
umgefahren f69a566
simplified, streamlined and added an example
umgefahren 6cd8ce0
Added some traits
umgefahren ab0197e
Added cargo fmt changes
umgefahren 0c31c37
fixed typo
umgefahren 9380b18
implemented suggestions
umgefahren 3dabad1
implemented suggestions
umgefahren cd9ff90
Merge branch 'master' into add-onion
umgefahren 0c7298c
Addressed latest issues
umgefahren e342500
Merge branch 'add-onion' of github.com:umgefahren/rust-libp2p into ad…
umgefahren d88af2e
fixed test failure
umgefahren 0680655
Ran cargo fmt
umgefahren e04869a
Implemented suggested changes
umgefahren e7db75f
Small module doc
umgefahren c6335b5
compiles again on wasm32
umgefahren 8c20c30
Refactor multiaddress parsing to avoid clones
thomaseizinger b6b2aac
Apply suggestions from code review
umgefahren 9d2d94c
renamed stuff
umgefahren 4076338
corrected issues of previous commit an implemented all recently reque…
umgefahren bf9df0a
formatted
umgefahren 548a815
Apply suggestions from code review
umgefahren 307f19c
Added license headers and added to README.md
umgefahren 6f4bca5
Merge branch 'master' into add-onion
umgefahren 96d0bf9
Merge branch 'master' into add-onion
umgefahren 03ba6e7
addressed todos by @thomaseizinger
umgefahren e6a51c0
Remove `cargo-deny` exception
thomaseizinger 8de49ca
Remove unnecessary empty lines
thomaseizinger 1492557
Merge `use` statements into one block
thomaseizinger 402835d
Make use of link in changelog
thomaseizinger b292cd0
Consistently reference `Pin` with FQP to avoid import
thomaseizinger 85f6eb6
Update `arti` to latest version
thomaseizinger 38ba200
Sort dependencies alphabetically
thomaseizinger 077ab94
Use link reference in CHANGELOG.md
thomaseizinger d57fe41
Fix compile errors
thomaseizinger 389ff09
Merge branch 'master' into add-onion
thomaseizinger ac6b9a8
Merge branch 'master' into add-onion
umgefahren 0a89bef
Merge branch 'master' into add-onion
umgefahren bd7f8ac
Really enable all features in full
umgefahren 86a511c
Merge branch 'master' into add-onion
umgefahren 49f0c79
Merge branch 'master' into add-onion
umgefahren f834d9e
Adjusted example to match the ping example
umgefahren 1a5668b
Fix formatting
thomaseizinger 2fa5162
Merge branch 'master' into add-onion
umgefahren 1d1a219
Merge branch 'master' into add-onion
umgefahren 9bcd2be
Changed CHANGELOG.md entry
umgefahren 65bc267
Update CHANGELOG.md
umgefahren 40765c6
Updated Cargo.toml responding to the requested changes
umgefahren 1f692de
Corrected error in the feature flag
umgefahren 76ed916
Fixed issues with featues
umgefahren 9332ee3
Alphabetically sort feature list
thomaseizinger c09fa80
Alphabetically sort dependency list
thomaseizinger 285bdc0
Update transports/onion/Cargo.toml
umgefahren 5c908a6
Merge branch 'master' into add-onion
umgefahren d2c6256
Changed to alpha version.
umgefahren 9b4fce9
Merge branch 'master' into add-onion
umgefahren 62d093b
Merge branch 'master' into add-onion
mxinden 25ea701
Merge branch 'master' into add-onion
thomaseizinger 7865af7
Fixed issue with Cargo.toml
umgefahren 37f170b
Merge master into add-onion
umgefahren dd5b8b7
Added type alias for DataStream and removed custom doc(cfg( in order …
umgefahren d4c6ff4
Merge branch 'master' into add-onion
umgefahren 6771e93
Merge branch 'master' into add-onion
umgefahren d236989
Merge branch 'master' into add-onion
umgefahren 4f72d3c
Made compileable again
umgefahren File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| // Copyright 2018 Parity Technologies (UK) Ltd. | ||
| // | ||
| // 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 example | ||
| //! | ||
| //! See ../src/tutorial.rs for a step-by-step guide building the example below. | ||
| //! | ||
| //! In the first terminal window, run: | ||
| //! | ||
| //! ```sh | ||
| //! cargo run --example ping-onion | ||
| //! ``` | ||
| //! | ||
| //! It will print the PeerId and the listening addresses, e.g. `Listening on | ||
| //! "/ip4/0.0.0.0/tcp/24915"` | ||
| //! | ||
| //! In the second terminal window, start a new instance of the example with: | ||
| //! | ||
| //! ```sh | ||
| //! cargo run --example ping-onion -- /ip4/127.0.0.1/tcp/24915 | ||
|
thomaseizinger marked this conversation as resolved.
Outdated
|
||
| //! ``` | ||
| //! | ||
| //! The two nodes establish a connection, negotiate the ping protocol | ||
| //! and begin pinging each other. | ||
|
thomaseizinger marked this conversation as resolved.
Outdated
|
||
|
|
||
| use futures::prelude::*; | ||
| use libp2p::swarm::{Swarm, SwarmEvent}; | ||
| use libp2p::{ | ||
| core::upgrade, dns, identity, mplex, noise, onion, ping, tcp, yamux, Multiaddr, 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 = { | ||
| let onion = onion::OnionClient::from_builder(onion::OnionClient::builder(), false)?; | ||
| println!("bootstrapping..."); | ||
| onion.bootstrap().await?; | ||
| println!("bootstrapped!"); | ||
| onion | ||
| }; | ||
|
umgefahren marked this conversation as resolved.
Outdated
|
||
|
|
||
| let noise_keys = noise::Keypair::<noise::X25519Spec>::new() | ||
| .into_authentic(&keypair) | ||
| .expect("Signing libp2p-noise static DH keypair failed."); | ||
| Ok(transport | ||
| .upgrade(upgrade::Version::V1) | ||
| .authenticate(noise::NoiseConfig::xx(noise_keys).into_authenticated()) | ||
|
umgefahren marked this conversation as resolved.
Outdated
|
||
| .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); | ||
|
|
||
| // create a transport | ||
| let transport = onion_transport(local_key).await?; | ||
|
|
||
| // Create a ping network behaviour. | ||
| // | ||
| // For illustrative purposes, the ping protocol is configured to | ||
| // keep the connection alive, so a continuous sequence of pings | ||
| // can be observed. | ||
| let behaviour = ping::Behaviour::new(ping::Config::new().with_keep_alive(true)); | ||
|
|
||
| let mut swarm = Swarm::new(transport, behaviour, 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") | ||
| } | ||
| SwarmEvent::Behaviour(event) => println!("{:?}", event), | ||
| _ => {} | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| [package] | ||
| name = "libp2p-onion" | ||
| version = "0.1.0" | ||
|
umgefahren marked this conversation as resolved.
Outdated
|
||
| edition = "2021" | ||
| license = "MIT" | ||
| resolver = "2" | ||
|
|
||
|
|
||
|
thomaseizinger marked this conversation as resolved.
Outdated
|
||
| [dependencies] | ||
| libp2p-core = { version = "0.36.0", path = "../../core", default-features = false } | ||
| futures = "0.3" | ||
| arti-client = { version = "0.6", default-features = false } | ||
| thiserror = "1" | ||
| tor-rtcompat = "0.6" | ||
| tokio-crate = { package = "tokio", version = "1", optional = true, default-features = false } | ||
| async-std-crate = { package = "async-std", version = "1", optional = true, default-features = false } | ||
|
|
||
| [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] | ||
| default = ["async-std", "native-tls"] | ||
| tokio = ["arti-client/tokio", "dep:tokio-crate"] | ||
| async-std = ["arti-client/async-std"] | ||
|
thomaseizinger marked this conversation as resolved.
Outdated
|
||
| 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.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| use std::net::{IpAddr, SocketAddr}; | ||
|
|
||
| use arti_client::{IntoTorAddr, TorAddr, TorAddrError}; | ||
| use libp2p_core::{multiaddr::Protocol, Multiaddr}; | ||
|
|
||
| pub fn dangerous_extract_tor_address(multiaddr: &Multiaddr) -> Result<TorAddr, TorAddrError> { | ||
| let socket_addr = try_extract_socket_addr(&mut multiaddr.clone())?; | ||
|
thomaseizinger marked this conversation as resolved.
Outdated
|
||
| TorAddr::dangerously_from(socket_addr) | ||
| } | ||
|
|
||
| pub fn safe_extract_tor_address(multiaddr: &mut Multiaddr) -> Result<TorAddr, TorAddrError> { | ||
|
thomaseizinger marked this conversation as resolved.
Outdated
|
||
| let tcp_variant = multiaddr.pop().ok_or(TorAddrError::NoPort)?; | ||
| let tcp_port = match tcp_variant { | ||
| Protocol::Tcp(p) => p, | ||
| _ => return Err(TorAddrError::NoPort), | ||
| }; | ||
| let dns_variant = multiaddr.pop().ok_or(TorAddrError::InvalidHostname)?; | ||
| let dns_addr = match dns_variant { | ||
| Protocol::Dns(dns) => dns, | ||
| Protocol::Dns4(dns) => dns, | ||
| Protocol::Dns6(dns) => dns, | ||
| _ => return Err(TorAddrError::InvalidHostname), | ||
| }; | ||
| let address_tuple = (dns_addr.as_ref(), tcp_port); | ||
| address_tuple.into_tor_addr() | ||
| } | ||
|
|
||
| fn try_extract_socket_addr(multiaddr: &mut Multiaddr) -> Result<SocketAddr, TorAddrError> { | ||
| let tcp_variant = multiaddr.pop().ok_or(TorAddrError::NoPort)?; | ||
| let port = match tcp_variant { | ||
| Protocol::Tcp(p) => p, | ||
| _ => return Err(TorAddrError::NoPort), | ||
| }; | ||
| let ip_variant = multiaddr.pop().ok_or(TorAddrError::InvalidHostname)?; | ||
| let ip = match ip_variant { | ||
| Protocol::Ip4(ip4) => IpAddr::V4(ip4), | ||
| Protocol::Ip6(ip6) => IpAddr::V6(ip6), | ||
| _ => return Err(TorAddrError::InvalidHostname), | ||
| }; | ||
| Ok(SocketAddr::new(ip, port)) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use std::{borrow::Cow, net::Ipv4Addr, ops::Not}; | ||
|
|
||
| use libp2p_core::{multiaddr::multiaddr, Multiaddr}; | ||
|
|
||
| use super::safe_extract_tor_address; | ||
|
|
||
| #[test] | ||
| fn extract_correct_address() { | ||
| let dns_test_addr = multiaddr!(Dns(Cow::Borrowed("ip.tld")), Tcp(10u16)); | ||
|
umgefahren marked this conversation as resolved.
Outdated
|
||
| let dns_test_addr_res = safe_extract_tor_address(&mut dns_test_addr.clone()); | ||
| assert!(dns_test_addr_res.is_ok()); | ||
| assert!(dns_test_addr_res.unwrap().is_ip_address().not()); | ||
|
|
||
| let dns_4_test_addr = multiaddr!(Dns4(Cow::Borrowed("dns.ip4.tld")), Tcp(11u16)); | ||
| let dns_4_test_addr_res = safe_extract_tor_address(&mut dns_4_test_addr.clone()); | ||
| assert!(dns_4_test_addr_res.is_ok()); | ||
| assert!(dns_4_test_addr_res.unwrap().is_ip_address().not()); | ||
|
|
||
| let dns_6_test_addr = multiaddr!(Dns6(Cow::Borrowed("dns.ip6.tld")), Tcp(12u16)); | ||
| let dns_6_test_addr_res = safe_extract_tor_address(&mut dns_6_test_addr.clone()); | ||
| assert!(dns_6_test_addr_res.is_ok()); | ||
| assert!(dns_6_test_addr_res.unwrap().is_ip_address().not()); | ||
|
umgefahren marked this conversation as resolved.
Outdated
|
||
|
|
||
| let addresses = [ | ||
| dns_test_addr, | ||
| dns_4_test_addr, | ||
| dns_6_test_addr, | ||
| Multiaddr::empty(), | ||
| ]; | ||
|
|
||
| for (idx, a) in addresses.iter().enumerate() { | ||
| for (idy, b) in addresses.iter().enumerate() { | ||
| if idx == idy { | ||
| continue; | ||
| } | ||
| let mut new = Multiaddr::empty(); | ||
| a.iter().for_each(|e| { | ||
| new.push(e); | ||
| }); | ||
| b.iter().for_each(|e| { | ||
| new.push(e); | ||
| }); | ||
| let new_addr_res = safe_extract_tor_address(&mut new); | ||
| assert!(new_addr_res.is_ok()); | ||
| } | ||
| } | ||
|
umgefahren marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| #[test] | ||
| fn detect_incorrect_address() { | ||
| let without_dns = multiaddr!(Tcp(10u16), Udp(12u16)); | ||
| let without_dns_res = safe_extract_tor_address(&mut without_dns.clone()); | ||
| assert_eq!(without_dns_res, Err(arti_client::TorAddrError::NoPort)); | ||
|
|
||
| let host = Cow::Borrowed("ip.tld"); | ||
| let without_port = multiaddr!(Dns(host.clone()), Dns4(host.clone()), Dns6(host.clone())); | ||
| let without_port_res = safe_extract_tor_address(&mut without_port.clone()); | ||
| assert_eq!(without_port_res, Err(arti_client::TorAddrError::NoPort)); | ||
| let with_ip_addr = multiaddr!(Tcp(10u16), Ip4("1.1.1.1".parse::<Ipv4Addr>().unwrap())); | ||
| let with_ip_addr_res = safe_extract_tor_address(&mut with_ip_addr.clone()); | ||
| assert_eq!(with_ip_addr_res, Err(arti_client::TorAddrError::NoPort)); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.