2013-09-28 16:58:28 +00:00
|
|
|
// Serves as a simple example/test of discovering UPnP devices on the local
|
|
|
|
// network.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/huin/goupnp"
|
|
|
|
)
|
|
|
|
|
|
|
|
var spaces = " "
|
|
|
|
|
|
|
|
type indentLevel int
|
|
|
|
|
|
|
|
func (i indentLevel) String() string {
|
|
|
|
return spaces[:i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func displayDevice(indent indentLevel, device *goupnp.Device) {
|
|
|
|
fmt.Println(indent.String(), device)
|
|
|
|
for _, srv := range device.Services {
|
2013-09-29 10:23:10 +00:00
|
|
|
fmt.Println((indent + 1).String(), srv, srv.SCPDURL.URL.String(), srv.ControlURL.URL.String())
|
|
|
|
fmt.Println(goupnp.ServiceTypeWANPPPConnection, srv.ServiceType)
|
|
|
|
if srv.ServiceType == goupnp.ServiceTypeWANPPPConnection {
|
|
|
|
results, err := goupnp.PerformSoapAction(goupnp.ServiceTypeWANPPPConnection, "GetExternalIPAddress", &srv.ControlURL.URL, nil)
|
|
|
|
if err != nil {
|
2013-09-29 15:09:10 +00:00
|
|
|
fmt.Println("Error calling GetExternalIPAddress:", err)
|
2013-09-29 10:23:10 +00:00
|
|
|
} else {
|
|
|
|
fmt.Println(results)
|
|
|
|
}
|
|
|
|
}
|
2013-09-28 16:58:28 +00:00
|
|
|
}
|
2013-09-29 14:59:53 +00:00
|
|
|
for i := range device.Devices {
|
|
|
|
displayDevice(indent+1, &device.Devices[i])
|
2013-09-28 16:58:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2013-09-29 15:15:35 +00:00
|
|
|
if results, err := goupnp.DiscoverDevices(goupnp.ServiceTypeWANPPPConnection); err != nil {
|
2013-09-28 16:58:28 +00:00
|
|
|
fmt.Println("Error discovering InternetGatewayDevice with UPnP: ", err)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Discovered %d InternetGatewayDevices:\n", len(results))
|
|
|
|
for _, maybeRootDevice := range results {
|
|
|
|
if maybeRootDevice.Err != nil {
|
|
|
|
fmt.Println(maybeRootDevice.Err)
|
|
|
|
} else {
|
2013-09-29 14:59:53 +00:00
|
|
|
displayDevice(0, &maybeRootDevice.Root.Device)
|
2013-09-28 16:58:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|