Skip to content

Commit

Permalink
feat: add WebRTC Direct support
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Nov 3, 2023
1 parent f1fec71 commit 9f3251c
Show file tree
Hide file tree
Showing 9 changed files with 248 additions and 5 deletions.
11 changes: 6 additions & 5 deletions config/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,13 @@ type Transports struct {
// listen on a transport, add the transport to your Addresses.Swarm.
Network struct {
// All default to on.
QUIC Flag `json:",omitempty"`
TCP Flag `json:",omitempty"`
Websocket Flag `json:",omitempty"`
Relay Flag `json:",omitempty"`
// except WebTransport which is experimental and optin.
QUIC Flag `json:",omitempty"`
TCP Flag `json:",omitempty"`
Websocket Flag `json:",omitempty"`
Relay Flag `json:",omitempty"`
WebTransport Flag `json:",omitempty"`
// except WebRTCDirect which is experimental and opt-in.
WebRTCDirect Flag `json:",omitempty"`
}

// Security specifies the transports used to encrypt insecure network
Expand Down
10 changes: 10 additions & 0 deletions core/node/libp2p/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/libp2p/go-libp2p/core/metrics"
quic "github.com/libp2p/go-libp2p/p2p/transport/quic"
"github.com/libp2p/go-libp2p/p2p/transport/tcp"
webrtc "github.com/libp2p/go-libp2p/p2p/transport/webrtc"
"github.com/libp2p/go-libp2p/p2p/transport/websocket"
webtransport "github.com/libp2p/go-libp2p/p2p/transport/webtransport"

Expand Down Expand Up @@ -49,6 +50,15 @@ func Transports(tptConfig config.Transports) interface{} {
opts.Opts = append(opts.Opts, libp2p.Transport(webtransport.New))
}

if tptConfig.Network.WebRTCDirect.WithDefault(false) {
if privateNetworkEnabled {
return opts, fmt.Errorf(
"WebRTC Direct transport does not support private networks, please disable Swarm.Transports.Network.WebRTCDirect",
)
}
opts.Opts = append(opts.Opts, libp2p.Transport(webrtc.New))
}

return opts, nil
}
}
Expand Down
12 changes: 12 additions & 0 deletions docs/changelogs/v0.24.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- [Gateway: the root of the CARs are no longer meaningful](#gateway-the-root-of-the-cars-are-no-longer-meaningful)
- [IPNS: improved publishing defaults](#ipns-improved-publishing-defaults)
- [IPNS: record TTL is used for caching](#ipns-record-ttl-is-used-for-caching)
- [WebRTC Direct Transport](#webrtc-direct-transport)
- [📝 Changelog](#-changelog)
- [👨‍👩‍👧‍👦 Contributors](#-contributors)

Expand Down Expand Up @@ -68,6 +69,17 @@ DHT, for updates. The TTL value in the IPNS record now serves as a hint for:
These changes make it easier for rarely updated IPNS-hosted websites to be
cached more efficiently and load faster in browser contexts.

#### WebRTC Direct Transport

[WebRTC Direct](https://github.com/libp2p/go-libp2p/pull/2337) has now been introduced
in [`go-libp2p`](https://github.com/libp2p/go-libp2p/releases/tag/v0.32.0) v0.32.0. This
transport protocol allows browser nodes to connect to other nodes without special configuration,
such as TLS certificates. This can be useful for browsers that do not yet support
WebTransport, for example.

Note that, at the moment, WebRTC Direct cannot be used to connect to a browser node to a
node that is behind a NAT or firewall. This is being worked on [`go-libp2p#2009`](https://github.com/libp2p/go-libp2p/issues/2009).

### 📝 Changelog

### 👨‍👩‍👧‍👦 Contributors
14 changes: 14 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ config file at runtime.
- [`Swarm.Transports.Network.QUIC`](#swarmtransportsnetworkquic)
- [`Swarm.Transports.Network.Relay`](#swarmtransportsnetworkrelay)
- [`Swarm.Transports.Network.WebTransport`](#swarmtransportsnetworkwebtransport)
- [`Swarm.Transports.Network.WebRTCDirect`](#swarmtransportsnetworkwebrtcdirect)
- [`Swarm.Transports.Security`](#swarmtransportssecurity)
- [`Swarm.Transports.Security.TLS`](#swarmtransportssecuritytls)
- [`Swarm.Transports.Security.SECIO`](#swarmtransportssecuritysecio)
Expand Down Expand Up @@ -2072,6 +2073,19 @@ Default: Enabled

Type: `flag`

#### `Swarm.Transports.Network.WebRTCDirect`

A new feature of [`go-libp2p`](https://github.com/libp2p/go-libp2p/releases/tag/v0.32.0)
is the [WebRTC Direct](https://github.com/libp2p/go-libp2p/pull/2337) transport.

WebRTC Direct is a transport protocol that provides another way for browsers to connect to the rest of the libp2p network. WebRTC Direct allows for browser nodes to connect to other nodes without special configuration, such as TLS certificates. This can be useful for browser nodes that do not yet support WebTransport, for example.

Note that, at the moment, WebRTC Direct cannot be used to connect to a browser node to a node that is behind a NAT or firewall. This is being worked on [`go-libp2p#2009`](https://github.com/libp2p/go-libp2p/issues/2009).

Default: Disabled

Type: `flag`

### `Swarm.Transports.Security`

Configuration section for libp2p _security_ transports. Transports enabled in
Expand Down
19 changes: 19 additions & 0 deletions docs/examples/kubo-as-a-library/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ require (
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/crackcomm/go-gitignore v0.0.0-20170627025303-887ab5e44cc3 // indirect
github.com/cskr/pubsub v1.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davidlazar/go-crypto v0.0.0-20200604182044-b73af7476f6c // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/dgraph-io/badger v1.6.2 // indirect
Expand Down Expand Up @@ -142,7 +143,24 @@ require (
github.com/openzipkin/zipkin-go v0.4.1 // indirect
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
github.com/petar/GoLLRB v0.0.0-20210522233825-ae3b015fd3e9 // indirect
github.com/pion/datachannel v1.5.5 // indirect
github.com/pion/dtls/v2 v2.2.7 // indirect
github.com/pion/ice/v2 v2.3.6 // indirect
github.com/pion/interceptor v0.1.17 // indirect
github.com/pion/logging v0.2.2 // indirect
github.com/pion/mdns v0.0.7 // indirect
github.com/pion/randutil v0.1.0 // indirect
github.com/pion/rtcp v1.2.10 // indirect
github.com/pion/rtp v1.7.13 // indirect
github.com/pion/sctp v1.8.7 // indirect
github.com/pion/sdp/v3 v3.0.6 // indirect
github.com/pion/srtp/v2 v2.0.15 // indirect
github.com/pion/stun v0.6.0 // indirect
github.com/pion/transport/v2 v2.2.1 // indirect
github.com/pion/turn/v2 v2.1.0 // indirect
github.com/pion/webrtc/v3 v3.2.9 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/polydawn/refmt v0.89.0 // indirect
github.com/prometheus/client_golang v1.17.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
Expand All @@ -155,6 +173,7 @@ require (
github.com/raulk/go-watchdog v1.3.0 // indirect
github.com/samber/lo v1.36.0 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
github.com/ucarion/urlpath v0.0.0-20200424170820-7ccc79b76bbb // indirect
github.com/whyrusleeping/base32 v0.0.0-20170828182744-c30ac30633cc // indirect
Expand Down
Loading

0 comments on commit 9f3251c

Please sign in to comment.