(
13 | socket: UdpSocket,
14 | remote_addr: SocketAddr,
15 | timeout_duration: Duration,
16 | ) -> Error {
17 | let punch_holes_future = punch_holes::(socket, remote_addr);
18 | futures::pin_mut!(punch_holes_future);
19 | match futures::future::select(P::sleep(timeout_duration), punch_holes_future).await {
20 | Either::Left(_) => Error::HandshakeTimedOut,
21 | Either::Right((Err(hole_punch_err), _)) => hole_punch_err,
22 | Either::Right((Ok(never), _)) => match never {},
23 | }
24 | }
25 |
26 | async fn punch_holes(
27 | socket: UdpSocket,
28 | remote_addr: SocketAddr,
29 | ) -> Result {
30 | loop {
31 | let contents: Vec = rand::thread_rng()
32 | .sample_iter(distributions::Standard)
33 | .take(64)
34 | .collect();
35 |
36 | tracing::trace!("Sending random UDP packet to {remote_addr}");
37 |
38 | P::send_to(&socket, &contents, remote_addr).await?;
39 |
40 | let sleep_duration = Duration::from_millis(rand::thread_rng().gen_range(10..=200));
41 | P::sleep(sleep_duration).await;
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/transports/tcp/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "libp2p-tcp"
3 | edition.workspace = true
4 | rust-version = { workspace = true }
5 | description = "TCP/IP transport protocol for libp2p"
6 | version = "0.43.0"
7 | authors = ["Parity Technologies "]
8 | license = "MIT"
9 | repository = "https://github.com/libp2p/rust-libp2p"
10 | keywords = ["peer-to-peer", "libp2p", "networking"]
11 | categories = ["network-programming", "asynchronous"]
12 |
13 | [dependencies]
14 | async-io = { version = "2.3.3", optional = true }
15 | futures = { workspace = true }
16 | futures-timer = "3.0"
17 | if-watch = { workspace = true }
18 | libc = "0.2.155"
19 | libp2p-core = { workspace = true }
20 | socket2 = { version = "0.5.7", features = ["all"] }
21 | tokio = { workspace = true, default-features = false, features = ["net"], optional = true }
22 | tracing = { workspace = true }
23 |
24 | [features]
25 | tokio = ["dep:tokio", "if-watch/tokio"]
26 | async-io = ["dep:async-io", "if-watch/smol"]
27 |
28 | [dev-dependencies]
29 | async-std = { version = "1.6.5", features = ["attributes"] }
30 | tokio = { workspace = true, features = ["full"] }
31 | tracing-subscriber = { workspace = true, features = ["env-filter"] }
32 |
33 | # Passing arguments to the docsrs builder in order to properly document cfg's.
34 | # More information: https://docs.rs/about/builds#cross-compiling
35 | [package.metadata.docs.rs]
36 | all-features = true
37 |
38 |
39 | [lints]
40 | workspace = true
41 |
--------------------------------------------------------------------------------
/transports/tls/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | ## 0.6.1
2 |
3 | - Upgrade `rcgen` to `v0.13`
4 | See [PR 5917](https://github.com/libp2p/rust-libp2p/pull/5917).
5 |
6 | ## 0.6.0
7 |
8 |
9 |
10 | ## 0.5.0
11 |
12 |
13 |
14 | ## 0.4.1
15 |
16 | - Fix a panic caused by `rustls` parsing the libp2p TLS extension.
17 | See [PR 5498](https://github.com/libp2p/rust-libp2p/pull/5498).
18 |
19 | ## 0.4.0
20 |
21 | - Upgrade `rustls` to `0.23`. See [PR 5385](https://github.com/libp2p/rust-libp2p/pull/5385)
22 |
23 | ## 0.3.0
24 |
25 | - Migrate to `{In,Out}boundConnectionUpgrade` traits.
26 | See [PR 4695](https://github.com/libp2p/rust-libp2p/pull/4695).
27 |
28 | ## 0.2.1
29 |
30 | - Switch from webpki to rustls-webpki.
31 | This is a part of the resolution of the [RUSTSEC-2023-0052].
32 | See [PR 4381].
33 |
34 | [PR 4381]: https://github.com/libp2p/rust-libp2p/pull/4381
35 | [RUSTSEC-2023-0052]: https://rustsec.org/advisories/RUSTSEC-2023-0052.html
36 |
37 | ## 0.2.0
38 |
39 | - Raise MSRV to 1.65.
40 | See [PR 3715].
41 |
42 | [PR 3715]: https://github.com/libp2p/rust-libp2p/pull/3715
43 |
44 | ## 0.1.0
45 |
46 | - Promote to `v0.1.0`.
47 |
48 | ## 0.1.0-alpha.2
49 |
50 | - Update to `libp2p-core` `v0.39.0`.
51 |
52 | ## 0.1.0-alpha
53 |
54 | Initial release.
55 |
--------------------------------------------------------------------------------
/transports/tls/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "libp2p-tls"
3 | version = "0.6.1"
4 | edition.workspace = true
5 | rust-version = { workspace = true }
6 | description = "TLS configuration based on libp2p TLS specs."
7 | repository = "https://github.com/libp2p/rust-libp2p"
8 | license = "MIT"
9 | exclude = ["src/test_assets"]
10 |
11 | [dependencies]
12 | futures = { workspace = true }
13 | futures-rustls = { workspace = true }
14 | libp2p-core = { workspace = true }
15 | libp2p-identity = { workspace = true }
16 | rcgen = { workspace = true }
17 | ring = { workspace = true }
18 | thiserror = { workspace = true }
19 | webpki = { version = "0.101.4", package = "rustls-webpki", features = ["std"] }
20 | x509-parser = "0.16.0"
21 | yasna = "0.5.2"
22 |
23 | # Exposed dependencies. Breaking changes to these are breaking changes to us.
24 | [dependencies.rustls]
25 | version = "0.23.9"
26 | default-features = false
27 | features = ["ring", "std"] # Must enable this to allow for custom verification code.
28 |
29 |
30 | [dev-dependencies]
31 | hex-literal = "0.4.1"
32 | libp2p-core = { workspace = true }
33 | libp2p-identity = { workspace = true, features = ["ed25519", "rsa", "secp256k1", "ecdsa", "rand"] }
34 | libp2p-swarm = { workspace = true, features = ["tokio"] }
35 | libp2p-yamux = { workspace = true }
36 | tokio = { workspace = true, features = ["full"] }
37 |
38 | # Passing arguments to the docsrs builder in order to properly document cfg's.
39 | # More information: https://docs.rs/about/builds#cross-compiling
40 | [package.metadata.docs.rs]
41 | all-features = true
42 |
43 | [lints]
44 | workspace = true
45 |
--------------------------------------------------------------------------------
/transports/tls/src/test_assets/ed25519.der:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sigp/rust-libp2p/e1bba263070194282cad48f07fb4aa0c87d03b55/transports/tls/src/test_assets/ed25519.der
--------------------------------------------------------------------------------
/transports/tls/src/test_assets/ed448.der:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sigp/rust-libp2p/e1bba263070194282cad48f07fb4aa0c87d03b55/transports/tls/src/test_assets/ed448.der
--------------------------------------------------------------------------------
/transports/tls/src/test_assets/nistp256_sha256.der:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sigp/rust-libp2p/e1bba263070194282cad48f07fb4aa0c87d03b55/transports/tls/src/test_assets/nistp256_sha256.der
--------------------------------------------------------------------------------
/transports/tls/src/test_assets/nistp384_sha256.der:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sigp/rust-libp2p/e1bba263070194282cad48f07fb4aa0c87d03b55/transports/tls/src/test_assets/nistp384_sha256.der
--------------------------------------------------------------------------------
/transports/tls/src/test_assets/nistp384_sha384.der:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sigp/rust-libp2p/e1bba263070194282cad48f07fb4aa0c87d03b55/transports/tls/src/test_assets/nistp384_sha384.der
--------------------------------------------------------------------------------
/transports/tls/src/test_assets/nistp521_sha512.der:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sigp/rust-libp2p/e1bba263070194282cad48f07fb4aa0c87d03b55/transports/tls/src/test_assets/nistp521_sha512.der
--------------------------------------------------------------------------------
/transports/tls/src/test_assets/openssl.cfg:
--------------------------------------------------------------------------------
1 | [ p2p_ext ]
2 | 1.3.6.1.4.1.53594.1.1 = critical,ASN1:SEQUENCE:ExtBody
3 |
4 | [ ExtBody ]
5 | pubkey = FORMAT:HEX,OCTETSTRING:08011220DF6491C415ED084B87E8F00CDB4A41C4035CFEA5F9D23D25FF9CA897E7FDDC0F
6 | signature = FORMAT:HEX,OCTETSTRING:94A89E52CC24FD29B4B49DE615C37D268362E8D7C7C096FB7CD013DC9402572AF4886480FEC507C3C03DB07A2EC816B2B6714427DC28F379E0859C6F3B15BB05
7 |
--------------------------------------------------------------------------------
/transports/tls/src/test_assets/pkcs1_sha256.der:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sigp/rust-libp2p/e1bba263070194282cad48f07fb4aa0c87d03b55/transports/tls/src/test_assets/pkcs1_sha256.der
--------------------------------------------------------------------------------
/transports/tls/src/test_assets/rsa_pkcs1_sha256.der:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sigp/rust-libp2p/e1bba263070194282cad48f07fb4aa0c87d03b55/transports/tls/src/test_assets/rsa_pkcs1_sha256.der
--------------------------------------------------------------------------------
/transports/tls/src/test_assets/rsa_pkcs1_sha384.der:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sigp/rust-libp2p/e1bba263070194282cad48f07fb4aa0c87d03b55/transports/tls/src/test_assets/rsa_pkcs1_sha384.der
--------------------------------------------------------------------------------
/transports/tls/src/test_assets/rsa_pkcs1_sha512.der:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sigp/rust-libp2p/e1bba263070194282cad48f07fb4aa0c87d03b55/transports/tls/src/test_assets/rsa_pkcs1_sha512.der
--------------------------------------------------------------------------------
/transports/tls/src/test_assets/rsa_pss_sha384.der:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sigp/rust-libp2p/e1bba263070194282cad48f07fb4aa0c87d03b55/transports/tls/src/test_assets/rsa_pss_sha384.der
--------------------------------------------------------------------------------
/transports/uds/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "libp2p-uds"
3 | edition.workspace = true
4 | rust-version = { workspace = true }
5 | description = "Unix domain sockets transport for libp2p"
6 | version = "0.42.0"
7 | authors = ["Parity Technologies "]
8 | license = "MIT"
9 | repository = "https://github.com/libp2p/rust-libp2p"
10 | keywords = ["peer-to-peer", "libp2p", "networking"]
11 | categories = ["network-programming", "asynchronous"]
12 |
13 | [dependencies]
14 | async-std = { version = "1.6.2", optional = true }
15 | libp2p-core = { workspace = true }
16 | futures = { workspace = true }
17 | tokio = { workspace = true, default-features = false, features = ["net"], optional = true }
18 | tracing = { workspace = true }
19 |
20 | [dev-dependencies]
21 | tempfile = "3.10"
22 |
23 | # Passing arguments to the docsrs builder in order to properly document cfg's.
24 | # More information: https://docs.rs/about/builds#cross-compiling
25 | [package.metadata.docs.rs]
26 | all-features = true
27 |
28 | [lints]
29 | workspace = true
30 |
--------------------------------------------------------------------------------
/transports/webrtc-websys/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | ## 0.4.0
2 |
3 | - Cut stable release.
4 | See [PR 5807](https://github.com/libp2p/rust-libp2p/pull/5807)
5 | - Bump version of web-sys and update `__Nonexhaustive` to `__Invalid`.
6 | See [PR 5569](https://github.com/libp2p/rust-libp2p/pull/5569)
7 |
8 |
9 |
10 | ## 0.4.0-alpha
11 |
12 | - Implement refactored `Transport`.
13 | See [PR 4568](https://github.com/libp2p/rust-libp2p/pull/4568)
14 |
15 | ## 0.3.0-alpha
16 |
17 | - Bump version in order to publish a new version dependent on latest `libp2p-core`.
18 | See [PR 4959](https://github.com/libp2p/rust-libp2p/pull/4959).
19 | - Remove `libp2p_noise` from the public API.
20 | See [PR 4969](https://github.com/libp2p/rust-libp2p/pull/4969).
21 |
22 | ## 0.2.0-alpha
23 |
24 | - Rename `Error::JsError` to `Error::Js`.
25 | See [PR 4653](https://github.com/libp2p/rust-libp2p/pull/4653)
26 |
27 | ## 0.1.0-alpha
28 |
29 | - Initial alpha release.
30 | See [PR 4248].
31 |
32 | [PR 4248]: https://github.com/libp2p/rust-libp2p/pull/4248
33 |
--------------------------------------------------------------------------------
/transports/webrtc-websys/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | authors = ["Doug Anderson "]
3 | categories = ["asynchronous", "network-programming", "wasm", "web-programming"]
4 | description = "WebRTC for libp2p under WASM environment"
5 | edition.workspace = true
6 | keywords = ["libp2p", "networking", "peer-to-peer"]
7 | license = "MIT"
8 | name = "libp2p-webrtc-websys"
9 | repository = "https://github.com/libp2p/rust-libp2p"
10 | rust-version = { workspace = true }
11 | version = "0.4.0"
12 | publish = true
13 |
14 | [dependencies]
15 | bytes = "1"
16 | futures = { workspace = true }
17 | getrandom = { workspace = true, features = ["js"] }
18 | hex = "0.4.3"
19 | js-sys = { version = "0.3" }
20 | libp2p-core = { workspace = true }
21 | libp2p-identity = { workspace = true }
22 | libp2p-webrtc-utils = { workspace = true }
23 | send_wrapper = { version = "0.6.0", features = ["futures"] }
24 | thiserror = { workspace = true }
25 | tracing = { workspace = true }
26 | wasm-bindgen = { version = "0.2.90" }
27 | wasm-bindgen-futures = { version = "0.4.42" }
28 | web-sys = { version = "0.3.70", features = ["Document", "Location", "MessageEvent", "Navigator", "RtcCertificate", "RtcConfiguration", "RtcDataChannel", "RtcDataChannelEvent", "RtcDataChannelInit", "RtcDataChannelState", "RtcDataChannelType", "RtcPeerConnection", "RtcSdpType", "RtcSessionDescription", "RtcSessionDescriptionInit", "Window"] }
29 |
30 | [lints]
31 | workspace = true
32 |
--------------------------------------------------------------------------------
/transports/webrtc-websys/README.md:
--------------------------------------------------------------------------------
1 | # Rust `libp2p-webrtc-websys`
2 |
3 | Browser Transport made available through `web-sys` bindings.
4 |
5 | ## Usage
6 |
7 | Use with `Swarm::with_wasm_executor` to enable the `wasm-bindgen` executor for the `Swarm`.
8 |
9 | See the [browser-webrtc](../../examples/browser-webrtc) example for a full example.
10 |
--------------------------------------------------------------------------------
/transports/webrtc-websys/src/error.rs:
--------------------------------------------------------------------------------
1 | use wasm_bindgen::{JsCast, JsValue};
2 |
3 | /// Errors that may happen on the [`Transport`](crate::Transport) or the
4 | /// [`Connection`](crate::Connection).
5 | #[derive(thiserror::Error, Debug)]
6 | pub enum Error {
7 | #[error("Invalid multiaddr: {0}")]
8 | InvalidMultiaddr(&'static str),
9 |
10 | #[error("JavaScript error: {0}")]
11 | Js(String),
12 |
13 | #[error("JavaScript typecasting failed")]
14 | JsCastFailed,
15 |
16 | #[error("Unknown remote peer ID")]
17 | UnknownRemotePeerId,
18 |
19 | #[error("Connection error: {0}")]
20 | Connection(String),
21 |
22 | #[error("Authentication error")]
23 | Authentication(#[from] AuthenticationError),
24 | }
25 |
26 | /// New-type wrapper to hide `libp2p_noise` from the public API.
27 | #[derive(thiserror::Error, Debug)]
28 | #[error(transparent)]
29 | pub struct AuthenticationError(pub(crate) libp2p_webrtc_utils::noise::Error);
30 |
31 | impl Error {
32 | pub(crate) fn from_js_value(value: JsValue) -> Self {
33 | let s = if value.is_instance_of::() {
34 | js_sys::Error::from(value)
35 | .to_string()
36 | .as_string()
37 | .unwrap_or_else(|| "Unknown error".to_string())
38 | } else {
39 | "Unknown error".to_string()
40 | };
41 |
42 | Error::Js(s)
43 | }
44 | }
45 |
46 | impl From for Error {
47 | fn from(value: JsValue) -> Self {
48 | Error::from_js_value(value)
49 | }
50 | }
51 |
52 | impl From for Error {
53 | fn from(value: String) -> Self {
54 | Error::Js(value)
55 | }
56 | }
57 |
58 | impl From for Error {
59 | fn from(value: std::io::Error) -> Self {
60 | Error::Js(value.to_string())
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/transports/webrtc-websys/src/lib.rs:
--------------------------------------------------------------------------------
1 | #![doc = include_str!("../README.md")]
2 |
3 | mod connection;
4 | mod error;
5 | mod sdp;
6 | mod stream;
7 | mod transport;
8 | mod upgrade;
9 |
10 | pub use self::{
11 | connection::Connection,
12 | error::Error,
13 | stream::Stream,
14 | transport::{Config, Transport},
15 | };
16 |
--------------------------------------------------------------------------------
/transports/webrtc-websys/src/sdp.rs:
--------------------------------------------------------------------------------
1 | use std::net::SocketAddr;
2 |
3 | use libp2p_webrtc_utils::Fingerprint;
4 | use web_sys::{RtcSdpType, RtcSessionDescriptionInit};
5 |
6 | /// Creates the SDP answer used by the client.
7 | pub(crate) fn answer(
8 | addr: SocketAddr,
9 | server_fingerprint: Fingerprint,
10 | client_ufrag: &str,
11 | ) -> RtcSessionDescriptionInit {
12 | let answer_obj = RtcSessionDescriptionInit::new(RtcSdpType::Answer);
13 | answer_obj.set_sdp(&libp2p_webrtc_utils::sdp::answer(
14 | addr,
15 | server_fingerprint,
16 | client_ufrag,
17 | ));
18 | answer_obj
19 | }
20 |
21 | /// Creates the munged SDP offer from the Browser's given SDP offer
22 | ///
23 | /// Certificate verification is disabled which is why we hardcode a dummy fingerprint here.
24 | pub(crate) fn offer(offer: String, client_ufrag: &str) -> RtcSessionDescriptionInit {
25 | // find line and replace a=ice-ufrag: with "\r\na=ice-ufrag:{client_ufrag}\r\n"
26 | // find line and replace a=ice-pwd: with "\r\na=ice-ufrag:{client_ufrag}\r\n"
27 |
28 | let mut munged_sdp_offer = String::new();
29 |
30 | for line in offer.split("\r\n") {
31 | if line.starts_with("a=ice-ufrag:") {
32 | munged_sdp_offer.push_str(&format!("a=ice-ufrag:{client_ufrag}\r\n"));
33 | continue;
34 | }
35 |
36 | if line.starts_with("a=ice-pwd:") {
37 | munged_sdp_offer.push_str(&format!("a=ice-pwd:{client_ufrag}\r\n"));
38 | continue;
39 | }
40 |
41 | if !line.is_empty() {
42 | munged_sdp_offer.push_str(&format!("{}\r\n", line));
43 | continue;
44 | }
45 | }
46 |
47 | // remove any double \r\n
48 | let munged_sdp_offer = munged_sdp_offer.replace("\r\n\r\n", "\r\n");
49 |
50 | tracing::trace!(offer=%munged_sdp_offer, "Created SDP offer");
51 |
52 | let offer_obj = RtcSessionDescriptionInit::new(RtcSdpType::Offer);
53 | offer_obj.set_sdp(&munged_sdp_offer);
54 |
55 | offer_obj
56 | }
57 |
--------------------------------------------------------------------------------
/transports/webrtc/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | ## 0.9.0-alpha
2 |
3 |
4 |
5 | ## 0.8.0-alpha
6 |
7 | - Implement refactored `Transport`.
8 | See [PR 4568](https://github.com/libp2p/rust-libp2p/pull/4568)
9 |
10 | ## 0.7.1-alpha
11 |
12 | - Bump `libp2p-webrtc-utils` dependency to `0.2.0`.
13 | See [PR 5118](https://github.com/libp2p/rust-libp2p/pull/5118).
14 |
15 | ## 0.7.0-alpha
16 |
17 | - Bump version in order to publish a new version dependent on latest `libp2p-core`.
18 | See [PR 4959](https://github.com/libp2p/rust-libp2p/pull/4959).
19 |
20 | ## 0.6.1-alpha
21 |
22 | - Move common dependencies to `libp2p-webrtc-utils` crate.
23 | See [PR 4248].
24 |
25 | [PR 4248]: https://github.com/libp2p/rust-libp2p/pull/4248
26 |
27 | ## 0.6.0-alpha
28 |
29 | - Update `webrtc` dependency to `v0.8.0`.
30 | See [PR 4099].
31 |
32 | [PR 4099]: https://github.com/libp2p/rust-libp2p/pull/4099
33 |
34 | ## 0.5.0-alpha
35 |
36 | - Raise MSRV to 1.65.
37 | See [PR 3715].
38 |
39 | [PR 3715]: https://github.com/libp2p/rust-libp2p/pull/3715
40 |
41 | ## 0.4.0-alpha.4
42 |
43 | - Make `Fingerprint` type public. See [PR 3648].
44 |
45 | [PR 3648]: https://github.com/libp2p/rust-libp2p/pull/3648
46 |
47 | ## 0.4.0-alpha.3
48 |
49 | - Gracefully handle `ConnectionReset` error on individual connections, avoiding shutdown of the entire listener upon disconnect of a single client.
50 | See [PR 3575].
51 |
52 | - Migrate from `prost` to `quick-protobuf`. This removes `protoc` dependency. See [PR 3312].
53 |
54 | [PR 3575]: https://github.com/libp2p/rust-libp2p/pull/3575
55 | [PR 3312]: https://github.com/libp2p/rust-libp2p/pull/3312
56 |
57 | ## 0.4.0-alpha.2
58 |
59 | - Update to `libp2p-noise` `v0.42.0`.
60 |
61 | - Update to `libp2p-core` `v0.39.0`.
62 |
63 | ## 0.4.0-alpha
64 |
65 | - Initial alpha release.
66 |
--------------------------------------------------------------------------------
/transports/webrtc/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "libp2p-webrtc"
3 | version = "0.9.0-alpha"
4 | authors = ["Parity Technologies "]
5 | description = "WebRTC transport for libp2p"
6 | repository = "https://github.com/libp2p/rust-libp2p"
7 | license = "MIT"
8 | edition.workspace = true
9 | rust-version = { workspace = true }
10 | keywords = ["peer-to-peer", "libp2p", "networking"]
11 | categories = ["network-programming", "asynchronous"]
12 |
13 | [dependencies]
14 | async-trait = "0.1"
15 | futures = { workspace = true }
16 | futures-timer = "3"
17 | hex = "0.4"
18 | if-watch = { workspace = true }
19 | libp2p-core = { workspace = true }
20 | libp2p-noise = { workspace = true }
21 | libp2p-identity = { workspace = true }
22 | libp2p-webrtc-utils = { workspace = true }
23 | multihash = { workspace = true }
24 | rand = "0.8"
25 | rcgen = "0.11"
26 | stun = "0.7"
27 | thiserror = { workspace = true }
28 | tokio = { workspace = true, features = ["net"], optional = true }
29 | tokio-util = { version = "0.7", features = ["compat"], optional = true }
30 | tracing = { workspace = true }
31 | webrtc = { version = "0.9.0", optional = true }
32 | webrtc-ice = "=0.10.0" # smoke tests only work with this version
33 |
34 | [features]
35 | tokio = ["dep:tokio", "dep:tokio-util", "dep:webrtc", "if-watch/tokio"]
36 | pem = ["webrtc?/pem"]
37 |
38 | [dev-dependencies]
39 | libp2p-identity = { workspace = true, features = ["rand"] }
40 | tokio = { workspace = true, features = ["full"] }
41 | quickcheck = "1.0.3"
42 | tracing-subscriber = { workspace = true, features = ["env-filter"] }
43 |
44 |
45 | [[test]]
46 | name = "smoke"
47 | required-features = ["tokio"]
48 |
49 | [lints]
50 | workspace = true
51 |
52 | # Passing arguments to the docsrs builder in order to properly document cfg's.
53 | # More information: https://docs.rs/about/builds#cross-compiling
54 | [package.metadata.docs.rs]
55 | all-features = true
56 |
--------------------------------------------------------------------------------
/transports/webrtc/src/tokio/mod.rs:
--------------------------------------------------------------------------------
1 | // Copyright 2022 Parity Technologies (UK) Ltd.
2 | //
3 | // Permission is hereby granted, free of charge, to any person obtaining a
4 | // copy of this software and associated documentation files (the "Software"),
5 | // to deal in the Software without restriction, including without limitation
6 | // the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 | // and/or sell copies of the Software, and to permit persons to whom the
8 | // Software is furnished to do so, subject to the following conditions:
9 | //
10 | // The above copyright notice and this permission notice shall be included in
11 | // all copies or substantial portions of the Software.
12 | //
13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19 | // DEALINGS IN THE SOFTWARE.
20 |
21 | pub mod certificate;
22 | mod connection;
23 | mod error;
24 | mod fingerprint;
25 | mod req_res_chan;
26 | mod sdp;
27 | mod stream;
28 | mod transport;
29 | mod udp_mux;
30 | mod upgrade;
31 |
32 | pub use certificate::Certificate;
33 | pub use connection::Connection;
34 | pub use error::Error;
35 | pub use fingerprint::Fingerprint;
36 | pub use transport::Transport;
37 |
--------------------------------------------------------------------------------
/transports/websocket-websys/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | ## 0.5.0
2 |
3 | - fix: Return `None` when extracting a `/dnsaddr` address
4 | See [PR 5613](https://github.com/libp2p/rust-libp2p/pull/5613)
5 |
6 | - Fix `cargo clippy` warnings in `rustc 1.84.0-beta.1`.
7 | See [PR 5700](https://github.com/libp2p/rust-libp2p/pull/5700).
8 |
9 |
10 |
11 | ## 0.4.0
12 |
13 | - Implement refactored `Transport`.
14 | See [PR 4568](https://github.com/libp2p/rust-libp2p/pull/4568)
15 | - Add support for `/tls/ws` and keep `/wss` backward compatible.
16 | See [PR 5523](https://github.com/libp2p/rust-libp2p/pull/5523).
17 |
18 | ## 0.3.3
19 |
20 | - Fix use-after-free handler invocation from JS side.
21 | See [PR 5521](https://github.com/libp2p/rust-libp2p/pull/5521).
22 |
23 | ## 0.3.2
24 |
25 | - Change close code in drop implementation to `1000` given that in browsers only
26 | the code `1000` and codes between `3000` and `4999` are allowed to be set by
27 | userland code.
28 | See [PR 5229](https://github.com/libp2p/rust-libp2p/pull/5229).
29 |
30 | ## 0.3.1
31 |
32 | - Add support for different WASM environments by introducing a `WebContext` that
33 | detects and abstracts the `Window` vs the `WorkerGlobalScope` API.
34 | See [PR 4889](https://github.com/libp2p/rust-libp2p/pull/4889).
35 |
36 | ## 0.3.0
37 |
38 |
39 | ## 0.2.0
40 |
41 | - Add Websys Websocket transport.
42 |
43 | ## 0.1.0
44 |
45 | - Crate claimed.
46 |
--------------------------------------------------------------------------------
/transports/websocket-websys/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "libp2p-websocket-websys"
3 | edition.workspace = true
4 | rust-version.workspace = true
5 | description = "WebSocket for libp2p under WASM environment"
6 | version = "0.5.0"
7 | authors = ["Vince Vasta "]
8 | license = "MIT"
9 | repository = "https://github.com/libp2p/rust-libp2p"
10 | keywords = ["peer-to-peer", "libp2p", "networking"]
11 | categories = ["network-programming", "asynchronous"]
12 |
13 | [dependencies]
14 | bytes = "1.6.0"
15 | futures = { workspace = true }
16 | js-sys = "0.3.69"
17 | libp2p-core = { workspace = true }
18 | tracing = { workspace = true }
19 | send_wrapper = "0.6.0"
20 | thiserror = { workspace = true }
21 | wasm-bindgen = "0.2.90"
22 | web-sys = { version = "0.3.69", features = ["BinaryType", "CloseEvent", "MessageEvent", "WebSocket", "Window", "WorkerGlobalScope"] }
23 |
24 | # Passing arguments to the docsrs builder in order to properly document cfg's.
25 | # More information: https://docs.rs/about/builds#cross-compiling
26 | [package.metadata.docs.rs]
27 | all-features = true
28 |
29 | [dev-dependencies]
30 | libp2p-yamux = { workspace = true }
31 | libp2p-noise = { workspace = true }
32 | libp2p-identity = { workspace = true, features = ["rand"] }
33 |
--------------------------------------------------------------------------------
/transports/websocket/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "libp2p-websocket"
3 | edition.workspace = true
4 | rust-version = { workspace = true }
5 | description = "WebSocket transport for libp2p"
6 | version = "0.45.1"
7 | authors = ["Parity Technologies "]
8 | license = "MIT"
9 | repository = "https://github.com/libp2p/rust-libp2p"
10 | keywords = ["peer-to-peer", "libp2p", "networking"]
11 | categories = ["network-programming", "asynchronous"]
12 |
13 | [dependencies]
14 | futures-rustls = { workspace = true, features = ["ring"] }
15 | either = "1.12.0"
16 | futures = { workspace = true }
17 | libp2p-core = { workspace = true }
18 | libp2p-identity = { workspace = true }
19 | parking_lot = "0.12.3"
20 | pin-project-lite = "0.2.14"
21 | rw-stream-sink = { workspace = true }
22 | soketto = "0.8.0"
23 | tracing = { workspace = true }
24 | thiserror = { workspace = true }
25 | url = "2.5"
26 | webpki-roots = "0.25"
27 |
28 | [dev-dependencies]
29 | libp2p-tcp = { workspace = true, features = ["async-io"] }
30 | libp2p-dns = { workspace = true, features = ["async-std"] }
31 | libp2p-identity = { workspace = true, features = ["rand"] }
32 | async-std = { version = "1.6.5", features = ["attributes"] }
33 | rcgen = { workspace = true }
34 |
35 | # Passing arguments to the docsrs builder in order to properly document cfg's.
36 | # More information: https://docs.rs/about/builds#cross-compiling
37 | [package.metadata.docs.rs]
38 | all-features = true
39 |
40 | [lints]
41 | workspace = true
42 |
--------------------------------------------------------------------------------
/transports/webtransport-websys/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | ## 0.5.1
2 |
3 | - Remove `once_cell` dependency.
4 | See [PR 5913](https://github.com/libp2p/rust-libp2p/pull/5913)
5 |
6 | ## 0.5.0
7 |
8 | - Fix `cargo clippy` warnings in `rustc 1.84.0-beta.1`.
9 | See [PR 5700](https://github.com/libp2p/rust-libp2p/pull/5700).
10 |
11 |
12 |
13 | ## 0.4.0
14 |
15 | - Implement refactored `Transport`.
16 | See [PR 4568](https://github.com/libp2p/rust-libp2p/pull/4568)
17 | - Bump version of web-sys and wasm-bindgen.
18 | See [PR 5569](https://github.com/libp2p/rust-libp2p/pull/5569)
19 |
20 | ## 0.3.0
21 |
22 | * Fix unhandled exceptions thrown when calling `Webtransport::close`.
23 | See [PR 5390](https://github.com/libp2p/rust-libp2p/pull/5390).
24 | * Change logs to debug level.
25 | See [PR 5396](https://github.com/libp2p/rust-libp2p/pull/5396).
26 |
27 |
28 | ## 0.2.0
29 |
30 |
31 | ## 0.1.0
32 |
33 | * Initial implementation of WebTransport transport using web-sys bindings. See [PR 4015].
34 |
35 | [PR 4015]: https://github.com/libp2p/rust-libp2p/pull/4015
36 |
--------------------------------------------------------------------------------
/transports/webtransport-websys/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "libp2p-webtransport-websys"
3 | edition.workspace = true
4 | rust-version = { workspace = true }
5 | description = "WebTransport for libp2p under WASM environment"
6 | version = "0.5.1"
7 | authors = [
8 | "Yiannis Marangos ",
9 | "oblique ",
10 | ]
11 | license = "MIT"
12 | repository = "https://github.com/libp2p/rust-libp2p"
13 | keywords = ["peer-to-peer", "libp2p", "networking"]
14 | categories = ["network-programming", "asynchronous"]
15 |
16 | [dependencies]
17 | futures = { workspace = true }
18 | js-sys = "0.3.70"
19 | libp2p-core = { workspace = true }
20 | libp2p-identity = { workspace = true }
21 | libp2p-noise = { workspace = true }
22 | multiaddr = { workspace = true }
23 | multihash = { workspace = true }
24 | send_wrapper = { version = "0.6.0", features = ["futures"] }
25 | thiserror = { workspace = true }
26 | tracing = { workspace = true }
27 | wasm-bindgen = "0.2.93"
28 | wasm-bindgen-futures = "0.4.43"
29 | web-sys = { version = "0.3.70", features = [
30 | "ReadableStreamDefaultReader",
31 | "WebTransport",
32 | "WebTransportBidirectionalStream",
33 | "WebTransportHash",
34 | "WebTransportOptions",
35 | "WebTransportReceiveStream",
36 | "WebTransportSendStream",
37 | "WritableStreamDefaultWriter",
38 | ] }
39 |
40 | [dev-dependencies]
41 | multibase = "0.9.1"
42 |
43 | # Passing arguments to the docsrs builder in order to properly document cfg's.
44 | # More information: https://docs.rs/about/builds#cross-compiling
45 | [package.metadata.docs.rs]
46 | all-features = true
47 |
48 | [lints]
49 | workspace = true
50 |
--------------------------------------------------------------------------------
/transports/webtransport-websys/src/error.rs:
--------------------------------------------------------------------------------
1 | use wasm_bindgen::{JsCast, JsValue};
2 |
3 | /// Errors that may happen on the [`Transport`](crate::Transport) or the
4 | /// [`Connection`](crate::Connection).
5 | #[derive(thiserror::Error, Debug)]
6 | pub enum Error {
7 | #[error("Invalid multiaddr: {0}")]
8 | InvalidMultiaddr(&'static str),
9 |
10 | #[error("Noise authentication failed")]
11 | Noise(#[from] libp2p_noise::Error),
12 |
13 | #[error("JavaScript error: {0}")]
14 | #[allow(clippy::enum_variant_names)]
15 | JsError(String),
16 |
17 | #[error("JavaScript typecasting failed")]
18 | JsCastFailed,
19 |
20 | #[error("Unknown remote peer ID")]
21 | UnknownRemotePeerId,
22 | }
23 |
24 | impl Error {
25 | pub(crate) fn from_js_value(value: JsValue) -> Self {
26 | let s = if value.is_instance_of::() {
27 | js_sys::Error::from(value)
28 | .to_string()
29 | .as_string()
30 | .unwrap_or_else(|| "Unknown error".to_string())
31 | } else {
32 | "Unknown error".to_string()
33 | };
34 |
35 | Error::JsError(s)
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/transports/webtransport-websys/src/fused_js_promise.rs:
--------------------------------------------------------------------------------
1 | use std::{
2 | future::Future,
3 | pin::Pin,
4 | task::{ready, Context, Poll},
5 | };
6 |
7 | use futures::FutureExt;
8 | use js_sys::Promise;
9 | use wasm_bindgen::JsValue;
10 | use wasm_bindgen_futures::JsFuture;
11 |
12 | /// Convenient wrapper to poll a promise to completion.
13 | ///
14 | /// # Panics
15 | ///
16 | /// Panics if polled and promise is not initialized. Use `maybe_init` if unsure.
17 | #[derive(Debug)]
18 | pub(crate) struct FusedJsPromise {
19 | promise: Option,
20 | }
21 |
22 | impl FusedJsPromise {
23 | /// Creates new uninitialized promise.
24 | pub(crate) fn new() -> Self {
25 | FusedJsPromise { promise: None }
26 | }
27 |
28 | /// Initialize promise if needed
29 | pub(crate) fn maybe_init(&mut self, init: F) -> &mut Self
30 | where
31 | F: FnOnce() -> Promise,
32 | {
33 | if self.promise.is_none() {
34 | self.promise = Some(JsFuture::from(init()));
35 | }
36 |
37 | self
38 | }
39 |
40 | /// Checks if promise is already running
41 | pub(crate) fn is_active(&self) -> bool {
42 | self.promise.is_some()
43 | }
44 | }
45 |
46 | impl Future for FusedJsPromise {
47 | type Output = Result;
48 |
49 | fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll {
50 | let val = ready!(self
51 | .promise
52 | .as_mut()
53 | .expect("FusedJsPromise not initialized")
54 | .poll_unpin(cx));
55 |
56 | // Future finished, drop it
57 | self.promise.take();
58 |
59 | Poll::Ready(val)
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/transports/webtransport-websys/src/lib.rs:
--------------------------------------------------------------------------------
1 | //! Libp2p WebTransport built on [web-sys](https://rustwasm.github.io/wasm-bindgen/web-sys/index.html)
2 |
3 | #![allow(unexpected_cfgs)]
4 |
5 | mod bindings;
6 | mod connection;
7 | mod endpoint;
8 | mod error;
9 | mod fused_js_promise;
10 | mod stream;
11 | mod transport;
12 | mod utils;
13 |
14 | pub use self::{
15 | connection::Connection,
16 | error::Error,
17 | stream::Stream,
18 | transport::{Config, Transport},
19 | };
20 |
--------------------------------------------------------------------------------
/wasm-tests/README.md:
--------------------------------------------------------------------------------
1 | # Dependencies
2 |
3 | Before you run the tests you need to install the following:
4 |
5 | * Chrome or Chromium
6 | * chromedriver (major version must be the same as Chrome's)
7 | * wasm-pack
8 |
9 | # Run tests
10 |
11 | Just call `run-all.sh` or `run.sh` in the test directory if you are interested.
12 |
--------------------------------------------------------------------------------
/wasm-tests/run-all.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | set -e
3 |
4 | # cd to this script directory
5 | cd "$(dirname "${BASH_SOURCE[0]}")" || exit 1
6 |
7 | ./webtransport-tests/run.sh
8 |
--------------------------------------------------------------------------------
/wasm-tests/webtransport-tests/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "webtransport-tests"
3 | version = "0.1.0"
4 | edition.workspace = true
5 | license = "MIT"
6 | publish = false
7 |
8 | [package.metadata.release]
9 | release = false
10 |
11 | [dependencies]
12 | futures = { workspace = true }
13 | getrandom = { workspace = true, features = ["js"] }
14 | libp2p-core = { workspace = true }
15 | libp2p-identity = { workspace = true, features = ["rand"] }
16 | libp2p-noise = { workspace = true }
17 | libp2p-webtransport-websys = { workspace = true }
18 | multiaddr = { workspace = true }
19 | multihash = { workspace = true }
20 | wasm-bindgen = "0.2.93"
21 | wasm-bindgen-futures = "0.4.43"
22 | wasm-bindgen-test = "0.3.43"
23 | web-sys = { version = "0.3.70", features = ["Response", "Window"] }
24 |
25 | [lints]
26 | workspace = true
27 |
--------------------------------------------------------------------------------
/wasm-tests/webtransport-tests/README.md:
--------------------------------------------------------------------------------
1 | # Manually run tests
2 |
3 | First you need to build and start the echo-server:
4 |
5 | ```
6 | docker build -t webtransport-echo-server echo-server
7 | docker run -it --rm --network=host webtransport-echo-server
8 | ```
9 |
10 | On another terminal run:
11 |
12 | ```
13 | wasm-pack test --chrome
14 | ```
15 |
16 | Navigate with your browser at http://127.0.0.1:8000.
17 |
18 | You can also run the tests on a headless browser:
19 |
20 | ```
21 | wasm-pack test --chrome --headless
22 | ```
23 |
24 | > **Note:** For headless tests your Chrome browser needs to be compatible
25 | > with chromedriver (i.e. they must have the same major version).
26 | >
27 | > You may need to define the path of chromedriver with `--chromedriver=/path/to/chromedriver`.
28 |
--------------------------------------------------------------------------------
/wasm-tests/webtransport-tests/echo-server/.gitignore:
--------------------------------------------------------------------------------
1 | /echo-server
2 |
--------------------------------------------------------------------------------
/wasm-tests/webtransport-tests/echo-server/Dockerfile:
--------------------------------------------------------------------------------
1 | # syntax=docker/dockerfile:1.5-labs
2 | FROM docker.io/library/golang:1.24 AS builder
3 | WORKDIR /workspace
4 | ADD . .
5 | RUN CGO_ENABLED=0 go build .
6 |
7 | FROM scratch
8 | COPY --from=builder /workspace/echo-server /
9 | ENTRYPOINT ["/echo-server"]
10 |
--------------------------------------------------------------------------------
/wasm-tests/webtransport-tests/run.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # Prefer podman over docker since it doesn't require root privileges
4 | if command -v podman > /dev/null; then
5 | docker=podman
6 | else
7 | docker=docker
8 | fi
9 |
10 | # cd to this script directory
11 | cd "$(dirname "${BASH_SOURCE[0]}")" || exit 1
12 |
13 | # Print the directory for debugging
14 | echo "Tests: $PWD"
15 |
16 | # Build and run echo-server
17 | $docker build -t webtransport-echo-server echo-server || exit 1
18 | id="$($docker run -d --network=host webtransport-echo-server)" || exit 1
19 |
20 | # Run tests
21 | wasm-pack test --chrome --headless
22 | exit_code=$?
23 |
24 | # Remove echo-server container
25 | $docker rm -f "$id"
26 |
27 | # Propagate wasm-pack's exit code
28 | exit $exit_code
29 |
--------------------------------------------------------------------------------