This commit is contained in:
crusader 2017-11-18 21:08:08 +09:00
parent d7f3ea3268
commit 2754bbc056
10 changed files with 66 additions and 47 deletions

View File

@ -33,18 +33,20 @@ func RetainScanner(zone *model.Zone) (PCapScanner, error) {
} }
func ReleaseScanner(zone *model.Zone) { func ReleaseScanner(zone *model.Zone) {
go func() {
time.Sleep(2 * time.Second)
mtx.Lock() mtx.Lock()
defer mtx.Unlock() defer mtx.Unlock()
if ps, ok := instances[zone.Network]; ok { if ps, ok := instances[zone.Network]; ok {
go func() {
time.Sleep(2 * time.Second)
if ps.release() { if ps.release() {
ps.stop() ps.stop()
delete(instances, zone.Network) delete(instances, zone.Network)
} }
}()
} }
}()
} }
func ReleaseScannerAll() { func ReleaseScannerAll() {

View File

@ -73,9 +73,9 @@ func (d *discovery) stop() {
} }
func (d *discovery) discoverZone(dz *model.DiscoveryZone) { func (d *discovery) discoverZone(dz *model.DiscoveryZone) {
taskScan(d, go taskScan(d,
func(resultChan chan interface{}, errChan chan error, doneChan chan struct{}) { func(resultChan chan interface{}, errChan chan error, doneChan chan struct{}, stopChan chan struct{}) {
scanZone(dz, resultChan, errChan, doneChan) scanZone(dz, resultChan, errChan, doneChan, stopChan)
}, },
func(result interface{}) { func(result interface{}) {
z := result.(*model.Zone) z := result.(*model.Zone)
@ -94,9 +94,9 @@ func (d *discovery) discoverZone(dz *model.DiscoveryZone) {
} }
func (d *discovery) discoverHost(zone *model.Zone, dh *model.DiscoveryHost) { func (d *discovery) discoverHost(zone *model.Zone, dh *model.DiscoveryHost) {
taskScan(d, go taskScan(d,
func(resultChan chan interface{}, errChan chan error, doneChan chan struct{}) { func(resultChan chan interface{}, errChan chan error, doneChan chan struct{}, stopChan chan struct{}) {
scanHost(zone, dh, resultChan, errChan, doneChan) scanHost(zone, dh, resultChan, errChan, doneChan, stopChan)
}, },
func(result interface{}) { func(result interface{}) {
h := result.(*model.Host) h := result.(*model.Host)
@ -109,9 +109,9 @@ func (d *discovery) discoverHost(zone *model.Zone, dh *model.DiscoveryHost) {
} }
func (d *discovery) discoverPort(host *model.Host, dp *model.DiscoveryPort) { func (d *discovery) discoverPort(host *model.Host, dp *model.DiscoveryPort) {
taskScan(d, go taskScan(d,
func(resultChan chan interface{}, errChan chan error, doneChan chan struct{}) { func(resultChan chan interface{}, errChan chan error, doneChan chan struct{}, stopChan chan struct{}) {
scanPort(host, dp, resultChan, errChan, doneChan) scanPort(host, dp, resultChan, errChan, doneChan, stopChan)
}, },
func(result interface{}) { func(result interface{}) {
p := result.(*model.Port) p := result.(*model.Port)
@ -124,9 +124,9 @@ func (d *discovery) discoverPort(host *model.Host, dp *model.DiscoveryPort) {
} }
func (d *discovery) discoverService(port *model.Port, ds *model.DiscoveryService) { func (d *discovery) discoverService(port *model.Port, ds *model.DiscoveryService) {
taskScan(d, go taskScan(d,
func(resultChan chan interface{}, errChan chan error, doneChan chan struct{}) { func(resultChan chan interface{}, errChan chan error, doneChan chan struct{}, stopChan chan struct{}) {
scanService(port, ds, resultChan, errChan, doneChan) scanService(port, ds, resultChan, errChan, doneChan, stopChan)
}, },
func(result interface{}) { func(result interface{}) {
s := result.(*model.Service) s := result.(*model.Service)
@ -143,20 +143,22 @@ func (d *discovery) sendError() {
} }
func taskScan(d *discovery, task func(resultChan chan interface{}, errChan chan error, doneChan chan struct{}), onResult func(result interface{})) { func taskScan(d *discovery, task func(resultChan chan interface{}, errChan chan error, doneChan chan struct{}, stopChan chan struct{}), onResult func(result interface{})) {
d.stopWg.Add(1) d.stopWg.Add(1)
resultChan := make(chan interface{}) resultChan := make(chan interface{})
errChan := make(chan error) errChan := make(chan error)
stopChan := make(chan struct{})
doneChan := make(chan struct{}) doneChan := make(chan struct{})
defer func() { defer func() {
close(resultChan) close(resultChan)
close(errChan) close(errChan)
close(stopChan)
close(doneChan) close(doneChan)
d.stopWg.Done() d.stopWg.Done()
}() }()
go task(resultChan, errChan, doneChan) go task(resultChan, errChan, doneChan, stopChan)
for { for {
select { select {
@ -168,6 +170,8 @@ func taskScan(d *discovery, task func(resultChan chan interface{}, errChan chan
logging.Logger().Debug(fmt.Sprintf("Discovery: task is complete")) logging.Logger().Debug(fmt.Sprintf("Discovery: task is complete"))
return return
case <-d.stopChan: case <-d.stopChan:
close(stopChan)
<-doneChan
return return
} }
} }

View File

@ -9,22 +9,24 @@ import (
"git.loafle.net/overflow/overflow_discovery/discovery/ipv6" "git.loafle.net/overflow/overflow_discovery/discovery/ipv6"
) )
func scanHost(zone *model.Zone, dh *model.DiscoveryHost, resultChan chan interface{}, errChan chan error, doneChan chan<- struct{}) { func scanHost(zone *model.Zone, dh *model.DiscoveryHost, resultChan chan interface{}, errChan chan error, doneChan chan<- struct{}, stopChan chan struct{}) {
defer func() {
doneChan <- struct{}{}
}()
_, ipNet, err := net.ParseCIDR(zone.Network) _, ipNet, err := net.ParseCIDR(zone.Network)
if nil != err { if nil != err {
errChan <- err errChan <- err
doneChan <- struct{}{}
return return
} }
switch len(ipNet.IP) { switch len(ipNet.IP) {
case net.IPv4len: case net.IPv4len:
ipv4.ScanHost(zone, dh, resultChan, errChan, doneChan) ipv4.ScanHost(zone, dh, resultChan, errChan, stopChan)
case net.IPv6len: case net.IPv6len:
ipv6.ScanHost(zone, dh, resultChan, errChan, doneChan) ipv6.ScanHost(zone, dh, resultChan, errChan, stopChan)
default: default:
errChan <- fmt.Errorf("Discovery: Not supported ip length") errChan <- fmt.Errorf("Discovery: Not supported ip length")
doneChan <- struct{}{}
return return
} }
} }

View File

@ -14,11 +14,7 @@ import (
"git.loafle.net/overflow/overflow_discovery/commons/pcap" "git.loafle.net/overflow/overflow_discovery/commons/pcap"
) )
func ScanHost(zone *model.Zone, dh *model.DiscoveryHost, resultChan chan interface{}, errChan chan error, doneChan chan<- struct{}) { func ScanHost(zone *model.Zone, dh *model.DiscoveryHost, resultChan chan interface{}, errChan chan error, stopChan chan struct{}) {
defer func() {
doneChan <- struct{}{}
}()
ps, err := pcap.RetainScanner(zone) ps, err := pcap.RetainScanner(zone)
if nil != err { if nil != err {
errChan <- fmt.Errorf("Discovery: Cannot retain pcap instance %v", err) errChan <- fmt.Errorf("Discovery: Cannot retain pcap instance %v", err)
@ -58,11 +54,13 @@ func ScanHost(zone *model.Zone, dh *model.DiscoveryHost, resultChan chan interfa
if h := handlePacketARP(zone, cr, hosts, packet); nil != h { if h := handlePacketARP(zone, cr, hosts, packet); nil != h {
logging.Logger().Debug(fmt.Sprintf("Discovery: Host[%v] is founded", h)) logging.Logger().Debug(fmt.Sprintf("Discovery: Host[%v] is founded", h))
} }
case <-stopChan:
return
} }
} }
}() }()
if err := sendARP(ps, zone, hostRanges); nil != err { if err := sendARP(ps, zone, hostRanges, stopChan); nil != err {
errChan <- err errChan <- err
return return
} }
@ -91,7 +89,7 @@ func handlePacketARP(zone *model.Zone, cr cidr.CIDRRanger, hosts map[string]*mod
return h return h
} }
func sendARP(ps pcap.PCapScanner, zone *model.Zone, hostRanges []net.IP) error { func sendARP(ps pcap.PCapScanner, zone *model.Zone, hostRanges []net.IP, stopChan chan struct{}) error {
hwAddr, err := net.ParseMAC(zone.Mac) hwAddr, err := net.ParseMAC(zone.Mac)
if nil != err { if nil != err {
return err return err
@ -113,6 +111,13 @@ func sendARP(ps pcap.PCapScanner, zone *model.Zone, hostRanges []net.IP) error {
if err := ps.WritePacketData(buf.Bytes()); err != nil { if err := ps.WritePacketData(buf.Bytes()); err != nil {
return err return err
} }
select {
case <-stopChan:
return nil
default:
}
time.Sleep(time.Microsecond * 100) time.Sleep(time.Microsecond * 100)
} }
return nil return nil

View File

@ -2,6 +2,6 @@ package ipv4
import "git.loafle.net/overflow/overflow_discovery/api/module/discovery/model" import "git.loafle.net/overflow/overflow_discovery/api/module/discovery/model"
func ScanPort(host *model.Host, dp *model.DiscoveryPort, resultChan chan interface{}, errChan chan error, doneChan chan<- struct{}) { func ScanPort(host *model.Host, dp *model.DiscoveryPort, resultChan chan interface{}, errChan chan error, stopChan chan struct{}) {
} }

View File

@ -2,6 +2,6 @@ package ipv6
import "git.loafle.net/overflow/overflow_discovery/api/module/discovery/model" import "git.loafle.net/overflow/overflow_discovery/api/module/discovery/model"
func ScanHost(zone *model.Zone, dh *model.DiscoveryHost, resultChan chan interface{}, errChan chan error, doneChan chan<- struct{}) { func ScanHost(zone *model.Zone, dh *model.DiscoveryHost, resultChan chan interface{}, errChan chan error, stopChan chan struct{}) {
} }

View File

@ -2,6 +2,6 @@ package ipv6
import "git.loafle.net/overflow/overflow_discovery/api/module/discovery/model" import "git.loafle.net/overflow/overflow_discovery/api/module/discovery/model"
func ScanPort(host *model.Host, dp *model.DiscoveryPort, resultChan chan interface{}, errChan chan error, doneChan chan<- struct{}) { func ScanPort(host *model.Host, dp *model.DiscoveryPort, resultChan chan interface{}, errChan chan error, stopChan chan struct{}) {
} }

View File

@ -9,22 +9,24 @@ import (
"git.loafle.net/overflow/overflow_discovery/discovery/ipv6" "git.loafle.net/overflow/overflow_discovery/discovery/ipv6"
) )
func scanPort(host *model.Host, dp *model.DiscoveryPort, resultChan chan interface{}, errChan chan error, doneChan chan<- struct{}) { func scanPort(host *model.Host, dp *model.DiscoveryPort, resultChan chan interface{}, errChan chan error, doneChan chan<- struct{}, stopChan chan struct{}) {
defer func() {
doneChan <- struct{}{}
}()
_, ipNet, err := net.ParseCIDR(host.Zone.Network) _, ipNet, err := net.ParseCIDR(host.Zone.Network)
if nil != err { if nil != err {
errChan <- err errChan <- err
doneChan <- struct{}{}
return return
} }
switch len(ipNet.IP) { switch len(ipNet.IP) {
case net.IPv4len: case net.IPv4len:
ipv4.ScanPort(host, dp, resultChan, errChan, doneChan) ipv4.ScanPort(host, dp, resultChan, errChan, stopChan)
case net.IPv6len: case net.IPv6len:
ipv6.ScanPort(host, dp, resultChan, errChan, doneChan) ipv6.ScanPort(host, dp, resultChan, errChan, stopChan)
default: default:
errChan <- fmt.Errorf("Discovery: Not supported ip length") errChan <- fmt.Errorf("Discovery: Not supported ip length")
doneChan <- struct{}{}
return return
} }
} }

View File

@ -2,6 +2,9 @@ package discovery
import "git.loafle.net/overflow/overflow_discovery/api/module/discovery/model" import "git.loafle.net/overflow/overflow_discovery/api/module/discovery/model"
func scanService(port *model.Port, ds *model.DiscoveryService, resultChan chan interface{}, errChan chan error, doneChan chan<- struct{}) { func scanService(port *model.Port, ds *model.DiscoveryService, resultChan chan interface{}, errChan chan error, doneChan chan<- struct{}, stopChan chan struct{}) {
defer func() {
doneChan <- struct{}{} doneChan <- struct{}{}
}()
} }

View File

@ -8,7 +8,11 @@ import (
"git.loafle.net/overflow/overflow_discovery/api/module/discovery/model" "git.loafle.net/overflow/overflow_discovery/api/module/discovery/model"
) )
func scanZone(dz *model.DiscoveryZone, resultChan chan interface{}, errChan chan error, doneChan chan<- struct{}) { func scanZone(dz *model.DiscoveryZone, resultChan chan interface{}, errChan chan error, doneChan chan<- struct{}, stopChan chan struct{}) {
defer func() {
doneChan <- struct{}{}
}()
var err error var err error
var ifaces []net.Interface var ifaces []net.Interface
var addrs []net.Addr var addrs []net.Addr
@ -58,9 +62,6 @@ func scanZone(dz *model.DiscoveryZone, resultChan chan interface{}, errChan chan
resultChan <- z resultChan <- z
} }
} }
doneChan <- struct{}{}
} }
func checkExclude(ep []string, iface string) bool { func checkExclude(ep []string, iface string) bool {