Enable passing in local IP, refactored IP detection (#48)

* Enable passing in local IP, refactored IP detection

* update docker server instructions
This commit is contained in:
David Zhao
2021-07-11 08:42:11 -07:00
committed by GitHub
parent 4c54234954
commit a80e8cf69f
13 changed files with 199 additions and 129 deletions
+4 -94
View File
@@ -1,18 +1,13 @@
package routing
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"
"github.com/livekit/livekit-server/pkg/config"
livekit "github.com/livekit/livekit-server/proto"
@@ -30,22 +25,16 @@ type NodeStats struct {
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
}
hostname, err := os.Hostname()
if err != nil {
return nil, err
}
if conf.RTC.NodeIP == "" {
return nil, ErrIPNotSet
}
return &livekit.Node{
Id: fmt.Sprintf("%s%s", utils.NodePrefix, HashedID(hostname)[:8]),
Ip: ip,
Ip: conf.RTC.NodeIP,
NumCpus: uint32(runtime.NumCPU()),
Stats: &livekit.NodeStats{
StartedAt: time.Now().Unix(),
@@ -54,59 +43,6 @@ func NewLocalNode(conf *config.Config) (LocalNode, error) {
}, nil
}
func GetLocalIP(stunServers []string) (string, error) {
if len(stunServers) == 0 {
return "", errors.New("STUN servers are required but not defined")
}
c, err := stun.Dial("udp4", stunServers[0])
if err != nil {
return "", err
}
defer c.Close()
message, err := stun.Build(stun.TransactionID, stun.BindingRequest)
if err != nil {
return "", err
}
var stunErr error
// sufficiently large buffer to not block it
ipChan := make(chan string, 20)
err = c.Start(message, func(res stun.Event) {
if res.Error != nil {
stunErr = res.Error
return
}
var xorAddr stun.XORMappedAddress
if err := xorAddr.GetFrom(res.Message); err != nil {
stunErr = err
return
}
ip := xorAddr.IP.To4()
if ip != nil {
ipChan <- ip.String()
}
})
if err != nil {
return "", err
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
select {
case nodeIP := <-ipChan:
return nodeIP, nil
case <-ctx.Done():
msg := "could not determine public IP"
if stunErr != nil {
return "", errors.Wrap(stunErr, msg)
} else {
return "", fmt.Errorf(msg)
}
}
}
// Creates a hashed ID from a unique string
func HashedID(id string) string {
h := sha1.New()
@@ -115,29 +51,3 @@ 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")
}