Skip to content

Commit

Permalink
libp2p: provide option for enabling webrtcprivate
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunrt committed Sep 25, 2023
1 parent 5472eb9 commit d56bef4
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 11 deletions.
8 changes: 8 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
relayv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
"github.com/libp2p/go-libp2p/p2p/protocol/holepunch"
"github.com/libp2p/go-libp2p/p2p/transport/quicreuse"
libp2pwebrtcprivate "github.com/libp2p/go-libp2p/p2p/transport/webrtcprivate"
"github.com/prometheus/client_golang/prometheus"

ma "github.com/multiformats/go-multiaddr"
Expand Down Expand Up @@ -128,6 +129,9 @@ type Config struct {
DialRanker network.DialRanker

SwarmOpts []swarm.Option

WebRTCPrivate bool
WebRTCStunServers []string
}

func (cfg *Config) makeSwarm(eventBus event.Bus, enableMetrics bool) (*swarm.Swarm, error) {
Expand Down Expand Up @@ -208,6 +212,7 @@ func (cfg *Config) addTransports(h host.Host) error {
fx.Provide(func() pnet.PSK { return cfg.PSK }),
fx.Provide(func() network.ResourceManager { return cfg.ResourceManager }),
fx.Provide(func() *madns.Resolver { return cfg.MultiaddrResolver }),
fx.Provide(func() []string { return cfg.WebRTCStunServers }),
}
fxopts = append(fxopts, cfg.Transports...)
if cfg.Insecure {
Expand Down Expand Up @@ -283,6 +288,9 @@ func (cfg *Config) addTransports(h host.Host) error {
if cfg.Relay {
fxopts = append(fxopts, fx.Invoke(circuitv2.AddTransport))
}
if cfg.WebRTCPrivate {
fxopts = append(fxopts, fx.Invoke(libp2pwebrtcprivate.AddTransport))
}
app := fx.New(fxopts...)
if err := app.Err(); err != nil {
h.Close()
Expand Down
8 changes: 8 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,3 +597,11 @@ func SwarmOpts(opts ...swarm.Option) Option {
return nil
}
}

func EnableWebRTCPrivate(stunServers []string) Option {
return func(cfg *Config) error {
cfg.WebRTCPrivate = true
cfg.WebRTCStunServers = stunServers
return nil
}
}
8 changes: 2 additions & 6 deletions p2p/test/swarm/swarm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/libp2p/go-libp2p/core/peerstore"
"github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/client"
"github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
libp2pwebrtcprivate "github.com/libp2p/go-libp2p/p2p/transport/webrtcprivate"
ma "github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -75,12 +74,14 @@ func TestDialPeerWebRTC(t *testing.T) {
h1, err := libp2p.New(
libp2p.NoListenAddrs,
libp2p.EnableRelay(),
libp2p.EnableWebRTCPrivate(nil),
)
require.NoError(t, err)

h2, err := libp2p.New(
libp2p.NoListenAddrs,
libp2p.EnableRelay(),
libp2p.EnableWebRTCPrivate(nil),
)
require.NoError(t, err)

Expand All @@ -101,11 +102,6 @@ func TestDialPeerWebRTC(t *testing.T) {
_, err = client.Reserve(context.Background(), h2, relay1info)
require.NoError(t, err)

_, err = libp2pwebrtcprivate.AddTransport(h1, nil)
require.NoError(t, err)
_, err = libp2pwebrtcprivate.AddTransport(h2, nil)
require.NoError(t, err)

webrtcAddr := ma.StringCast(relay1info.Addrs[0].String() + "/p2p/" + relay1info.ID.String() + "/p2p-circuit/webrtc/p2p/" + h2.ID().String())
relayAddrs := ma.StringCast(relay1info.Addrs[0].String() + "/p2p/" + relay1info.ID.String() + "/p2p-circuit/p2p/" + h2.ID().String())

Expand Down
14 changes: 11 additions & 3 deletions p2p/transport/webrtcprivate/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,16 @@ type transport struct {

var _ tpt.Transport = &transport{}

func AddTransport(h host.Host, gater connmgr.ConnectionGater) (*transport, error) {
func AddTransport(h host.Host, gater connmgr.ConnectionGater, stunServers []string) (*transport, error) {
fmt.Println(h)
fmt.Println("gater", gater)
fmt.Println("stun", stunServers)
n, ok := h.Network().(tpt.TransportNetwork)
if !ok {
return nil, fmt.Errorf("%v is not a transport network", h.Network())
}

t, err := newTransport(h, gater)
t, err := newTransport(h, gater, stunServers)
if err != nil {
return nil, err
}
Expand All @@ -82,7 +85,7 @@ func AddTransport(h host.Host, gater connmgr.ConnectionGater) (*transport, error
return t, nil
}

func newTransport(h host.Host, gater connmgr.ConnectionGater) (*transport, error) {
func newTransport(h host.Host, gater connmgr.ConnectionGater, stunServers []string) (*transport, error) {
// We use elliptic P-256 since it is widely supported by browsers.
//
// Implementation note: Testing with the browser,
Expand All @@ -102,8 +105,13 @@ func newTransport(h host.Host, gater connmgr.ConnectionGater) (*transport, error
if err != nil {
return nil, fmt.Errorf("generate certificate: %w", err)
}
servers := make([]webrtc.ICEServer, len(stunServers))
for i := 0; i < len(stunServers); i++ {
servers[i] = webrtc.ICEServer{URLs: []string{stunServers[i]}}
}
config := webrtc.Configuration{
Certificates: []webrtc.Certificate{*cert},
ICEServers: servers,
}

return &transport{
Expand Down
4 changes: 2 additions & 2 deletions p2p/transport/webrtcprivate/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func newWebRTCHost(t *testing.T) *webrtcHost {
upg := swarmt.GenUpgrader(t, as, nil)
err := client.AddTransport(a, upg)
require.NoError(t, err)
ta, err := newTransport(a, nil)
ta, err := newTransport(a, nil, nil)
require.NoError(t, err)
return &webrtcHost{
Host: a,
Expand All @@ -68,7 +68,7 @@ func newRelayedHost(t *testing.T) *relayedHost {
client.AddTransport(p, upg)
_, err = client.Reserve(context.Background(), p, peer.AddrInfo{ID: rh.ID(), Addrs: rh.Addrs()})
require.NoError(t, err)
tp, err := newTransport(p, nil)
tp, err := newTransport(p, nil, nil)
require.NoError(t, err)
return &relayedHost{
webrtcHost: webrtcHost{
Expand Down

0 comments on commit d56bef4

Please sign in to comment.