mirror of
https://github.com/bettercap/bettercap
synced 2025-08-20 13:33:21 -07:00
fix: using sync.Map to avoid race conditions on the packets.Queue
This commit is contained in:
parent
b676d68b4c
commit
64a5ce2b58
4 changed files with 34 additions and 71 deletions
|
@ -61,8 +61,11 @@ func (mod *Discovery) getRow(e *network.Endpoint, withMeta bool) [][]string {
|
|||
|
||||
var traffic *packets.Traffic
|
||||
var found bool
|
||||
if traffic, found = mod.Session.Queue.Traffic[e.IpAddress]; !found {
|
||||
var v interface{}
|
||||
if v, found = mod.Session.Queue.Traffic.Load(e.IpAddress); !found {
|
||||
traffic = &packets.Traffic{}
|
||||
} else {
|
||||
traffic = v.(*packets.Traffic)
|
||||
}
|
||||
|
||||
seen := e.LastSeen.Format("15:04:05")
|
||||
|
@ -203,9 +206,6 @@ func (mod *Discovery) colNames(hasMeta bool) []string {
|
|||
}
|
||||
|
||||
func (mod *Discovery) showStatusBar() {
|
||||
mod.Session.Queue.Stats.RLock()
|
||||
defer mod.Session.Queue.Stats.RUnlock()
|
||||
|
||||
parts := []string{
|
||||
fmt.Sprintf("%s %s", tui.Red("↑"), humanize.Bytes(mod.Session.Queue.Stats.Sent)),
|
||||
fmt.Sprintf("%s %s", tui.Green("↓"), humanize.Bytes(mod.Session.Queue.Stats.Received)),
|
||||
|
|
|
@ -41,24 +41,19 @@ func (a BySeenSorter) Less(i, j int) bool { return a[i].LastSeen.Before(a[j].Las
|
|||
|
||||
type BySentSorter []*network.Endpoint
|
||||
|
||||
func trafficOf(ip string) *packets.Traffic {
|
||||
if v, found := session.I.Queue.Traffic.Load(ip); !found {
|
||||
return &packets.Traffic{}
|
||||
} else {
|
||||
return v.(*packets.Traffic)
|
||||
}
|
||||
}
|
||||
|
||||
func (a BySentSorter) Len() int { return len(a) }
|
||||
func (a BySentSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
func (a BySentSorter) Less(i, j int) bool {
|
||||
session.I.Queue.Lock()
|
||||
defer session.I.Queue.Unlock()
|
||||
|
||||
var found bool = false
|
||||
var aTraffic *packets.Traffic = nil
|
||||
var bTraffic *packets.Traffic = nil
|
||||
|
||||
if aTraffic, found = session.I.Queue.Traffic[a[i].IpAddress]; !found {
|
||||
aTraffic = &packets.Traffic{}
|
||||
}
|
||||
|
||||
if bTraffic, found = session.I.Queue.Traffic[a[j].IpAddress]; !found {
|
||||
bTraffic = &packets.Traffic{}
|
||||
}
|
||||
|
||||
aTraffic := trafficOf(a[i].IpAddress)
|
||||
bTraffic := trafficOf(a[j].IpAddress)
|
||||
return bTraffic.Sent > aTraffic.Sent
|
||||
}
|
||||
|
||||
|
@ -67,20 +62,7 @@ type ByRcvdSorter []*network.Endpoint
|
|||
func (a ByRcvdSorter) Len() int { return len(a) }
|
||||
func (a ByRcvdSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
func (a ByRcvdSorter) Less(i, j int) bool {
|
||||
session.I.Queue.Lock()
|
||||
defer session.I.Queue.Unlock()
|
||||
|
||||
var found bool = false
|
||||
var aTraffic *packets.Traffic = nil
|
||||
var bTraffic *packets.Traffic = nil
|
||||
|
||||
if aTraffic, found = session.I.Queue.Traffic[a[i].IpAddress]; !found {
|
||||
aTraffic = &packets.Traffic{}
|
||||
}
|
||||
|
||||
if bTraffic, found = session.I.Queue.Traffic[a[j].IpAddress]; !found {
|
||||
bTraffic = &packets.Traffic{}
|
||||
}
|
||||
|
||||
aTraffic := trafficOf(a[i].IpAddress)
|
||||
bTraffic := trafficOf(a[j].IpAddress)
|
||||
return bTraffic.Received > aTraffic.Received
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue