| 1 | package events_streamscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/ |
| 2 | |
| 3 | import ( |
| 4 | "encoding/jsonscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/" |
| 5 | "fmtscip-go gomod github.com/golang/go/src go1.16 fmt/" |
| 6 | "regexpscip-go gomod github.com/golang/go/src go1.16 regexp/" |
| 7 | "stringsscip-go gomod github.com/golang/go/src go1.16 strings/" |
| 8 | "syncscip-go gomod github.com/golang/go/src go1.16 sync/" |
| 9 | |
| 10 | "github.com/bettercap/bettercap/sessionscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/" |
| 11 | |
| 12 | "github.com/antchfx/jsonqueryscip-go gomod github.com/antchfx/jsonquery v1.3.5 `github.com/antchfx/jsonquery`/" |
| 13 | "github.com/evilsocket/islazy/strscip-go gomod github.com/evilsocket/islazy v1.11.0 `github.com/evilsocket/islazy/str`/" |
| 14 | "github.com/evilsocket/islazy/tuiscip-go gomod github.com/evilsocket/islazy v1.11.0 `github.com/evilsocket/islazy/tui`/" |
| 15 | ) |
| 16 | |
| 17 | var reQueryCapturescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/reQueryCapture. = regexpscip-go gomod github.com/golang/go/src go1.16 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.16 regexp/MustCompile().(`{{([^}]+)}}`) |
| 18 | |
| 19 | type Triggerscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/Trigger# struct { |
| 20 | Forscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/Trigger#For. string |
| 21 | Actionscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/Trigger#Action. string |
| 22 | } |
| 23 | |
| 24 | type TriggerListscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/TriggerList# struct { |
| 25 | syncscip-go gomod github.com/golang/go/src go1.16 sync/.Mutexscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/TriggerList#Mutex. |
| 26 | triggersscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/TriggerList#triggers. map[string]Triggerscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/Trigger# |
| 27 | } |
| 28 | |
| 29 | func NewTriggerListscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/NewTriggerList().() *TriggerListscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/TriggerList# { |
| 30 | return &TriggerListscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/TriggerList#{ |
| 31 | triggersscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/TriggerList#triggers.: make(map[string]Triggerscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/Trigger#), |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | func (l *TriggerListscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/TriggerList#) Addscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/TriggerList#Add().(tag string, command string) (error, string) { |
| 36 | l.Lockscip-go gomod github.com/golang/go/src go1.16 sync/Mutex#Lock().() |
| 37 | defer l.Unlockscip-go gomod github.com/golang/go/src go1.16 sync/Mutex#Unlock().() |
| 38 | |
| 39 | idNum := 0 |
| 40 | command = strscip-go gomod github.com/evilsocket/islazy v1.11.0 `github.com/evilsocket/islazy/str`/.Trimscip-go gomod github.com/evilsocket/islazy v1.11.0 `github.com/evilsocket/islazy/str`/Trim().(command) |
| 41 | |
| 42 | for id, t := range l.triggersscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/TriggerList#triggers. { |
| 43 | if t.Forscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/Trigger#For. == tag { |
| 44 | if t.Actionscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/Trigger#Action. == command { |
| 45 | return fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("duplicate: trigger '%s' found for action '%s'", tuiscip-go gomod github.com/evilsocket/islazy v1.11.0 `github.com/evilsocket/islazy/tui`/.Boldscip-go gomod github.com/evilsocket/islazy v1.11.0 `github.com/evilsocket/islazy/tui`/Bold().(id), command), "" |
| 46 | } |
| 47 | idNum++ |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | id := fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.16 fmt/Sprintf().("%s-%d", tag, idNum) |
| 52 | l.triggersscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/TriggerList#triggers.[id] = Triggerscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/Trigger#{ |
| 53 | Forscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/Trigger#For.: tag, |
| 54 | Actionscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/Trigger#Action.: command, |
| 55 | } |
| 56 | |
| 57 | return nil, id |
| 58 | } |
| 59 | |
| 60 | func (l *TriggerListscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/TriggerList#) Delscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/TriggerList#Del().(id string) (err error) { |
| 61 | l.Lockscip-go gomod github.com/golang/go/src go1.16 sync/Mutex#Lock().() |
| 62 | defer l.Unlockscip-go gomod github.com/golang/go/src go1.16 sync/Mutex#Unlock().() |
| 63 | if _, found := l.triggersscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/TriggerList#triggers.[id]; found { |
| 64 | delete(l.triggersscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/TriggerList#triggers., id) |
| 65 | } else { |
| 66 | err = fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("trigger '%s' not found", tuiscip-go gomod github.com/evilsocket/islazy v1.11.0 `github.com/evilsocket/islazy/tui`/.Boldscip-go gomod github.com/evilsocket/islazy v1.11.0 `github.com/evilsocket/islazy/tui`/Bold().(id)) |
| 67 | } |
| 68 | return err |
| 69 | } |
| 70 | |
| 71 | func (l *TriggerListscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/TriggerList#) Eachscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/TriggerList#Each().(cb func(id string, t Triggerscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/Trigger#)) { |
| 72 | l.Lockscip-go gomod github.com/golang/go/src go1.16 sync/Mutex#Lock().() |
| 73 | defer l.Unlockscip-go gomod github.com/golang/go/src go1.16 sync/Mutex#Unlock().() |
| 74 | for id, t := range l.triggersscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/TriggerList#triggers. { |
| 75 | cb(id, t) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | func (l *TriggerListscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/TriggerList#) Completerscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/TriggerList#Completer().(prefix string) []string { |
| 80 | ids := []string{} |
| 81 | l.Eachscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/TriggerList#Each().(func(id string, t Triggerscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/Trigger#) { |
| 82 | if prefix == "" || stringsscip-go gomod github.com/golang/go/src go1.16 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.16 strings/HasPrefix().(id, prefix) { |
| 83 | ids = append(ids, id) |
| 84 | } |
| 85 | }) |
| 86 | return ids |
| 87 | } |
| 88 | |
| 89 | func (l *TriggerListscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/TriggerList#) Dispatchscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/TriggerList#Dispatch().(e sessionscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/.Eventscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/Event#) (ident string, cmd string, err error, found bool) { |
| 90 | l.Lockscip-go gomod github.com/golang/go/src go1.16 sync/Mutex#Lock().() |
| 91 | defer l.Unlockscip-go gomod github.com/golang/go/src go1.16 sync/Mutex#Unlock().() |
| 92 | |
| 93 | for id, t := range l.triggersscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/TriggerList#triggers. { |
| 94 | if e.Tagscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/Event#Tag. == t.Forscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/Trigger#For. { |
| 95 | // this is ugly but it's also the only way to allow |
| 96 | // the user to do this easily - since each event Data |
| 97 | // field is an interface and type casting is not possible |
| 98 | // via golang default text/template system, we transform |
| 99 | // the field to JSON, parse it again and then allow the |
| 100 | // user to access it in the command via JSON-Query, example: |
| 101 | // |
| 102 | // events.on wifi.client.new "wifi.deauth {{Client\mac}}" |
| 103 | cmd = t.Actionscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/Trigger#Action. |
| 104 | found = true |
| 105 | ident = id |
| 106 | buf := ([]byte)(nil) |
| 107 | doc := (*jsonqueryscip-go gomod github.com/antchfx/jsonquery v1.3.5 `github.com/antchfx/jsonquery`/.Nodescip-go gomod github.com/antchfx/jsonquery v1.3.5 `github.com/antchfx/jsonquery`/Node#)(nil) |
| 108 | // parse each {EXPR} |
| 109 | for _, m := range reQueryCapturescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/reQueryCapture..FindAllStringscip-go gomod github.com/golang/go/src go1.16 regexp/Regexp#FindAllString().(t.Actionscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/modules/events_stream`/Trigger#Action., -1) { |
| 110 | // parse the event Data field as a JSON objects once |
| 111 | if doc == nil { |
| 112 | if buf, err = jsonscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/Marshal().(e.Datascip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/session`/Event#Data.); err != nil { |
| 113 | err = fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("error while encoding event for trigger %s: %v", tuiscip-go gomod github.com/evilsocket/islazy v1.11.0 `github.com/evilsocket/islazy/tui`/.Boldscip-go gomod github.com/evilsocket/islazy v1.11.0 `github.com/evilsocket/islazy/tui`/Bold().(id), err) |
| 114 | return |
| 115 | } else if doc, err = jsonqueryscip-go gomod github.com/antchfx/jsonquery v1.3.5 `github.com/antchfx/jsonquery`/.Parsescip-go gomod github.com/antchfx/jsonquery v1.3.5 `github.com/antchfx/jsonquery`/Parse().(stringsscip-go gomod github.com/golang/go/src go1.16 strings/.NewReaderscip-go gomod github.com/golang/go/src go1.16 strings/NewReader().(string(buf))); err != nil { |
| 116 | err = fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("error while parsing event for trigger %s: %v", tuiscip-go gomod github.com/evilsocket/islazy v1.11.0 `github.com/evilsocket/islazy/tui`/.Boldscip-go gomod github.com/evilsocket/islazy v1.11.0 `github.com/evilsocket/islazy/tui`/Bold().(id), err) |
| 117 | return |
| 118 | } |
| 119 | } |
| 120 | // {EXPR} -> EXPR |
| 121 | expr := stringsscip-go gomod github.com/golang/go/src go1.16 strings/.Trimscip-go gomod github.com/golang/go/src go1.16 strings/Trim().(m, "{}") |
| 122 | // use EXPR as a JSON query |
| 123 | if node := jsonqueryscip-go gomod github.com/antchfx/jsonquery v1.3.5 `github.com/antchfx/jsonquery`/.FindOnescip-go gomod github.com/antchfx/jsonquery v1.3.5 `github.com/antchfx/jsonquery`/FindOne().(doc, expr); node != nil { |
| 124 | cmd = stringsscip-go gomod github.com/golang/go/src go1.16 strings/.Replacescip-go gomod github.com/golang/go/src go1.16 strings/Replace().(cmd, m, node.InnerTextscip-go gomod github.com/antchfx/jsonquery v1.3.5 `github.com/antchfx/jsonquery`/Node#InnerText().(), -1) |
| 125 | } else { |
| 126 | err = fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().( |
| 127 | "error while parsing expressionfor trigger %s: '%s' doesn't resolve any object: %v", |
| 128 | tuiscip-go gomod github.com/evilsocket/islazy v1.11.0 `github.com/evilsocket/islazy/tui`/.Boldscip-go gomod github.com/evilsocket/islazy v1.11.0 `github.com/evilsocket/islazy/tui`/Bold().(id), |
| 129 | expr, |
| 130 | err, |
| 131 | ) |
| 132 | return |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | return |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | return |
| 141 | } |