mirror of
https://github.com/bettercap/bettercap
synced 2025-08-20 21:43:18 -07:00
new: implemented events.waitfor command (closes #91)
This commit is contained in:
parent
fe00cc707f
commit
0108e41e20
1 changed files with 52 additions and 0 deletions
|
@ -3,6 +3,7 @@ package modules
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/bettercap/bettercap/core"
|
"github.com/bettercap/bettercap/core"
|
||||||
"github.com/bettercap/bettercap/log"
|
"github.com/bettercap/bettercap/log"
|
||||||
|
@ -12,6 +13,8 @@ import (
|
||||||
type EventsStream struct {
|
type EventsStream struct {
|
||||||
session.SessionModule
|
session.SessionModule
|
||||||
ignoreList *IgnoreList
|
ignoreList *IgnoreList
|
||||||
|
waitFor string
|
||||||
|
waitChan chan *session.Event
|
||||||
quit chan bool
|
quit chan bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,6 +22,8 @@ func NewEventsStream(s *session.Session) *EventsStream {
|
||||||
stream := &EventsStream{
|
stream := &EventsStream{
|
||||||
SessionModule: session.NewSessionModule("events.stream", s),
|
SessionModule: session.NewSessionModule("events.stream", s),
|
||||||
quit: make(chan bool),
|
quit: make(chan bool),
|
||||||
|
waitChan: make(chan *session.Event),
|
||||||
|
waitFor: "",
|
||||||
ignoreList: NewIgnoreList(),
|
ignoreList: NewIgnoreList(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,6 +50,24 @@ func NewEventsStream(s *session.Session) *EventsStream {
|
||||||
return stream.Show(limit)
|
return stream.Show(limit)
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
stream.AddHandler(session.NewModuleHandler("events.waitfor TAG TIMEOUT?", `events.waitfor ([^\s]+)([\s\d]*)`,
|
||||||
|
"Show events stream.",
|
||||||
|
func(args []string) error {
|
||||||
|
tag := args[0]
|
||||||
|
timeout := 0
|
||||||
|
if len(args) == 2 {
|
||||||
|
t := core.Trim(args[1])
|
||||||
|
if t != "" {
|
||||||
|
n, err := strconv.Atoi(t)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
timeout = n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return stream.startWaitingFor(tag, timeout)
|
||||||
|
}))
|
||||||
|
|
||||||
stream.AddHandler(session.NewModuleHandler("events.ignore FILTER", "events.ignore ([^\\s]+)",
|
stream.AddHandler(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).",
|
"Events with an identifier matching this filter will not be shown (use multiple times to add more filters).",
|
||||||
func(args []string) error {
|
func(args []string) error {
|
||||||
|
@ -105,6 +128,11 @@ func (s *EventsStream) Start() error {
|
||||||
var e session.Event
|
var e session.Event
|
||||||
select {
|
select {
|
||||||
case e = <-s.Session.Events.NewEvents:
|
case e = <-s.Session.Events.NewEvents:
|
||||||
|
if e.Tag == s.waitFor {
|
||||||
|
s.waitFor = ""
|
||||||
|
s.waitChan <- &e
|
||||||
|
}
|
||||||
|
|
||||||
if s.ignoreList.Ignored(e) == false {
|
if s.ignoreList.Ignored(e) == false {
|
||||||
s.View(e, true)
|
s.View(e, true)
|
||||||
} else {
|
} else {
|
||||||
|
@ -137,6 +165,30 @@ func (s *EventsStream) Show(limit int) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *EventsStream) startWaitingFor(tag string, timeout int) error {
|
||||||
|
if timeout == 0 {
|
||||||
|
log.Info("Waiting for event %s ...", core.Green(tag))
|
||||||
|
} else {
|
||||||
|
log.Info("Waiting for event %s for %d seconds ...", core.Green(tag), timeout)
|
||||||
|
go func() {
|
||||||
|
time.Sleep(time.Duration(timeout) * time.Second)
|
||||||
|
s.waitFor = ""
|
||||||
|
s.waitChan <- nil
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
s.waitFor = tag
|
||||||
|
event := <-s.waitChan
|
||||||
|
|
||||||
|
if event == nil {
|
||||||
|
return fmt.Errorf("'events.waitFor %s %d' timed out.", tag, timeout)
|
||||||
|
} else {
|
||||||
|
log.Debug("Got event: %v", event)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *EventsStream) Stop() error {
|
func (s *EventsStream) Stop() error {
|
||||||
return s.SetRunning(false, func() {
|
return s.SetRunning(false, func() {
|
||||||
s.quit <- true
|
s.quit <- true
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue