mirror of
https://github.com/bettercap/bettercap
synced 2025-08-19 13:09:49 -07:00
misc: small fix or general refactoring i did not bother commenting
This commit is contained in:
parent
2fe9a2af2f
commit
282b58c024
5 changed files with 68 additions and 70 deletions
|
@ -46,7 +46,7 @@ func NewDiscovery(s *session.Session) *Discovery {
|
||||||
return d.Show(args[0])
|
return d.Show(args[0])
|
||||||
}))
|
}))
|
||||||
|
|
||||||
d.selector = ViewSelectorFor(&d.SessionModule, "net.show", []string{"ip", "mac", "seen", "sent", "rcvd"}, "")
|
d.selector = ViewSelectorFor(&d.SessionModule, "net.show", []string{"ip", "mac", "seen", "sent", "rcvd"}, "ip asc")
|
||||||
|
|
||||||
return d
|
return d
|
||||||
}
|
}
|
||||||
|
|
|
@ -143,7 +143,7 @@ func (d *Discovery) doSelection(arg string) (err error, targets []*network.Endpo
|
||||||
}
|
}
|
||||||
targets = filtered
|
targets = filtered
|
||||||
|
|
||||||
switch d.selector.SortBy {
|
switch d.selector.SortField {
|
||||||
case "ip":
|
case "ip":
|
||||||
sort.Sort(ByIpSorter(targets))
|
sort.Sort(ByIpSorter(targets))
|
||||||
case "mac":
|
case "mac":
|
||||||
|
@ -185,25 +185,17 @@ func (d *Discovery) colNames(hasMeta bool) []string {
|
||||||
colNames = append(colNames, "Meta")
|
colNames = append(colNames, "Meta")
|
||||||
}
|
}
|
||||||
|
|
||||||
dir := tui.Blue("▾")
|
switch d.selector.SortField {
|
||||||
if d.selector.Sort == "asc" {
|
|
||||||
dir = tui.Blue("▴")
|
|
||||||
}
|
|
||||||
|
|
||||||
if d.selector.SortBy == "" {
|
|
||||||
d.selector.SortBy = "ip"
|
|
||||||
}
|
|
||||||
switch d.selector.SortBy {
|
|
||||||
case "mac":
|
case "mac":
|
||||||
colNames[1] += " " + dir
|
colNames[1] += " " + d.selector.SortSymbol
|
||||||
case "sent":
|
case "sent":
|
||||||
colNames[4] += " " + dir
|
colNames[4] += " " + d.selector.SortSymbol
|
||||||
case "rcvd":
|
case "rcvd":
|
||||||
colNames[5] += " " + dir
|
colNames[5] += " " + d.selector.SortSymbol
|
||||||
case "seen":
|
case "seen":
|
||||||
colNames[6] += " " + dir
|
colNames[6] += " " + d.selector.SortSymbol
|
||||||
case "ip":
|
case "ip":
|
||||||
colNames[0] += " " + dir
|
colNames[0] += " " + d.selector.SortSymbol
|
||||||
}
|
}
|
||||||
|
|
||||||
return colNames
|
return colNames
|
||||||
|
|
|
@ -2,10 +2,13 @@ package modules
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/bettercap/bettercap/session"
|
"github.com/bettercap/bettercap/session"
|
||||||
|
|
||||||
|
"github.com/evilsocket/islazy/tui"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ViewSelector struct {
|
type ViewSelector struct {
|
||||||
|
@ -16,46 +19,44 @@ type ViewSelector struct {
|
||||||
filterPrev string
|
filterPrev string
|
||||||
Expression *regexp.Regexp
|
Expression *regexp.Regexp
|
||||||
|
|
||||||
SortBy string
|
SortField string
|
||||||
sortBys map[string]bool
|
Sort string
|
||||||
sortByName string
|
SortSymbol string
|
||||||
|
sortFields map[string]bool
|
||||||
Sort string
|
sortName string
|
||||||
sortName string
|
sortParser string
|
||||||
|
sortParse *regexp.Regexp
|
||||||
|
|
||||||
Limit int
|
Limit int
|
||||||
limitName string
|
limitName string
|
||||||
}
|
}
|
||||||
|
|
||||||
func ViewSelectorFor(m *session.SessionModule, prefix string, sortBys []string, defSortBy string) *ViewSelector {
|
func ViewSelectorFor(m *session.SessionModule, prefix string, sortFields []string, defExpression string) *ViewSelector {
|
||||||
|
parser := "(" + strings.Join(sortFields, "|") + ") (desc|asc)"
|
||||||
s := &ViewSelector{
|
s := &ViewSelector{
|
||||||
owner: m,
|
owner: m,
|
||||||
filterName: prefix + ".filter",
|
filterName: prefix + ".filter",
|
||||||
filterPrev: "",
|
|
||||||
sortByName: prefix + ".sort_by",
|
|
||||||
sortBys: make(map[string]bool),
|
|
||||||
sortName: prefix + ".sort",
|
sortName: prefix + ".sort",
|
||||||
|
sortParser: parser,
|
||||||
|
sortParse: regexp.MustCompile(parser),
|
||||||
limitName: prefix + ".limit",
|
limitName: prefix + ".limit",
|
||||||
|
|
||||||
SortBy: defSortBy,
|
|
||||||
Sort: "asc",
|
|
||||||
Limit: 0,
|
|
||||||
Filter: "",
|
|
||||||
Expression: nil,
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, sb := range sortBys {
|
|
||||||
s.sortBys[sb] = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m.AddParam(session.NewStringParameter(s.filterName, "", "", "Defines a regular expression filter for "+prefix))
|
m.AddParam(session.NewStringParameter(s.filterName, "", "", "Defines a regular expression filter for "+prefix))
|
||||||
m.AddParam(session.NewStringParameter(s.sortByName, defSortBy, "", "Defines sorting field for "+prefix+", available: "+strings.Join(sortBys, ", ")))
|
m.AddParam(session.NewStringParameter(
|
||||||
m.AddParam(session.NewStringParameter(s.sortName, "asc", "", "Defines sorting direction for "+prefix))
|
s.sortName,
|
||||||
|
defExpression,
|
||||||
|
s.sortParser,
|
||||||
|
"Defines sorting field ("+strings.Join(sortFields, ", ")+") and direction (asc or desc) for "+prefix))
|
||||||
|
|
||||||
m.AddParam(session.NewIntParameter(s.limitName, "0", "Defines limit for "+prefix))
|
m.AddParam(session.NewIntParameter(s.limitName, "0", "Defines limit for "+prefix))
|
||||||
|
|
||||||
|
s.parseSorting()
|
||||||
|
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ViewSelector) Update() (err error) {
|
func (s *ViewSelector) parseFilter() (err error) {
|
||||||
if err, s.Filter = s.owner.StringParam(s.filterName); err != nil {
|
if err, s.Filter = s.owner.StringParam(s.filterName); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -70,24 +71,37 @@ func (s *ViewSelector) Update() (err error) {
|
||||||
s.Expression = nil
|
s.Expression = nil
|
||||||
}
|
}
|
||||||
s.filterPrev = s.Filter
|
s.filterPrev = s.Filter
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if err, s.SortBy = s.owner.StringParam(s.sortByName); err != nil {
|
func (s *ViewSelector) parseSorting() (err error) {
|
||||||
|
expr := ""
|
||||||
|
if err, expr = s.owner.StringParam(s.sortName); err != nil {
|
||||||
return
|
return
|
||||||
} else if s.SortBy != "" {
|
|
||||||
if _, found := s.sortBys[s.SortBy]; !found {
|
|
||||||
return fmt.Errorf("'%s' is not valid for %s", s.SortBy, s.sortByName)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err, s.Sort = s.owner.StringParam(s.sortName); err != nil {
|
tokens := s.sortParse.FindAllStringSubmatch(expr, -1)
|
||||||
return
|
if tokens == nil {
|
||||||
} else if s.Sort != "asc" && s.Sort != "desc" {
|
return fmt.Errorf("expression '%s' doesn't parse", expr)
|
||||||
return fmt.Errorf("'%s' is not valid for %s", s.Sort, s.sortName)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err, s.Limit = s.owner.IntParam(s.limitName); err != nil {
|
s.SortField = tokens[0][1]
|
||||||
return
|
s.Sort = tokens[0][2]
|
||||||
|
s.SortSymbol = tui.Blue("▾")
|
||||||
|
if s.Sort == "asc" {
|
||||||
|
s.SortSymbol = tui.Blue("▴")
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *ViewSelector) Update() (err error) {
|
||||||
|
if err = s.parseFilter(); err != nil {
|
||||||
|
return
|
||||||
|
} else if err = s.parseSorting(); err != nil {
|
||||||
|
return
|
||||||
|
} else if err, s.Limit = s.owner.IntParam(s.limitName); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
|
@ -148,7 +148,7 @@ func NewWiFiModule(s *session.Session) *WiFiModule {
|
||||||
}))
|
}))
|
||||||
|
|
||||||
w.selector = ViewSelectorFor(&w.SessionModule, "wifi.show",
|
w.selector = ViewSelectorFor(&w.SessionModule, "wifi.show",
|
||||||
[]string{"rssi", "bssid", "essid", "channel", "encryption", "clients", "seen", "sent", "rcvd"}, "rssi")
|
[]string{"rssi", "bssid", "essid", "channel", "encryption", "clients", "seen", "sent", "rcvd"}, "rssi asc")
|
||||||
|
|
||||||
w.AddHandler(session.NewModuleHandler("wifi.recon.channel", `wifi\.recon\.channel[\s]+([0-9]+(?:[, ]+[0-9]+)*|clear)`,
|
w.AddHandler(session.NewModuleHandler("wifi.recon.channel", `wifi\.recon\.channel[\s]+([0-9]+(?:[, ]+[0-9]+)*|clear)`,
|
||||||
"WiFi channels (comma separated) or 'clear' for channel hopping.",
|
"WiFi channels (comma separated) or 'clear' for channel hopping.",
|
||||||
|
|
|
@ -146,7 +146,7 @@ func (w *WiFiModule) doSelection() (err error, stations []*network.Station) {
|
||||||
}
|
}
|
||||||
stations = filtered
|
stations = filtered
|
||||||
|
|
||||||
switch w.selector.SortBy {
|
switch w.selector.SortField {
|
||||||
case "seen":
|
case "seen":
|
||||||
sort.Sort(ByWiFiSeenSorter(stations))
|
sort.Sort(ByWiFiSeenSorter(stations))
|
||||||
case "essid":
|
case "essid":
|
||||||
|
@ -211,33 +211,25 @@ func (w *WiFiModule) colNames(nrows int) []string {
|
||||||
}
|
}
|
||||||
|
|
||||||
if columns != nil {
|
if columns != nil {
|
||||||
dir := tui.Blue("▾")
|
switch w.selector.SortField {
|
||||||
if w.selector.Sort == "asc" {
|
|
||||||
dir = tui.Blue("▴")
|
|
||||||
}
|
|
||||||
|
|
||||||
if w.selector.SortBy == "" {
|
|
||||||
w.selector.SortBy = "rssi"
|
|
||||||
}
|
|
||||||
switch w.selector.SortBy {
|
|
||||||
case "seen":
|
case "seen":
|
||||||
w.colDecorate(columns, "Last Seen", dir)
|
w.colDecorate(columns, "Last Seen", w.selector.SortSymbol)
|
||||||
case "essid":
|
case "essid":
|
||||||
w.colDecorate(columns, "SSID", dir)
|
w.colDecorate(columns, "SSID", w.selector.SortSymbol)
|
||||||
case "bssid":
|
case "bssid":
|
||||||
w.colDecorate(columns, "BSSID", dir)
|
w.colDecorate(columns, "BSSID", w.selector.SortSymbol)
|
||||||
case "channel":
|
case "channel":
|
||||||
w.colDecorate(columns, "Channel", dir)
|
w.colDecorate(columns, "Channel", w.selector.SortSymbol)
|
||||||
case "clients":
|
case "clients":
|
||||||
w.colDecorate(columns, "Clients", dir)
|
w.colDecorate(columns, "Clients", w.selector.SortSymbol)
|
||||||
case "encryption":
|
case "encryption":
|
||||||
w.colDecorate(columns, "Encryption", dir)
|
w.colDecorate(columns, "Encryption", w.selector.SortSymbol)
|
||||||
case "sent":
|
case "sent":
|
||||||
w.colDecorate(columns, "Sent", dir)
|
w.colDecorate(columns, "Sent", w.selector.SortSymbol)
|
||||||
case "rcvd":
|
case "rcvd":
|
||||||
w.colDecorate(columns, "Recvd", dir)
|
w.colDecorate(columns, "Recvd", w.selector.SortSymbol)
|
||||||
case "rssi":
|
case "rssi":
|
||||||
w.colDecorate(columns, "RSSI", dir)
|
w.colDecorate(columns, "RSSI", w.selector.SortSymbol)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue