fix: fixed a bug in the gatt library which prevented ble.recon/ble.enum to work multiple times (fixes #471)

This commit is contained in:
evilsocket 2019-03-13 15:35:46 +01:00
commit 120db4db3d
No known key found for this signature in database
GPG key ID: 1564D7F30393A456
7 changed files with 75 additions and 13 deletions

View file

@ -6,7 +6,6 @@ package ble
import (
"encoding/hex"
"fmt"
"io/ioutil"
golog "log"
"time"
@ -15,6 +14,8 @@ import (
"github.com/bettercap/bettercap/session"
"github.com/bettercap/gatt"
"github.com/evilsocket/islazy/str"
)
type BLERecon struct {
@ -126,15 +127,25 @@ func (mod *BLERecon) isEnumerating() bool {
return mod.currDevice != nil
}
type dummyWriter struct {
mod *BLERecon
}
func (w dummyWriter) Write(p []byte) (n int, err error) {
w.mod.Debug("[gatt.log] %s", str.Trim(string(p)))
return len(p), nil
}
func (mod *BLERecon) Configure() (err error) {
if mod.Running() {
return session.ErrAlreadyStarted
} else if mod.gattDevice == nil {
mod.Debug("initializing device ...")
// hey Paypal GATT library, could you please just STFU?!
golog.SetOutput(ioutil.Discard)
golog.SetFlags(0)
golog.SetOutput(dummyWriter{mod})
if mod.gattDevice, err = gatt.NewDevice(defaultBLEClientOptions...); err != nil {
mod.Debug("error while creating new gatt device: %v", err)
return err
}
@ -162,7 +173,17 @@ func (mod *BLERecon) Start() error {
mod.Info("stopping scan ...")
mod.gattDevice.StopScanning()
if mod.currDevice != nil && mod.currDevice.Device != nil && mod.gattDevice != nil {
mod.Debug("resetting connection with %v", mod.currDevice.Device)
mod.gattDevice.CancelConnection(mod.currDevice.Device)
}
mod.Debug("stopping device")
if err := mod.gattDevice.Stop(); err != nil {
mod.Warning("error while stopping device: %v", err)
} else {
mod.Debug("gatt device closed")
}
mod.done <- true
})
@ -172,6 +193,9 @@ func (mod *BLERecon) Stop() error {
return mod.SetRunning(false, func() {
mod.quit <- true
<-mod.done
mod.Debug("module stopped, cleaning state")
mod.gattDevice = nil
mod.setCurrentDevice(nil)
})
}

View file

@ -15,8 +15,12 @@ func (mod *BLERecon) onStateChanged(dev gatt.Device, s gatt.State) {
if mod.currDevice == nil {
mod.Info("starting discovery ...")
dev.Scan([]gatt.UUID{}, true)
} else {
mod.Debug("current device was not cleaned: %v", mod.currDevice)
}
case gatt.StatePoweredOff:
mod.Debug("resetting device instance")
mod.gattDevice.StopScanning()
mod.setCurrentDevice(nil)
mod.gattDevice = nil
@ -51,6 +55,7 @@ func (mod *BLERecon) onPeriphConnected(p gatt.Peripheral, err error) {
defer func(per gatt.Peripheral) {
mod.Info("disconnecting from %s ...", per.ID())
per.Device().CancelConnection(per)
mod.setCurrentDevice(nil)
}(p)
mod.Session.Events.Add("ble.device.connected", mod.currDevice)