UPnP in go
Easy for dialog with a UPnP device.
(Sorry if my english is bad in the project or the examples, I try)
I built a tool at https://github.com/micmonay/UpnpTools
This exemple is for get the IPV4 in your router. For exemple your router has duty a UPnP active
up := upnp.NewUPNP(upnp.SERVICE_GATEWAY_IPV4_V2)
Interface, err := upnp.GetInterfaceByName("eth0")
if err != nil {
log.Println(err)
return
}
devices := up.GetAllCompatibleDevice(Interface, 1)
if len(devices) == 0 {
return
}
services := devices[0].GetServicesByType(upnp.SERVICE_GATEWAY_IPV4_V2)
if len(services) == 0 {
return
}
service := services[0]
if service == nil {
log.Println("not found service")
return
}
response, err := service.GetAction("GetExternalIPAddress").Send()
if err != nil {
log.Println(err)
return
}
ip, err := response.GetValueArgument("NewExternalIPAddress")
if err != nil {
log.Println(err)
return
}
fmt.Println("Your WAN ip address is " + ip)
If your action containe arguments at sending.
Action := Services[0].GetAction("ActionName")
Action.AddVariable("nameOfArgument","value")
Action.Send()