fix: command handlers are now atomically locked

This commit is contained in:
evilsocket 2019-02-26 13:26:28 +01:00
commit e46ea6c9a9
No known key found for this signature in database
GPG key ID: 1564D7F30393A456
3 changed files with 21 additions and 12 deletions

View file

@ -3,14 +3,16 @@ package session
import ( import (
"github.com/bettercap/readline" "github.com/bettercap/readline"
"regexp" "regexp"
"sync"
) )
type CommandHandler struct { type CommandHandler struct {
sync.Mutex
Name string Name string
Description string Description string
Completer *readline.PrefixCompleter Completer *readline.PrefixCompleter
Parser *regexp.Regexp Parser *regexp.Regexp
Exec func(args []string, s *Session) error exec func(args []string, s *Session) error
} }
func NewCommandHandler(name string, expr string, desc string, exec func(args []string, s *Session) error) CommandHandler { func NewCommandHandler(name string, expr string, desc string, exec func(args []string, s *Session) error) CommandHandler {
@ -19,7 +21,7 @@ func NewCommandHandler(name string, expr string, desc string, exec func(args []s
Description: desc, Description: desc,
Completer: nil, Completer: nil,
Parser: regexp.MustCompile(expr), Parser: regexp.MustCompile(expr),
Exec: exec, exec: exec,
} }
} }
@ -31,3 +33,9 @@ func (h *CommandHandler) Parse(line string) (bool, []string) {
return false, nil return false, nil
} }
} }
func (h *CommandHandler) Exec(args []string, s *Session) error {
h.Lock()
defer h.Unlock()
return h.exec(args, s)
}

View file

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"regexp" "regexp"
"strconv" "strconv"
"sync"
"github.com/evilsocket/islazy/str" "github.com/evilsocket/islazy/str"
"github.com/evilsocket/islazy/tui" "github.com/evilsocket/islazy/tui"
@ -15,11 +16,13 @@ import (
const IPv4Validator = `^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$` const IPv4Validator = `^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$`
type ModuleHandler struct { type ModuleHandler struct {
sync.Mutex
Name string Name string
Description string Description string
Parser *regexp.Regexp Parser *regexp.Regexp
Exec func(args []string) error
Completer *readline.PrefixCompleter Completer *readline.PrefixCompleter
exec func(args []string) error
} }
func NewModuleHandler(name string, expr string, desc string, exec func(args []string) error) ModuleHandler { func NewModuleHandler(name string, expr string, desc string, exec func(args []string) error) ModuleHandler {
@ -27,7 +30,7 @@ func NewModuleHandler(name string, expr string, desc string, exec func(args []st
Name: name, Name: name,
Description: desc, Description: desc,
Parser: nil, Parser: nil,
Exec: exec, exec: exec,
} }
if expr != "" { if expr != "" {
@ -62,6 +65,12 @@ func (h *ModuleHandler) Parse(line string) (bool, []string) {
return false, nil return false, nil
} }
func (h *ModuleHandler) Exec(args []string) error {
h.Lock()
defer h.Unlock()
return h.exec(args)
}
type JSONModuleHandler struct { type JSONModuleHandler struct {
Name string `json:"name"` Name string `json:"name"`
Description string `json:"description"` Description string `json:"description"`

View file

@ -11,7 +11,6 @@ import (
"runtime/pprof" "runtime/pprof"
"sort" "sort"
"strings" "strings"
"sync"
"time" "time"
"github.com/bettercap/readline" "github.com/bettercap/readline"
@ -95,8 +94,6 @@ type Session struct {
Events *EventPool `json:"-"` Events *EventPool `json:"-"`
UnkCmdCallback UnknownCommandCallback `json:"-"` UnkCmdCallback UnknownCommandCallback `json:"-"`
Firewall firewall.FirewallManager `json:"-"` Firewall firewall.FirewallManager `json:"-"`
cmdLock sync.Mutex
} }
func New() (*Session, error) { func New() (*Session, error) {
@ -121,8 +118,6 @@ func New() (*Session, error) {
Modules: make([]Module, 0), Modules: make([]Module, 0),
Events: nil, Events: nil,
UnkCmdCallback: nil, UnkCmdCallback: nil,
cmdLock: sync.Mutex{},
} }
if *s.Options.CpuProfile != "" { if *s.Options.CpuProfile != "" {
@ -391,9 +386,6 @@ func parseCapletCommand(line string) (is bool, caplet *caplets.Caplet, argv []st
} }
func (s *Session) Run(line string) error { func (s *Session) Run(line string) error {
s.cmdLock.Lock()
defer s.cmdLock.Unlock()
line = str.TrimRight(line) line = str.TrimRight(line)
// remove extra spaces after the first command // remove extra spaces after the first command
// so that 'arp.spoof on' is normalized // so that 'arp.spoof on' is normalized