Skip to content

Commit

Permalink
fix: command handlers are now atomically locked
Browse files Browse the repository at this point in the history
  • Loading branch information
evilsocket committed Feb 26, 2019
1 parent 3dbc904 commit e46ea6c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
12 changes: 10 additions & 2 deletions session/command_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ package session
import (
"github.com/bettercap/readline"
"regexp"
"sync"
)

type CommandHandler struct {
sync.Mutex
Name string
Description string
Completer *readline.PrefixCompleter
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 {
Expand All @@ -19,7 +21,7 @@ func NewCommandHandler(name string, expr string, desc string, exec func(args []s
Description: desc,
Completer: nil,
Parser: regexp.MustCompile(expr),
Exec: exec,
exec: exec,
}
}

Expand All @@ -31,3 +33,9 @@ func (h *CommandHandler) Parse(line string) (bool, []string) {
return false, nil
}
}

func (h *CommandHandler) Exec(args []string, s *Session) error {
h.Lock()
defer h.Unlock()
return h.exec(args, s)
}
13 changes: 11 additions & 2 deletions session/module_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"regexp"
"strconv"
"sync"

"github.com/evilsocket/islazy/str"
"github.com/evilsocket/islazy/tui"
Expand All @@ -15,19 +16,21 @@ import (
const IPv4Validator = `^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$`

type ModuleHandler struct {
sync.Mutex

Name string
Description string
Parser *regexp.Regexp
Exec func(args []string) error
Completer *readline.PrefixCompleter
exec func(args []string) error
}

func NewModuleHandler(name string, expr string, desc string, exec func(args []string) error) ModuleHandler {
h := ModuleHandler{
Name: name,
Description: desc,
Parser: nil,
Exec: exec,
exec: exec,
}

if expr != "" {
Expand Down Expand Up @@ -62,6 +65,12 @@ func (h *ModuleHandler) Parse(line string) (bool, []string) {
return false, nil
}

func (h *ModuleHandler) Exec(args []string) error {
h.Lock()
defer h.Unlock()
return h.exec(args)
}

type JSONModuleHandler struct {
Name string `json:"name"`
Description string `json:"description"`
Expand Down
8 changes: 0 additions & 8 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"runtime/pprof"
"sort"
"strings"
"sync"
"time"

"github.com/bettercap/readline"
Expand Down Expand Up @@ -95,8 +94,6 @@ type Session struct {
Events *EventPool `json:"-"`
UnkCmdCallback UnknownCommandCallback `json:"-"`
Firewall firewall.FirewallManager `json:"-"`

cmdLock sync.Mutex
}

func New() (*Session, error) {
Expand All @@ -121,8 +118,6 @@ func New() (*Session, error) {
Modules: make([]Module, 0),
Events: nil,
UnkCmdCallback: nil,

cmdLock: sync.Mutex{},
}

if *s.Options.CpuProfile != "" {
Expand Down Expand Up @@ -391,9 +386,6 @@ func parseCapletCommand(line string) (is bool, caplet *caplets.Caplet, argv []st
}

func (s *Session) Run(line string) error {
s.cmdLock.Lock()
defer s.cmdLock.Unlock()

line = str.TrimRight(line)
// remove extra spaces after the first command
// so that 'arp.spoof on' is normalized
Expand Down

0 comments on commit e46ea6c

Please sign in to comment.