forked from TrueCloudLab/rclone
dlna: specify SSDP interface names from command line
This commit is contained in:
parent
8d1fff9a82
commit
1107da7247
4 changed files with 39 additions and 12 deletions
|
@ -51,7 +51,10 @@ files that they are not able to play back correctly.
|
||||||
f := cmd.NewFsSrc(args)
|
f := cmd.NewFsSrc(args)
|
||||||
|
|
||||||
cmd.Run(false, false, command, func() error {
|
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 {
|
if err := s.Serve(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -92,17 +95,32 @@ type server struct {
|
||||||
vfs *vfs.VFS
|
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
|
friendlyName := opt.FriendlyName
|
||||||
if friendlyName == "" {
|
if friendlyName == "" {
|
||||||
friendlyName = makeDefaultFriendlyName()
|
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{
|
s := &server{
|
||||||
AnnounceInterval: 10 * time.Second,
|
AnnounceInterval: 10 * time.Second,
|
||||||
FriendlyName: friendlyName,
|
FriendlyName: friendlyName,
|
||||||
RootDeviceUUID: makeDeviceUUID(friendlyName),
|
RootDeviceUUID: makeDeviceUUID(friendlyName),
|
||||||
Interfaces: listInterfaces(),
|
Interfaces: interfaces,
|
||||||
|
|
||||||
httpListenAddr: opt.ListenAddr,
|
httpListenAddr: opt.ListenAddr,
|
||||||
|
|
||||||
|
@ -138,7 +156,7 @@ func newServer(f fs.Fs, opt *dlnaflags.Options) *server {
|
||||||
http.FileServer(data.Assets))))
|
http.FileServer(data.Assets))))
|
||||||
s.handler = logging(withHeader("Server", serverField, r))
|
s.handler = logging(withHeader("Server", serverField, r))
|
||||||
|
|
||||||
return s
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UPnPService is the interface for the SOAP service.
|
// UPnPService is the interface for the SOAP service.
|
||||||
|
|
|
@ -35,7 +35,9 @@ const (
|
||||||
func startServer(t *testing.T, f fs.Fs) {
|
func startServer(t *testing.T, f fs.Fs) {
|
||||||
opt := dlnaflags.DefaultOpt
|
opt := dlnaflags.DefaultOpt
|
||||||
opt.ListenAddr = testBindAddress
|
opt.ListenAddr = testBindAddress
|
||||||
dlnaServer = newServer(f, &opt)
|
var err error
|
||||||
|
dlnaServer, err = newServer(f, &opt)
|
||||||
|
assert.NoError(t, err)
|
||||||
assert.NoError(t, dlnaServer.Serve())
|
assert.NoError(t, dlnaServer.Serve())
|
||||||
baseURL = "http://" + dlnaServer.HTTPConn.Addr().String()
|
baseURL = "http://" + dlnaServer.HTTPConn.Addr().String()
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,13 +47,17 @@ func listInterfaces() []net.Interface {
|
||||||
|
|
||||||
var active []net.Interface
|
var active []net.Interface
|
||||||
for _, intf := range ifs {
|
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)
|
active = append(active, intf)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return active
|
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 {
|
func didlLite(chardata string) string {
|
||||||
return `<DIDL-Lite` +
|
return `<DIDL-Lite` +
|
||||||
` xmlns:dc="http://purl.org/dc/elements/1.1/"` +
|
` xmlns:dc="http://purl.org/dc/elements/1.1/"` +
|
||||||
|
|
|
@ -23,16 +23,18 @@ logging of all UPNP traffic.
|
||||||
|
|
||||||
// Options is the type for DLNA serving options.
|
// Options is the type for DLNA serving options.
|
||||||
type Options struct {
|
type Options struct {
|
||||||
ListenAddr string
|
ListenAddr string
|
||||||
FriendlyName string
|
FriendlyName string
|
||||||
LogTrace bool
|
LogTrace bool
|
||||||
|
InterfaceNames []string
|
||||||
}
|
}
|
||||||
|
|
||||||
// DefaultOpt contains the defaults options for DLNA serving.
|
// DefaultOpt contains the defaults options for DLNA serving.
|
||||||
var DefaultOpt = Options{
|
var DefaultOpt = Options{
|
||||||
ListenAddr: ":7879",
|
ListenAddr: ":7879",
|
||||||
FriendlyName: "",
|
FriendlyName: "",
|
||||||
LogTrace: false,
|
LogTrace: false,
|
||||||
|
InterfaceNames: []string{},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Opt contains the options for DLNA serving.
|
// 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.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.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.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.
|
// AddFlags add the command line flags for DLNA serving.
|
||||||
|
|
Loading…
Reference in a new issue