1
mirror of https://github.com/rclone/rclone synced 2024-12-25 17:03:45 +01:00

dlna: specify SSDP interface names from command line

This commit is contained in:
Simon Bos 2022-08-08 18:48:36 +02:00 committed by Nick Craig-Wood
parent 8d1fff9a82
commit 1107da7247
4 changed files with 39 additions and 12 deletions

View File

@ -51,7 +51,10 @@ files that they are not able to play back correctly.
f := cmd.NewFsSrc(args)
cmd.Run(false, false, command, func() error {
s := newServer(f, &dlnaflags.Opt)
s, err := newServer(f, &dlnaflags.Opt)
if err != nil {
return err
}
if err := s.Serve(); err != nil {
return err
}
@ -92,17 +95,32 @@ type server struct {
vfs *vfs.VFS
}
func newServer(f fs.Fs, opt *dlnaflags.Options) *server {
func newServer(f fs.Fs, opt *dlnaflags.Options) (*server, error) {
friendlyName := opt.FriendlyName
if friendlyName == "" {
friendlyName = makeDefaultFriendlyName()
}
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()
}
s := &server{
AnnounceInterval: 10 * time.Second,
FriendlyName: friendlyName,
RootDeviceUUID: makeDeviceUUID(friendlyName),
Interfaces: listInterfaces(),
Interfaces: interfaces,
httpListenAddr: opt.ListenAddr,
@ -138,7 +156,7 @@ func newServer(f fs.Fs, opt *dlnaflags.Options) *server {
http.FileServer(data.Assets))))
s.handler = logging(withHeader("Server", serverField, r))
return s
return s, nil
}
// UPnPService is the interface for the SOAP service.

View File

@ -35,7 +35,9 @@ const (
func startServer(t *testing.T, f fs.Fs) {
opt := dlnaflags.DefaultOpt
opt.ListenAddr = testBindAddress
dlnaServer = newServer(f, &opt)
var err error
dlnaServer, err = newServer(f, &opt)
assert.NoError(t, err)
assert.NoError(t, dlnaServer.Serve())
baseURL = "http://" + dlnaServer.HTTPConn.Addr().String()
}

View File

@ -47,13 +47,17 @@ func listInterfaces() []net.Interface {
var active []net.Interface
for _, intf := range ifs {
if intf.Flags&net.FlagUp != 0 && intf.Flags&net.FlagMulticast != 0 && intf.MTU > 0 {
if isAppropriatelyConfigured(intf) {
active = append(active, intf)
}
}
return active
}
func isAppropriatelyConfigured(intf net.Interface) bool {
return intf.Flags&net.FlagUp != 0 && intf.Flags&net.FlagMulticast != 0 && intf.MTU > 0
}
func didlLite(chardata string) string {
return `<DIDL-Lite` +
` xmlns:dc="http://purl.org/dc/elements/1.1/"` +

View File

@ -23,16 +23,18 @@ logging of all UPNP traffic.
// Options is the type for DLNA serving options.
type Options struct {
ListenAddr string
FriendlyName string
LogTrace bool
ListenAddr string
FriendlyName string
LogTrace bool
InterfaceNames []string
}
// DefaultOpt contains the defaults options for DLNA serving.
var DefaultOpt = Options{
ListenAddr: ":7879",
FriendlyName: "",
LogTrace: false,
ListenAddr: ":7879",
FriendlyName: "",
LogTrace: false,
InterfaceNames: []string{},
}
// Opt contains the options for DLNA serving.
@ -45,6 +47,7 @@ func addFlagsPrefix(flagSet *pflag.FlagSet, prefix string, Opt *Options) {
flags.StringVarP(flagSet, &Opt.ListenAddr, prefix+"addr", "", Opt.ListenAddr, "The ip:port or :port to bind the DLNA http server to")
flags.StringVarP(flagSet, &Opt.FriendlyName, prefix+"name", "", Opt.FriendlyName, "Name of DLNA server")
flags.BoolVarP(flagSet, &Opt.LogTrace, prefix+"log-trace", "", Opt.LogTrace, "Enable trace logging of SOAP traffic")
flags.StringArrayVarP(flagSet, &Opt.InterfaceNames, prefix+"interface", "", Opt.InterfaceNames, "The interface to use for SSDP (repeat as necessary)")
}
// AddFlags add the command line flags for DLNA serving.