From d6a60c73d0d45ae7577dfd29fb9421588bc5381e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Tue, 8 Mar 2022 13:54:41 +0100 Subject: [PATCH] UPnP/SSDP Server: allow advertising slice of URLs This makes it possible for the Manager to expose multiple URLs. This way the Worker can try them out and see which ones work. --- cmd/flamenco-manager/main.go | 25 +++++++++++++++++++------ internal/upnp_ssdp/server.go | 14 ++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/cmd/flamenco-manager/main.go b/cmd/flamenco-manager/main.go index 6960681e..ba87a201 100644 --- a/cmd/flamenco-manager/main.go +++ b/cmd/flamenco-manager/main.go @@ -31,6 +31,7 @@ import ( "git.blender.org/flamenco/internal/manager/swagger_ui" "git.blender.org/flamenco/internal/manager/task_logs" "git.blender.org/flamenco/internal/manager/task_state_machine" + "git.blender.org/flamenco/internal/own_url" "git.blender.org/flamenco/internal/upnp_ssdp" "git.blender.org/flamenco/pkg/api" ) @@ -67,12 +68,7 @@ func main() { _, port, _ := net.SplitHostPort(listen) log.Info().Str("port", port).Msg("listening") - ssdp, err := upnp_ssdp.NewServer(log.Logger) - if err != nil { - log.Error().Err(err).Msg("error creating UPnP/SSDP server") - } else { - ssdp.AddAdvertisement(listen) // TODO: convert this to an entire URL. - } + ssdp := makeAutoDiscoverable("http", listen) // Construct the services. persist := openDB(*configService) @@ -271,3 +267,20 @@ func installSignalHandler(cancelFunc context.CancelFunc) { } }() } + +func makeAutoDiscoverable(scheme, listen string) *upnp_ssdp.Server { + urls, err := own_url.AvailableURLs("http", listen) + if err != nil { + log.Error().Err(err).Msg("unable to figure out my own URL") + return nil + } + + ssdp, err := upnp_ssdp.NewServer(log.Logger) + if err != nil { + log.Error().Err(err).Msg("error creating UPnP/SSDP server") + return nil + } + + ssdp.AddAdvertisementURLs(urls) + return ssdp +} diff --git a/internal/upnp_ssdp/server.go b/internal/upnp_ssdp/server.go index 32a92de8..8c235219 100644 --- a/internal/upnp_ssdp/server.go +++ b/internal/upnp_ssdp/server.go @@ -22,6 +22,8 @@ package upnp_ssdp import ( "context" + "net/url" + "strings" "github.com/fromkeith/gossdp" "github.com/rs/zerolog" @@ -54,6 +56,18 @@ func (s *Server) AddAdvertisement(serviceLocation string) { MaxAge: 3600, // Number of seconds this advertisement is valid for. } s.ssdp.AdvertiseServer(serverDef) + s.log.Info().Str("location", serviceLocation).Msg("UPnP/SSDP location registered") +} + +// AddAdvertisementURLs constructs a service location from the given URLs, and +// adds the advertisement for it. +func (s *Server) AddAdvertisementURLs(urls []url.URL) { + urlStrings := make([]string, len(urls)) + for idx := range urls { + urlStrings[idx] = urls[idx].String() + } + location := strings.Join(urlStrings, LocationSeparator) + s.AddAdvertisement(location) } // Run starts the advertisement, and blocks until the context is closed.