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:
+43
-11
@@ -1,11 +1,13 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/mitchellh/go-homedir"
|
||||
"github.com/pion/webrtc/v3"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/urfave/cli/v2"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
@@ -30,6 +32,7 @@ type RTCConfig struct {
|
||||
TCPPort uint32 `yaml:"tcp_port"`
|
||||
ICEPortRangeStart uint32 `yaml:"port_range_start"`
|
||||
ICEPortRangeEnd uint32 `yaml:"port_range_end"`
|
||||
NodeIP string `yaml:"node_ip"`
|
||||
// for testing, disable UDP
|
||||
ForceTCP bool `yaml:"force_tcp"`
|
||||
StunServers []string `yaml:"stun_servers"`
|
||||
@@ -90,7 +93,7 @@ type TURNConfig struct {
|
||||
TLSPort int `yaml:"tls_port"`
|
||||
}
|
||||
|
||||
func NewConfig(confString string) (*Config, error) {
|
||||
func NewConfig(confString string, c *cli.Context) (*Config, error) {
|
||||
// start with defaults
|
||||
conf := &Config{
|
||||
Port: 7880,
|
||||
@@ -98,8 +101,8 @@ func NewConfig(confString string) (*Config, error) {
|
||||
UseExternalIP: false,
|
||||
TCPPort: 7881,
|
||||
UDPPort: 0,
|
||||
ICEPortRangeStart: 50000,
|
||||
ICEPortRangeEnd: 60000,
|
||||
ICEPortRangeStart: 0,
|
||||
ICEPortRangeEnd: 0,
|
||||
StunServers: []string{
|
||||
"stun.l.google.com:19302",
|
||||
"stun1.l.google.com:19302",
|
||||
@@ -136,7 +139,39 @@ func NewConfig(confString string) (*Config, error) {
|
||||
Keys: map[string]string{},
|
||||
}
|
||||
if confString != "" {
|
||||
_ = yaml.Unmarshal([]byte(confString), conf)
|
||||
if err := yaml.Unmarshal([]byte(confString), conf); err != nil {
|
||||
return nil, fmt.Errorf("could not parse config: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if c != nil {
|
||||
if err := conf.updateFromCLI(c); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// expand env vars in filenames
|
||||
file, err := homedir.Expand(os.ExpandEnv(conf.KeyFile))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
conf.KeyFile = file
|
||||
|
||||
// set defaults for ports if none are set
|
||||
if conf.RTC.UDPPort == 0 && conf.RTC.ICEPortRangeStart == 0 {
|
||||
if conf.Development {
|
||||
conf.RTC.UDPPort = 7882
|
||||
} else {
|
||||
conf.RTC.ICEPortRangeStart = 50000
|
||||
conf.RTC.ICEPortRangeEnd = 60000
|
||||
}
|
||||
}
|
||||
|
||||
if conf.RTC.NodeIP == "" {
|
||||
conf.RTC.NodeIP, err = conf.determineIP()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return conf, nil
|
||||
}
|
||||
@@ -145,7 +180,7 @@ func (conf *Config) HasRedis() bool {
|
||||
return conf.Redis.Address != ""
|
||||
}
|
||||
|
||||
func (conf *Config) UpdateFromCLI(c *cli.Context) error {
|
||||
func (conf *Config) updateFromCLI(c *cli.Context) error {
|
||||
if c.IsSet("dev") {
|
||||
conf.Development = c.Bool("dev")
|
||||
}
|
||||
@@ -154,7 +189,7 @@ func (conf *Config) UpdateFromCLI(c *cli.Context) error {
|
||||
}
|
||||
if c.IsSet("keys") {
|
||||
if err := conf.unmarshalKeys(c.String("keys")); err != nil {
|
||||
return err
|
||||
return errors.New("Could not parse keys, it needs to be \"key: secret\", one per line")
|
||||
}
|
||||
}
|
||||
if c.IsSet("redis-host") {
|
||||
@@ -169,12 +204,9 @@ func (conf *Config) UpdateFromCLI(c *cli.Context) error {
|
||||
if c.IsSet("turn-key") {
|
||||
conf.TURN.KeyFile = c.String("turn-key")
|
||||
}
|
||||
// expand env vars in filenames
|
||||
file, err := homedir.Expand(os.ExpandEnv(conf.KeyFile))
|
||||
if err != nil {
|
||||
return err
|
||||
if c.IsSet("node-ip") {
|
||||
conf.RTC.NodeIP = c.String("node-ip")
|
||||
}
|
||||
conf.KeyFile = file
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
)
|
||||
|
||||
func TestConfig_UnmarshalKeys(t *testing.T) {
|
||||
conf, err := NewConfig("")
|
||||
conf, err := NewConfig("", nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.NoError(t, conf.unmarshalKeys("key1: secret1"))
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/logger"
|
||||
"github.com/pion/stun"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func (conf *Config) determineIP() (string, error) {
|
||||
if conf.RTC.UseExternalIP {
|
||||
ip, err := GetExternalIP(conf.RTC.StunServers)
|
||||
if err == nil {
|
||||
return ip, nil
|
||||
} else {
|
||||
logger.Errorw("could not get external IP", err)
|
||||
}
|
||||
}
|
||||
|
||||
// use local ip instead
|
||||
return GetLocalIPAddress()
|
||||
}
|
||||
|
||||
func GetLocalIPAddress() (string, error) {
|
||||
ifaces, err := net.Interfaces()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
// handle err
|
||||
var loopBack string
|
||||
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
|
||||
default:
|
||||
continue
|
||||
}
|
||||
if ip.IsLoopback() {
|
||||
loopBack = ip.String()
|
||||
} else {
|
||||
return ip.String(), nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if loopBack != "" {
|
||||
return loopBack, nil
|
||||
}
|
||||
return "", fmt.Errorf("could not find local IP address")
|
||||
}
|
||||
|
||||
func GetExternalIP(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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user