probe/discovery/protocol/upnp/upnp.go

64 lines
1.3 KiB
Go
Raw Normal View History

2018-08-29 12:04:23 +00:00
package upnp
import (
"fmt"
omd "git.loafle.net/overflow/model/discovery"
omu "git.loafle.net/overflow/model/util"
"git.loafle.net/overflow_scanner/probe/discovery/session"
"github.com/huin/goupnp"
)
const (
TargetRootDevice = "upnp:rootdevice"
TargetSSDPAll = "ssdp:all"
)
2018-08-31 05:15:46 +00:00
func Scan(discoverySession session.DiscoverySession) error {
2018-08-29 12:04:23 +00:00
devs, err := goupnp.DiscoverDevices(TargetRootDevice)
if nil != err {
2018-08-31 02:56:33 +00:00
fmt.Println("DiscoverDevices: ", err)
2018-08-31 05:15:46 +00:00
return nil
2018-08-29 12:04:23 +00:00
}
LOOP:
for _, dev := range devs {
rd := dev.Root.Device
if !rd.PresentationURL.Ok {
continue LOOP
}
2018-09-12 04:55:51 +00:00
h := omd.NewHost(
discoverySession.Zone(),
discoverySession.Zone().MetaIPType,
rd.PresentationURL.URL.Host,
)
h.Name = rd.FriendlyName
h.DiscoveredDate = omu.NowPtr()
2018-09-11 07:59:21 +00:00
discoverySession.AddHost(
"UPnP",
2018-09-12 04:55:51 +00:00
h,
2018-09-11 07:59:21 +00:00
map[string]string{
2018-08-29 12:04:23 +00:00
"DeviceType": rd.DeviceType,
"Manufacturer": rd.Manufacturer,
"ManufacturerURL": rd.ManufacturerURL.Str,
"ModelName": rd.ModelName,
"ModelDescription": rd.ModelDescription,
"ModelNumber": rd.ModelNumber,
"SerialNumber": rd.SerialNumber,
"UDN": rd.UDN,
"UPC": rd.UPC,
},
2018-09-11 07:59:21 +00:00
)
2018-09-03 14:14:51 +00:00
select {
case <-discoverySession.StopChan():
return nil
default:
}
2018-08-29 12:04:23 +00:00
}
2018-08-31 05:15:46 +00:00
return nil
2018-08-29 12:04:23 +00:00
}