Skip to content

Commit

Permalink
libp2p: use webrtc.iceserver for configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunrt committed Sep 26, 2023
1 parent 87f2c2f commit 7a47233
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
7 changes: 5 additions & 2 deletions config/config.go
Expand Up @@ -35,6 +35,7 @@ import (
"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/pion/webrtc/v3"
"github.com/prometheus/client_golang/prometheus"

ma "github.com/multiformats/go-multiaddr"
Expand Down Expand Up @@ -66,6 +67,8 @@ type Security struct {
Constructor interface{}
}

type ICEServer = webrtc.ICEServer

// Config describes a set of settings for a libp2p node
//
// This is *not* a stable interface. Use the options defined in the root
Expand Down Expand Up @@ -131,7 +134,7 @@ type Config struct {
SwarmOpts []swarm.Option

WebRTCPrivate bool
WebRTCStunServers []string
WebRTCStunServers []ICEServer
}

func (cfg *Config) makeSwarm(eventBus event.Bus, enableMetrics bool) (*swarm.Swarm, error) {
Expand Down Expand Up @@ -212,7 +215,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 }),
fx.Provide(func() []ICEServer { return cfg.WebRTCStunServers }),
}
fxopts = append(fxopts, cfg.Transports...)
if cfg.Insecure {
Expand Down
2 changes: 1 addition & 1 deletion options.go
Expand Up @@ -598,7 +598,7 @@ func SwarmOpts(opts ...swarm.Option) Option {
}
}

func EnableWebRTCPrivate(stunServers []string) Option {
func EnableWebRTCPrivate(stunServers []config.ICEServer) Option {
return func(cfg *Config) error {
cfg.WebRTCPrivate = true
cfg.WebRTCStunServers = stunServers
Expand Down
10 changes: 3 additions & 7 deletions p2p/transport/webrtcprivate/transport.go
Expand Up @@ -60,7 +60,7 @@ type transport struct {

var _ tpt.Transport = &transport{}

func AddTransport(h host.Host, gater connmgr.ConnectionGater, stunServers []string) (*transport, error) {
func AddTransport(h host.Host, gater connmgr.ConnectionGater, stunServers []webrtc.ICEServer) (*transport, error) {
n, ok := h.Network().(tpt.TransportNetwork)
if !ok {
return nil, fmt.Errorf("%v is not a transport network", h.Network())
Expand All @@ -82,7 +82,7 @@ func AddTransport(h host.Host, gater connmgr.ConnectionGater, stunServers []stri
return t, nil
}

func newTransport(h host.Host, gater connmgr.ConnectionGater, stunServers []string) (*transport, error) {
func newTransport(h host.Host, gater connmgr.ConnectionGater, stunServers []webrtc.ICEServer) (*transport, error) {
// We use elliptic P-256 since it is widely supported by browsers.
//
// Implementation note: Testing with the browser,
Expand All @@ -102,13 +102,9 @@ func newTransport(h host.Host, gater connmgr.ConnectionGater, stunServers []stri
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,
ICEServers: stunServers,
}

return &transport{
Expand Down

0 comments on commit 7a47233

Please sign in to comment.