mirror of
https://github.com/bettercap/bettercap
synced 2025-08-14 02:36:57 -07:00
refact: refactored session core handlers
This commit is contained in:
parent
77667bbef0
commit
f1f9ca4ef0
2 changed files with 159 additions and 133 deletions
|
@ -6,11 +6,8 @@ import (
|
|||
"os"
|
||||
"os/signal"
|
||||
"os/user"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/chzyer/readline"
|
||||
|
||||
|
@ -71,136 +68,6 @@ func New() (*Session, error) {
|
|||
return s, nil
|
||||
}
|
||||
|
||||
func (s *Session) registerCoreHandlers() {
|
||||
s.CoreHandlers = append(s.CoreHandlers, NewCommandHandler("help", "^(help|\\?)$",
|
||||
"Display list of available commands.",
|
||||
func(args []string, s *Session) error {
|
||||
fmt.Println()
|
||||
fmt.Printf("Basic commands:\n\n")
|
||||
for _, h := range s.CoreHandlers {
|
||||
fmt.Printf(" "+core.Bold("%"+strconv.Itoa(s.HelpPadding)+"s")+" : %s\n", h.Name, h.Description)
|
||||
}
|
||||
|
||||
sort.Slice(s.Modules, func(i, j int) bool {
|
||||
return s.Modules[i].Name() < s.Modules[j].Name()
|
||||
})
|
||||
|
||||
for _, m := range s.Modules {
|
||||
fmt.Println()
|
||||
status := ""
|
||||
if m.Running() {
|
||||
status = core.Green("active")
|
||||
} else {
|
||||
status = core.Red("not active")
|
||||
}
|
||||
fmt.Printf("%s [%s]\n", m.Name(), status)
|
||||
fmt.Println(core.Dim(m.Description()) + "\n")
|
||||
for _, h := range m.Handlers() {
|
||||
fmt.Printf(h.Help(s.HelpPadding))
|
||||
}
|
||||
|
||||
params := m.Parameters()
|
||||
if len(params) > 0 {
|
||||
fmt.Printf("\n Parameters\n\n")
|
||||
for _, p := range params {
|
||||
fmt.Printf(p.Help(s.HelpPadding))
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}))
|
||||
|
||||
s.CoreHandlers = append(s.CoreHandlers, NewCommandHandler("active", "^active$",
|
||||
"Show information about active modules.",
|
||||
func(args []string, s *Session) error {
|
||||
for _, m := range s.Modules {
|
||||
if m.Running() == false {
|
||||
continue
|
||||
}
|
||||
fmt.Printf("[%s] %s (%s)\n", core.Green("active"), m.Name(), core.Dim(m.Description()))
|
||||
params := m.Parameters()
|
||||
if len(params) > 0 {
|
||||
for _, p := range params {
|
||||
_, p.Value = s.Env.Get(p.Name)
|
||||
fmt.Printf(" %s: '%s'\n", p.Name, core.Yellow(p.Value))
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}))
|
||||
|
||||
s.CoreHandlers = append(s.CoreHandlers, NewCommandHandler("exit", "^(q|quit|e|exit)$",
|
||||
"Close the session and exit.",
|
||||
func(args []string, s *Session) error {
|
||||
s.Active = false
|
||||
s.Input.Close()
|
||||
return nil
|
||||
}))
|
||||
|
||||
s.CoreHandlers = append(s.CoreHandlers, NewCommandHandler("sleep SECONDS", "^sleep\\s+(\\d+)$",
|
||||
"Sleep for the given amount of seconds.",
|
||||
func(args []string, s *Session) error {
|
||||
if secs, err := strconv.Atoi(args[0]); err == nil {
|
||||
time.Sleep(time.Duration(secs) * time.Second)
|
||||
return nil
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}))
|
||||
|
||||
s.CoreHandlers = append(s.CoreHandlers, NewCommandHandler("get NAME", "^get\\s+(.+)",
|
||||
"Get the value of variable NAME, use * for all.",
|
||||
func(args []string, s *Session) error {
|
||||
key := args[0]
|
||||
if key == "*" {
|
||||
prev_ns := ""
|
||||
|
||||
fmt.Println()
|
||||
for _, k := range s.Env.Sorted() {
|
||||
ns := ""
|
||||
toks := strings.Split(k, ".")
|
||||
if len(toks) > 0 {
|
||||
ns = toks[0]
|
||||
}
|
||||
|
||||
if ns != prev_ns {
|
||||
fmt.Println()
|
||||
prev_ns = ns
|
||||
}
|
||||
|
||||
fmt.Printf(" %"+strconv.Itoa(s.Env.Padding)+"s: '%s'\n", k, s.Env.Storage[k])
|
||||
}
|
||||
fmt.Println()
|
||||
} else if found, value := s.Env.Get(key); found == true {
|
||||
fmt.Println()
|
||||
fmt.Printf(" %s: '%s'\n", key, value)
|
||||
fmt.Println()
|
||||
} else {
|
||||
return fmt.Errorf("%s not found", key)
|
||||
}
|
||||
|
||||
return nil
|
||||
}))
|
||||
|
||||
s.CoreHandlers = append(s.CoreHandlers, NewCommandHandler("set NAME VALUE", "^set\\s+([^\\s]+)\\s+(.+)",
|
||||
"Set the VALUE of variable NAME.",
|
||||
func(args []string, s *Session) error {
|
||||
key := args[0]
|
||||
value := args[1]
|
||||
|
||||
if value == "\"\"" {
|
||||
value = ""
|
||||
}
|
||||
|
||||
s.Env.Set(key, value)
|
||||
return nil
|
||||
}))
|
||||
}
|
||||
|
||||
func (s *Session) setupInput() error {
|
||||
var err error
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue