probe/discovery/protocol/upnp/upnp.go

163 lines
4.0 KiB
Go
Raw Normal View History

2018-08-29 12:04:23 +00:00
package upnp
import (
"fmt"
2018-09-13 16:14:34 +00:00
"strconv"
"strings"
2018-08-29 12:04:23 +00:00
omd "git.loafle.net/overflow/model/discovery"
2018-09-13 16:14:34 +00:00
omm "git.loafle.net/overflow/model/meta"
2018-08-29 12:04:23 +00:00
omu "git.loafle.net/overflow/model/util"
2018-09-13 16:14:34 +00:00
oun "git.loafle.net/overflow/util-go/net"
2018-08-29 12:04:23 +00:00
"git.loafle.net/overflow_scanner/probe/discovery/session"
"github.com/huin/goupnp"
)
const (
TargetRootDevice = "upnp:rootdevice"
TargetSSDPAll = "ssdp:all"
)
2018-10-10 13:38:53 +00:00
var (
deviceTypes = map[string]omm.MetaHostTypeEnum{
"urn:schemas-upnp-org:device:InternetGatewayDevice:1": omm.MetaHostTypeEnumRouter,
"urn:schemas-upnp-org:device:InternetGatewayDevice:2": omm.MetaHostTypeEnumRouter,
"urn:schemas-upnp-org:device:WLANAccessPointDevice:1": omm.MetaHostTypeEnumWLANAccessPoint,
"urn:schemas-upnp-org:device:MediaServer:1": omm.MetaHostTypeEnumMediaServer,
2018-10-12 16:45:35 +00:00
"urn:schemas-upnp-org:device:MediaRenderer:1": omm.MetaHostTypeEnumMediaServer,
2018-10-10 13:38:53 +00:00
"urn:schemas-upnp-org:device:MediaServer:2": omm.MetaHostTypeEnumMediaServer,
2018-10-12 16:45:35 +00:00
"urn:schemas-upnp-org:device:MediaRenderer:2": omm.MetaHostTypeEnumMediaServer,
2018-10-10 13:38:53 +00:00
"urn:schemas-upnp-org:device:MediaServer:3": omm.MetaHostTypeEnumMediaServer,
2018-10-12 16:45:35 +00:00
"urn:schemas-upnp-org:device:MediaRenderer:3": omm.MetaHostTypeEnumMediaServer,
2018-10-10 13:38:53 +00:00
"urn:schemas-upnp-org:device:MediaServer:4": omm.MetaHostTypeEnumMediaServer,
2018-10-12 16:45:35 +00:00
"urn:schemas-upnp-org:device:MediaRenderer:4": omm.MetaHostTypeEnumMediaServer,
2018-10-10 13:38:53 +00:00
}
)
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
}
2018-09-13 16:14:34 +00:00
metaIPTypeEnum := omm.ToMetaIPTypeEnum(discoverySession.Zone().MetaIPType)
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-13 16:14:34 +00:00
pURL := rd.PresentationURL.URL
ip := oun.ParseIP(pURL.Hostname())
if nil == ip {
continue LOOP
}
switch metaIPTypeEnum {
case omm.MetaIPTypeEnumV4:
if 4 != ip.Version() {
continue LOOP
}
case omm.MetaIPTypeEnumV6:
if 6 != ip.Version() {
continue LOOP
}
default:
continue LOOP
}
meta := map[string]string{
"DeviceType": rd.DeviceType,
"FriendlyName": rd.FriendlyName,
"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-12 04:55:51 +00:00
h := omd.NewHost(
discoverySession.Zone(),
discoverySession.Zone().MetaIPType,
2018-09-13 16:14:34 +00:00
ip.String(),
2018-09-12 04:55:51 +00:00
)
2018-09-13 16:14:34 +00:00
h.Name = rd.ModelName
2018-09-12 04:55:51 +00:00
h.DiscoveredDate = omu.NowPtr()
2018-10-10 13:38:53 +00:00
mht, ok := deviceTypes[rd.DeviceType]
if ok {
h.HostType = mht.String()
} else {
h.HostType = omm.MetaHostTypeEnumHost.String()
}
2018-09-11 07:59:21 +00:00
discoverySession.AddHost(
2018-09-17 14:18:49 +00:00
omm.ToMetaDiscovererType(omm.MetaDiscovererTypeEnumUPnP),
2018-09-12 04:55:51 +00:00
h,
2018-09-13 16:14:34 +00:00
meta,
)
portNumber, err := strconv.Atoi(pURL.Port())
if nil != err {
continue LOOP
}
p := omd.NewPort(
h,
omm.ToMetaPortType(omm.MetaPortTypeEnumTCP),
portNumber,
)
p.DiscoveredDate = omu.NowPtr()
p = discoverySession.AddPort(
2018-09-17 14:18:49 +00:00
omm.ToMetaDiscovererType(omm.MetaDiscovererTypeEnumUPnP),
2018-09-13 16:14:34 +00:00
p,
meta,
)
metaCryptoType := omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumNONE)
serviceType := omm.MetaServiceTypeEnumUNKNOWN.String()
serviceKey := "UNKNOWN"
switch strings.ToLower(pURL.Scheme) {
case "http":
serviceKey = "HTTP"
serviceType = omm.MetaServiceTypeEnumWeb.String()
case "https":
serviceKey = "HTTPS"
metaCryptoType = omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumTLS)
serviceType = omm.MetaServiceTypeEnumWeb.String()
default:
}
s := omd.NewService(
p,
metaCryptoType,
serviceKey,
)
s.Name = pURL.Scheme
s.ServiceType = serviceType
s.DiscoveredDate = omu.NowPtr()
discoverySession.AddService(
2018-09-17 14:18:49 +00:00
omm.ToMetaDiscovererType(omm.MetaDiscovererTypeEnumUPnP),
2018-09-13 16:14:34 +00:00
s,
meta,
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
}