Skip to content

Commit 4725f46

Browse files
authored
feat(websocket,websocket-websys): Add support for /tls/ws
To keep backward compatibility the following rules were implemented: * On dialing, `/tls/ws` and `/wss` are equivalent. * On listening: * If user listens with `/tls/ws` then `/tls/ws` will be advertised. * If user listens with `/wss` then `/wss` will be advertised. Closes #2449 Closes #5509 Pull-Request: #5523.
1 parent 7b2a77e commit 4725f46

File tree

6 files changed

+289
-43
lines changed

6 files changed

+289
-43
lines changed

interop-tests/src/arch.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ pub(crate) mod wasm {
245245
.with_behaviour(behaviour_constructor)?
246246
.with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(5)))
247247
.build(),
248-
format!("/ip4/{ip}/tcp/0/wss"),
248+
format!("/ip4/{ip}/tcp/0/tls/ws"),
249249
),
250250
(Transport::Ws, Some(SecProtocol::Noise), Some(Muxer::Yamux)) => (
251251
libp2p::SwarmBuilder::with_new_identity()
@@ -262,7 +262,7 @@ pub(crate) mod wasm {
262262
.with_behaviour(behaviour_constructor)?
263263
.with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(5)))
264264
.build(),
265-
format!("/ip4/{ip}/tcp/0/wss"),
265+
format!("/ip4/{ip}/tcp/0/tls/ws"),
266266
),
267267
(Transport::WebRtcDirect, None, None) => (
268268
libp2p::SwarmBuilder::with_new_identity()

transports/websocket-websys/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
- Implement refactored `Transport`.
44
See [PR 4568](https://github.com/libp2p/rust-libp2p/pull/4568)
5+
- Add support for `/tls/ws` and keep `/wss` backward compatible.
6+
See [PR 5523](https://github.com/libp2p/rust-libp2p/pull/5523).
7+
58
## 0.3.3
69

710
- Fix use-after-free handler invocation from JS side.

transports/websocket-websys/src/lib.rs

Lines changed: 104 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,10 @@ fn extract_websocket_url(addr: &Multiaddr) -> Option<String> {
137137
_ => return None,
138138
};
139139

140-
let (scheme, wspath) = match protocols.next() {
141-
Some(Protocol::Ws(path)) => ("ws", path.into_owned()),
142-
Some(Protocol::Wss(path)) => ("wss", path.into_owned()),
140+
let (scheme, wspath) = match (protocols.next(), protocols.next()) {
141+
(Some(Protocol::Tls), Some(Protocol::Ws(path))) => ("wss", path.into_owned()),
142+
(Some(Protocol::Ws(path)), _) => ("ws", path.into_owned()),
143+
(Some(Protocol::Wss(path)), _) => ("wss", path.into_owned()),
143144
_ => return None,
144145
};
145146

@@ -453,3 +454,103 @@ impl Drop for Connection {
453454
.clear_interval_with_handle(self.inner.buffered_amount_low_interval);
454455
}
455456
}
457+
458+
#[cfg(test)]
459+
mod tests {
460+
use super::*;
461+
use libp2p_identity::PeerId;
462+
463+
#[test]
464+
fn extract_url() {
465+
let peer_id = PeerId::random();
466+
467+
// Check `/tls/ws`
468+
let addr = "/dns4/example.com/tcp/2222/tls/ws"
469+
.parse::<Multiaddr>()
470+
.unwrap();
471+
let url = extract_websocket_url(&addr).unwrap();
472+
assert_eq!(url, "wss://example.com:2222/");
473+
474+
// Check `/tls/ws` with `/p2p`
475+
let addr = format!("/dns4/example.com/tcp/2222/tls/ws/p2p/{peer_id}")
476+
.parse()
477+
.unwrap();
478+
let url = extract_websocket_url(&addr).unwrap();
479+
assert_eq!(url, "wss://example.com:2222/");
480+
481+
// Check `/tls/ws` with `/ip4`
482+
let addr = "/ip4/127.0.0.1/tcp/2222/tls/ws"
483+
.parse::<Multiaddr>()
484+
.unwrap();
485+
let url = extract_websocket_url(&addr).unwrap();
486+
assert_eq!(url, "wss://127.0.0.1:2222/");
487+
488+
// Check `/tls/ws` with `/ip6`
489+
let addr = "/ip6/::1/tcp/2222/tls/ws".parse::<Multiaddr>().unwrap();
490+
let url = extract_websocket_url(&addr).unwrap();
491+
assert_eq!(url, "wss://[::1]:2222/");
492+
493+
// Check `/wss`
494+
let addr = "/dns4/example.com/tcp/2222/wss"
495+
.parse::<Multiaddr>()
496+
.unwrap();
497+
let url = extract_websocket_url(&addr).unwrap();
498+
assert_eq!(url, "wss://example.com:2222/");
499+
500+
// Check `/wss` with `/p2p`
501+
let addr = format!("/dns4/example.com/tcp/2222/wss/p2p/{peer_id}")
502+
.parse()
503+
.unwrap();
504+
let url = extract_websocket_url(&addr).unwrap();
505+
assert_eq!(url, "wss://example.com:2222/");
506+
507+
// Check `/wss` with `/ip4`
508+
let addr = "/ip4/127.0.0.1/tcp/2222/wss".parse::<Multiaddr>().unwrap();
509+
let url = extract_websocket_url(&addr).unwrap();
510+
assert_eq!(url, "wss://127.0.0.1:2222/");
511+
512+
// Check `/wss` with `/ip6`
513+
let addr = "/ip6/::1/tcp/2222/wss".parse::<Multiaddr>().unwrap();
514+
let url = extract_websocket_url(&addr).unwrap();
515+
assert_eq!(url, "wss://[::1]:2222/");
516+
517+
// Check `/ws`
518+
let addr = "/dns4/example.com/tcp/2222/ws"
519+
.parse::<Multiaddr>()
520+
.unwrap();
521+
let url = extract_websocket_url(&addr).unwrap();
522+
assert_eq!(url, "ws://example.com:2222/");
523+
524+
// Check `/ws` with `/p2p`
525+
let addr = format!("/dns4/example.com/tcp/2222/ws/p2p/{peer_id}")
526+
.parse()
527+
.unwrap();
528+
let url = extract_websocket_url(&addr).unwrap();
529+
assert_eq!(url, "ws://example.com:2222/");
530+
531+
// Check `/ws` with `/ip4`
532+
let addr = "/ip4/127.0.0.1/tcp/2222/ws".parse::<Multiaddr>().unwrap();
533+
let url = extract_websocket_url(&addr).unwrap();
534+
assert_eq!(url, "ws://127.0.0.1:2222/");
535+
536+
// Check `/ws` with `/ip6`
537+
let addr = "/ip6/::1/tcp/2222/ws".parse::<Multiaddr>().unwrap();
538+
let url = extract_websocket_url(&addr).unwrap();
539+
assert_eq!(url, "ws://[::1]:2222/");
540+
541+
// Check `/ws` with `/ip4`
542+
let addr = "/ip4/127.0.0.1/tcp/2222/ws".parse::<Multiaddr>().unwrap();
543+
let url = extract_websocket_url(&addr).unwrap();
544+
assert_eq!(url, "ws://127.0.0.1:2222/");
545+
546+
// Check that `/tls/wss` is invalid
547+
let addr = "/ip4/127.0.0.1/tcp/2222/tls/wss"
548+
.parse::<Multiaddr>()
549+
.unwrap();
550+
assert!(extract_websocket_url(&addr).is_none());
551+
552+
// Check non-ws address
553+
let addr = "/ip4/127.0.0.1/tcp/2222".parse::<Multiaddr>().unwrap();
554+
assert!(extract_websocket_url(&addr).is_none());
555+
}
556+
}

transports/websocket/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
See [PR 4568](https://github.com/libp2p/rust-libp2p/pull/4568)
55
- Allow wss connections on IP addresses.
66
See [PR 5525](https://github.com/libp2p/rust-libp2p/pull/5525).
7+
- Add support for `/tls/ws` and keep `/wss` backward compatible.
8+
See [PR 5523](https://github.com/libp2p/rust-libp2p/pull/5523).
79

810
## 0.43.2
911

0 commit comments

Comments
 (0)