Skip to content

feat(transports/webrtc-websys): Browser to Browser WebRTC #5978

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 24 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ members = [
"swarm-derive",
"swarm-test",
"swarm",
"transports/circuit-relay-v2",
"transports/dns",
"transports/noise",
"transports/plaintext",
Expand All @@ -77,6 +78,7 @@ edition = "2021"
libp2p = { version = "0.55.1", path = "libp2p" }
libp2p-allow-block-list = { version = "0.5.0", path = "misc/allow-block-list" }
libp2p-autonat = { version = "0.14.1", path = "protocols/autonat" }
libp2p-circuit-relay-v2 = { version = "0.1.0", path = "transports/circuit-relay-v2" }
libp2p-connection-limits = { version = "0.5.1", path = "misc/connection-limits" }
libp2p-core = { version = "0.43.1", path = "core" }
libp2p-dcutr = { version = "0.13.0", path = "protocols/dcutr" }
Expand Down
2 changes: 1 addition & 1 deletion misc/webrtc-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ mod transport;

pub use fingerprint::{Fingerprint, SHA256};
pub use stream::{DropListener, Stream, MAX_MSG_LEN};
pub use transport::{parse_webrtc_dial_addr, is_valid_browser_webrtc_addr};
pub use transport::{is_valid_browser_webrtc_addr, parse_webrtc_dial_addr};
4 changes: 2 additions & 2 deletions misc/webrtc-utils/src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ pub fn parse_webrtc_dial_addr(addr: &Multiaddr) -> Option<(SocketAddr, Fingerpri
let fingerprint = Fingerprint::try_from_multihash(cert_hash)?;

(port, fingerprint)
},
}
(Protocol::Udp(port), Protocol::WebRTC, Protocol::Certhash(cert_hash)) => {
let fingerprint = Fingerprint::try_from_multihash(cert_hash)?;
(port, fingerprint)
},
}
_ => return None,
};

Expand Down
65 changes: 65 additions & 0 deletions transports/circuit-relay-v2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
[package]
authors = ["Elijah Hampton <[email protected]>"]
categories = ["asynchronous", "network-programming", "wasm", "web-programming"]
description = "Circuit Relay V2 for libp2p under WASM environment"
edition.workspace = true
keywords = ["circuit-relay", "libp2p", "networking", "peer-to-peer"]
license = "MIT"
name = "libp2p-circuit-relay-v2"
repository = "https://github.com/libp2p/rust-libp2p"
rust-version = { workspace = true }
version = "0.1.0"
publish = true

[build-dependencies]
prost-build = "0.13.5"

[dependencies]
futures = { workspace = true }
js-sys = { version = "0.3" }
libp2p-core = { workspace = true }
libp2p-webrtc-utils = { workspace = true }
libp2p-tcp = { path = "../../transports/tcp" }
libp2p-relay = { workspace = true }
libp2p-gossipsub = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true, features = ["full"] }
tracing = { workspace = true }
wasm-bindgen = { version = "0.2.90" }
wasm-bindgen-futures = { version = "0.4.42" }
web-sys = { version = "0.3.70", features = [
"RtcPeerConnection",
"RtcConfiguration",
"RtcSessionDescription",
"RtcSessionDescriptionInit",
"RtcSdpType",
"RtcDataChannel",
"RtcDataChannelEvent",
"RtcDataChannelInit",
"RtcDataChannelState",
"RtcDataChannelType",
"RtcIceConnectionState",
"RtcIceGatheringState",
"RtcIceCandidate",
"RtcIceCandidateInit",
"RtcPeerConnectionIceEvent",
"Event",
"EventTarget",
"MessageEvent",
"Window",
"Document",
"Navigator",
"RtcCertificate",
"RtcPeerConnectionState",
"RtcSignalingState",
"Location",
"HtmlDocument",
"WebSocket"
]}
libp2p-swarm = { workspace = true }
quick-protobuf = "0.8"
quick-protobuf-codec = { workspace = true }
prost = "0.13.5"

