This commit is contained in:
crusader 2018-09-11 21:04:53 +09:00
parent d2e70072e3
commit b3a85b5e26
2 changed files with 166 additions and 20 deletions

View File

@ -6,9 +6,11 @@ import (
"log"
"net"
"strconv"
"strings"
"sync"
omd "git.loafle.net/overflow/model/discovery"
ouej "git.loafle.net/overflow/util-go/encoding/json"
ounc "git.loafle.net/overflow/util-go/net/cidr"
"git.loafle.net/overflow_scanner/probe/discovery/types"
"git.loafle.net/overflow_scanner/probe/internal/pcap"
@ -236,6 +238,26 @@ func (ds *ofDiscoverySession) AddHost(discoveredBy string, host *omd.Host, meta
modified = true
}
if "" == h.OsType && "" != host.OsType {
h.OsType = host.OsType
modified = true
}
if "HOST" == h.DeviceType && "" != host.DeviceType {
h.DeviceType = host.DeviceType
modified = true
}
if h.DeviceVendor != host.DeviceVendor {
h.DeviceVendor = host.DeviceVendor
modified = true
}
if h.DeviceModel != host.DeviceModel {
h.DeviceModel = host.DeviceModel
modified = true
}
discoveredBys, discoveredByModified := ds.appendDiscoveredBy(discoveredBy, h.DiscoveredBy)
if discoveredByModified {
h.DiscoveredBy = discoveredBys
@ -248,8 +270,13 @@ func (ds *ofDiscoverySession) AddHost(discoveredBy string, host *omd.Host, meta
modified = true
}
if modified && nil != ds.discoveryDelegator {
ds.discoveryDelegator <- h
h, addtionalModified := ds.addtionalHost(discoveredBy, h)
if addtionalModified {
modified = true
}
if modified {
ds.delegate(h)
}
return h
@ -273,8 +300,8 @@ func (ds *ofDiscoverySession) AddPort(discoveredBy string, port *omd.Port, meta
modified = metaModified
}
if modified && nil != ds.discoveryDelegator {
ds.discoveryDelegator <- p
if modified {
ds.delegate(p)
}
return p
@ -298,8 +325,13 @@ func (ds *ofDiscoverySession) AddService(discoveredBy string, service *omd.Servi
modified = metaModified
}
if modified && nil != ds.discoveryDelegator {
ds.discoveryDelegator <- s
s, addtionalModified := ds.addtionalService(discoveredBy, s)
if addtionalModified {
modified = true
}
if modified {
ds.delegate(s)
}
return s
@ -376,10 +408,27 @@ func (ds *ofDiscoverySession) DiscoveredAllServices() map[*omd.Port]map[string]m
return ds.services
}
func (ds *ofDiscoverySession) StopChan() <-chan struct{} {
return ds.stopChan
}
func (ds *ofDiscoverySession) delegate(data interface{}) {
if nil != ds.discoveryDelegator {
ds.discoveryDelegator <- data
}
}
func (ds *ofDiscoverySession) findHost(host *omd.Host) (h *omd.Host, modified bool) {
modified = false
var ok bool
if "" == host.DeviceType {
host.DeviceType = "HOST"
}
if "" == host.OsType {
host.OsType = "UNKNOWN"
}
h, ok = ds.hosts[host.Address]
if !ok {
ds.hosts[host.Address] = host
@ -393,10 +442,6 @@ func (ds *ofDiscoverySession) findHost(host *omd.Host) (h *omd.Host, modified bo
return
}
func (ds *ofDiscoverySession) StopChan() <-chan struct{} {
return ds.stopChan
}
func (ds *ofDiscoverySession) findPort(port *omd.Port) (p *omd.Port, modified bool) {
modified = false
var ok bool
@ -459,6 +504,108 @@ func (ds *ofDiscoverySession) findService(service *omd.Service) (s *omd.Service,
return
}
func (ds *ofDiscoverySession) addtionalHost(discoveredBy string, host *omd.Host) (h *omd.Host, modified bool) {
h = host
modified = false
if nil == host.Meta {
return
}
meta, ok := host.Meta[discoveredBy]
if !ok || nil == meta || 0 == len(meta) {
return
}
switch discoveredBy {
case "mDNS":
_h, _modified := ds.addtionalHostMDNS(h, meta)
if _modified {
h = _h
modified = true
}
}
return
}
func (ds *ofDiscoverySession) addtionalHostMDNS(host *omd.Host, meta map[string]string) (h *omd.Host, modified bool) {
h = host
modified = false
_vendor, ok := meta["vendor"]
if ok || "" != _vendor {
h.DeviceVendor = _vendor
modified = true
}
_model, ok := meta["model"]
if ok || "" != _model {
h.DeviceModel = _model
if "Synology" == _vendor && strings.Contains(_model, "DS1817+") {
h.DeviceType = "NAS"
}
modified = true
}
return
}
func (ds *ofDiscoverySession) addtionalService(discoveredBy string, service *omd.Service) (s *omd.Service, modified bool) {
s = service
modified = false
if nil == service.Meta {
return
}
meta, ok := service.Meta[discoveredBy]
if !ok || nil == meta || 0 == len(meta) {
return
}
switch discoveredBy {
case "mDNS":
_s, _modified := ds.addtionalServiceMDNS(s, meta)
if _modified {
s = _s
modified = true
}
}
return
}
func (ds *ofDiscoverySession) addtionalServiceMDNS(service *omd.Service, meta map[string]string) (s *omd.Service, modified bool) {
s = service
modified = false
portNumber, err := ouej.NumberToInt(service.Port.PortNumber)
if nil != err {
log.Print(err)
return
}
switch portNumber {
case 515:
if "printer" == service.Key {
_h := service.Port.Host
_h.DeviceType = "PRINTER"
_h.Name = service.Name
ds.AddHost("mDNS", _h, nil)
}
case 9100:
if "pdl-datastream" == service.Key {
_h := service.Port.Host
_h.DeviceType = "PRINTER"
_h.Name = service.Name
ds.AddHost("mDNS", _h, nil)
}
}
return
}
func (ds *ofDiscoverySession) appendDiscoveredBy(discoveredBy string, oriDiscoveredBy []string) (resultDiscoveredBy []string, modified bool) {
modified = false

View File

@ -1,7 +1,6 @@
package service
import (
"log"
"reflect"
"git.loafle.net/overflow_scanner/probe/internal/pubsub"
@ -52,7 +51,7 @@ func (s *DiscoveryService) InitService() {
),
"/scanner",
)
log.Print("Start ", data)
// log.Print("Start ", data)
case types.DiscoveryMessageTypeStop:
s.PubSub.Pub(
rpc.MakeRPCMessage(
@ -62,7 +61,7 @@ func (s *DiscoveryService) InitService() {
),
"/scanner",
)
log.Print("Stop ", data)
// log.Print("Stop ", data)
case types.DiscoveryMessageTypeQueueing:
s.PubSub.Pub(
rpc.MakeRPCMessage(
@ -72,7 +71,7 @@ func (s *DiscoveryService) InitService() {
),
"/scanner",
)
log.Print("Queueing ", data)
// log.Print("Queueing ", data)
case types.DiscoveryMessageTypeQueueingFailed:
s.PubSub.Pub(
rpc.MakeRPCMessage(
@ -82,7 +81,7 @@ func (s *DiscoveryService) InitService() {
),
"/scanner",
)
log.Print("QueueingFailed ", data)
// log.Print("QueueingFailed ", data)
case types.DiscoveryMessageTypeQueueingTimeout:
s.PubSub.Pub(
rpc.MakeRPCMessage(
@ -92,7 +91,7 @@ func (s *DiscoveryService) InitService() {
),
"/scanner",
)
log.Print("QueueingTimeout ", data)
// log.Print("QueueingTimeout ", data)
case types.DiscoveryMessageTypeError:
s.PubSub.Pub(
rpc.MakeRPCMessage(
@ -102,7 +101,7 @@ func (s *DiscoveryService) InitService() {
),
"/scanner",
)
log.Print("Error ", err)
// log.Print("Error ", err)
case types.DiscoveryMessageTypeHost:
s.PubSub.Pub(
rpc.MakeRPCMessage(
@ -112,7 +111,7 @@ func (s *DiscoveryService) InitService() {
),
"/scanner",
)
log.Print("Host ", data)
// log.Print("Host ", data)
case types.DiscoveryMessageTypePort:
s.PubSub.Pub(
rpc.MakeRPCMessage(
@ -122,7 +121,7 @@ func (s *DiscoveryService) InitService() {
),
"/scanner",
)
log.Print("Port ", data)
// log.Print("Port ", data)
case types.DiscoveryMessageTypeService:
s.PubSub.Pub(
rpc.MakeRPCMessage(
@ -132,7 +131,7 @@ func (s *DiscoveryService) InitService() {
),
"/scanner",
)
log.Print("Service ", data)
// log.Print("Service ", data)
default:
}