From 7141beff7fb5e7753a9e6cb5ae31ff6c8091b14f Mon Sep 17 00:00:00 2001 From: Max Inden Date: Fri, 11 Aug 2023 10:06:50 +0200 Subject: [PATCH 1/3] feat: add (secure) websocket support --- Cargo.lock | 100 ++++++++++++++++++++++++++++++++++++++++++++++++---- Cargo.toml | 2 +- src/main.rs | 24 +++++++++++-- 3 files changed, 116 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 28b251b..31dea1d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1190,6 +1190,17 @@ dependencies = [ "syn 2.0.28", ] +[[package]] +name = "futures-rustls" +version = "0.22.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2411eed028cdf8c8034eaf21f9915f956b6c3abec4d4c7949ee67f0721127bd" +dependencies = [ + "futures-io", + "rustls 0.20.8", + "webpki", +] + [[package]] name = "futures-rustls" version = "0.24.0" @@ -1197,7 +1208,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "35bd3cf68c183738046838e300353e4716c674dc5e56890de4826801a6622a28" dependencies = [ "futures-io", - "rustls", + "rustls 0.21.5", ] [[package]] @@ -1634,6 +1645,7 @@ dependencies = [ "libp2p-relay", "libp2p-swarm", "libp2p-tcp", + "libp2p-websocket", "libp2p-yamux", "multiaddr", "pin-project", @@ -1882,7 +1894,7 @@ dependencies = [ "parking_lot", "quinn", "rand 0.8.5", - "rustls", + "rustls 0.21.5", "thiserror", ] @@ -2003,18 +2015,37 @@ version = "0.2.0" source = "git+https://github.com/libp2p/rust-libp2p#6478fafc70edfd9857daaacb9d8086f41a392d89" dependencies = [ "futures", - "futures-rustls", + "futures-rustls 0.24.0", "libp2p-core", "libp2p-identity", "rcgen", "ring", - "rustls", + "rustls 0.21.5", "thiserror", "webpki", "x509-parser", "yasna", ] +[[package]] +name = "libp2p-websocket" +version = "0.42.0" +source = "git+https://github.com/libp2p/rust-libp2p#6478fafc70edfd9857daaacb9d8086f41a392d89" +dependencies = [ + "either", + "futures", + "futures-rustls 0.22.2", + "libp2p-core", + "libp2p-identity", + "log", + "parking_lot", + "quicksink", + "rw-stream-sink", + "soketto", + "url", + "webpki-roots", +] + [[package]] name = "libp2p-yamux" version = "0.44.1" @@ -2603,6 +2634,17 @@ dependencies = [ "unsigned-varint", ] +[[package]] +name = "quicksink" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77de3c815e5a160b1539c6592796801df2043ae35e123b46d73380cfa57af858" +dependencies = [ + "futures-core", + "futures-sink", + "pin-project-lite 0.1.12", +] + [[package]] name = "quinn" version = "0.10.1" @@ -2617,7 +2659,7 @@ dependencies = [ "quinn-proto", "quinn-udp", "rustc-hash", - "rustls", + "rustls 0.21.5", "thiserror", "tokio", "tracing", @@ -2633,7 +2675,7 @@ dependencies = [ "rand 0.8.5", "ring", "rustc-hash", - "rustls", + "rustls 0.21.5", "slab", "thiserror", "tinyvec", @@ -2895,6 +2937,18 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "rustls" +version = "0.20.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fff78fc74d175294f4e83b28343315ffcfb114b156f0185e9741cb5570f50e2f" +dependencies = [ + "log", + "ring", + "sct", + "webpki", +] + [[package]] name = "rustls" version = "0.21.5" @@ -3033,6 +3087,19 @@ dependencies = [ "serde", ] +[[package]] +name = "sha-1" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99cd6713db3cf16b6c84e06321e049a9b9f699826e16096d23bbcc44d15d51a6" +dependencies = [ + "block-buffer 0.9.0", + "cfg-if 1.0.0", + "cpufeatures", + "digest 0.9.0", + "opaque-debug", +] + [[package]] name = "sha1" version = "0.6.1" @@ -3175,6 +3242,21 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "soketto" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d1c5305e39e09653383c2c7244f2f78b3bcae37cf50c64cb4789c9f5096ec2" +dependencies = [ + "base64 0.13.1", + "bytes", + "futures", + "httparse", + "log", + "rand 0.8.5", + "sha-1", +] + [[package]] name = "spin" version = "0.5.2" @@ -3911,6 +3993,12 @@ dependencies = [ "untrusted", ] +[[package]] +name = "webpki-roots" +version = "0.25.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14247bb57be4f377dfb94c72830b8ce8fc6beac03cf4bf7b9732eadd414123fc" + [[package]] name = "widestring" version = "1.0.2" diff --git a/Cargo.toml b/Cargo.toml index b1ec66c..a36019c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ base64 = "0.21" env_logger = "0.10.0" futures = "0.3.27" futures-timer = "3" -libp2p = { git = "https://github.com/libp2p/rust-libp2p", version = "0.52.1", default-features = false, features = ["autonat", "dns", "async-std", "noise", "tcp", "yamux", "identify", "kad", "ping", "relay", "metrics", "rsa", "macros"] } +libp2p = { git = "https://github.com/libp2p/rust-libp2p", version = "0.52.1", default-features = false, features = ["autonat", "dns", "async-std", "noise", "tcp", "yamux", "identify", "kad", "ping", "relay", "metrics", "rsa", "macros", "websocket"] } libp2p-quic = { git = "https://github.com/libp2p/rust-libp2p", version = "0.9.0-alpha", default-features = false, features = ["async-std"] } log = "0.4" prometheus-client = "0.21.2" diff --git a/src/main.rs b/src/main.rs index f45e867..896445b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ use futures::future::Either; use futures::stream::StreamExt; use futures_timer::Delay; use libp2p::core::muxing::StreamMuxerBox; +use libp2p::core::transport::OrTransport; use libp2p::core::upgrade; use libp2p::dns; use libp2p::identify; @@ -95,7 +96,7 @@ fn main() -> Result<(), Box> { libp2p_quic::async_std::Transport::new(config) }; - block_on(dns::DnsConfig::system( + let tcp_and_quic = block_on(dns::DnsConfig::system( libp2p::core::transport::OrTransport::new(quic_transport, tcp_transport), )) .unwrap() @@ -103,8 +104,25 @@ fn main() -> Result<(), Box> { Either::Left((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)), Either::Right((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)), }) - .map_err(|err| io::Error::new(io::ErrorKind::Other, err)) - .boxed() + .map_err(|err| io::Error::new(io::ErrorKind::Other, err)); + + let websocket = libp2p::websocket::WsConfig::new( + block_on(libp2p::dns::DnsConfig::system( + libp2p::tcp::async_io::Transport::new(libp2p::tcp::Config::default()), + )) + .unwrap(), + ) + .upgrade(upgrade::Version::V1) + .authenticate(noise::Config::new(&local_keypair)?) + .multiplex(yamux::Config::default()) + .timeout(Duration::from_secs(20)); + + OrTransport::new(websocket, tcp_and_quic) + .map(|either_output, _| match either_output { + Either::Left((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)), + Either::Right((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)), + }) + .boxed() }; let behaviour = behaviour::Behaviour::new( From 47b28d04619337825e560993f851373f11438300 Mon Sep 17 00:00:00 2001 From: Max Inden Date: Fri, 11 Aug 2023 10:10:44 +0200 Subject: [PATCH 2/3] chore: bump version and add changelog entry --- CHANGELOG.md | 7 +++++++ Cargo.lock | 2 +- Cargo.toml | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a09b1d3..5c3fc72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.12.1] +### Added +- Support websocket. + See [PR 270]. + +[PR 270]: https://github.com/mxinden/rust-libp2p-server/pull/270 + ## [0.8.0] ### Changed - Remove mplex support. diff --git a/Cargo.lock b/Cargo.lock index 31dea1d..a54997d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1940,7 +1940,7 @@ dependencies = [ [[package]] name = "libp2p-server" -version = "0.12.0" +version = "0.12.1" dependencies = [ "async-std", "base64 0.21.2", diff --git a/Cargo.toml b/Cargo.toml index a36019c..69c5ff6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "libp2p-server" -version = "0.12.0" +version = "0.12.1" authors = ["Max Inden "] edition = "2021" description = "A rust-libp2p server binary." From 38991d60a9716e03a5aa61d30147ca68d252ff4b Mon Sep 17 00:00:00 2001 From: Max Inden Date: Fri, 11 Aug 2023 10:21:54 +0200 Subject: [PATCH 3/3] chore: add `- unreleased` --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c3fc72..396c9e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [0.12.1] +## [0.12.1] - unreleased ### Added - Support websocket. See [PR 270].