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 (
|
2013-10-06 11:57:26 +00:00
|
|
|
"encoding/xml"
|
2013-09-28 16:58:28 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/huin/goupnp"
|
|
|
|
)
|
|
|
|
|
|
|
|
var spaces = " "
|
|
|
|
|
2013-10-06 11:57:26 +00:00
|
|
|
type GetExternalIPAddressRequest struct {
|
|
|
|
XMLName xml.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetExternalIPAddressResponse struct {
|
|
|
|
XMLName xml.Name
|
|
|
|
NewExternalIPAddress string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Uint16Value struct {
|
|
|
|
XMLName xml.Name
|
|
|
|
Value uint16 `xml:",chardata"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetGenericPortMappingEntryRequest struct {
|
|
|
|
XMLName xml.Name
|
|
|
|
NewPortMappingIndex Uint16Value
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetGenericPortMappingEntryResponse struct {
|
|
|
|
XMLName xml.Name
|
|
|
|
NewRemoteHost string
|
|
|
|
NewExternalPort uint16
|
|
|
|
NewProtocol string
|
|
|
|
NewInternalPort uint16
|
|
|
|
NewInternalClient string
|
|
|
|
NewEnabled string // boolean
|
|
|
|
NewLeaseDuration uint32
|
|
|
|
}
|
|
|
|
|
2013-09-28 16:58:28 +00:00
|
|
|
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-10-03 19:38:23 +00:00
|
|
|
fmt.Printf("%sService %s\n", indent+1, srv.ServiceType)
|
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)
|
2013-10-03 19:38:23 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
device := &maybeRootDevice.Root.Device
|
|
|
|
|
|
|
|
displayDevice(0, device)
|
|
|
|
wanPPPSrvs := device.FindService(goupnp.ServiceTypeWANPPPConnection)
|
|
|
|
if len(wanPPPSrvs) < 1 {
|
|
|
|
fmt.Printf("Could not find expected service on device %s\n", device.FriendlyName)
|
|
|
|
continue
|
|
|
|
} else if len(wanPPPSrvs) > 1 {
|
|
|
|
fmt.Printf("Got more than one expected service on device %s\n", device.FriendlyName)
|
|
|
|
}
|
|
|
|
srv := wanPPPSrvs[0]
|
2013-10-06 11:57:26 +00:00
|
|
|
|
|
|
|
if scdp, err := srv.RequestSCDP(); err != nil {
|
|
|
|
fmt.Printf("Error requesting SCPD: %v\n", err)
|
|
|
|
} else {
|
|
|
|
fmt.Println("Available SCPD actions:")
|
|
|
|
for _, action := range scdp.Actions {
|
|
|
|
fmt.Println(" ", action.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-06 12:23:11 +00:00
|
|
|
srvClient := srv.NewSOAPClient()
|
2013-10-06 11:57:26 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
inAction := GetExternalIPAddressRequest{XMLName: xml.Name{Space: goupnp.ServiceTypeWANPPPConnection, Local: "GetExternalIPAddress"}}
|
|
|
|
var outAction GetExternalIPAddressResponse
|
|
|
|
err := srvClient.PerformAction(goupnp.ServiceTypeWANPPPConnection, "GetExternalIPAddress", &inAction, &outAction)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to GetExternalIPAddress from %s: %v\n", device.FriendlyName, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fmt.Printf("Got GetExternalIPAddress result from %s: %+v\n", device.FriendlyName, outAction)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := uint16(0); i < 10; i++ {
|
|
|
|
inAction := GetGenericPortMappingEntryRequest{XMLName: xml.Name{Space: goupnp.ServiceTypeWANPPPConnection, Local: "GetGenericPortMappingEntry"}, NewPortMappingIndex: Uint16Value{XMLName: xml.Name{"", "NewPortMappingIndex"}, Value: i}}
|
|
|
|
var outAction GetGenericPortMappingEntryResponse
|
|
|
|
err := srvClient.PerformAction(goupnp.ServiceTypeWANPPPConnection, "GetGenericPortMappingEntry", &inAction, &outAction)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to GetGenericPortMappingEntry on %s: %v\n", device.FriendlyName, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fmt.Printf("Got GetGenericPortMappingEntry from %s: %+v\n", device.FriendlyName, outAction)
|
2013-09-28 16:58:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|