for test environments, allow use of internal IP if external IP isn't available

This commit is contained in:
David Zhao
2021-06-30 15:14:58 -07:00
parent cc30d2e025
commit 4e6eafa63c
+33
View File
@@ -4,11 +4,13 @@ import (
"context" "context"
"crypto/sha1" "crypto/sha1"
"fmt" "fmt"
"net"
"os" "os"
"runtime" "runtime"
"time" "time"
"github.com/jxskiss/base62" "github.com/jxskiss/base62"
"github.com/livekit/livekit-server/pkg/logger"
"github.com/pion/stun" "github.com/pion/stun"
"github.com/pkg/errors" "github.com/pkg/errors"
@@ -29,6 +31,11 @@ type LocalNode *livekit.Node
func NewLocalNode(conf *config.Config) (LocalNode, error) { func NewLocalNode(conf *config.Config) (LocalNode, error) {
ip, err := GetLocalIP(conf.RTC.StunServers) 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 { if err != nil {
return nil, err return nil, err
} }
@@ -108,3 +115,29 @@ func HashedID(id string) string {
return base62.EncodeToString(val) 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")
}