From d56bef499bd1510b27615105a448cbbb93b5cbd8 Mon Sep 17 00:00:00 2001 From: sukun Date: Mon, 25 Sep 2023 15:18:37 +0530 Subject: [PATCH] libp2p: provide option for enabling webrtcprivate --- config/config.go | 8 ++++++++ options.go | 8 ++++++++ p2p/test/swarm/swarm_test.go | 8 ++------ p2p/transport/webrtcprivate/transport.go | 14 +++++++++++--- p2p/transport/webrtcprivate/transport_test.go | 4 ++-- 5 files changed, 31 insertions(+), 11 deletions(-) diff --git a/config/config.go b/config/config.go index bb3a121356..9dca6ca25e 100644 --- a/config/config.go +++ b/config/config.go @@ -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" @@ -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) { @@ -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 { @@ -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() diff --git a/options.go b/options.go index beb4930f7c..4be32cc877 100644 --- a/options.go +++ b/options.go @@ -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 + } +} diff --git a/p2p/test/swarm/swarm_test.go b/p2p/test/swarm/swarm_test.go index ecc3061ef6..d8e901ffd8 100644 --- a/p2p/test/swarm/swarm_test.go +++ b/p2p/test/swarm/swarm_test.go @@ -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" @@ -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) @@ -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()) diff --git a/p2p/transport/webrtcprivate/transport.go b/p2p/transport/webrtcprivate/transport.go index 67d16909bf..c3c9d7b4a4 100644 --- a/p2p/transport/webrtcprivate/transport.go +++ b/p2p/transport/webrtcprivate/transport.go @@ -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 } @@ -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, @@ -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{ diff --git a/p2p/transport/webrtcprivate/transport_test.go b/p2p/transport/webrtcprivate/transport_test.go index 5d3e69ae11..f9d889cee2 100644 --- a/p2p/transport/webrtcprivate/transport_test.go +++ b/p2p/transport/webrtcprivate/transport_test.go @@ -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, @@ -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{