55 lines
879 B
Go
55 lines
879 B
Go
|
package pcap
|
||
|
|
||
|
import (
|
||
|
"sync"
|
||
|
|
||
|
"git.loafle.net/overflow/overflow_discovery/api/module/discovery/model"
|
||
|
)
|
||
|
|
||
|
var mtx sync.Mutex
|
||
|
var instances map[string]PCapScanner
|
||
|
|
||
|
func init() {
|
||
|
instances = make(map[string]PCapScanner, 0)
|
||
|
}
|
||
|
|
||
|
func RetainScanner(zone *model.Zone) (PCapScanner, error) {
|
||
|
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
|
||
|
}
|
||
|
|
||
|
func ReleaseScanner(zone *model.Zone) {
|
||
|
mtx.Lock()
|
||
|
defer mtx.Unlock()
|
||
|
|
||
|
if ps, ok := instances[zone.Network]; ok {
|
||
|
if ps.release() {
|
||
|
ps.stop()
|
||
|
delete(instances, zone.Network)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func ReleaseScannerAll() {
|
||
|
mtx.Lock()
|
||
|
defer mtx.Unlock()
|
||
|
|
||
|
for k, ps := range instances {
|
||
|
ps.stop()
|
||
|
delete(instances, k)
|
||
|
}
|
||
|
}
|