2018-08-29 12:04:23 +00:00
|
|
|
package discovery
|
|
|
|
|
|
|
|
import (
|
2018-08-31 05:15:46 +00:00
|
|
|
"log"
|
2018-08-29 12:04:23 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
omd "git.loafle.net/overflow/model/discovery"
|
|
|
|
omu "git.loafle.net/overflow/model/util"
|
2018-08-30 12:56:32 +00:00
|
|
|
"git.loafle.net/overflow_scanner/probe/discovery/protocol/mdns"
|
|
|
|
"git.loafle.net/overflow_scanner/probe/discovery/protocol/snmp"
|
|
|
|
"git.loafle.net/overflow_scanner/probe/discovery/protocol/upnp"
|
2018-08-29 12:04:23 +00:00
|
|
|
"git.loafle.net/overflow_scanner/probe/discovery/session"
|
|
|
|
"git.loafle.net/overflow_scanner/probe/discovery/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Discoverer interface {
|
|
|
|
DiscoverHost(requesterID string, zone *omd.Zone, dh *omd.DiscoverHost)
|
|
|
|
DiscoverPort(requesterID string, host *omd.Host, dp *omd.DiscoverPort)
|
|
|
|
DiscoverService(requesterID string, port *omd.Port, ds *omd.DiscoverService)
|
|
|
|
Message() <-chan types.DiscoveryMessage
|
|
|
|
|
|
|
|
Shutdown()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Instance() Discoverer {
|
|
|
|
_once.Do(func() {
|
|
|
|
_instance = newDiscoverer()
|
|
|
|
})
|
|
|
|
return _instance
|
|
|
|
}
|
|
|
|
|
|
|
|
var _instance Discoverer
|
|
|
|
var _once sync.Once
|
|
|
|
|
|
|
|
func newDiscoverer() Discoverer {
|
|
|
|
i := &ofDiscoverer{}
|
|
|
|
i.start()
|
|
|
|
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
type ofDiscoverer struct {
|
|
|
|
stopChan chan struct{}
|
|
|
|
stopWg sync.WaitGroup
|
|
|
|
|
|
|
|
requestQueue chan types.DiscoveryRequest
|
|
|
|
messageChan chan types.DiscoveryMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *ofDiscoverer) DiscoverHost(requesterID string, zone *omd.Zone, dh *omd.DiscoverHost) {
|
2018-08-30 12:56:32 +00:00
|
|
|
d.enqueue(retainDiscoveryRequest(requesterID, types.DiscoveryRequestTypeHost, zone, dh))
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *ofDiscoverer) DiscoverPort(requesterID string, host *omd.Host, dp *omd.DiscoverPort) {
|
2018-08-30 12:56:32 +00:00
|
|
|
d.enqueue(retainDiscoveryRequest(requesterID, types.DiscoveryRequestTypePort, host, dp))
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *ofDiscoverer) DiscoverService(requesterID string, port *omd.Port, ds *omd.DiscoverService) {
|
2018-08-30 12:56:32 +00:00
|
|
|
d.enqueue(retainDiscoveryRequest(requesterID, types.DiscoveryRequestTypeService, port, ds))
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *ofDiscoverer) Message() <-chan types.DiscoveryMessage {
|
|
|
|
return d.messageChan
|
|
|
|
}
|
|
|
|
|
2018-08-30 12:56:32 +00:00
|
|
|
func (d *ofDiscoverer) SendMessage(discoveryRequest types.DiscoveryRequest, messageType types.DiscoveryMessageType, data interface{}, err error) {
|
|
|
|
d.messageChan <- retainDiscoveryMessage(discoveryRequest, messageType, data, err)
|
|
|
|
}
|
|
|
|
|
2018-08-29 12:04:23 +00:00
|
|
|
func (d *ofDiscoverer) Shutdown() {
|
|
|
|
if d.stopChan == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
close(d.requestQueue)
|
|
|
|
close(d.messageChan)
|
|
|
|
|
|
|
|
close(d.stopChan)
|
|
|
|
d.stopWg.Wait()
|
|
|
|
|
|
|
|
d.stopChan = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *ofDiscoverer) start() {
|
|
|
|
d.stopChan = make(chan struct{})
|
|
|
|
|
|
|
|
d.requestQueue = make(chan types.DiscoveryRequest, 10)
|
|
|
|
d.messageChan = make(chan types.DiscoveryMessage, 256)
|
|
|
|
|
|
|
|
d.stopWg.Add(1)
|
|
|
|
go d.handleRequest()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *ofDiscoverer) enqueue(req *ofDiscoveryRequest) {
|
|
|
|
select {
|
|
|
|
case d.requestQueue <- req:
|
2018-08-30 12:56:32 +00:00
|
|
|
d.SendMessage(req, types.DiscoveryMessageTypeQueueing, omu.Now(), nil)
|
2018-08-29 12:04:23 +00:00
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-req.dequeue:
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
req.timeout = true
|
2018-08-30 12:56:32 +00:00
|
|
|
d.SendMessage(req, types.DiscoveryMessageTypeTimeout, omu.Now(), nil)
|
2018-08-29 12:04:23 +00:00
|
|
|
req.release()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
default:
|
2018-08-30 12:56:32 +00:00
|
|
|
d.SendMessage(req, types.DiscoveryMessageTypeFailedQueueing, omu.Now(), nil)
|
2018-08-29 12:04:23 +00:00
|
|
|
req.release()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *ofDiscoverer) handleRequest() {
|
|
|
|
defer func() {
|
|
|
|
d.stopWg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
LOOP:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case req, ok := <-d.requestQueue:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if req.(*ofDiscoveryRequest).timeout {
|
|
|
|
continue LOOP
|
|
|
|
}
|
|
|
|
req.(*ofDiscoveryRequest).dequeue <- true
|
|
|
|
|
2018-08-30 12:56:32 +00:00
|
|
|
d.SendMessage(req, types.DiscoveryMessageTypeStart, omu.Now(), nil)
|
2018-08-29 12:04:23 +00:00
|
|
|
d.discover(req)
|
2018-08-30 12:56:32 +00:00
|
|
|
d.SendMessage(req, types.DiscoveryMessageTypeStop, omu.Now(), nil)
|
2018-08-29 12:04:23 +00:00
|
|
|
|
|
|
|
req.(*ofDiscoveryRequest).release()
|
|
|
|
case <-d.stopChan:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *ofDiscoverer) discover(req types.DiscoveryRequest) {
|
|
|
|
if types.DiscoveryRequestTypeNone == req.RequestType() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s := session.RetainDiscoverySession()
|
|
|
|
defer func() {
|
|
|
|
session.ReleaseDiscoverySession(s)
|
|
|
|
}()
|
|
|
|
|
2018-08-29 18:20:51 +00:00
|
|
|
if err := s.InitWithRequest(req); nil != err {
|
2018-08-30 12:56:32 +00:00
|
|
|
d.SendMessage(req, types.DiscoveryMessageTypeError, nil, err)
|
2018-08-29 18:20:51 +00:00
|
|
|
return
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 12:56:32 +00:00
|
|
|
d.complexDiscover(s)
|
|
|
|
d.hierarchyDiscover(s)
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 12:56:32 +00:00
|
|
|
func (d *ofDiscoverer) complexDiscover(s session.DiscoverySession) {
|
2018-08-29 13:23:41 +00:00
|
|
|
var wg sync.WaitGroup
|
2018-08-29 12:04:23 +00:00
|
|
|
|
2018-08-30 12:56:32 +00:00
|
|
|
discoveredChan := make(chan interface{})
|
|
|
|
s.SetDiscoveryDelegator(discoveredChan)
|
|
|
|
defer func() {
|
|
|
|
s.SetDiscoveryDelegator(nil)
|
|
|
|
close(discoveredChan)
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case target, ok := <-discoveredChan:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch target.(type) {
|
|
|
|
case *omd.Host:
|
|
|
|
d.SendMessage(s.DiscoveryRequest(), types.DiscoveryMessageTypeHost, target, nil)
|
|
|
|
case *omd.Port:
|
|
|
|
d.SendMessage(s.DiscoveryRequest(), types.DiscoveryMessageTypePort, target, nil)
|
|
|
|
case *omd.Service:
|
|
|
|
d.SendMessage(s.DiscoveryRequest(), types.DiscoveryMessageTypeService, target, nil)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-08-29 13:23:41 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2018-08-31 05:15:46 +00:00
|
|
|
err := upnp.Scan(s)
|
|
|
|
if nil != err {
|
|
|
|
log.Printf("UPnP %v", err)
|
|
|
|
}
|
2018-08-29 13:23:41 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2018-08-31 05:15:46 +00:00
|
|
|
err := mdns.Scan(s)
|
|
|
|
if nil != err {
|
|
|
|
log.Printf("mDNS %v", err)
|
|
|
|
}
|
2018-08-29 13:23:41 +00:00
|
|
|
}()
|
|
|
|
|
2018-08-30 12:56:32 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2018-08-31 05:15:46 +00:00
|
|
|
err := snmp.Scan(s)
|
|
|
|
if nil != err {
|
|
|
|
log.Printf("SNMP %v", err)
|
|
|
|
}
|
2018-08-30 12:56:32 +00:00
|
|
|
}()
|
|
|
|
|
2018-08-29 13:23:41 +00:00
|
|
|
wg.Wait()
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 12:56:32 +00:00
|
|
|
func (d *ofDiscoverer) hierarchyDiscover(s session.DiscoverySession) {
|
|
|
|
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|