Skip to content

Commit

Permalink
feat(websocket,websocket-websys): Add support for /tls/ws
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
oblique authored Aug 8, 2024
1 parent 7b2a77e commit 4725f46
Show file tree
Hide file tree
Showing 6 changed files with 289 additions and 43 deletions.
4 changes: 2 additions & 2 deletions interop-tests/src/arch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ pub(crate) mod wasm {
.with_behaviour(behaviour_constructor)?
.with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(5)))
.build(),
format!("/ip4/{ip}/tcp/0/wss"),
format!("/ip4/{ip}/tcp/0/tls/ws"),
),
(Transport::Ws, Some(SecProtocol::Noise), Some(Muxer::Yamux)) => (
libp2p::SwarmBuilder::with_new_identity()
Expand All @@ -262,7 +262,7 @@ pub(crate) mod wasm {
.with_behaviour(behaviour_constructor)?
.with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(5)))
.build(),
format!("/ip4/{ip}/tcp/0/wss"),
format!("/ip4/{ip}/tcp/0/tls/ws"),
),
(Transport::WebRtcDirect, None, None) => (
libp2p::SwarmBuilder::with_new_identity()
Expand Down
3 changes: 3 additions & 0 deletions transports/websocket-websys/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

- Implement refactored `Transport`.
See [PR 4568](https://github.com/libp2p/rust-libp2p/pull/4568)
- Add support for `/tls/ws` and keep `/wss` backward compatible.
See [PR 5523](https://github.com/libp2p/rust-libp2p/pull/5523).

## 0.3.3

- Fix use-after-free handler invocation from JS side.
Expand Down
107 changes: 104 additions & 3 deletions transports/websocket-websys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,10 @@ fn extract_websocket_url(addr: &Multiaddr) -> Option<String> {
_ => return None,
};

let (scheme, wspath) = match protocols.next() {
Some(Protocol::Ws(path)) => ("ws", path.into_owned()),
Some(Protocol::Wss(path)) => ("wss", path.into_owned()),
let (scheme, wspath) = match (protocols.next(), protocols.next()) {
(Some(Protocol::Tls), Some(Protocol::Ws(path))) => ("wss", path.into_owned()),
(Some(Protocol::Ws(path)), _) => ("ws", path.into_owned()),
(Some(Protocol::Wss(path)), _) => ("wss", path.into_owned()),
_ => return None,
};

Expand Down Expand Up @@ -453,3 +454,103 @@ impl Drop for Connection {
.clear_interval_with_handle(self.inner.buffered_amount_low_interval);
}
}

#[cfg(test)]
mod tests {
use super::*;
use libp2p_identity::PeerId;

#[test]
fn extract_url() {
let peer_id = PeerId::random();

// Check `/tls/ws`
let addr = "/dns4/example.com/tcp/2222/tls/ws"
.parse::<Multiaddr>()
.unwrap();
let url = extract_websocket_url(&addr).unwrap();
assert_eq!(url, "wss://example.com:2222/");

// Check `/tls/ws` with `/p2p`
let addr = format!("/dns4/example.com/tcp/2222/tls/ws/p2p/{peer_id}")
.parse()
.unwrap();
let url = extract_websocket_url(&addr).unwrap();
assert_eq!(url, "wss://example.com:2222/");

// Check `/tls/ws` with `/ip4`
let addr = "/ip4/127.0.0.1/tcp/2222/tls/ws"
.parse::<Multiaddr>()
.unwrap();
let url = extract_websocket_url(&addr).unwrap();
assert_eq!(url, "wss://127.0.0.1:2222/");

// Check `/tls/ws` with `/ip6`
let addr = "/ip6/::1/tcp/2222/tls/ws".parse::<Multiaddr>().unwrap();
let url = extract_websocket_url(&addr).unwrap();
assert_eq!(url, "wss://[::1]:2222/");

// Check `/wss`
let addr = "/dns4/example.com/tcp/2222/wss"
.parse::<Multiaddr>()
.unwrap();
let url = extract_websocket_url(&addr).unwrap();
assert_eq!(url, "wss://example.com:2222/");

// Check `/wss` with `/p2p`
let addr = format!("/dns4/example.com/tcp/2222/wss/p2p/{peer_id}")
.parse()
.unwrap();
let url = extract_websocket_url(&addr).unwrap();
assert_eq!(url, "wss://example.com:2222/");

// Check `/wss` with `/ip4`
let addr = "/ip4/127.0.0.1/tcp/2222/wss".parse::<Multiaddr>().unwrap();
let url = extract_websocket_url(&addr).unwrap();
assert_eq!(url, "wss://127.0.0.1:2222/");

// Check `/wss` with `/ip6`
let addr = "/ip6/::1/tcp/2222/wss".parse::<Multiaddr>().unwrap();
let url = extract_websocket_url(&addr).unwrap();
assert_eq!(url, "wss://[::1]:2222/");

// Check `/ws`
let addr = "/dns4/example.com/tcp/2222/ws"
.parse::<Multiaddr>()
.unwrap();
let url = extract_websocket_url(&addr).unwrap();
assert_eq!(url, "ws://example.com:2222/");

// Check `/ws` with `/p2p`
let addr = format!("/dns4/example.com/tcp/2222/ws/p2p/{peer_id}")
.parse()
.unwrap();
let url = extract_websocket_url(&addr).unwrap();
assert_eq!(url, "ws://example.com:2222/");

// Check `/ws` with `/ip4`
let addr = "/ip4/127.0.0.1/tcp/2222/ws".parse::<Multiaddr>().unwrap();
let url = extract_websocket_url(&addr).unwrap();
assert_eq!(url, "ws://127.0.0.1:2222/");

// Check `/ws` with `/ip6`
let addr = "/ip6/::1/tcp/2222/ws".parse::<Multiaddr>().unwrap();
let url = extract_websocket_url(&addr).unwrap();
assert_eq!(url, "ws://[::1]:2222/");

// Check `/ws` with `/ip4`
let addr = "/ip4/127.0.0.1/tcp/2222/ws".parse::<Multiaddr>().unwrap();
let url = extract_websocket_url(&addr).unwrap();
assert_eq!(url, "ws://127.0.0.1:2222/");

// Check that `/tls/wss` is invalid
let addr = "/ip4/127.0.0.1/tcp/2222/tls/wss"
.parse::<Multiaddr>()
.unwrap();
assert!(extract_websocket_url(&addr).is_none());

// Check non-ws address
let addr = "/ip4/127.0.0.1/tcp/2222".parse::<Multiaddr>().unwrap();
assert!(extract_websocket_url(&addr).is_none());
}
}
2 changes: 2 additions & 0 deletions transports/websocket/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
See [PR 4568](https://github.com/libp2p/rust-libp2p/pull/4568)
- Allow wss connections on IP addresses.
See [PR 5525](https://github.com/libp2p/rust-libp2p/pull/5525).
- Add support for `/tls/ws` and keep `/wss` backward compatible.
See [PR 5523](https://github.com/libp2p/rust-libp2p/pull/5523).

## 0.43.2

Expand Down
Loading

0 comments on commit 4725f46

Please sign in to comment.