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

Commit 4b54e4b

Browse files
authored
Merge pull request #270 from mxinden/wss
feat: add (secure) websocket support
2 parents 221a7ba + 38991d6 commit 4b54e4b

File tree

4 files changed

+125
-12
lines changed

4 files changed

+125
-12
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [0.12.1] - unreleased
8+
### Added
9+
- Support websocket.
10+
See [PR 270].
11+
12+
[PR 270]: https://github.com/mxinden/rust-libp2p-server/pull/270
13+
714
## [0.8.0]
815
### Changed
916
- Remove mplex support.

Cargo.lock

Lines changed: 95 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "libp2p-server"
3-
version = "0.12.0"
3+
version = "0.12.1"
44
authors = ["Max Inden <mail@max-inden.de>"]
55
edition = "2021"
66
description = "A rust-libp2p server binary."
@@ -13,7 +13,7 @@ base64 = "0.21"
1313
env_logger = "0.10.0"
1414
futures = "0.3.27"
1515
futures-timer = "3"
16-
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"] }
16+
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"] }
1717
libp2p-quic = { git = "https://github.com/libp2p/rust-libp2p", version = "0.9.0-alpha", default-features = false, features = ["async-std"] }
1818
log = "0.4"
1919
prometheus-client = "0.21.2"

src/main.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use futures::future::Either;
44
use futures::stream::StreamExt;
55
use futures_timer::Delay;
66
use libp2p::core::muxing::StreamMuxerBox;
7+
use libp2p::core::transport::OrTransport;
78
use libp2p::core::upgrade;
89
use libp2p::dns;
910
use libp2p::identify;
@@ -95,16 +96,33 @@ fn main() -> Result<(), Box<dyn Error>> {
9596
libp2p_quic::async_std::Transport::new(config)
9697
};
9798

98-
block_on(dns::DnsConfig::system(
99+
let tcp_and_quic = block_on(dns::DnsConfig::system(
99100
libp2p::core::transport::OrTransport::new(quic_transport, tcp_transport),
100101
))
101102
.unwrap()
102103
.map(|either_output, _| match either_output {
103104
Either::Left((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)),
104105
Either::Right((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)),
105106
})
106-
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))
107-
.boxed()
107+
.map_err(|err| io::Error::new(io::ErrorKind::Other, err));
108+
109+
let websocket = libp2p::websocket::WsConfig::new(
110+
block_on(libp2p::dns::DnsConfig::system(
111+
libp2p::tcp::async_io::Transport::new(libp2p::tcp::Config::default()),
112+
))
113+
.unwrap(),
114+
)
115+
.upgrade(upgrade::Version::V1)
116+
.authenticate(noise::Config::new(&local_keypair)?)
117+
.multiplex(yamux::Config::default())
118+
.timeout(Duration::from_secs(20));
119+
120+
OrTransport::new(websocket, tcp_and_quic)
121+
.map(|either_output, _| match either_output {
122+
Either::Left((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)),
123+
Either::Right((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)),
124+
})
125+
.boxed()
108126
};
109127

110128
let behaviour = behaviour::Behaviour::new(

0 commit comments

Comments
 (0)