arcast/pkg/server/mdns.go

45 lines
904 B
Go
Raw Permalink Normal View History

2023-12-13 20:07:22 +01:00
package server
import (
"context"
"forge.cadoles.com/arcad/arcast/pkg/network"
2023-12-13 20:07:22 +01:00
"github.com/grandcat/zeroconf"
"github.com/pkg/errors"
"github.com/wlynxg/anet"
"gitlab.com/wpetit/goweb/logger"
)
const (
MDNSService = "_arcast._http._tcp"
MDNSDomain = "local."
)
func (s *Server) startMDNServer(ctx context.Context) error {
logger.Debug(ctx, "starting mdns server")
ifaces, err := anet.Interfaces()
if err != nil {
return errors.WithStack(err)
}
ips, err := network.GetLANIPv4Addrs()
2023-12-13 20:07:22 +01:00
if err != nil {
return errors.WithStack(err)
}
logger.Debug(ctx, "advertising ips", logger.F("ips", ips))
server, err := zeroconf.RegisterProxy(s.instanceID, MDNSService, MDNSDomain, s.port, s.instanceID, ips, []string{}, ifaces)
if err != nil {
return errors.WithStack(err)
}
go func() {
<-ctx.Done()
logger.Debug(ctx, "closing mdns server")
server.Shutdown()
}()
return nil
}