From d4f30c126f3110490adbb6e8b38e3cc3bc0e6875 Mon Sep 17 00:00:00 2001 From: David Zhao Date: Sat, 5 Dec 2020 23:36:45 -0800 Subject: [PATCH] Fix node IP discovery, force to ipv4 --- README.md | 17 ++++++++++++++--- cmd/cli/commands/utils.go | 2 +- pkg/node/node.go | 40 +++++++++++++++++++++++++++++---------- 3 files changed, 45 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index f7f1a6f41..3813fa565 100644 --- a/README.md +++ b/README.md @@ -24,13 +24,24 @@ To run a peer publishing to a room, do the following: 2. Create a room ``` - ./bin/livekit-cli create-room --room-id hello + ./bin/livekit-cli create-room ``` + + It'll print out something like this. note the room id + + ```json + { + "sid": "RM_CkjigXb6oZQyZ4JNFZqBen", + "node_ip": "98.35.19.21", + "creation_time": 1607240104, + "token": "b9e8c9f6-fbb3-46d5-b6fc-5517186510a6" + } + ``` 3. Join room as publishing client ``` - ./ bin/livekit-cli join --room-id hello --audio --video + ./ bin/livekit-cli join --audio --video --room-id ``` That's it, join the room with another peer id and see it receiving those tracks @@ -48,7 +59,7 @@ see `rtc.proto` for the message structure. The flow for interaction is: 1. Establish WebSocket to ws://:/rtc -1. Server will send back a `SignalResponse` with a `join` response. It'll include the new participant's details (and in the future, room info) +1. Server will send back a `SignalResponse` with a `join` response. It'll include the new participant's details, and what other participants are in the room 1. Client sends a `SignalRequest` with an WebRTC `offer` 1. Server will send back a `SignalResponse` with an `answer` 1. Client and server will exchange ice candidates via `trickle` in the request & responses diff --git a/cmd/cli/commands/utils.go b/cmd/cli/commands/utils.go index 0fae82411..5266ea026 100644 --- a/cmd/cli/commands/utils.go +++ b/cmd/cli/commands/utils.go @@ -26,7 +26,7 @@ var ( ) func PrintJSON(obj interface{}) { - txt, _ := json.Marshal(obj) + txt, _ := json.MarshalIndent(obj, "", " ") fmt.Println(string(txt)) } diff --git a/pkg/node/node.go b/pkg/node/node.go index 1dfd8d384..f3cc8edbc 100644 --- a/pkg/node/node.go +++ b/pkg/node/node.go @@ -1,18 +1,19 @@ package node import ( - "errors" + "context" + "fmt" + "time" "github.com/google/wire" "github.com/pion/stun" + "github.com/pkg/errors" "github.com/livekit/livekit-server/pkg/config" "github.com/livekit/livekit-server/pkg/utils" "github.com/livekit/livekit-server/proto/livekit" ) -const () - var NodeSet = wire.NewSet(NewLocalNode) type Node struct { @@ -46,7 +47,7 @@ func (n *Node) DiscoverNetworkInfo() error { if len(n.config.RTC.StunServers) == 0 { return errors.New("STUN servers are required but not defined") } - c, err := stun.Dial("udp", n.config.RTC.StunServers[0]) + c, err := stun.Dial("udp4", n.config.RTC.StunServers[0]) if err != nil { return err } @@ -58,7 +59,7 @@ func (n *Node) DiscoverNetworkInfo() error { } var stunErr error - err = c.Do(message, func(res stun.Event) { + err = c.Start(message, func(res stun.Event) { if res.Error != nil { stunErr = res.Error return @@ -69,11 +70,30 @@ func (n *Node) DiscoverNetworkInfo() error { stunErr = err return } - n.Ip = xorAddr.IP.String() + ip := xorAddr.IP.To4() + if ip != nil { + n.Ip = ip.String() + } }) - - if stunErr != nil { - err = stunErr + if err != nil { + return err } - return err + + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + for n.Ip == "" { + select { + case <-ctx.Done(): + msg := "could not determine public IP" + if stunErr != nil { + return errors.Wrap(stunErr, msg) + } else { + return fmt.Errorf(msg) + } + case <-time.After(100 * time.Millisecond): + continue + } + } + + return nil }