2013-12-31 20:49:32 +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.
|
2013-09-26 22:11:06 +00:00
|
|
|
package goupnp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2013-09-28 16:57:51 +00:00
|
|
|
"net/url"
|
2015-06-02 15:20:27 +00:00
|
|
|
"time"
|
2015-06-06 10:01:33 +00:00
|
|
|
|
2013-10-06 12:23:11 +00:00
|
|
|
"github.com/huin/goupnp/ssdp"
|
2020-05-10 11:36:36 +00:00
|
|
|
"golang.org/x/net/html/charset"
|
2013-09-26 22:11:06 +00:00
|
|
|
)
|
|
|
|
|
2013-10-06 12:14:31 +00:00
|
|
|
// ContextError is an error that wraps an error with some context information.
|
2013-09-28 16:57:51 +00:00
|
|
|
type ContextError struct {
|
|
|
|
Context string
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
2020-05-10 11:36:36 +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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-28 16:57:51 +00:00
|
|
|
func (err ContextError) Error() string {
|
|
|
|
return fmt.Sprintf("%s: %v", err.Context, err.Err)
|
|
|
|
}
|
|
|
|
|
2015-06-06 10:04:25 +00:00
|
|
|
// MaybeRootDevice contains either a RootDevice or an error.
|
2013-09-28 16:57:51 +00:00
|
|
|
type MaybeRootDevice struct {
|
2020-05-16 10:58:37 +00:00
|
|
|
// Identifier of the device.
|
|
|
|
USN string
|
|
|
|
|
2015-06-06 10:01:33 +00:00
|
|
|
// Set iff Err == nil.
|
2013-09-28 16:57:51 +00:00
|
|
|
Root *RootDevice
|
2015-06-06 10:01:33 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
// Any error encountered probing a discovered device.
|
|
|
|
Err error
|
2013-09-28 16:57:51 +00:00
|
|
|
}
|
|
|
|
|
2013-10-06 21:06:22 +00:00
|
|
|
// DiscoverDevices attempts to find targets of the given type. This is
|
2013-12-31 18:36:26 +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.
|
2013-09-28 16:57:51 +00:00
|
|
|
func DiscoverDevices(searchTarget string) ([]MaybeRootDevice, error) {
|
2020-05-10 11:36:36 +00:00
|
|
|
hc, hcCleanup, err := httpuClient()
|
2013-09-27 23:09:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2013-09-26 22:11:06 +00:00
|
|
|
}
|
2020-05-10 11:36:36 +00:00
|
|
|
defer hcCleanup()
|
|
|
|
responses, err := ssdp.SSDPRawSearch(hc, string(searchTarget), 2, 3)
|
2013-09-28 16:57:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-09-27 23:09:38 +00:00
|
|
|
|
2013-09-28 16:57:51 +00:00
|
|
|
results := make([]MaybeRootDevice, len(responses))
|
|
|
|
for i, response := range responses {
|
|
|
|
maybe := &results[i]
|
2020-05-16 10:58:37 +00:00
|
|
|
maybe.USN = response.Header.Get("USN")
|
2013-09-27 23:09:38 +00:00
|
|
|
loc, err := response.Location()
|
|
|
|
if err != nil {
|
2013-09-28 16:57:51 +00:00
|
|
|
maybe.Err = ContextError{"unexpected bad location from search", err}
|
2013-09-27 23:09:38 +00:00
|
|
|
continue
|
|
|
|
}
|
2015-06-06 10:01:33 +00:00
|
|
|
maybe.Location = loc
|
|
|
|
if root, err := DeviceByURL(loc); err != nil {
|
|
|
|
maybe.Err = err
|
2014-01-05 19:39:24 +00:00
|
|
|
} else {
|
2015-06-06 10:01:33 +00:00
|
|
|
maybe.Root = root
|
2013-09-27 23:09:38 +00:00
|
|
|
}
|
2013-09-26 22:11:06 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 23:09:38 +00:00
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
2015-06-06 10:01:33 +00:00
|
|
|
func DeviceByURL(loc *url.URL) (*RootDevice, error) {
|
|
|
|
locStr := loc.String()
|
|
|
|
root := new(RootDevice)
|
|
|
|
if err := requestXml(locStr, DeviceXMLNamespace, root); err != nil {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-09-28 16:57:51 +00:00
|
|
|
func requestXml(url string, defaultSpace string, doc interface{}) error {
|
2015-06-02 15:20:27 +00:00
|
|
|
timeout := time.Duration(3 * time.Second)
|
|
|
|
client := http.Client{
|
|
|
|
Timeout: timeout,
|
|
|
|
}
|
|
|
|
resp, err := client.Get(url)
|
2013-09-26 22:11:06 +00:00
|
|
|
if err != nil {
|
2013-09-28 16:57:51 +00:00
|
|
|
return err
|
2013-09-26 22:11:06 +00:00
|
|
|
}
|
2013-09-27 23:09:38 +00:00
|
|
|
defer resp.Body.Close()
|
2013-09-26 22:11:06 +00:00
|
|
|
|
2013-09-27 23:09:38 +00:00
|
|
|
if resp.StatusCode != 200 {
|
2013-09-28 16:57:51 +00:00
|
|
|
return fmt.Errorf("goupnp: got response status %s from %q",
|
|
|
|
resp.Status, url)
|
2013-09-26 22:11:06 +00:00
|
|
|
}
|
|
|
|
|
2013-09-27 23:09:38 +00:00
|
|
|
decoder := xml.NewDecoder(resp.Body)
|
2013-09-28 16:57:51 +00:00
|
|
|
decoder.DefaultSpace = defaultSpace
|
2015-04-06 13:16:50 +00:00
|
|
|
decoder.CharsetReader = charset.NewReaderLabel
|
2013-09-26 22:11:06 +00:00
|
|
|
|
2013-09-28 16:57:51 +00:00
|
|
|
return decoder.Decode(doc)
|
2013-09-26 22:11:06 +00:00
|
|
|
}
|