Skip to content

Commit

Permalink
interop: run with webrtc
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunrt committed Sep 26, 2023
1 parent 7a47233 commit d091c75
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
50 changes: 40 additions & 10 deletions test-plans/cmd/ping/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import (
"github.com/go-redis/redis/v8"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/peer"

"github.com/libp2p/go-libp2p/p2p/muxer/yamux"
"github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/client"
"github.com/libp2p/go-libp2p/p2p/protocol/ping"
"github.com/libp2p/go-libp2p/p2p/security/noise"
libp2ptls "github.com/libp2p/go-libp2p/p2p/security/tls"
Expand All @@ -42,7 +44,6 @@ func main() {
redisAddr = os.Getenv("redis_addr")
testTimeoutStr = os.Getenv("test_timeout_seconds")
)

testTimeout := 3 * time.Minute
if testTimeoutStr != "" {
secs, err := strconv.ParseInt(testTimeoutStr, 10, 32)
Expand Down Expand Up @@ -75,7 +76,6 @@ func main() {
if ctx.Err() != nil {
log.Fatal("timeout waiting for redis")
}

// Wait for redis to be ready
_, err := rClient.Ping(ctx).Result()
if err == nil {
Expand Down Expand Up @@ -108,10 +108,15 @@ func main() {
case "webrtc-direct":
options = append(options, libp2p.Transport(libp2pwebrtc.New))
listenAddr = fmt.Sprintf("/ip4/%s/udp/0/webrtc-direct", ip)
case "webrtc":
options = append(options, libp2p.Transport(websocket.New), libp2p.EnableWebRTCPrivate(nil), libp2p.EnableRelay())
default:
log.Fatalf("Unsupported transport: %s", transport)
}
options = append(options, libp2p.ListenAddrStrings(listenAddr))
if listenAddr != "" {
// We do not set listenAddr here for /webrtc addresses
options = append(options, libp2p.ListenAddrStrings(listenAddr))
}

// Skipped for certain transports
var skipMuxer bool
Expand Down Expand Up @@ -145,9 +150,7 @@ func main() {
log.Fatalf("Unsupported muxer: %s", muxer)
}
}

host, err := libp2p.New(options...)

if err != nil {
log.Fatalf("failed to instantiate libp2p instance: %s", err)
}
Expand All @@ -174,7 +177,7 @@ func main() {
Addrs: []ma.Multiaddr{otherMa},
})
if err != nil {
log.Fatal("Failed to connect to other peer")
log.Fatal("Failed to connect to other peer", err)
}

ping := ping.NewPingService(host)
Expand All @@ -200,10 +203,37 @@ func main() {
fmt.Println(string(testResultJSON))
} else {
var listenAddr ma.Multiaddr
for _, addr := range host.Addrs() {
if !manet.IsIPLoopback(addr) {
listenAddr = addr
break
if transport == "webrtc" {
// /ws so browsers can dial us
relayHost, err := libp2p.New(libp2p.Transport(websocket.New),
libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/0/ws"),
libp2p.ForceReachabilityPublic(),
libp2p.EnableRelayService(),
)
if err != nil {
log.Fatalf("failed to create relay host: %v", err)
}
defer relayHost.Close()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
_, err = client.Reserve(ctx, host, peer.AddrInfo{ID: relayHost.ID(), Addrs: relayHost.Addrs()})
if err != nil {
log.Fatalf("failed to obtain relay reservation: %v", err)
}
for _, addr := range relayHost.Addrs() {
if !manet.IsIPLoopback(addr) {
listenAddr = ma.StringCast(
fmt.Sprintf("%s/p2p/%s/p2p-circuit/webrtc", addr, relayHost.ID()),
)
break
}
}
} else {
for _, addr := range host.Addrs() {
if !manet.IsIPLoopback(addr) {
listenAddr = addr
break
}
}
}
_, err := rClient.RPush(ctx, "listenerAddr", listenAddr.Encapsulate(ma.StringCast("/p2p/"+host.ID().String())).String()).Result()
Expand Down
3 changes: 2 additions & 1 deletion test-plans/ping-version.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"wss",
"quic-v1",
"webtransport",
"webrtc-direct"
"webrtc-direct",
"webrtc"
],
"secureChannels": [
"tls",
Expand Down

0 comments on commit d091c75

Please sign in to comment.