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"
|
2013-12-31 18:36:26 +00:00
|
|
|
"github.com/huin/goupnp/dcps/internetgateway1"
|
2013-09-28 16:58:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2013-12-31 18:36:26 +00:00
|
|
|
results, err := goupnp.DiscoverDevices(internetgateway1.URN_WANPPPConnection_1)
|
|
|
|
if err != nil {
|
2013-09-28 16:58:28 +00:00
|
|
|
fmt.Println("Error discovering InternetGatewayDevice with UPnP: ", err)
|
2013-12-31 18:36:26 +00:00
|
|
|
return
|
|
|
|
}
|
2013-10-06 11:57:26 +00:00
|
|
|
|
2013-12-31 18:36:26 +00:00
|
|
|
fmt.Printf("Discovered %d InternetGatewayDevices:\n", len(results))
|
|
|
|
for _, maybeRootDevice := range results {
|
|
|
|
if maybeRootDevice.Err != nil {
|
|
|
|
fmt.Println(maybeRootDevice.Err)
|
|
|
|
continue
|
|
|
|
}
|
2013-10-06 11:57:26 +00:00
|
|
|
|
2013-12-31 18:36:26 +00:00
|
|
|
device := &maybeRootDevice.Root.Device
|
2013-10-06 11:57:26 +00:00
|
|
|
|
2013-12-31 18:36:26 +00:00
|
|
|
fmt.Println("Device ", device.FriendlyName)
|
|
|
|
wanPPPSrvs := device.FindService(internetgateway1.URN_WANPPPConnection_1)
|
|
|
|
if len(wanPPPSrvs) < 1 {
|
|
|
|
fmt.Printf("Could not find expected service on device %s\n", device.FriendlyName)
|
|
|
|
continue
|
|
|
|
}
|
2013-10-06 11:57:26 +00:00
|
|
|
|
2013-12-31 18:36:26 +00:00
|
|
|
for _, srv := range wanPPPSrvs {
|
|
|
|
client := internetgateway1.WANPPPConnection1{srv.NewSOAPClient()}
|
|
|
|
if addr, err := client.GetExternalIPAddress(); err != nil {
|
|
|
|
fmt.Printf("Failed to get external IP address: %v\n", err)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("External IP address: %v\n", addr)
|
2013-09-28 16:58:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|