2018-08-12 10:24:23 +00:00
|
|
|
package pcap
|
|
|
|
|
|
|
|
import (
|
2018-09-04 18:58:33 +00:00
|
|
|
"log"
|
2018-08-12 10:24:23 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2018-08-13 07:19:59 +00:00
|
|
|
omd "git.loafle.net/overflow/model/discovery"
|
2018-08-12 10:24:23 +00:00
|
|
|
)
|
|
|
|
|
2018-09-04 18:58:33 +00:00
|
|
|
var instances sync.Map
|
|
|
|
|
|
|
|
// var instances map[string]PCapScanner
|
2018-08-12 10:24:23 +00:00
|
|
|
|
|
|
|
func init() {
|
2018-09-04 18:58:33 +00:00
|
|
|
// instances = make(map[string]PCapScanner, 0)
|
2018-08-12 10:24:23 +00:00
|
|
|
}
|
|
|
|
|
2018-08-13 07:19:59 +00:00
|
|
|
func RetainScanner(zone *omd.Zone) (PCapScanner, error) {
|
2018-08-12 10:24:23 +00:00
|
|
|
var ps PCapScanner
|
2018-09-04 18:58:33 +00:00
|
|
|
// if ps, ok = instances[zone.Network]; !ok {
|
|
|
|
|
|
|
|
_ps, ok := instances.Load(zone.Network)
|
|
|
|
if !ok {
|
|
|
|
ps = NewPCapScanner(zone)
|
|
|
|
if err := ps.Start(); nil != err {
|
2018-08-12 10:24:23 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-04 18:58:33 +00:00
|
|
|
instances.Store(zone.Network, ps)
|
|
|
|
} else {
|
|
|
|
ps = _ps.(PCapScanner)
|
2018-08-12 10:24:23 +00:00
|
|
|
}
|
|
|
|
ps.retain()
|
2018-09-04 18:58:33 +00:00
|
|
|
log.Print("Pcap retain")
|
2018-08-12 10:24:23 +00:00
|
|
|
|
|
|
|
return ps, nil
|
|
|
|
}
|
|
|
|
|
2018-08-13 07:19:59 +00:00
|
|
|
func ReleaseScanner(zone *omd.Zone) {
|
2018-08-12 10:24:23 +00:00
|
|
|
|
|
|
|
go func() {
|
2018-09-04 18:58:33 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
2018-08-12 10:24:23 +00:00
|
|
|
|
2018-09-04 18:58:33 +00:00
|
|
|
if _ps, ok := instances.Load(zone.Network); ok {
|
|
|
|
ps := _ps.(PCapScanner)
|
|
|
|
log.Print("Pcap release")
|
2018-08-12 10:24:23 +00:00
|
|
|
if ps.release() {
|
2018-09-04 18:58:33 +00:00
|
|
|
ps.Stop()
|
|
|
|
instances.Delete(zone.Network)
|
2018-08-12 10:24:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReleaseScannerAll() {
|
2018-09-04 18:58:33 +00:00
|
|
|
instances.Range(func(k, v interface{}) bool {
|
|
|
|
ps := v.(PCapScanner)
|
|
|
|
if ps.release() {
|
|
|
|
ps.Stop()
|
|
|
|
instances.Delete(k)
|
|
|
|
return false
|
|
|
|
}
|
2018-08-12 10:24:23 +00:00
|
|
|
|
2018-09-04 18:58:33 +00:00
|
|
|
return true
|
|
|
|
})
|
2018-08-12 10:24:23 +00:00
|
|
|
}
|