2024-01-13 18:14:16 +01:00
|
|
|
//go:build go1.21
|
|
|
|
|
2022-08-28 13:21:57 +02:00
|
|
|
// Package dlna provides DLNA server.
|
2019-01-03 23:23:50 +01:00
|
|
|
package dlna
|
|
|
|
|
|
|
|
import (
|
2019-05-26 19:35:51 +02:00
|
|
|
"bytes"
|
2019-01-03 23:23:50 +01:00
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-05-26 20:45:01 +02:00
|
|
|
dms_dlna "github.com/anacrolix/dms/dlna"
|
2019-01-03 23:23:50 +01:00
|
|
|
"github.com/anacrolix/dms/soap"
|
|
|
|
"github.com/anacrolix/dms/ssdp"
|
|
|
|
"github.com/anacrolix/dms/upnp"
|
2022-12-16 15:32:39 +01:00
|
|
|
"github.com/anacrolix/log"
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/cmd"
|
|
|
|
"github.com/rclone/rclone/cmd/serve/dlna/data"
|
|
|
|
"github.com/rclone/rclone/cmd/serve/dlna/dlnaflags"
|
|
|
|
"github.com/rclone/rclone/fs"
|
2023-09-04 17:32:04 +02:00
|
|
|
"github.com/rclone/rclone/lib/systemd"
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/vfs"
|
|
|
|
"github.com/rclone/rclone/vfs/vfsflags"
|
2019-01-03 23:23:50 +01:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
dlnaflags.AddFlags(Command.Flags())
|
|
|
|
vfsflags.AddFlags(Command.Flags())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Command definition for cobra.
|
|
|
|
var Command = &cobra.Command{
|
|
|
|
Use: "dlna remote:path",
|
|
|
|
Short: `Serve remote:path over DLNA`,
|
2022-06-19 19:18:14 +02:00
|
|
|
Long: `Run a DLNA media server for media stored in an rclone remote. Many
|
|
|
|
devices, such as the Xbox and PlayStation, can automatically discover
|
|
|
|
this server in the LAN and play audio/video from it. VLC is also
|
|
|
|
supported. Service discovery uses UDP multicast packets (SSDP) and
|
|
|
|
will thus only work on LANs.
|
2019-01-03 23:23:50 +01:00
|
|
|
|
2022-06-19 19:18:14 +02:00
|
|
|
Rclone will list all files present in the remote, without filtering
|
|
|
|
based on media formats or file extensions. Additionally, there is no
|
|
|
|
media transcoding support. This means that some players might show
|
|
|
|
files that they are not able to play back correctly.
|
2019-01-03 23:23:50 +01:00
|
|
|
|
2024-04-05 13:27:33 +02:00
|
|
|
` + dlnaflags.Help + vfs.Help(),
|
2022-11-26 23:40:49 +01:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"versionIntroduced": "v1.46",
|
2023-07-10 19:34:10 +02:00
|
|
|
"groups": "Filter",
|
2022-11-26 23:40:49 +01:00
|
|
|
},
|
2019-01-03 23:23:50 +01:00
|
|
|
Run: func(command *cobra.Command, args []string) {
|
|
|
|
cmd.CheckArgs(1, 1, command, args)
|
|
|
|
f := cmd.NewFsSrc(args)
|
|
|
|
|
|
|
|
cmd.Run(false, false, command, func() error {
|
2022-08-08 18:48:36 +02:00
|
|
|
s, err := newServer(f, &dlnaflags.Opt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-03 23:23:50 +01:00
|
|
|
if err := s.Serve(); err != nil {
|
2019-05-26 20:51:22 +02:00
|
|
|
return err
|
2019-01-03 23:23:50 +01:00
|
|
|
}
|
2023-09-04 17:32:04 +02:00
|
|
|
defer systemd.Notify()()
|
2019-01-03 23:23:50 +01:00
|
|
|
s.Wait()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2019-05-26 20:45:01 +02:00
|
|
|
serverField = "Linux/3.4 DLNADOC/1.50 UPnP/1.0 DMS/1.0"
|
|
|
|
rootDescPath = "/rootDesc.xml"
|
2019-10-07 17:25:02 +02:00
|
|
|
resPath = "/r/"
|
2019-05-26 20:45:01 +02:00
|
|
|
serviceControlURL = "/ctl"
|
2019-01-03 23:23:50 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type server struct {
|
|
|
|
// The service SOAP handler keyed by service URN.
|
|
|
|
services map[string]UPnPService
|
|
|
|
|
|
|
|
Interfaces []net.Interface
|
|
|
|
|
|
|
|
HTTPConn net.Listener
|
|
|
|
httpListenAddr string
|
2019-05-26 19:58:55 +02:00
|
|
|
handler http.Handler
|
2019-01-03 23:23:50 +01:00
|
|
|
|
2019-05-26 19:35:51 +02:00
|
|
|
RootDeviceUUID string
|
2019-01-03 23:23:50 +01:00
|
|
|
|
|
|
|
FriendlyName string
|
|
|
|
|
|
|
|
// For waiting on the listener to close
|
|
|
|
waitChan chan struct{}
|
|
|
|
|
|
|
|
// Time interval between SSPD announces
|
|
|
|
AnnounceInterval time.Duration
|
|
|
|
|
|
|
|
f fs.Fs
|
|
|
|
vfs *vfs.VFS
|
|
|
|
}
|
|
|
|
|
2022-08-08 18:48:36 +02:00
|
|
|
func newServer(f fs.Fs, opt *dlnaflags.Options) (*server, error) {
|
2019-05-26 20:51:22 +02:00
|
|
|
friendlyName := opt.FriendlyName
|
|
|
|
if friendlyName == "" {
|
|
|
|
friendlyName = makeDefaultFriendlyName()
|
2019-01-03 23:23:50 +01:00
|
|
|
}
|
2022-08-08 18:48:36 +02:00
|
|
|
interfaces := make([]net.Interface, 0, len(opt.InterfaceNames))
|
|
|
|
for _, interfaceName := range opt.InterfaceNames {
|
|
|
|
var err error
|
|
|
|
intf, err := net.InterfaceByName(interfaceName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to resolve interface name '%s': %w", interfaceName, err)
|
|
|
|
}
|
|
|
|
if !isAppropriatelyConfigured(*intf) {
|
|
|
|
return nil, fmt.Errorf("interface '%s' is not appropriately configured (it should be UP, MULTICAST and MTU > 0)", interfaceName)
|
|
|
|
}
|
|
|
|
interfaces = append(interfaces, *intf)
|
|
|
|
}
|
|
|
|
if len(interfaces) == 0 {
|
|
|
|
interfaces = listInterfaces()
|
|
|
|
}
|
2019-01-03 23:23:50 +01:00
|
|
|
|
|
|
|
s := &server{
|
2022-10-01 11:18:50 +02:00
|
|
|
AnnounceInterval: opt.AnnounceInterval,
|
2019-05-26 20:51:22 +02:00
|
|
|
FriendlyName: friendlyName,
|
|
|
|
RootDeviceUUID: makeDeviceUUID(friendlyName),
|
2022-08-08 18:48:36 +02:00
|
|
|
Interfaces: interfaces,
|
2023-10-31 17:44:25 +01:00
|
|
|
waitChan: make(chan struct{}),
|
|
|
|
httpListenAddr: opt.ListenAddr,
|
|
|
|
f: f,
|
|
|
|
vfs: vfs.New(f, &vfsflags.Opt),
|
2019-01-03 23:23:50 +01:00
|
|
|
}
|
2019-05-26 20:51:22 +02:00
|
|
|
|
2019-05-26 19:58:55 +02:00
|
|
|
s.services = map[string]UPnPService{
|
|
|
|
"ContentDirectory": &contentDirectoryService{
|
|
|
|
server: s,
|
|
|
|
},
|
2019-05-26 20:17:19 +02:00
|
|
|
"ConnectionManager": &connectionManagerService{
|
|
|
|
server: s,
|
|
|
|
},
|
2019-09-14 21:03:48 +02:00
|
|
|
"X_MS_MediaReceiverRegistrar": &mediaReceiverRegistrarService{
|
|
|
|
server: s,
|
|
|
|
},
|
2019-01-03 23:23:50 +01:00
|
|
|
}
|
2019-05-26 19:58:55 +02:00
|
|
|
|
|
|
|
// Setup the various http routes.
|
|
|
|
r := http.NewServeMux()
|
2019-10-07 17:25:02 +02:00
|
|
|
r.Handle(resPath, http.StripPrefix(resPath,
|
|
|
|
http.HandlerFunc(s.resourceHandler)))
|
2019-05-26 20:51:22 +02:00
|
|
|
if opt.LogTrace {
|
|
|
|
r.Handle(rootDescPath, traceLogging(http.HandlerFunc(s.rootDescHandler)))
|
|
|
|
r.Handle(serviceControlURL, traceLogging(http.HandlerFunc(s.serviceControlHandler)))
|
|
|
|
} else {
|
|
|
|
r.HandleFunc(rootDescPath, s.rootDescHandler)
|
|
|
|
r.HandleFunc(serviceControlURL, s.serviceControlHandler)
|
|
|
|
}
|
2019-05-26 19:58:55 +02:00
|
|
|
r.Handle("/static/", http.StripPrefix("/static/",
|
|
|
|
withHeader("Cache-Control", "public, max-age=86400",
|
|
|
|
http.FileServer(data.Assets))))
|
2019-05-26 20:51:22 +02:00
|
|
|
s.handler = logging(withHeader("Server", serverField, r))
|
2019-01-03 23:23:50 +01:00
|
|
|
|
2022-08-08 18:48:36 +02:00
|
|
|
return s, nil
|
2019-01-03 23:23:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// UPnPService is the interface for the SOAP service.
|
|
|
|
type UPnPService interface {
|
|
|
|
Handle(action string, argsXML []byte, r *http.Request) (respArgs map[string]string, err error)
|
|
|
|
Subscribe(callback []*url.URL, timeoutSeconds int) (sid string, actualTimeout int, err error)
|
|
|
|
Unsubscribe(sid string) error
|
|
|
|
}
|
|
|
|
|
2019-05-26 20:51:22 +02:00
|
|
|
// Formats the server as a string (used for logging.)
|
|
|
|
func (s *server) String() string {
|
|
|
|
return fmt.Sprintf("DLNA server on %v", s.httpListenAddr)
|
|
|
|
}
|
|
|
|
|
2019-05-26 19:35:51 +02:00
|
|
|
// Returns rclone version number as the model number.
|
|
|
|
func (s *server) ModelNumber() string {
|
|
|
|
return fs.Version
|
|
|
|
}
|
|
|
|
|
|
|
|
// Renders the root device descriptor.
|
|
|
|
func (s *server) rootDescHandler(w http.ResponseWriter, r *http.Request) {
|
2019-09-15 18:44:28 +02:00
|
|
|
tmpl, err := data.GetTemplate()
|
|
|
|
if err != nil {
|
|
|
|
serveError(s, w, "Failed to load root descriptor template", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-26 19:35:51 +02:00
|
|
|
buffer := new(bytes.Buffer)
|
2019-09-15 18:44:28 +02:00
|
|
|
err = tmpl.Execute(buffer, s)
|
2019-05-26 19:35:51 +02:00
|
|
|
if err != nil {
|
2019-09-15 18:44:28 +02:00
|
|
|
serveError(s, w, "Failed to render root descriptor XML", err)
|
2019-05-26 19:35:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("content-type", `text/xml; charset="utf-8"`)
|
|
|
|
w.Header().Set("cache-control", "private, max-age=60")
|
|
|
|
w.Header().Set("content-length", strconv.FormatInt(int64(buffer.Len()), 10))
|
2019-05-26 20:51:22 +02:00
|
|
|
_, err = buffer.WriteTo(w)
|
|
|
|
if err != nil {
|
|
|
|
// Network error
|
|
|
|
fs.Debugf(s, "Error writing rootDesc: %v", err)
|
|
|
|
}
|
2019-05-26 19:35:51 +02:00
|
|
|
}
|
|
|
|
|
2019-01-03 23:23:50 +01:00
|
|
|
// Handle a service control HTTP request.
|
|
|
|
func (s *server) serviceControlHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
soapActionString := r.Header.Get("SOAPACTION")
|
2022-08-06 02:59:00 +02:00
|
|
|
soapAction, err := upnp.ParseActionHTTPHeader(soapActionString)
|
2019-01-03 23:23:50 +01:00
|
|
|
if err != nil {
|
2019-05-26 20:51:22 +02:00
|
|
|
serveError(s, w, "Could not parse SOAPACTION header", err)
|
2019-01-03 23:23:50 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
var env soap.Envelope
|
|
|
|
if err := xml.NewDecoder(r.Body).Decode(&env); err != nil {
|
2019-05-26 20:51:22 +02:00
|
|
|
serveError(s, w, "Could not parse SOAP request body", err)
|
2019-01-03 23:23:50 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", `text/xml; charset="utf-8"`)
|
|
|
|
w.Header().Set("Ext", "")
|
|
|
|
soapRespXML, code := func() ([]byte, int) {
|
|
|
|
respArgs, err := s.soapActionResponse(soapAction, env.Body.Action, r)
|
|
|
|
if err != nil {
|
2019-05-26 20:51:22 +02:00
|
|
|
fs.Errorf(s, "Error invoking %v: %v", soapAction, err)
|
2019-01-03 23:23:50 +01:00
|
|
|
upnpErr := upnp.ConvertError(err)
|
2019-05-26 20:51:22 +02:00
|
|
|
return mustMarshalXML(soap.NewFault("UPnPError", upnpErr)), http.StatusInternalServerError
|
2019-01-03 23:23:50 +01:00
|
|
|
}
|
2019-05-26 20:51:22 +02:00
|
|
|
return marshalSOAPResponse(soapAction, respArgs), http.StatusOK
|
2019-01-03 23:23:50 +01:00
|
|
|
}()
|
|
|
|
bodyStr := fmt.Sprintf(`<?xml version="1.0" encoding="utf-8" standalone="yes"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body>%s</s:Body></s:Envelope>`, soapRespXML)
|
|
|
|
w.WriteHeader(code)
|
|
|
|
if _, err := w.Write([]byte(bodyStr)); err != nil {
|
2019-05-26 20:51:22 +02:00
|
|
|
fs.Infof(s, "Error writing response: %v", err)
|
2019-01-03 23:23:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle a SOAP request and return the response arguments or UPnP error.
|
|
|
|
func (s *server) soapActionResponse(sa upnp.SoapAction, actionRequestXML []byte, r *http.Request) (map[string]string, error) {
|
|
|
|
service, ok := s.services[sa.Type]
|
|
|
|
if !ok {
|
|
|
|
// TODO: What's the invalid service error?
|
|
|
|
return nil, upnp.Errorf(upnp.InvalidActionErrorCode, "Invalid service: %s", sa.Type)
|
|
|
|
}
|
|
|
|
return service.Handle(sa.Action, actionRequestXML, r)
|
|
|
|
}
|
|
|
|
|
2019-05-26 19:58:55 +02:00
|
|
|
// Serves actual resources (media files).
|
|
|
|
func (s *server) resourceHandler(w http.ResponseWriter, r *http.Request) {
|
2019-10-07 17:25:02 +02:00
|
|
|
remotePath := r.URL.Path
|
|
|
|
node, err := s.vfs.Stat(r.URL.Path)
|
2019-05-26 19:58:55 +02:00
|
|
|
if err != nil {
|
2019-05-26 20:51:22 +02:00
|
|
|
http.NotFound(w, r)
|
2019-05-26 19:58:55 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Length", strconv.FormatInt(node.Size(), 10))
|
|
|
|
|
2019-05-26 20:45:01 +02:00
|
|
|
// add some DLNA specific headers
|
|
|
|
if r.Header.Get("getContentFeatures.dlna.org") != "" {
|
|
|
|
w.Header().Set("contentFeatures.dlna.org", dms_dlna.ContentFeatures{
|
|
|
|
SupportRange: true,
|
|
|
|
}.String())
|
|
|
|
}
|
|
|
|
w.Header().Set("transferMode.dlna.org", "Streaming")
|
|
|
|
|
2019-05-26 19:58:55 +02:00
|
|
|
file := node.(*vfs.File)
|
|
|
|
in, err := file.Open(os.O_RDONLY)
|
|
|
|
if err != nil {
|
2019-05-26 20:51:22 +02:00
|
|
|
serveError(node, w, "Could not open resource", err)
|
|
|
|
return
|
2019-05-26 19:58:55 +02:00
|
|
|
}
|
|
|
|
defer fs.CheckClose(in, &err)
|
|
|
|
|
|
|
|
http.ServeContent(w, r, remotePath, node.ModTime(), in)
|
|
|
|
}
|
|
|
|
|
2019-01-03 23:23:50 +01:00
|
|
|
// Serve runs the server - returns the error only if
|
|
|
|
// the listener was not started; does not block, so
|
|
|
|
// use s.Wait() to block on the listener indefinitely.
|
|
|
|
func (s *server) Serve() (err error) {
|
|
|
|
if s.HTTPConn == nil {
|
2022-10-01 09:26:02 +02:00
|
|
|
// Currently, the SSDP server only listens on an IPv4 multicast address.
|
|
|
|
// Differentiate between two INADDR_ANY addresses,
|
|
|
|
// so that 0.0.0.0 can only listen on IPv4 addresses.
|
|
|
|
network := "tcp4"
|
|
|
|
if strings.Count(s.httpListenAddr, ":") > 1 {
|
|
|
|
network = "tcp"
|
|
|
|
}
|
|
|
|
s.HTTPConn, err = net.Listen(network, s.httpListenAddr)
|
2019-01-03 23:23:50 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
s.startSSDP()
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
fs.Logf(s.f, "Serving HTTP on %s", s.HTTPConn.Addr().String())
|
|
|
|
|
2023-04-20 18:29:07 +02:00
|
|
|
err := s.serveHTTP()
|
2019-01-03 23:23:50 +01:00
|
|
|
if err != nil {
|
|
|
|
fs.Logf(s.f, "Error on serving HTTP server: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait blocks while the listener is open.
|
|
|
|
func (s *server) Wait() {
|
|
|
|
<-s.waitChan
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) Close() {
|
|
|
|
err := s.HTTPConn.Close()
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(s.f, "Error closing HTTP server: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
close(s.waitChan)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run SSDP (multicast for server discovery) on all interfaces.
|
|
|
|
func (s *server) startSSDP() {
|
|
|
|
active := 0
|
|
|
|
stopped := make(chan struct{})
|
|
|
|
for _, intf := range s.Interfaces {
|
|
|
|
active++
|
|
|
|
go func(intf2 net.Interface) {
|
|
|
|
defer func() {
|
|
|
|
stopped <- struct{}{}
|
|
|
|
}()
|
|
|
|
s.ssdpInterface(intf2)
|
|
|
|
}(intf)
|
|
|
|
}
|
|
|
|
for active > 0 {
|
|
|
|
<-stopped
|
|
|
|
active--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run SSDP server on an interface.
|
|
|
|
func (s *server) ssdpInterface(intf net.Interface) {
|
2022-10-01 09:26:02 +02:00
|
|
|
// Figure out whether should an ip be announced
|
|
|
|
ipfilterFn := func(ip net.IP) bool {
|
|
|
|
listenaddr := s.HTTPConn.Addr().String()
|
|
|
|
listenip := listenaddr[:strings.LastIndex(listenaddr, ":")]
|
|
|
|
switch listenip {
|
|
|
|
case "0.0.0.0":
|
|
|
|
if strings.Contains(ip.String(), ":") {
|
|
|
|
// Any IPv6 address should not be announced
|
|
|
|
// because SSDP only listen on IPv4 multicast address
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
case "[::]":
|
|
|
|
// In the @Serve() section, the default settings have been made to not listen on IPv6 addresses.
|
|
|
|
// If actually still listening on [::], then allow to announce any address.
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
if listenip == ip.String() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 23:23:50 +01:00
|
|
|
// Figure out which HTTP location to advertise based on the interface IP.
|
|
|
|
advertiseLocationFn := func(ip net.IP) string {
|
|
|
|
url := url.URL{
|
|
|
|
Scheme: "http",
|
|
|
|
Host: (&net.TCPAddr{
|
|
|
|
IP: ip,
|
|
|
|
Port: s.HTTPConn.Addr().(*net.TCPAddr).Port,
|
|
|
|
}).String(),
|
|
|
|
Path: rootDescPath,
|
|
|
|
}
|
|
|
|
return url.String()
|
|
|
|
}
|
|
|
|
|
2022-10-01 09:26:02 +02:00
|
|
|
_, err := intf.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
fs.Logf(s, "Started SSDP on %v", intf.Name)
|
|
|
|
|
2019-05-26 19:58:55 +02:00
|
|
|
// Note that the devices and services advertised here via SSDP should be
|
|
|
|
// in agreement with the rootDesc XML descriptor that is defined above.
|
2019-01-03 23:23:50 +01:00
|
|
|
ssdpServer := ssdp.Server{
|
2019-05-26 19:58:55 +02:00
|
|
|
Interface: intf,
|
|
|
|
Devices: []string{
|
|
|
|
"urn:schemas-upnp-org:device:MediaServer:1"},
|
|
|
|
Services: []string{
|
|
|
|
"urn:schemas-upnp-org:service:ContentDirectory:1",
|
2019-05-26 20:17:19 +02:00
|
|
|
"urn:schemas-upnp-org:service:ConnectionManager:1",
|
|
|
|
"urn:microsoft.com:service:X_MS_MediaReceiverRegistrar:1"},
|
2022-10-01 09:26:02 +02:00
|
|
|
IPFilter: ipfilterFn,
|
2019-01-03 23:23:50 +01:00
|
|
|
Location: advertiseLocationFn,
|
|
|
|
Server: serverField,
|
2019-05-26 19:35:51 +02:00
|
|
|
UUID: s.RootDeviceUUID,
|
2019-01-03 23:23:50 +01:00
|
|
|
NotifyInterval: s.AnnounceInterval,
|
2022-12-16 15:32:39 +01:00
|
|
|
Logger: log.Default,
|
2019-01-03 23:23:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// An interface with these flags should be valid for SSDP.
|
|
|
|
const ssdpInterfaceFlags = net.FlagUp | net.FlagMulticast
|
|
|
|
|
|
|
|
if err := ssdpServer.Init(); err != nil {
|
|
|
|
if intf.Flags&ssdpInterfaceFlags != ssdpInterfaceFlags {
|
|
|
|
// Didn't expect it to work anyway.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if strings.Contains(err.Error(), "listen") {
|
|
|
|
// OSX has a lot of dud interfaces. Failure to create a socket on
|
|
|
|
// the interface are what we're expecting if the interface is no
|
|
|
|
// good.
|
|
|
|
return
|
|
|
|
}
|
2019-05-26 20:51:22 +02:00
|
|
|
fs.Errorf(s, "Error creating ssdp server on %s: %s", intf.Name, err)
|
2019-01-03 23:23:50 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer ssdpServer.Close()
|
2019-05-26 20:51:22 +02:00
|
|
|
fs.Infof(s, "Started SSDP on %v", intf.Name)
|
2019-01-03 23:23:50 +01:00
|
|
|
stopped := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer close(stopped)
|
|
|
|
if err := ssdpServer.Serve(); err != nil {
|
2019-05-26 20:51:22 +02:00
|
|
|
fs.Errorf(s, "%q: %q\n", intf.Name, err)
|
2019-01-03 23:23:50 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-s.waitChan:
|
|
|
|
// Returning will close the server.
|
|
|
|
case <-stopped:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) serveHTTP() error {
|
|
|
|
srv := &http.Server{
|
2019-05-26 19:58:55 +02:00
|
|
|
Handler: s.handler,
|
2019-01-03 23:23:50 +01:00
|
|
|
}
|
|
|
|
err := srv.Serve(s.HTTPConn)
|
|
|
|
select {
|
|
|
|
case <-s.waitChan:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|