2017-11-16 11:01:42 +00:00
|
|
|
package pcap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2017-11-18 10:49:58 +00:00
|
|
|
"time"
|
2017-11-16 11:01:42 +00:00
|
|
|
|
2017-12-04 09:37:39 +00:00
|
|
|
discoveryM "git.loafle.net/overflow/overflow_commons_go/modules/discovery/model"
|
2017-11-16 11:01:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var mtx sync.Mutex
|
|
|
|
var instances map[string]PCapScanner
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
instances = make(map[string]PCapScanner, 0)
|
|
|
|
}
|
|
|
|
|
2017-12-04 09:37:39 +00:00
|
|
|
func RetainScanner(zone *discoveryM.Zone) (PCapScanner, error) {
|
2017-11-16 11:01:42 +00:00
|
|
|
mtx.Lock()
|
|
|
|
defer mtx.Unlock()
|
|
|
|
|
|
|
|
var ps PCapScanner
|
|
|
|
var ok bool
|
|
|
|
if ps, ok = instances[zone.Network]; !ok {
|
|
|
|
ps = newPCapScanner(zone)
|
|
|
|
if err := ps.start(); nil != err {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
instances[zone.Network] = ps
|
|
|
|
}
|
|
|
|
ps.retain()
|
|
|
|
|
|
|
|
return ps, nil
|
|
|
|
}
|
|
|
|
|
2017-12-04 09:37:39 +00:00
|
|
|
func ReleaseScanner(zone *discoveryM.Zone) {
|
2017-11-16 11:01:42 +00:00
|
|
|
|
2017-11-18 12:08:08 +00:00
|
|
|
go func() {
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
|
|
|
mtx.Lock()
|
|
|
|
defer mtx.Unlock()
|
|
|
|
|
|
|
|
if ps, ok := instances[zone.Network]; ok {
|
2017-11-18 10:49:58 +00:00
|
|
|
if ps.release() {
|
|
|
|
ps.stop()
|
|
|
|
delete(instances, zone.Network)
|
|
|
|
}
|
2017-11-18 12:08:08 +00:00
|
|
|
}
|
|
|
|
}()
|
2017-11-16 11:01:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ReleaseScannerAll() {
|
|
|
|
mtx.Lock()
|
|
|
|
defer mtx.Unlock()
|
|
|
|
|
|
|
|
for k, ps := range instances {
|
|
|
|
ps.stop()
|
|
|
|
delete(instances, k)
|
|
|
|
}
|
|
|
|
}
|