From 2668073c292560b27f5b791786eef56e2de95d6a Mon Sep 17 00:00:00 2001 From: Juan Navarro Date: Thu, 29 Jun 2023 01:52:43 +0200 Subject: [PATCH] Honor bind address passed as `--bind` also for RTC ports (#1815) * Use net.JoinHostPort to build "host:port" strings for `net.Listen` net.JoinHostPort provides a unified way of building strings of the form "Host:Port", abstracting the particular syntax requirements of some methods in the `net` package (namely, that IPv4 addresses can be given as-is to `net.Listen`, but IPv6 addresses must be given enclosed in square brackets). This change makes sense because an address such as `[::1]` is *not* a valid IPv6 address; the square brackets are just a detail particular to the Go `net` library. As such, this syntax shouldn't be exposed to the user, and configuration should just accept valid IPv6 addresses and convert them as needed for usage within the code. * Use '--bind' CLI flag to also filter RTC bind address The local address passed to a command such as livekit-server --dev --bind 127.0.0.1 was being used as binding address for the TCP WebSocket port, but was being ignored for RTC connections. With `--dev`, the conf.RTC.UDPPort config is set to 7882, which enables "UDP muxing" mechanism. Without interface or address filtering, Pion would try to bind to port 7882 on *all* interfaces. This was failing on a system with IPv6 enabled, when trying to bind to an IPv6 address of the `docker0` interface. It seems to make sense that the user-passed bind addresses are also honored for the RTC port bindings. --- cmd/server/main.go | 5 ++++- pkg/service/server.go | 5 +++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/cmd/server/main.go b/cmd/server/main.go index 29b8a6588..5e93b7cd2 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -208,9 +208,12 @@ func getConfig(c *cli.Context) (*config.Config, error) { if conf.BindAddresses == nil { conf.BindAddresses = []string{ "127.0.0.1", - "[::1]", + "::1", } } + for _, bindAddr := range conf.BindAddresses { + conf.RTC.IPs.Includes = append(conf.RTC.IPs.Includes, bindAddr + "/24") + } } } return conf, nil diff --git a/pkg/service/server.go b/pkg/service/server.go index 69e3d404c..c21b98fe0 100644 --- a/pkg/service/server.go +++ b/pkg/service/server.go @@ -10,6 +10,7 @@ import ( _ "net/http/pprof" "runtime" "runtime/pprof" + "strconv" "time" "github.com/pion/turn/v2" @@ -177,14 +178,14 @@ func (s *LivekitServer) Start() error { listeners := make([]net.Listener, 0) promListeners := make([]net.Listener, 0) for _, addr := range addresses { - ln, err := net.Listen("tcp", fmt.Sprintf("%s:%d", addr, s.config.Port)) + ln, err := net.Listen("tcp", net.JoinHostPort(addr, strconv.Itoa(int(s.config.Port)))) if err != nil { return err } listeners = append(listeners, ln) if s.promServer != nil { - ln, err = net.Listen("tcp", fmt.Sprintf("%s:%d", addr, s.config.PrometheusPort)) + ln, err = net.Listen("tcp", net.JoinHostPort(addr, strconv.Itoa(int(s.config.PrometheusPort)))) if err != nil { return err }