2017-08-16 16:09:20 +00:00
|
|
|
package zgrab2
|
|
|
|
|
2017-10-04 03:57:56 +00:00
|
|
|
// Monitor is a collection of states per scans and a channel to communicate
|
|
|
|
// those scans to the monitor
|
2017-08-16 16:09:20 +00:00
|
|
|
type Monitor struct {
|
2017-08-25 16:04:16 +00:00
|
|
|
states map[string]*State
|
2017-08-16 16:09:20 +00:00
|
|
|
statusesChan chan moduleStatus
|
2018-05-14 15:24:25 +00:00
|
|
|
// Callback is invoked after each scan.
|
|
|
|
Callback func(string)
|
2017-08-16 16:09:20 +00:00
|
|
|
}
|
|
|
|
|
2017-10-04 03:57:56 +00:00
|
|
|
// State contains the respective number of successes and failures
|
|
|
|
// for a given scan
|
2017-08-25 16:04:16 +00:00
|
|
|
type State struct {
|
|
|
|
Successes uint `json:"successes"`
|
|
|
|
Failures uint `json:"failures"`
|
2017-08-16 16:09:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type moduleStatus struct {
|
|
|
|
name string
|
|
|
|
st status
|
|
|
|
}
|
|
|
|
|
|
|
|
type status uint
|
|
|
|
|
|
|
|
const (
|
2017-10-04 03:57:56 +00:00
|
|
|
statusSuccess status = iota
|
|
|
|
statusFailure status = iota
|
2017-08-16 16:09:20 +00:00
|
|
|
)
|
|
|
|
|
2017-10-04 03:57:56 +00:00
|
|
|
// GetStatuses returns a mapping from scanner names to the current number
|
|
|
|
// of successes and failures for that scanner
|
2017-08-25 16:04:16 +00:00
|
|
|
func (m *Monitor) GetStatuses() map[string]*State {
|
2017-08-16 16:09:20 +00:00
|
|
|
return m.states
|
|
|
|
}
|
|
|
|
|
2017-10-04 03:57:56 +00:00
|
|
|
// MakeMonitor returns a Monitor object that can be used to collect and send
|
|
|
|
// the status of a running scan
|
2017-08-16 16:09:20 +00:00
|
|
|
func MakeMonitor() *Monitor {
|
|
|
|
m := new(Monitor)
|
|
|
|
m.statusesChan = make(chan moduleStatus, config.Senders*4)
|
2017-08-25 16:04:16 +00:00
|
|
|
m.states = make(map[string]*State, 10)
|
2017-08-16 16:09:20 +00:00
|
|
|
go func() {
|
|
|
|
for s := range m.statusesChan {
|
|
|
|
if m.states[s.name] == nil {
|
2017-08-25 16:04:16 +00:00
|
|
|
m.states[s.name] = new(State)
|
2017-08-16 16:09:20 +00:00
|
|
|
}
|
2018-05-14 15:24:25 +00:00
|
|
|
if m.Callback != nil {
|
|
|
|
m.Callback(s.name)
|
|
|
|
}
|
2017-08-16 16:09:20 +00:00
|
|
|
switch s.st {
|
2017-10-04 03:57:56 +00:00
|
|
|
case statusSuccess:
|
2017-08-25 16:04:16 +00:00
|
|
|
m.states[s.name].Successes++
|
2017-10-04 03:57:56 +00:00
|
|
|
case statusFailure:
|
2017-08-25 16:04:16 +00:00
|
|
|
m.states[s.name].Failures++
|
2017-08-16 16:09:20 +00:00
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return m
|
|
|
|
}
|