mirror of
https://github.com/bettercap/bettercap
synced 2025-07-06 21:12:05 -07:00
Cleaner handling of modules start/stop
This commit is contained in:
parent
1ee605d01d
commit
b1678b5902
6 changed files with 66 additions and 42 deletions
|
@ -15,12 +15,18 @@ type ModuleHandler struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
return ModuleHandler{
|
h := ModuleHandler{
|
||||||
Name: name,
|
Name: name,
|
||||||
Description: desc,
|
Description: desc,
|
||||||
Parser: regexp.MustCompile(expr),
|
Parser: nil,
|
||||||
Exec: exec,
|
Exec: exec,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if expr != "" {
|
||||||
|
h.Parser = regexp.MustCompile(expr)
|
||||||
|
}
|
||||||
|
|
||||||
|
return h
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *ModuleHandler) Help(padding int) string {
|
func (h *ModuleHandler) Help(padding int) string {
|
||||||
|
@ -28,10 +34,18 @@ func (h *ModuleHandler) Help(padding int) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *ModuleHandler) Parse(line string) (bool, []string) {
|
func (h *ModuleHandler) Parse(line string) (bool, []string) {
|
||||||
result := h.Parser.FindStringSubmatch(line)
|
if h.Parser == nil {
|
||||||
if len(result) == h.Parser.NumSubexp()+1 {
|
if line == h.Name {
|
||||||
return true, result[1:len(result)]
|
return true, nil
|
||||||
|
} else {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return false, nil
|
result := h.Parser.FindStringSubmatch(line)
|
||||||
|
if len(result) == h.Parser.NumSubexp()+1 {
|
||||||
|
return true, result[1:len(result)]
|
||||||
|
} else {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,14 +23,16 @@ func NewArpSpoofer(s *session.Session) *ArpSpoofer {
|
||||||
|
|
||||||
p.AddParam(session.NewStringParameter("arp.spoof.targets", "<entire subnet>", "", "IP addresses to spoof."))
|
p.AddParam(session.NewStringParameter("arp.spoof.targets", "<entire subnet>", "", "IP addresses to spoof."))
|
||||||
|
|
||||||
p.AddHandler(session.NewModuleHandler("arp.spoof (on|off)", "^arp\\.spoof\\s+(on|off)$",
|
p.AddHandler(session.NewModuleHandler("arp.spoof on", "",
|
||||||
"Start/stop ARP spoofer.",
|
"Start ARP spoofer.",
|
||||||
func(args []string) error {
|
func(args []string) error {
|
||||||
if args[0] == "on" {
|
return p.Start()
|
||||||
return p.Start()
|
}))
|
||||||
} else {
|
|
||||||
return p.Stop()
|
p.AddHandler(session.NewModuleHandler("arp.spoof off", "",
|
||||||
}
|
"Stop ARP spoofer.",
|
||||||
|
func(args []string) error {
|
||||||
|
return p.Stop()
|
||||||
}))
|
}))
|
||||||
|
|
||||||
return p
|
return p
|
||||||
|
|
|
@ -99,14 +99,16 @@ func NewHttpProxy(s *session.Session) *HttpProxy {
|
||||||
p.AddParam(session.NewStringParameter("http.proxy.address", "<interface address>", `^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$`, "Address to bind the HTTP proxy to."))
|
p.AddParam(session.NewStringParameter("http.proxy.address", "<interface address>", `^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$`, "Address to bind the HTTP proxy to."))
|
||||||
p.AddParam(session.NewStringParameter("http.proxy.post.filter", "", "", "SED like syntax to replace things in the response ( example |</head>|<script src='...'></script></head>| )."))
|
p.AddParam(session.NewStringParameter("http.proxy.post.filter", "", "", "SED like syntax to replace things in the response ( example |</head>|<script src='...'></script></head>| )."))
|
||||||
|
|
||||||
p.AddHandler(session.NewModuleHandler("http.proxy (on|off)", "^http\\.proxy (on|off)$",
|
p.AddHandler(session.NewModuleHandler("http.proxy on", "",
|
||||||
"Start/stop HTTP proxy.",
|
"Start HTTP proxy.",
|
||||||
func(args []string) error {
|
func(args []string) error {
|
||||||
if args[0] == "on" {
|
return p.Start()
|
||||||
return p.Start()
|
}))
|
||||||
} else {
|
|
||||||
return p.Stop()
|
p.AddHandler(session.NewModuleHandler("http.proxy off", "",
|
||||||
}
|
"Stop HTTP proxy.",
|
||||||
|
func(args []string) error {
|
||||||
|
return p.Stop()
|
||||||
}))
|
}))
|
||||||
|
|
||||||
p.proxy.NonproxyHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
p.proxy.NonproxyHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||||
|
|
|
@ -20,14 +20,16 @@ func NewProber(s *session.Session) *Prober {
|
||||||
|
|
||||||
p.AddParam(session.NewIntParameter("net.probe.throttle", "10", "", "If greater than 0, probe packets will be throttled by this value in milliseconds."))
|
p.AddParam(session.NewIntParameter("net.probe.throttle", "10", "", "If greater than 0, probe packets will be throttled by this value in milliseconds."))
|
||||||
|
|
||||||
p.AddHandler(session.NewModuleHandler("net.probe (on|off)", "^net\\.probe\\s+(on|off)$",
|
p.AddHandler(session.NewModuleHandler("net.probe on", "",
|
||||||
"Start/stop network hosts probing in background.",
|
"Start network hosts probing in background.",
|
||||||
func(args []string) error {
|
func(args []string) error {
|
||||||
if args[0] == "on" {
|
return p.Start()
|
||||||
return p.Start()
|
}))
|
||||||
} else {
|
|
||||||
return p.Stop()
|
p.AddHandler(session.NewModuleHandler("net.probe off", "",
|
||||||
}
|
"Stop network hosts probing in background.",
|
||||||
|
func(args []string) error {
|
||||||
|
return p.Stop()
|
||||||
}))
|
}))
|
||||||
|
|
||||||
return p
|
return p
|
||||||
|
|
|
@ -26,17 +26,19 @@ func NewDiscovery(s *session.Session) *Discovery {
|
||||||
quit: make(chan bool),
|
quit: make(chan bool),
|
||||||
}
|
}
|
||||||
|
|
||||||
d.AddHandler(session.NewModuleHandler("net.recon (on|off)", "^net\\.recon\\s+(on|off)$",
|
d.AddHandler(session.NewModuleHandler("net.recon on", "",
|
||||||
"Start/stop network hosts discovery in background.",
|
"Start network hosts discovery.",
|
||||||
func(args []string) error {
|
func(args []string) error {
|
||||||
if args[0] == "on" {
|
return d.Start()
|
||||||
return d.Start()
|
|
||||||
} else {
|
|
||||||
return d.Stop()
|
|
||||||
}
|
|
||||||
}))
|
}))
|
||||||
|
|
||||||
d.AddHandler(session.NewModuleHandler("net.show", "^net\\.show$",
|
d.AddHandler(session.NewModuleHandler("net.recon off", "",
|
||||||
|
"Stop network hosts discovery.",
|
||||||
|
func(args []string) error {
|
||||||
|
return d.Stop()
|
||||||
|
}))
|
||||||
|
|
||||||
|
d.AddHandler(session.NewModuleHandler("net.show", "",
|
||||||
"Show current hosts list.",
|
"Show current hosts list.",
|
||||||
func(args []string) error {
|
func(args []string) error {
|
||||||
return d.Show()
|
return d.Show()
|
||||||
|
|
|
@ -101,14 +101,16 @@ func NewSniffer(s *session.Session) *Sniffer {
|
||||||
sniff.AddParam(session.NewStringParameter("net.sniffer.regexp", "", "", "If filled, only packets matching this regular expression will be considered."))
|
sniff.AddParam(session.NewStringParameter("net.sniffer.regexp", "", "", "If filled, only packets matching this regular expression will be considered."))
|
||||||
sniff.AddParam(session.NewStringParameter("net.sniffer.output", "", "", "If set, the sniffer will write captured packets to this file."))
|
sniff.AddParam(session.NewStringParameter("net.sniffer.output", "", "", "If set, the sniffer will write captured packets to this file."))
|
||||||
|
|
||||||
sniff.AddHandler(session.NewModuleHandler("net.sniffer (on|off)", "^net\\.sniffer\\s+(on|off)$",
|
sniff.AddHandler(session.NewModuleHandler("net.sniffer on", "",
|
||||||
"Start/stop network sniffer in background.",
|
"Start network sniffer in background.",
|
||||||
func(args []string) error {
|
func(args []string) error {
|
||||||
if args[0] == "on" {
|
return sniff.Start()
|
||||||
return sniff.Start()
|
}))
|
||||||
} else {
|
|
||||||
return sniff.Stop()
|
sniff.AddHandler(session.NewModuleHandler("net.sniffer off", "",
|
||||||
}
|
"Stop network sniffer in background.",
|
||||||
|
func(args []string) error {
|
||||||
|
return sniff.Stop()
|
||||||
}))
|
}))
|
||||||
|
|
||||||
return sniff
|
return sniff
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue