2021-08-31 13:57:26 +00:00
|
|
|
package stats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
|
|
|
|
"github.com/projectdiscovery/gologger"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Storage is a storage for storing statistics information
|
|
|
|
// about the nuclei engine displaying it at user-defined intervals.
|
|
|
|
type Storage struct {
|
|
|
|
data map[string]*storageDataItem
|
|
|
|
mutex *sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
type storageDataItem struct {
|
|
|
|
description string
|
|
|
|
value int64
|
|
|
|
}
|
|
|
|
|
|
|
|
var Default *Storage
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Default = New()
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewEntry creates a new entry in the storage object
|
|
|
|
func NewEntry(name, description string) {
|
|
|
|
Default.NewEntry(name, description)
|
|
|
|
}
|
|
|
|
|
2021-11-25 16:54:16 +00:00
|
|
|
// Increment increments the value for a name string
|
2021-08-31 13:57:26 +00:00
|
|
|
func Increment(name string) {
|
|
|
|
Default.Increment(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display displays the stats for a name
|
|
|
|
func Display(name string) {
|
|
|
|
Default.Display(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetValue returns the value for a set variable
|
|
|
|
func GetValue(name string) int64 {
|
|
|
|
return Default.GetValue(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new storage object
|
|
|
|
func New() *Storage {
|
|
|
|
return &Storage{data: make(map[string]*storageDataItem), mutex: &sync.RWMutex{}}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewEntry creates a new entry in the storage object
|
|
|
|
func (s *Storage) NewEntry(name, description string) {
|
|
|
|
s.mutex.Lock()
|
|
|
|
s.data[name] = &storageDataItem{description: description, value: 0}
|
|
|
|
s.mutex.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-11-25 16:54:16 +00:00
|
|
|
// Increment increments the value for a name string
|
2021-08-31 13:57:26 +00:00
|
|
|
func (s *Storage) Increment(name string) {
|
|
|
|
s.mutex.RLock()
|
|
|
|
data, ok := s.data[name]
|
2021-08-31 16:09:20 +00:00
|
|
|
s.mutex.RUnlock()
|
2021-08-31 13:57:26 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
atomic.AddInt64(&data.value, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display displays the stats for a name
|
|
|
|
func (s *Storage) Display(name string) {
|
|
|
|
s.mutex.RLock()
|
|
|
|
data, ok := s.data[name]
|
2021-08-31 16:09:20 +00:00
|
|
|
s.mutex.RUnlock()
|
2021-08-31 13:57:26 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dataValue := atomic.LoadInt64(&data.value)
|
|
|
|
if dataValue == 0 {
|
|
|
|
return // don't show for nil stats
|
|
|
|
}
|
2021-08-31 14:28:11 +00:00
|
|
|
gologger.Error().Label("WRN").Msgf(data.description, dataValue)
|
2021-08-31 13:57:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetValue returns the value for a set variable
|
|
|
|
func (s *Storage) GetValue(name string) int64 {
|
|
|
|
s.mutex.RLock()
|
|
|
|
data, ok := s.data[name]
|
2021-08-31 16:09:20 +00:00
|
|
|
s.mutex.RUnlock()
|
2021-08-31 13:57:26 +00:00
|
|
|
if !ok {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
dataValue := atomic.LoadInt64(&data.value)
|
|
|
|
return dataValue
|
|
|
|
}
|