mirror of
https://github.com/bettercap/bettercap
synced 2025-08-21 05:53:20 -07:00
fix: events.include and events.ignore now filter for both events.stream and api.rest
This commit is contained in:
parent
f23c780eee
commit
2e3e4f453b
5 changed files with 54 additions and 42 deletions
|
@ -29,7 +29,6 @@ type EventsStream struct {
|
|||
outputName string
|
||||
output *os.File
|
||||
rotation rotation
|
||||
ignoreList *IgnoreList
|
||||
triggerList *TriggerList
|
||||
waitFor string
|
||||
waitChan chan *session.Event
|
||||
|
@ -47,7 +46,6 @@ func NewEventsStream(s *session.Session) *EventsStream {
|
|||
quit: make(chan bool),
|
||||
waitChan: make(chan *session.Event),
|
||||
waitFor: "",
|
||||
ignoreList: NewIgnoreList(),
|
||||
triggerList: NewTriggerList(),
|
||||
}
|
||||
|
||||
|
@ -127,7 +125,7 @@ func NewEventsStream(s *session.Session) *EventsStream {
|
|||
ignore := session.NewModuleHandler("events.ignore FILTER", "events.ignore ([^\\s]+)",
|
||||
"Events with an identifier matching this filter will not be shown (use multiple times to add more filters).",
|
||||
func(args []string) error {
|
||||
return mod.ignoreList.Add(args[0])
|
||||
return mod.Session.EventsIgnoreList.Add(args[0])
|
||||
})
|
||||
|
||||
ignore.Complete("events.ignore", s.EventsCompleter)
|
||||
|
@ -137,7 +135,7 @@ func NewEventsStream(s *session.Session) *EventsStream {
|
|||
include := session.NewModuleHandler("events.include FILTER", "events.include ([^\\s]+)",
|
||||
"Used to remove filters passed with the events.ignore command.",
|
||||
func(args []string) error {
|
||||
return mod.ignoreList.Remove(args[0])
|
||||
return mod.Session.EventsIgnoreList.Remove(args[0])
|
||||
})
|
||||
|
||||
include.Complete("events.include", s.EventsCompleter)
|
||||
|
@ -147,13 +145,13 @@ func NewEventsStream(s *session.Session) *EventsStream {
|
|||
mod.AddHandler(session.NewModuleHandler("events.filters", "",
|
||||
"Print the list of filters used to ignore events.",
|
||||
func(args []string) error {
|
||||
if mod.ignoreList.Empty() {
|
||||
if mod.Session.EventsIgnoreList.Empty() {
|
||||
fmt.Printf("Ignore filters list is empty.\n")
|
||||
} else {
|
||||
mod.ignoreList.RLock()
|
||||
defer mod.ignoreList.RUnlock()
|
||||
mod.Session.EventsIgnoreList.RLock()
|
||||
defer mod.Session.EventsIgnoreList.RUnlock()
|
||||
|
||||
for _, filter := range mod.ignoreList.Filters() {
|
||||
for _, filter := range mod.Session.EventsIgnoreList.Filters() {
|
||||
fmt.Printf(" '%s'\n", string(filter))
|
||||
}
|
||||
}
|
||||
|
@ -163,7 +161,7 @@ func NewEventsStream(s *session.Session) *EventsStream {
|
|||
mod.AddHandler(session.NewModuleHandler("events.filters.clear", "",
|
||||
"Clear the list of filters passed with the events.ignore command.",
|
||||
func(args []string) error {
|
||||
mod.ignoreList = NewIgnoreList()
|
||||
mod.Session.EventsIgnoreList.Clear()
|
||||
return nil
|
||||
}))
|
||||
|
||||
|
@ -281,7 +279,7 @@ func (mod *EventsStream) Start() error {
|
|||
mod.waitChan <- &e
|
||||
}
|
||||
|
||||
if !mod.ignoreList.Ignored(e) {
|
||||
if !mod.Session.EventsIgnoreList.Ignored(e) {
|
||||
mod.View(e, true)
|
||||
}
|
||||
|
||||
|
@ -303,7 +301,7 @@ func (mod *EventsStream) Show(limit int) error {
|
|||
selected := []session.Event{}
|
||||
for i := range events {
|
||||
e := events[num-1-i]
|
||||
if !mod.ignoreList.Ignored(e) {
|
||||
if !mod.Session.EventsIgnoreList.Ignored(e) {
|
||||
selected = append(selected, e)
|
||||
if len(selected) == limit {
|
||||
break
|
||||
|
|
|
@ -1,113 +0,0 @@
|
|||
package events_stream
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/bettercap/bettercap/session"
|
||||
|
||||
"github.com/evilsocket/islazy/str"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrEmptyExpression = errors.New("expression can not be empty")
|
||||
)
|
||||
|
||||
type IgnoreFilter string
|
||||
|
||||
func (f IgnoreFilter) Matches(s string) bool {
|
||||
return string(f) == s || strings.HasPrefix(s, string(f))
|
||||
}
|
||||
|
||||
type IgnoreList struct {
|
||||
sync.RWMutex
|
||||
filters []IgnoreFilter
|
||||
}
|
||||
|
||||
func NewIgnoreList() *IgnoreList {
|
||||
return &IgnoreList{
|
||||
filters: make([]IgnoreFilter, 0),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *IgnoreList) checkExpression(expr string) (string, error) {
|
||||
expr = str.Trim(expr)
|
||||
if expr == "" {
|
||||
return "", ErrEmptyExpression
|
||||
}
|
||||
|
||||
return expr, nil
|
||||
}
|
||||
|
||||
func (l *IgnoreList) Add(expr string) (err error) {
|
||||
if expr, err = l.checkExpression(expr); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
l.Lock()
|
||||
defer l.Unlock()
|
||||
|
||||
// first check for duplicates
|
||||
for _, filter := range l.filters {
|
||||
if filter.Matches(expr) {
|
||||
return fmt.Errorf("filter '%s' already matches the expression '%s'", filter, expr)
|
||||
}
|
||||
}
|
||||
|
||||
// all good
|
||||
l.filters = append(l.filters, IgnoreFilter(expr))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *IgnoreList) Remove(expr string) (err error) {
|
||||
if expr, err = l.checkExpression(expr); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
l.Lock()
|
||||
defer l.Unlock()
|
||||
|
||||
// build a new list with everything that does not match
|
||||
toRemove := IgnoreFilter(expr)
|
||||
newList := make([]IgnoreFilter, 0)
|
||||
for _, filter := range l.filters {
|
||||
if !toRemove.Matches(string(filter)) {
|
||||
newList = append(newList, filter)
|
||||
}
|
||||
}
|
||||
|
||||
if len(newList) == len(l.filters) {
|
||||
return fmt.Errorf("expression '%s' did not match any filter", expr)
|
||||
}
|
||||
|
||||
// swap
|
||||
l.filters = newList
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *IgnoreList) Ignored(e session.Event) bool {
|
||||
l.RLock()
|
||||
defer l.RUnlock()
|
||||
|
||||
for _, filter := range l.filters {
|
||||
if filter.Matches(e.Tag) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (l *IgnoreList) Empty() bool {
|
||||
l.RLock()
|
||||
defer l.RUnlock()
|
||||
return len(l.filters) == 0
|
||||
}
|
||||
|
||||
func (l *IgnoreList) Filters() []IgnoreFilter {
|
||||
return l.filters
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue