| 1 | package net_recon |
| 2 | |
| 3 | import ( |
| 4 | "github.com/bettercap/bettercap/network" |
| 5 | "github.com/bettercap/bettercap/packets" |
| 6 | "github.com/bettercap/bettercap/session" |
| 7 | ) |
| 8 | |
| 9 | type ByAddressSorter []*network.Endpoint |
| 10 | |
| 11 | func (a ByAddressSorter) Len() int { return len(a) } |
| 12 | func (a ByAddressSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] } |
| 13 | func (a ByAddressSorter) Less(i, j int) bool { |
| 14 | if a[i].IpAddressUint32 == a[j].IpAddressUint32 { |
| 15 | return a[i].HwAddress < a[j].HwAddress |
| 16 | } |
| 17 | return a[i].IpAddressUint32 < a[j].IpAddressUint32 |
| 18 | } |
| 19 | |
| 20 | type ByIpSorter []*network.Endpoint |
| 21 | |
| 22 | func (a ByIpSorter) Len() int { return len(a) } |
| 23 | func (a ByIpSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] } |
| 24 | func (a ByIpSorter) Less(i, j int) bool { |
| 25 | return a[i].IpAddressUint32 < a[j].IpAddressUint32 |
| 26 | } |
| 27 | |
| 28 | type ByMacSorter []*network.Endpoint |
| 29 | |
| 30 | func (a ByMacSorter) Len() int { return len(a) } |
| 31 | func (a ByMacSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] } |
| 32 | func (a ByMacSorter) Less(i, j int) bool { |
| 33 | return a[i].HwAddress < a[j].HwAddress |
| 34 | } |
| 35 | |
| 36 | type BySeenSorter []*network.Endpoint |
| 37 | |
| 38 | func (a BySeenSorter) Len() int { return len(a) } |
| 39 | func (a BySeenSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] } |
| 40 | func (a BySeenSorter) Less(i, j int) bool { return a[i].LastSeen.Before(a[j].LastSeen) } |
| 41 | |
| 42 | type BySentSorter []*network.Endpoint |
| 43 | |
| 44 | func trafficOf(ip string) *packets.Traffic { |
| 45 | if v, found := session.I.Queue.Traffic.Load(ip); !found { |
| 46 | return &packets.Traffic{} |
| 47 | } else { |
| 48 | return v.(*packets.Traffic) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func (a BySentSorter) Len() int { return len(a) } |
| 53 | func (a BySentSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] } |
| 54 | func (a BySentSorter) Less(i, j int) bool { |
| 55 | aTraffic := trafficOf(a[i].IpAddress) |
| 56 | bTraffic := trafficOf(a[j].IpAddress) |
| 57 | return bTraffic.Sent > aTraffic.Sent |
| 58 | } |
| 59 | |
| 60 | type ByRcvdSorter []*network.Endpoint |
| 61 | |
| 62 | func (a ByRcvdSorter) Len() int { return len(a) } |
| 63 | func (a ByRcvdSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] } |
| 64 | func (a ByRcvdSorter) Less(i, j int) bool { |
| 65 | aTraffic := trafficOf(a[i].IpAddress) |
| 66 | bTraffic := trafficOf(a[j].IpAddress) |
| 67 | return bTraffic.Received > aTraffic.Received |
| 68 | } |