From 4e6eafa63ca73ae22524a3ff5b1f5f00143f6934 Mon Sep 17 00:00:00 2001 From: David Zhao Date: Wed, 30 Jun 2021 15:14:58 -0700 Subject: [PATCH] for test environments, allow use of internal IP if external IP isn't available --- pkg/routing/node.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/pkg/routing/node.go b/pkg/routing/node.go index 2e6e06d7d..4868e0b60 100644 --- a/pkg/routing/node.go +++ b/pkg/routing/node.go @@ -4,11 +4,13 @@ import ( "context" "crypto/sha1" "fmt" + "net" "os" "runtime" "time" "github.com/jxskiss/base62" + "github.com/livekit/livekit-server/pkg/logger" "github.com/pion/stun" "github.com/pkg/errors" @@ -29,6 +31,11 @@ type LocalNode *livekit.Node func NewLocalNode(conf *config.Config) (LocalNode, error) { ip, err := GetLocalIP(conf.RTC.StunServers) + if err != nil { + logger.Errorw("could not get local IP", err) + // use local ip instead + ip, err = getLocalIPAddress() + } if err != nil { return nil, err } @@ -108,3 +115,29 @@ func HashedID(id string) string { return base62.EncodeToString(val) } + +func getLocalIPAddress() (string, error) { + ifaces, err := net.Interfaces() + if err != nil { + return "", err + } + // handle err + for _, i := range ifaces { + addrs, err := i.Addrs() + if err != nil { + continue + } + for _, addr := range addrs { + var ip net.IP + switch v := addr.(type) { + case *net.IPNet: + ip = v.IP + case *net.IPAddr: + + ip = v.IP + } + return ip.String(), nil + } + } + return "", fmt.Errorf("could not find local IP address") +}