mirror of
https://github.com/bettercap/bettercap
synced 2025-08-14 02:36:57 -07:00
fix: gracefully handling wifi device disconnection
This commit is contained in:
parent
54116f7fbe
commit
afe300cd8a
4 changed files with 53 additions and 22 deletions
|
@ -561,6 +561,17 @@ func (mod *WiFiModule) Start() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (mod *WiFiModule) forcedStop() error {
|
||||
return mod.SetRunning(false, func() {
|
||||
// signal the main for loop we want to exit
|
||||
if !mod.pktSourceChanClosed {
|
||||
mod.pktSourceChan <- nil
|
||||
}
|
||||
// close the pcap handle to make the main for exit
|
||||
mod.handle.Close()
|
||||
})
|
||||
}
|
||||
|
||||
func (mod *WiFiModule) Stop() error {
|
||||
return mod.SetRunning(false, func() {
|
||||
// wait any pending write operation
|
||||
|
|
|
@ -1,11 +1,47 @@
|
|||
package wifi
|
||||
|
||||
import (
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/bettercap/bettercap/network"
|
||||
)
|
||||
|
||||
func (mod *WiFiModule) isInterfaceConnected() bool {
|
||||
ifaces, err := net.Interfaces()
|
||||
if err != nil {
|
||||
mod.Error("error while enumerating interfaces: %s", err)
|
||||
return false
|
||||
}
|
||||
|
||||
for _, iface := range ifaces {
|
||||
if mod.iface.HwAddress == network.NormalizeMac(iface.HardwareAddr.String()) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (mod *WiFiModule) hop(channel int) (mustStop bool) {
|
||||
mod.chanLock.Lock()
|
||||
defer mod.chanLock.Unlock()
|
||||
|
||||
mod.Debug("hopping on channel %d", channel)
|
||||
|
||||
if err := network.SetInterfaceChannel(mod.iface.Name(), channel); err != nil {
|
||||
// check if the device has been disconnected
|
||||
if mod.isInterfaceConnected() == false {
|
||||
mod.Error("interface %s disconnected, stopping module", mod.iface.Name())
|
||||
mustStop = true
|
||||
} else {
|
||||
mod.Warning("error while hopping to channel %d: %s", channel, err)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (mod *WiFiModule) onChannel(channel int, cb func()) {
|
||||
mod.chanLock.Lock()
|
||||
defer mod.chanLock.Unlock()
|
||||
|
@ -13,11 +49,7 @@ func (mod *WiFiModule) onChannel(channel int, cb func()) {
|
|||
prev := mod.stickChan
|
||||
mod.stickChan = channel
|
||||
|
||||
if err := network.SetInterfaceChannel(mod.iface.Name(), channel); err != nil {
|
||||
mod.Warning("error while hopping to channel %d: %s", channel, err)
|
||||
} else {
|
||||
mod.Debug("hopped on channel %d", channel)
|
||||
}
|
||||
mod.hop(channel)
|
||||
|
||||
cb()
|
||||
|
||||
|
@ -50,13 +82,10 @@ func (mod *WiFiModule) channelHopper() {
|
|||
channel = mod.stickChan
|
||||
}
|
||||
|
||||
mod.Debug("hopping on channel %d", channel)
|
||||
|
||||
mod.chanLock.Lock()
|
||||
if err := network.SetInterfaceChannel(mod.iface.Name(), channel); err != nil {
|
||||
mod.Warning("error while hopping to channel %d: %s", channel, err)
|
||||
if stop := mod.hop(channel); stop {
|
||||
mod.forcedStop()
|
||||
return
|
||||
}
|
||||
mod.chanLock.Unlock()
|
||||
|
||||
select {
|
||||
case _ = <-mod.hopChanges:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue