2016-06-14 16:17:44 +00:00
|
|
|
// goupnp is an implementation of a client for various UPnP services.
|
|
|
|
//
|
|
|
|
// For most uses, it is recommended to use the code-generated packages under
|
|
|
|
// github.com/huin/goupnp/dcps. Example use is shown at
|
|
|
|
// http://godoc.org/github.com/huin/goupnp/example
|
|
|
|
//
|
|
|
|
// A commonly used client is internetgateway1.WANPPPConnection1:
|
|
|
|
// http://godoc.org/github.com/huin/goupnp/dcps/internetgateway1#WANPPPConnection1
|
|
|
|
//
|
|
|
|
// Currently only a couple of schemas have code generated for them from the
|
|
|
|
// UPnP example XML specifications. Not all methods will work on these clients,
|
|
|
|
// because the generated stubs contain the full set of specified methods from
|
|
|
|
// the XML specifications, and the discovered services will likely support a
|
|
|
|
// subset of those methods.
|
|
|
|
package goupnp
|
|
|
|
|
|
|
|
import (
|
2023-05-19 20:23:55 +00:00
|
|
|
"context"
|
2016-06-14 16:17:44 +00:00
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
2021-07-07 06:11:09 +00:00
|
|
|
"io"
|
2022-08-19 16:34:07 +00:00
|
|
|
"net"
|
2016-06-14 16:17:44 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
2022-08-19 16:34:07 +00:00
|
|
|
"github.com/huin/goupnp/httpu"
|
2016-06-14 16:17:44 +00:00
|
|
|
"github.com/huin/goupnp/ssdp"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ContextError is an error that wraps an error with some context information.
|
|
|
|
type ContextError struct {
|
|
|
|
Context string
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
2021-06-28 06:53:50 +00:00
|
|
|
func ctxError(err error, msg string) ContextError {
|
|
|
|
return ContextError{
|
|
|
|
Context: msg,
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ctxErrorf(err error, msg string, args ...interface{}) ContextError {
|
|
|
|
return ContextError{
|
|
|
|
Context: fmt.Sprintf(msg, args...),
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-14 16:17:44 +00:00
|
|
|
func (err ContextError) Error() string {
|
|
|
|
return fmt.Sprintf("%s: %v", err.Context, err.Err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MaybeRootDevice contains either a RootDevice or an error.
|
|
|
|
type MaybeRootDevice struct {
|
2021-07-07 06:11:09 +00:00
|
|
|
// Identifier of the device. Note that this in combination with Location
|
|
|
|
// uniquely identifies a result from DiscoverDevices.
|
2021-06-28 06:53:50 +00:00
|
|
|
USN string
|
|
|
|
|
2016-06-14 16:17:44 +00:00
|
|
|
// Set iff Err == nil.
|
|
|
|
Root *RootDevice
|
|
|
|
|
|
|
|
// The location the device was discovered at. This can be used with
|
|
|
|
// DeviceByURL, assuming the device is still present. A location represents
|
|
|
|
// the discovery of a device, regardless of if there was an error probing it.
|
|
|
|
Location *url.URL
|
|
|
|
|
2022-08-19 16:34:07 +00:00
|
|
|
// The address from which the device was discovered (if known - otherwise nil).
|
|
|
|
LocalAddr net.IP
|
|
|
|
|
2016-06-14 16:17:44 +00:00
|
|
|
// Any error encountered probing a discovered device.
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
2023-05-19 20:23:55 +00:00
|
|
|
// DiscoverDevicesCtx attempts to find targets of the given type. This is
|
2016-06-14 16:17:44 +00:00
|
|
|
// typically the entry-point for this package. searchTarget is typically a URN
|
|
|
|
// in the form "urn:schemas-upnp-org:device:..." or
|
|
|
|
// "urn:schemas-upnp-org:service:...". A single error is returned for errors
|
|
|
|
// while attempting to send the query. An error or RootDevice is returned for
|
|
|
|
// each discovered RootDevice.
|
2023-05-19 20:23:55 +00:00
|
|
|
func DiscoverDevicesCtx(ctx context.Context, searchTarget string) ([]MaybeRootDevice, error) {
|
2021-06-28 06:53:50 +00:00
|
|
|
hc, hcCleanup, err := httpuClient()
|
2016-06-14 16:17:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-28 06:53:50 +00:00
|
|
|
defer hcCleanup()
|
2024-01-18 18:54:54 +00:00
|
|
|
responses, err := ssdp.SSDPRawSearchCtx(ctx, hc, string(searchTarget), 2, 3)
|
2016-06-14 16:17:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
results := make([]MaybeRootDevice, len(responses))
|
|
|
|
for i, response := range responses {
|
|
|
|
maybe := &results[i]
|
2021-06-28 06:53:50 +00:00
|
|
|
maybe.USN = response.Header.Get("USN")
|
2016-06-14 16:17:44 +00:00
|
|
|
loc, err := response.Location()
|
|
|
|
if err != nil {
|
|
|
|
maybe.Err = ContextError{"unexpected bad location from search", err}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
maybe.Location = loc
|
2023-05-19 20:23:55 +00:00
|
|
|
if root, err := DeviceByURLCtx(ctx, loc); err != nil {
|
2016-06-14 16:17:44 +00:00
|
|
|
maybe.Err = err
|
|
|
|
} else {
|
|
|
|
maybe.Root = root
|
|
|
|
}
|
2022-08-19 16:34:07 +00:00
|
|
|
if i := response.Header.Get(httpu.LocalAddressHeader); len(i) > 0 {
|
|
|
|
maybe.LocalAddr = net.ParseIP(i)
|
|
|
|
}
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
2023-05-19 20:23:55 +00:00
|
|
|
// DiscoverDevices is the legacy version of DiscoverDevicesCtx, but uses
|
|
|
|
// context.Background() as the context.
|
|
|
|
func DiscoverDevices(searchTarget string) ([]MaybeRootDevice, error) {
|
|
|
|
return DiscoverDevicesCtx(context.Background(), searchTarget)
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeviceByURLCtx(ctx context.Context, loc *url.URL) (*RootDevice, error) {
|
2016-06-14 16:17:44 +00:00
|
|
|
locStr := loc.String()
|
|
|
|
root := new(RootDevice)
|
2023-05-19 20:23:55 +00:00
|
|
|
if err := requestXml(ctx, locStr, DeviceXMLNamespace, root); err != nil {
|
2016-06-14 16:17:44 +00:00
|
|
|
return nil, ContextError{fmt.Sprintf("error requesting root device details from %q", locStr), err}
|
|
|
|
}
|
|
|
|
var urlBaseStr string
|
|
|
|
if root.URLBaseStr != "" {
|
|
|
|
urlBaseStr = root.URLBaseStr
|
|
|
|
} else {
|
|
|
|
urlBaseStr = locStr
|
|
|
|
}
|
|
|
|
urlBase, err := url.Parse(urlBaseStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ContextError{fmt.Sprintf("error parsing location URL %q", locStr), err}
|
|
|
|
}
|
|
|
|
root.SetURLBase(urlBase)
|
|
|
|
return root, nil
|
|
|
|
}
|
|
|
|
|
2023-05-19 20:23:55 +00:00
|
|
|
func DeviceByURL(loc *url.URL) (*RootDevice, error) {
|
|
|
|
return DeviceByURLCtx(context.Background(), loc)
|
|
|
|
}
|
|
|
|
|
2021-07-07 06:11:09 +00:00
|
|
|
// CharsetReaderDefault specifies the charset reader used while decoding the output
|
|
|
|
// from a UPnP server. It can be modified in an init function to allow for non-utf8 encodings,
|
|
|
|
// but should not be changed after requesting clients.
|
|
|
|
var CharsetReaderDefault func(charset string, input io.Reader) (io.Reader, error)
|
|
|
|
|
2023-05-19 20:23:55 +00:00
|
|
|
// HTTPClient specifies the http.Client object used when fetching the XML from the UPnP server.
|
|
|
|
// HTTPClient defaults the http.DefaultClient. This may be overridden by the importing application.
|
|
|
|
var HTTPClientDefault = http.DefaultClient
|
|
|
|
|
|
|
|
func requestXml(ctx context.Context, url string, defaultSpace string, doc interface{}) error {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
2023-05-19 20:23:55 +00:00
|
|
|
|
|
|
|
resp, err := HTTPClientDefault.Do(req)
|
2016-06-14 16:17:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return fmt.Errorf("goupnp: got response status %s from %q",
|
|
|
|
resp.Status, url)
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder := xml.NewDecoder(resp.Body)
|
|
|
|
decoder.DefaultSpace = defaultSpace
|
2021-07-07 06:11:09 +00:00
|
|
|
decoder.CharsetReader = CharsetReaderDefault
|
2016-06-14 16:17:44 +00:00
|
|
|
|
|
|
|
return decoder.Decode(doc)
|
|
|
|
}
|