2021-08-31 19:27:26 +05:30
|
|
|
package stats
|
|
|
|
|
|
|
|
|
|
import (
|
2024-03-10 23:31:17 +03:00
|
|
|
"fmt"
|
2021-08-31 19:27:26 +05:30
|
|
|
"sync/atomic"
|
|
|
|
|
|
2024-03-10 23:31:17 +03:00
|
|
|
"github.com/logrusorgru/aurora"
|
2021-08-31 19:27:26 +05:30
|
|
|
"github.com/projectdiscovery/gologger"
|
2024-05-16 13:42:40 +02:00
|
|
|
mapsutil "github.com/projectdiscovery/utils/maps"
|
2021-08-31 19:27:26 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Storage is a storage for storing statistics information
|
|
|
|
|
// about the nuclei engine displaying it at user-defined intervals.
|
|
|
|
|
type Storage struct {
|
2024-05-16 13:42:40 +02:00
|
|
|
data *mapsutil.SyncLockMap[string, *storageDataItem]
|
2021-08-31 19:27:26 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type storageDataItem struct {
|
|
|
|
|
description string
|
2024-05-16 13:42:40 +02:00
|
|
|
value atomic.Int64
|
2021-08-31 19:27:26 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 18:54:16 +02:00
|
|
|
// Increment increments the value for a name string
|
2021-08-31 19:27:26 +05:30
|
|
|
func Increment(name string) {
|
|
|
|
|
Default.Increment(name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Display displays the stats for a name
|
|
|
|
|
func Display(name string) {
|
|
|
|
|
Default.Display(name)
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-16 14:34:52 +05:30
|
|
|
func DisplayAsWarning(name string) {
|
|
|
|
|
Default.DisplayAsWarning(name)
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-10 23:31:17 +03:00
|
|
|
// ForceDisplayWarning forces the display of a warning
|
|
|
|
|
// regardless of current verbosity level
|
|
|
|
|
func ForceDisplayWarning(name string) {
|
|
|
|
|
Default.ForceDisplayWarning(name)
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-31 19:27:26 +05:30
|
|
|
// 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 {
|
2024-05-16 13:42:40 +02:00
|
|
|
data := mapsutil.NewSyncLockMap[string, *storageDataItem]()
|
|
|
|
|
return &Storage{data: data}
|
2021-08-31 19:27:26 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewEntry creates a new entry in the storage object
|
|
|
|
|
func (s *Storage) NewEntry(name, description string) {
|
2024-05-16 13:48:28 +02:00
|
|
|
_ = s.data.Set(name, &storageDataItem{description: description, value: atomic.Int64{}})
|
2021-08-31 19:27:26 +05:30
|
|
|
}
|
|
|
|
|
|
2021-11-25 18:54:16 +02:00
|
|
|
// Increment increments the value for a name string
|
2021-08-31 19:27:26 +05:30
|
|
|
func (s *Storage) Increment(name string) {
|
2024-05-16 13:42:40 +02:00
|
|
|
data, ok := s.data.Get(name)
|
2021-08-31 19:27:26 +05:30
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-05-16 13:42:40 +02:00
|
|
|
data.value.Add(1)
|
2021-08-31 19:27:26 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Display displays the stats for a name
|
|
|
|
|
func (s *Storage) Display(name string) {
|
2024-05-16 13:42:40 +02:00
|
|
|
data, ok := s.data.Get(name)
|
2021-08-31 19:27:26 +05:30
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-16 13:42:40 +02:00
|
|
|
dataValue := data.value.Load()
|
2021-08-31 19:27:26 +05:30
|
|
|
if dataValue == 0 {
|
|
|
|
|
return // don't show for nil stats
|
|
|
|
|
}
|
2021-08-31 19:58:11 +05:30
|
|
|
gologger.Error().Label("WRN").Msgf(data.description, dataValue)
|
2021-08-31 19:27:26 +05:30
|
|
|
}
|
|
|
|
|
|
2023-10-16 14:34:52 +05:30
|
|
|
func (s *Storage) DisplayAsWarning(name string) {
|
2024-05-16 13:42:40 +02:00
|
|
|
data, ok := s.data.Get(name)
|
2023-10-16 14:34:52 +05:30
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-16 13:42:40 +02:00
|
|
|
dataValue := data.value.Load()
|
2023-10-16 14:34:52 +05:30
|
|
|
if dataValue == 0 {
|
|
|
|
|
return // don't show for nil stats
|
|
|
|
|
}
|
|
|
|
|
gologger.Warning().Label("WRN").Msgf(data.description, dataValue)
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-10 23:31:17 +03:00
|
|
|
// ForceDisplayWarning forces the display of a warning
|
|
|
|
|
// regardless of current verbosity level
|
|
|
|
|
func (s *Storage) ForceDisplayWarning(name string) {
|
2024-05-16 13:42:40 +02:00
|
|
|
data, ok := s.data.Get(name)
|
2024-03-10 23:31:17 +03:00
|
|
|
if !ok {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-16 13:42:40 +02:00
|
|
|
dataValue := data.value.Load()
|
2024-03-10 23:31:17 +03:00
|
|
|
if dataValue == 0 {
|
|
|
|
|
return // don't show for nil stats
|
|
|
|
|
}
|
|
|
|
|
gologger.Print().Msgf("[%v] %v", aurora.BrightYellow("WRN"), fmt.Sprintf(data.description, dataValue))
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-31 19:27:26 +05:30
|
|
|
// GetValue returns the value for a set variable
|
|
|
|
|
func (s *Storage) GetValue(name string) int64 {
|
2024-05-16 13:42:40 +02:00
|
|
|
data, ok := s.data.Get(name)
|
2021-08-31 19:27:26 +05:30
|
|
|
if !ok {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-16 13:42:40 +02:00
|
|
|
return data.value.Load()
|
2021-08-31 19:27:26 +05:30
|
|
|
}
|