Debian Codegraph @

modules/wifi/wifi_hopping.go

raw symbols SCIP
1 package wifi
2
3 import (
4 "net"
5 "time"
6
7 "github.com/bettercap/bettercap/network"
8 )
9
10 func (mod *WiFiModule) isInterfaceConnected() bool {
11 ifaces, err := net.Interfaces()
12 if err != nil {
13 mod.Error("error while enumerating interfaces: %s", err)
14 return false
15 }
16
17 for _, iface := range ifaces {
18 if mod.iface.HwAddress == network.NormalizeMac(iface.HardwareAddr.String()) {
19 return true
20 }
21 }
22
23 return false
24 }
25
26 func (mod *WiFiModule) hopUnlocked(channel int) (mustStop bool) {
27 // mod.Debug("hopping on channel %d", channel)
28
29 if err := network.SetInterfaceChannel(mod.iface.Name(), channel); err != nil {
30 // check if the device has been disconnected
31 if mod.isInterfaceConnected() == false {
32 mod.Error("interface %s disconnected, stopping module", mod.iface.Name())
33 mustStop = true
34 } else {
35 mod.Warning("error while hopping to channel %d: %s", channel, err)
36 }
37 }
38
39 return
40 }
41
42 func (mod *WiFiModule) hop(channel int) (mustStop bool) {
43 mod.chanLock.Lock()
44 defer mod.chanLock.Unlock()
45
46 return mod.hopUnlocked(channel)
47 }
48
49 func (mod *WiFiModule) onChannel(channel int, cb func()) {
50 mod.chanLock.Lock()
51 defer mod.chanLock.Unlock()
52
53 prev := mod.stickChan
54 mod.stickChan = channel
55
56 mod.hopUnlocked(channel)
57
58 cb()
59
60 mod.stickChan = prev
61 }
62
63 func (mod *WiFiModule) channelHopper() {
64 mod.reads.Add(1)
65 defer mod.reads.Done()
66
67 mod.Info("channel hopper started.")
68
69 for mod.Running() {
70 delay := mod.hopPeriod
71 // if we have both 2.4 and 5ghz capabilities, we have
72 // more channels, therefore we need to increase the time
73 // we hop on each one otherwise me lose information
74 if len(mod.frequencies) > 14 {
75 delay = delay * 2
76 }
77
78 frequencies := mod.frequencies
79
80 loopCurrentChannels:
81 for _, frequency := range frequencies {
82 channel := network.Dot11Freq2Chan(frequency)
83 // stick to the access point channel as long as it's selected
84 // or as long as we're deauthing on it
85 if mod.stickChan != 0 {
86 channel = mod.stickChan
87 }
88
89 if stop := mod.hop(channel); stop {
90 mod.forcedStop()
91 return
92 }
93
94 select {
95 case <-mod.hopChanges:
96 mod.Debug("hop changed")
97 break loopCurrentChannels
98 case <-time.After(delay):
99 if !mod.Running() {
100 return
101 }
102 }
103 }
104 }
105 }