From 570a0241a067d3f5873e91d35469b2d9d3e0441b Mon Sep 17 00:00:00 2001 From: crusader Date: Wed, 15 Nov 2017 21:33:46 +0900 Subject: [PATCH] ing --- discovery/discovery.go | 147 +++++++++++++++++++++++++++++++++-------- discovery/host.go | 4 +- discovery/port.go | 7 ++ discovery/service.go | 7 ++ discovery/zone.go | 10 +-- 5 files changed, 141 insertions(+), 34 deletions(-) create mode 100644 discovery/port.go create mode 100644 discovery/service.go diff --git a/discovery/discovery.go b/discovery/discovery.go index 7d44db0..ec28e82 100644 --- a/discovery/discovery.go +++ b/discovery/discovery.go @@ -25,15 +25,15 @@ func DiscoverZone(dz *model.DiscoveryZone) { } func DiscoverHost(dh *model.DiscoveryHost) { - discoverer.discoverHost(dh) + discoverer.discoverHost(dh.Zone, dh) } func DiscoverPort(dp *model.DiscoveryPort) { - discoverer.discoverPort(dp) + discoverer.discoverPort(dp.Host, dp) } func DiscoverService(ds *model.DiscoveryService) { - discoverer.discoverService(ds) + discoverer.discoverService(ds.Port, ds) } func Stop() { @@ -57,9 +57,6 @@ func (d *discovery) start() { d.sendChan = make(chan interface{}) d.errChan = make(chan error) logging.Logger().Info(fmt.Sprintf("Discovery: discovery is started")) - - d.stopWg.Add(1) - go handle(d) } func (d *discovery) stop() { @@ -73,47 +70,141 @@ func (d *discovery) stop() { logging.Logger().Info(fmt.Sprintf("Discovery: discovery is stopped")) } -func handle(d *discovery) { - defer d.stopWg.Done() +func (d *discovery) discoverZone(dz *model.DiscoveryZone) { + logging.Logger().Debug(fmt.Sprintf("Discovery: DiscoverZone is start")) + + d.stopWg.Add(1) + resultChan := make(chan *model.Zone) + errChan := make(chan error) + doneChan := make(chan struct{}) + + defer func() { + close(resultChan) + close(errChan) + close(doneChan) + d.stopWg.Done() + }() + + go scanZone(dz, resultChan, errChan, doneChan) for { select { + case z := <-resultChan: + logging.Logger().Info(fmt.Sprintf("zone: %v", z)) + if nil != dz.DiscoveryHost { + d.discoverHost(z, dz.DiscoveryHost) + } + case err := <-errChan: + logging.Logger().Info(fmt.Sprintf("zone err: %v", err)) case <-d.stopChan: return - case data := <-d.sendChan: - logging.Logger().Info(fmt.Sprintf("chan: %v", data)) + case <-doneChan: + logging.Logger().Debug(fmt.Sprintf("Discovery: DiscoverZone is complete")) + return } } } -func (d *discovery) discoverZone(dz *model.DiscoveryZone) { - logging.Logger().Debug(fmt.Sprintf("Discovery: DiscoverZone is start")) - - doneChan := make(chan struct{}) +func (d *discovery) discoverHost(zone *model.Zone, dh *model.DiscoveryHost) { + logging.Logger().Debug(fmt.Sprintf("Discovery: DiscoverHost is start")) d.stopWg.Add(1) - defer d.stopWg.Done() + resultChan := make(chan *model.Host) + errChan := make(chan error) + doneChan := make(chan struct{}) - go scanZone(d, dz, doneChan) + defer func() { + close(resultChan) + close(errChan) + close(doneChan) + d.stopWg.Done() + }() - select { - case <-d.stopChan: - <-doneChan - case <-doneChan: - logging.Logger().Debug(fmt.Sprintf("Discovery: DiscoverZone is complete")) + go scanHost(zone, dh, resultChan, errChan, doneChan) + + for { + select { + case h := <-resultChan: + logging.Logger().Info(fmt.Sprintf("host: %v", h)) + if nil != dh.DiscoveryPort { + d.discoverPort(h, dh.DiscoveryPort) + } + case err := <-errChan: + logging.Logger().Info(fmt.Sprintf("host err: %v", err)) + case <-d.stopChan: + return + case <-doneChan: + logging.Logger().Debug(fmt.Sprintf("Discovery: DiscoverHost is complete")) + return + } } } -func (d *discovery) discoverHost(dh *model.DiscoveryHost) { - logging.Logger().Debug(fmt.Sprintf("DiscoverHost")) +func (d *discovery) discoverPort(host *model.Host, dp *model.DiscoveryPort) { + logging.Logger().Debug(fmt.Sprintf("Discovery: DiscoverPort is start")) + + d.stopWg.Add(1) + resultChan := make(chan *model.Port) + errChan := make(chan error) + doneChan := make(chan struct{}) + + defer func() { + close(resultChan) + close(errChan) + close(doneChan) + d.stopWg.Done() + }() + + go scanPort(host, dp, resultChan, errChan, doneChan) + + for { + select { + case p := <-resultChan: + logging.Logger().Info(fmt.Sprintf("port: %v", p)) + if nil != dp.DiscoveryService { + d.discoverService(p, dp.DiscoveryService) + } + case err := <-errChan: + logging.Logger().Info(fmt.Sprintf("port err: %v", err)) + case <-d.stopChan: + return + case <-doneChan: + logging.Logger().Debug(fmt.Sprintf("Discovery: DiscoverPort is complete")) + return + } + } } -func (d *discovery) discoverPort(dp *model.DiscoveryPort) { - logging.Logger().Debug(fmt.Sprintf("DiscoverPort")) -} +func (d *discovery) discoverService(port *model.Port, ds *model.DiscoveryService) { + logging.Logger().Debug(fmt.Sprintf("Discovery: DiscoverService is start")) -func (d *discovery) discoverService(ds *model.DiscoveryService) { - logging.Logger().Debug(fmt.Sprintf("DiscoverService")) + d.stopWg.Add(1) + resultChan := make(chan *model.Service) + errChan := make(chan error) + doneChan := make(chan struct{}) + + defer func() { + close(resultChan) + close(errChan) + close(doneChan) + d.stopWg.Done() + }() + + go scanService(port, ds, resultChan, errChan, doneChan) + + for { + select { + case s := <-resultChan: + logging.Logger().Info(fmt.Sprintf("service: %v", s)) + case err := <-errChan: + logging.Logger().Info(fmt.Sprintf("service err: %v", err)) + case <-d.stopChan: + return + case <-doneChan: + logging.Logger().Debug(fmt.Sprintf("Discovery: DiscoverService is complete")) + return + } + } } func (d *discovery) sendResult() { diff --git a/discovery/host.go b/discovery/host.go index e682b76..e17c3a7 100644 --- a/discovery/host.go +++ b/discovery/host.go @@ -2,5 +2,7 @@ package discovery import "git.loafle.net/overflow/overflow_discovery/api/module/discovery/model" -func scanHost(d *discovery, dh *model.DiscoveryHost, doneChan chan<- struct{}) { +func scanHost(zone *model.Zone, dh *model.DiscoveryHost, resultChan chan *model.Host, errChan chan error, doneChan chan<- struct{}) { + + doneChan <- struct{}{} } diff --git a/discovery/port.go b/discovery/port.go new file mode 100644 index 0000000..c94c900 --- /dev/null +++ b/discovery/port.go @@ -0,0 +1,7 @@ +package discovery + +import "git.loafle.net/overflow/overflow_discovery/api/module/discovery/model" + +func scanPort(host *model.Host, dp *model.DiscoveryPort, resultChan chan *model.Port, errChan chan error, doneChan chan<- struct{}) { + doneChan <- struct{}{} +} diff --git a/discovery/service.go b/discovery/service.go new file mode 100644 index 0000000..11d8b20 --- /dev/null +++ b/discovery/service.go @@ -0,0 +1,7 @@ +package discovery + +import "git.loafle.net/overflow/overflow_discovery/api/module/discovery/model" + +func scanService(port *model.Port, ds *model.DiscoveryService, resultChan chan *model.Service, errChan chan error, doneChan chan<- struct{}) { + doneChan <- struct{}{} +} diff --git a/discovery/zone.go b/discovery/zone.go index a3e52f2..7ea9edb 100644 --- a/discovery/zone.go +++ b/discovery/zone.go @@ -8,7 +8,7 @@ import ( "git.loafle.net/overflow/overflow_discovery/api/module/discovery/model" ) -func scanZone(d *discovery, dz *model.DiscoveryZone, doneChan chan<- struct{}) { +func scanZone(dz *model.DiscoveryZone, resultChan chan *model.Zone, errChan chan error, doneChan chan<- struct{}) { var err error var ifaces []net.Interface var addrs []net.Addr @@ -23,7 +23,7 @@ func scanZone(d *discovery, dz *model.DiscoveryZone, doneChan chan<- struct{}) { // } if ifaces, err = net.Interfaces(); nil != err { - d.errChan <- err + errChan <- err return } @@ -32,14 +32,14 @@ func scanZone(d *discovery, dz *model.DiscoveryZone, doneChan chan<- struct{}) { for _, i := range ifaces { if addrs, err = i.Addrs(); nil != err { - d.errChan <- err + errChan <- err continue } for _, addr := range addrs { if _, ipnet, err = net.ParseCIDR(addr.String()); nil != err { - d.errChan <- err + errChan <- err continue } if ipnet.IP.IsLoopback() || checkSameZone(zones, ipnet) || checkExclude(dz.ExcludePatterns, i.Name) { @@ -55,7 +55,7 @@ func scanZone(d *discovery, dz *model.DiscoveryZone, doneChan chan<- struct{}) { IP: strings.Split(addr.String(), "/")[0], } - d.sendChan <- z + resultChan <- z } }