This commit is contained in:
crusader 2018-09-13 19:20:58 +09:00
parent 5f46929717
commit ec0e83e38b
2 changed files with 62 additions and 20 deletions

View File

@ -10,6 +10,8 @@ import (
"sync"
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
omu "git.loafle.net/overflow/model/util"
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"
@ -40,13 +42,15 @@ type DiscoverySession interface {
AddHost(discoveredBy string, host *omd.Host, meta map[string]string) *omd.Host
AddPort(discoveredBy string, port *omd.Port, meta map[string]string) *omd.Port
AddService(discoveredBy string, service *omd.Service, meta map[string]string) *omd.Service
AddServiceUnknown(discoveredBy string, port *omd.Port) *omd.Service
DiscoveredHost(address string) *omd.Host
DiscoveredAllHosts(includeMac bool) map[string]*omd.Host
DiscoveredPort(host *omd.Host, portNumber int) map[string]*omd.Port
DiscoveredAllPorts() map[*omd.Host]map[json.Number]map[string]*omd.Port
DiscoveredService(port *omd.Port, name string) map[string]*omd.Service
DiscoveredAllServices() map[*omd.Port]map[string]map[string]*omd.Service
DiscoveredService(port *omd.Port, name string) *omd.Service
DiscoveredServices(port *omd.Port) map[string]*omd.Service
DiscoveredAllServices() map[*omd.Port]map[string]*omd.Service
StopChan() <-chan struct{}
}
@ -73,7 +77,7 @@ type ofDiscoverySession struct {
hosts map[string]*omd.Host
includeMachosts map[string]*omd.Host
ports map[*omd.Host]map[json.Number]map[string]*omd.Port
services map[*omd.Port]map[string]map[string]*omd.Service
services map[*omd.Port]map[string]*omd.Service
stopChan chan struct{}
}
@ -91,7 +95,7 @@ func (ds *ofDiscoverySession) init(request types.DiscoveryRequest) {
ds.hosts = make(map[string]*omd.Host)
ds.includeMachosts = make(map[string]*omd.Host)
ds.ports = make(map[*omd.Host]map[json.Number]map[string]*omd.Port)
ds.services = make(map[*omd.Port]map[string]map[string]*omd.Service)
ds.services = make(map[*omd.Port]map[string]*omd.Service)
ds.stopChan = make(chan struct{})
}
@ -353,6 +357,24 @@ func (ds *ofDiscoverySession) AddService(discoveredBy string, service *omd.Servi
return s
}
func (ds *ofDiscoverySession) AddServiceUnknown(discoveredBy string, port *omd.Port) *omd.Service {
if nil == ds.DiscoveredServices(port) {
s := omd.NewService(
port,
omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumNONE),
omd.UnknownServiceKey,
)
s.Name = ""
s.ServiceType = omm.MetaServiceTypeEnumUNKNOWN.String()
s.ServiceVendor = ""
s.ServiceVersion = ""
s.DiscoveredDate = omu.NowPtr()
return ds.AddService(discoveredBy, s, nil)
}
return nil
}
func (ds *ofDiscoverySession) DiscoveredHost(address string) *omd.Host {
ds.discoveredMtx.RLock()
defer ds.discoveredMtx.RUnlock()
@ -398,7 +420,7 @@ func (ds *ofDiscoverySession) DiscoveredAllPorts() map[*omd.Host]map[json.Number
return ds.ports
}
func (ds *ofDiscoverySession) DiscoveredService(port *omd.Port, name string) map[string]*omd.Service {
func (ds *ofDiscoverySession) DiscoveredService(port *omd.Port, name string) *omd.Service {
ds.discoveredMtx.RLock()
defer ds.discoveredMtx.RUnlock()
@ -407,12 +429,29 @@ func (ds *ofDiscoverySession) DiscoveredService(port *omd.Port, name string) map
return nil
}
portServices, ok := ds.services[p]
services, ok := ds.services[p]
if !ok {
return nil
}
services, ok := portServices[name]
s, ok := services[name]
if !ok {
return nil
}
return s
}
func (ds *ofDiscoverySession) DiscoveredServices(port *omd.Port) map[string]*omd.Service {
ds.discoveredMtx.RLock()
defer ds.discoveredMtx.RUnlock()
p, _ := ds.findPort(port)
if nil == p {
return nil
}
services, ok := ds.services[p]
if !ok {
return nil
}
@ -420,7 +459,7 @@ func (ds *ofDiscoverySession) DiscoveredService(port *omd.Port, name string) map
return services
}
func (ds *ofDiscoverySession) DiscoveredAllServices() map[*omd.Port]map[string]map[string]*omd.Service {
func (ds *ofDiscoverySession) DiscoveredAllServices() map[*omd.Port]map[string]*omd.Service {
return ds.services
}
@ -498,22 +537,20 @@ func (ds *ofDiscoverySession) findService(service *omd.Service) (s *omd.Service,
return
}
portServices, ok := ds.services[p]
services, ok := ds.services[p]
if !ok {
ds.services[p] = make(map[string]map[string]*omd.Service)
portServices = ds.services[p]
ds.services[p] = make(map[string]*omd.Service)
services = ds.services[p]
} else {
if _, ok := services[omd.UnknownServiceKey]; ok {
delete(services, omd.UnknownServiceKey)
}
}
services, ok := portServices[service.Key]
s, ok = services[service.Key]
if !ok {
portServices[service.Key] = make(map[string]*omd.Service)
services = portServices[service.Key]
}
s, ok = services[service.MetaCryptoType.Key]
if !ok {
services[service.MetaCryptoType.Key] = service
s = services[service.MetaCryptoType.Key]
services[service.Key] = service
s = services[service.Key]
modified = true
}

View File

@ -93,6 +93,11 @@ LOOP:
s,
matchCtx.GetAttributes(),
)
} else {
discoverySession.AddServiceUnknown(
"Service Matcher",
targetPort,
)
}
return nil