From 8ec91c920661ef3cdc59dea8ea546af9f32abe5a Mon Sep 17 00:00:00 2001 From: evilsocket Date: Sun, 8 Sep 2019 16:13:46 +0200 Subject: [PATCH] new: implemented ble.timeout and ble.ttl parameters (ref #560) --- modules/ble/ble_recon.go | 25 +++++++++++++++++++++---- modules/ble/ble_show.go | 4 ++-- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/modules/ble/ble_recon.go b/modules/ble/ble_recon.go index 8b2fb62c..ec888eaa 100644 --- a/modules/ble/ble_recon.go +++ b/modules/ble/ble_recon.go @@ -26,7 +26,8 @@ type BLERecon struct { writeUUID *gatt.UUID writeData []byte connected bool - connTimeout time.Duration + connTimeout int + devTTL int quit chan bool done chan bool selector *utils.ViewSelector @@ -39,7 +40,8 @@ func NewBLERecon(s *session.Session) *BLERecon { gattDevice: nil, quit: make(chan bool), done: make(chan bool), - connTimeout: time.Duration(5) * time.Second, + connTimeout: 5, + devTTL: 30, currDevice: nil, connected: false, } @@ -116,6 +118,14 @@ func NewBLERecon(s *session.Session) *BLERecon { fmt.Sprintf("%d", mod.deviceId), "Index of the HCI device to use, -1 to autodetect.")) + mod.AddParam(session.NewIntParameter("ble.timeout", + fmt.Sprintf("%d", mod.connTimeout), + "Connection timeout in seconds.")) + + mod.AddParam(session.NewIntParameter("ble.ttl", + fmt.Sprintf("%d", mod.devTTL), + "Seconds of inactivity for a device to be pruned.")) + return mod } @@ -176,6 +186,12 @@ func (mod *BLERecon) Configure() (err error) { mod.gattDevice.Init(mod.onStateChanged) } + if err, mod.connTimeout = mod.IntParam("ble.timeout"); err != nil { + return err + } else if err, mod.devTTL = mod.IntParam("ble.ttl"); err != nil { + return err + } + return nil } @@ -221,7 +237,8 @@ func (mod *BLERecon) Stop() error { } func (mod *BLERecon) pruner() { - mod.Debug("started devices pruner ...") + blePresentInterval := time.Duration(mod.devTTL) * time.Second + mod.Debug("started devices pruner with ttl %s", blePresentInterval) for mod.Running() { for _, dev := range mod.Session.BLE.Devices() { @@ -262,7 +279,7 @@ func (mod *BLERecon) enumAllTheThings(mac string) error { mod.Info("connecting to %s ...", mac) go func() { - time.Sleep(mod.connTimeout) + time.Sleep(time.Duration(mod.connTimeout) * time.Second) if mod.isEnumerating() && !mod.connected { mod.Warning("connection timeout") mod.Session.Events.Add("ble.connection.timeout", mod.currDevice) diff --git a/modules/ble/ble_show.go b/modules/ble/ble_show.go index a14cf2dd..d76da90d 100644 --- a/modules/ble/ble_show.go +++ b/modules/ble/ble_show.go @@ -15,8 +15,7 @@ import ( ) var ( - bleAliveInterval = time.Duration(5) * time.Second - blePresentInterval = time.Duration(30) * time.Second + bleAliveInterval = time.Duration(5) * time.Second ) func (mod *BLERecon) getRow(dev *network.BLEDevice, withName bool) []string { @@ -27,6 +26,7 @@ func (mod *BLERecon) getRow(dev *network.BLEDevice, withName bool) []string { sinceSeen := time.Since(dev.LastSeen) lastSeen := dev.LastSeen.Format("15:04:05") + blePresentInterval := time.Duration(mod.devTTL) * time.Second if sinceSeen <= bleAliveInterval { lastSeen = tui.Bold(lastSeen) } else if sinceSeen > blePresentInterval {