Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

feat: add (secure) websocket support #270

Merged
merged 3 commits into from
Aug 11, 2023
Merged
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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] - unreleased
### Added
- Support websocket.
See [PR 270].

[PR 270]: https://github.com/mxinden/rust-libp2p-server/pull/270

## [0.8.0]
### Changed
- Remove mplex support.
Expand Down
102 changes: 95 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "libp2p-server"
version = "0.12.0"
version = "0.12.1"
authors = ["Max Inden <[email protected]>"]
edition = "2021"
description = "A rust-libp2p server binary."
Expand All @@ -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"
Expand Down
24 changes: 21 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -95,16 +96,33 @@ fn main() -> Result<(), Box<dyn Error>> {
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()
.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)),
})
.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(
Expand Down