[lints]
workspace = true
4 changes: 4 additions & 0 deletions transports/circuit-relay-v2/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
prost_build::compile_protos(&["src/proto/message.proto"], &["src/proto"])
.unwrap();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a quick FYI, we should not use prost but instead use quick-protobuf :). See #3024

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Thanks for catching that.

Empty file.
3 changes: 3 additions & 0 deletions transports/circuit-relay-v2/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod protocol_id;

pub use protocol_id;
77 changes: 77 additions & 0 deletions transports/circuit-relay-v2/src/proto/message.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
syntax = "proto3";
package circuit_relay;

message Voucher {
// These fields are marked optional for backwards compatibility with proto2.
// Users should make sure to always set these.
optional bytes relay = 1;
optional bytes peer = 2;
optional uint64 expiration = 3;
}

message HopMessage {
enum Type {
RESERVE = 0;
CONNECT = 1;
STATUS = 2;
}

// This field is marked optional for backwards compatibility with proto2.
// Users should make sure to always set this.
optional Type type = 1;

optional Peer peer = 2;
optional Reservation reservation = 3;
optional Limit limit = 4;

optional Status status = 5;
}

message StopMessage {
enum Type {
CONNECT = 0;
STATUS = 1;
}

// This field is marked optional for backwards compatibility with proto2.
// Users should make sure to always set this.
optional Type type = 1;

optional Peer peer = 2;
optional Limit limit = 3;

optional Status status = 4;
}

message Peer {
// This field is marked optional for backwards compatibility with proto2.
// Users should make sure to always set this.
optional bytes id = 1;
repeated bytes addrs = 2;
}

message Reservation {
// This field is marked optional for backwards compatibility with proto2.
// Users should make sure to always set this.
optional uint64 expire = 1; // Unix expiration time (UTC)
repeated bytes addrs = 2; // relay addrs for reserving peer
optional bytes voucher = 3; // reservation voucher
}

message Limit {
optional uint32 duration = 1; // seconds
optional uint64 data = 2; // bytes
}

enum Status {
// zero value field required for proto3 compatibility
UNUSED = 0;
OK = 100;
RESERVATION_REFUSED = 200;
RESOURCE_LIMIT_EXCEEDED = 201;
PERMISSION_DENIED = 202;
CONNECTION_FAILED = 203;
NO_RESERVATION = 204;
MALFORMED_MESSAGE = 400;
UNEXPECTED_MESSAGE = 401;
}
9 changes: 9 additions & 0 deletions transports/circuit-relay-v2/src/protocol_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// Protocol ID for the Hop protocol.
pub const RELAY_V2_HOP_CODEC: &str = "/libp2p/circuit/relay/0.2.0/hop";
/// Protocol ID for the Stop protocol.
pub const RELAY_V2_STOP_CODEC: &str = "/libp2p/circuit/relay/0.2.0/stop";

/// Voucher envelope domain.
pub const RELAY_VOUCHER_DOMAIN: &str = "libp2p-relay-rsvp";
/// Voucher Multicodec code.
pub const RELAY_VOUCHER_CODEC: u64 = 0x0302;
2 changes: 1 addition & 1 deletion transports/webrtc-websys/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fn main() {
prost_build::compile_protos(&["src/browser/proto/message.proto"], &["src/browser/proto"])
.unwrap();
}
}
6 changes: 3 additions & 3 deletions transports/webrtc-websys/src/browser/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
mod signaling;
mod transport;
mod stream;
mod transport;

pub mod pb {
include!(concat!(env!("OUT_DIR"), "/signaling.rs"));
}

pub use self::{
signaling::{Signaling, SIGNALING_PROTOCOL_ID},
stream::ProtobufStream,
transport::BrowserTransport,
stream::ProtobufStream
};
};
Loading
Loading