mirror of
https://github.com/bettercap/bettercap
synced 2025-08-21 05:53:20 -07:00
misc: small fix or general refactoring i did not bother commenting
This commit is contained in:
parent
bf3671465b
commit
4eead7eafa
58 changed files with 2052 additions and 2052 deletions
|
@ -29,7 +29,7 @@ type BLERecon struct {
|
|||
}
|
||||
|
||||
func NewBLERecon(s *session.Session) *BLERecon {
|
||||
d := &BLERecon{
|
||||
mod := &BLERecon{
|
||||
SessionModule: session.NewSessionModule("ble.recon", s),
|
||||
gattDevice: nil,
|
||||
quit: make(chan bool),
|
||||
|
@ -39,38 +39,38 @@ func NewBLERecon(s *session.Session) *BLERecon {
|
|||
connected: false,
|
||||
}
|
||||
|
||||
d.AddHandler(session.NewModuleHandler("ble.recon on", "",
|
||||
mod.AddHandler(session.NewModuleHandler("ble.recon on", "",
|
||||
"Start Bluetooth Low Energy devices discovery.",
|
||||
func(args []string) error {
|
||||
return d.Start()
|
||||
return mod.Start()
|
||||
}))
|
||||
|
||||
d.AddHandler(session.NewModuleHandler("ble.recon off", "",
|
||||
mod.AddHandler(session.NewModuleHandler("ble.recon off", "",
|
||||
"Stop Bluetooth Low Energy devices discovery.",
|
||||
func(args []string) error {
|
||||
return d.Stop()
|
||||
return mod.Stop()
|
||||
}))
|
||||
|
||||
d.AddHandler(session.NewModuleHandler("ble.show", "",
|
||||
mod.AddHandler(session.NewModuleHandler("ble.show", "",
|
||||
"Show discovered Bluetooth Low Energy devices.",
|
||||
func(args []string) error {
|
||||
return d.Show()
|
||||
return mod.Show()
|
||||
}))
|
||||
|
||||
d.AddHandler(session.NewModuleHandler("ble.enum MAC", "ble.enum "+network.BLEMacValidator,
|
||||
mod.AddHandler(session.NewModuleHandler("ble.enum MAC", "ble.enum "+network.BLEMacValidator,
|
||||
"Enumerate services and characteristics for the given BLE device.",
|
||||
func(args []string) error {
|
||||
if d.isEnumerating() {
|
||||
return fmt.Errorf("An enumeration for %s is already running, please wait.", d.currDevice.Device.ID())
|
||||
if mod.isEnumerating() {
|
||||
return fmt.Errorf("An enumeration for %s is already running, please wait.", mod.currDevice.Device.ID())
|
||||
}
|
||||
|
||||
d.writeData = nil
|
||||
d.writeUUID = nil
|
||||
mod.writeData = nil
|
||||
mod.writeUUID = nil
|
||||
|
||||
return d.enumAllTheThings(network.NormalizeMac(args[0]))
|
||||
return mod.enumAllTheThings(network.NormalizeMac(args[0]))
|
||||
}))
|
||||
|
||||
d.AddHandler(session.NewModuleHandler("ble.write MAC UUID HEX_DATA", "ble.write "+network.BLEMacValidator+" ([a-fA-F0-9]+) ([a-fA-F0-9]+)",
|
||||
mod.AddHandler(session.NewModuleHandler("ble.write MAC UUID HEX_DATA", "ble.write "+network.BLEMacValidator+" ([a-fA-F0-9]+) ([a-fA-F0-9]+)",
|
||||
"Write the HEX_DATA buffer to the BLE device with the specified MAC address, to the characteristics with the given UUID.",
|
||||
func(args []string) error {
|
||||
mac := network.NormalizeMac(args[0])
|
||||
|
@ -83,84 +83,84 @@ func NewBLERecon(s *session.Session) *BLERecon {
|
|||
return fmt.Errorf("Error parsing %s: %s", args[2], err)
|
||||
}
|
||||
|
||||
return d.writeBuffer(mac, uuid, data)
|
||||
return mod.writeBuffer(mac, uuid, data)
|
||||
}))
|
||||
|
||||
return d
|
||||
return mod
|
||||
}
|
||||
|
||||
func (d BLERecon) Name() string {
|
||||
func (mod BLERecon) Name() string {
|
||||
return "ble.recon"
|
||||
}
|
||||
|
||||
func (d BLERecon) Description() string {
|
||||
func (mod BLERecon) Description() string {
|
||||
return "Bluetooth Low Energy devices discovery."
|
||||
}
|
||||
|
||||
func (d BLERecon) Author() string {
|
||||
func (mod BLERecon) Author() string {
|
||||
return "Simone Margaritelli <evilsocket@gmail.com>"
|
||||
}
|
||||
|
||||
func (d *BLERecon) isEnumerating() bool {
|
||||
return d.currDevice != nil
|
||||
func (mod *BLERecon) isEnumerating() bool {
|
||||
return mod.currDevice != nil
|
||||
}
|
||||
|
||||
func (d *BLERecon) Configure() (err error) {
|
||||
if d.Running() {
|
||||
func (mod *BLERecon) Configure() (err error) {
|
||||
if mod.Running() {
|
||||
return session.ErrAlreadyStarted
|
||||
} else if d.gattDevice == nil {
|
||||
d.Info("Initializing BLE device ...")
|
||||
} else if mod.gattDevice == nil {
|
||||
mod.Info("Initializing BLE device ...")
|
||||
|
||||
// hey Paypal GATT library, could you please just STFU?!
|
||||
golog.SetOutput(ioutil.Discard)
|
||||
if d.gattDevice, err = gatt.NewDevice(defaultBLEClientOptions...); err != nil {
|
||||
if mod.gattDevice, err = gatt.NewDevice(defaultBLEClientOptions...); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
d.gattDevice.Handle(
|
||||
gatt.PeripheralDiscovered(d.onPeriphDiscovered),
|
||||
gatt.PeripheralConnected(d.onPeriphConnected),
|
||||
gatt.PeripheralDisconnected(d.onPeriphDisconnected),
|
||||
mod.gattDevice.Handle(
|
||||
gatt.PeripheralDiscovered(mod.onPeriphDiscovered),
|
||||
gatt.PeripheralConnected(mod.onPeriphConnected),
|
||||
gatt.PeripheralDisconnected(mod.onPeriphDisconnected),
|
||||
)
|
||||
|
||||
d.gattDevice.Init(d.onStateChanged)
|
||||
mod.gattDevice.Init(mod.onStateChanged)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *BLERecon) Start() error {
|
||||
if err := d.Configure(); err != nil {
|
||||
func (mod *BLERecon) Start() error {
|
||||
if err := mod.Configure(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return d.SetRunning(true, func() {
|
||||
go d.pruner()
|
||||
return mod.SetRunning(true, func() {
|
||||
go mod.pruner()
|
||||
|
||||
<-d.quit
|
||||
<-mod.quit
|
||||
|
||||
d.Info("Stopping BLE scan ...")
|
||||
mod.Info("Stopping BLE scan ...")
|
||||
|
||||
d.gattDevice.StopScanning()
|
||||
mod.gattDevice.StopScanning()
|
||||
|
||||
d.done <- true
|
||||
mod.done <- true
|
||||
})
|
||||
}
|
||||
|
||||
func (d *BLERecon) Stop() error {
|
||||
return d.SetRunning(false, func() {
|
||||
d.quit <- true
|
||||
<-d.done
|
||||
func (mod *BLERecon) Stop() error {
|
||||
return mod.SetRunning(false, func() {
|
||||
mod.quit <- true
|
||||
<-mod.done
|
||||
})
|
||||
}
|
||||
|
||||
func (d *BLERecon) pruner() {
|
||||
d.Debug("Started BLE devices pruner ...")
|
||||
func (mod *BLERecon) pruner() {
|
||||
mod.Debug("Started BLE devices pruner ...")
|
||||
|
||||
for d.Running() {
|
||||
for _, dev := range d.Session.BLE.Devices() {
|
||||
for mod.Running() {
|
||||
for _, dev := range mod.Session.BLE.Devices() {
|
||||
if time.Since(dev.LastSeen) > blePresentInterval {
|
||||
d.Session.BLE.Remove(dev.Device.ID())
|
||||
mod.Session.BLE.Remove(dev.Device.ID())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -168,41 +168,41 @@ func (d *BLERecon) pruner() {
|
|||
}
|
||||
}
|
||||
|
||||
func (d *BLERecon) setCurrentDevice(dev *network.BLEDevice) {
|
||||
d.connected = false
|
||||
d.currDevice = dev
|
||||
func (mod *BLERecon) setCurrentDevice(dev *network.BLEDevice) {
|
||||
mod.connected = false
|
||||
mod.currDevice = dev
|
||||
}
|
||||
|
||||
func (d *BLERecon) writeBuffer(mac string, uuid gatt.UUID, data []byte) error {
|
||||
d.writeUUID = &uuid
|
||||
d.writeData = data
|
||||
return d.enumAllTheThings(mac)
|
||||
func (mod *BLERecon) writeBuffer(mac string, uuid gatt.UUID, data []byte) error {
|
||||
mod.writeUUID = &uuid
|
||||
mod.writeData = data
|
||||
return mod.enumAllTheThings(mac)
|
||||
}
|
||||
|
||||
func (d *BLERecon) enumAllTheThings(mac string) error {
|
||||
dev, found := d.Session.BLE.Get(mac)
|
||||
func (mod *BLERecon) enumAllTheThings(mac string) error {
|
||||
dev, found := mod.Session.BLE.Get(mac)
|
||||
if !found || dev == nil {
|
||||
return fmt.Errorf("BLE device with address %s not found.", mac)
|
||||
} else if d.Running() {
|
||||
d.gattDevice.StopScanning()
|
||||
} else if mod.Running() {
|
||||
mod.gattDevice.StopScanning()
|
||||
}
|
||||
|
||||
d.setCurrentDevice(dev)
|
||||
if err := d.Configure(); err != nil && err != session.ErrAlreadyStarted {
|
||||
mod.setCurrentDevice(dev)
|
||||
if err := mod.Configure(); err != nil && err != session.ErrAlreadyStarted {
|
||||
return err
|
||||
}
|
||||
|
||||
d.Info("Connecting to %s ...", mac)
|
||||
mod.Info("Connecting to %s ...", mac)
|
||||
|
||||
go func() {
|
||||
time.Sleep(d.connTimeout)
|
||||
if d.isEnumerating() && !d.connected {
|
||||
d.Session.Events.Add("ble.connection.timeout", d.currDevice)
|
||||
d.onPeriphDisconnected(nil, nil)
|
||||
time.Sleep(mod.connTimeout)
|
||||
if mod.isEnumerating() && !mod.connected {
|
||||
mod.Session.Events.Add("ble.connection.timeout", mod.currDevice)
|
||||
mod.onPeriphDisconnected(nil, nil)
|
||||
}
|
||||
}()
|
||||
|
||||
d.gattDevice.Connect(dev.Device)
|
||||
mod.gattDevice.Connect(dev.Device)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -7,65 +7,65 @@ import (
|
|||
"github.com/bettercap/gatt"
|
||||
)
|
||||
|
||||
func (d *BLERecon) onStateChanged(dev gatt.Device, s gatt.State) {
|
||||
d.Info("BLE state changed to %v", s)
|
||||
func (mod *BLERecon) onStateChanged(dev gatt.Device, s gatt.State) {
|
||||
mod.Info("BLE state changed to %v", s)
|
||||
|
||||
switch s {
|
||||
case gatt.StatePoweredOn:
|
||||
if d.currDevice == nil {
|
||||
d.Info("Starting BLE discovery ...")
|
||||
if mod.currDevice == nil {
|
||||
mod.Info("Starting BLE discovery ...")
|
||||
dev.Scan([]gatt.UUID{}, true)
|
||||
}
|
||||
case gatt.StatePoweredOff:
|
||||
d.gattDevice = nil
|
||||
mod.gattDevice = nil
|
||||
|
||||
default:
|
||||
d.Warning("Unexpected BLE state: %v", s)
|
||||
mod.Warning("Unexpected BLE state: %v", s)
|
||||
}
|
||||
}
|
||||
|
||||
func (d *BLERecon) onPeriphDiscovered(p gatt.Peripheral, a *gatt.Advertisement, rssi int) {
|
||||
d.Session.BLE.AddIfNew(p.ID(), p, a, rssi)
|
||||
func (mod *BLERecon) onPeriphDiscovered(p gatt.Peripheral, a *gatt.Advertisement, rssi int) {
|
||||
mod.Session.BLE.AddIfNew(p.ID(), p, a, rssi)
|
||||
}
|
||||
|
||||
func (d *BLERecon) onPeriphDisconnected(p gatt.Peripheral, err error) {
|
||||
if d.Running() {
|
||||
func (mod *BLERecon) onPeriphDisconnected(p gatt.Peripheral, err error) {
|
||||
if mod.Running() {
|
||||
// restore scanning
|
||||
d.Info("Device disconnected, restoring BLE discovery.")
|
||||
d.setCurrentDevice(nil)
|
||||
d.gattDevice.Scan([]gatt.UUID{}, true)
|
||||
mod.Info("Device disconnected, restoring BLE discovery.")
|
||||
mod.setCurrentDevice(nil)
|
||||
mod.gattDevice.Scan([]gatt.UUID{}, true)
|
||||
}
|
||||
}
|
||||
|
||||
func (d *BLERecon) onPeriphConnected(p gatt.Peripheral, err error) {
|
||||
func (mod *BLERecon) onPeriphConnected(p gatt.Peripheral, err error) {
|
||||
if err != nil {
|
||||
d.Warning("Connected to %s but with error: %s", p.ID(), err)
|
||||
mod.Warning("Connected to %s but with error: %s", p.ID(), err)
|
||||
return
|
||||
} else if d.currDevice == nil {
|
||||
} else if mod.currDevice == nil {
|
||||
// timed out
|
||||
d.Warning("Connected to %s but after the timeout :(", p.ID())
|
||||
mod.Warning("Connected to %s but after the timeout :(", p.ID())
|
||||
return
|
||||
}
|
||||
|
||||
d.connected = true
|
||||
mod.connected = true
|
||||
|
||||
defer func(per gatt.Peripheral) {
|
||||
d.Info("Disconnecting from %s ...", per.ID())
|
||||
mod.Info("Disconnecting from %s ...", per.ID())
|
||||
per.Device().CancelConnection(per)
|
||||
}(p)
|
||||
|
||||
d.Session.Events.Add("ble.device.connected", d.currDevice)
|
||||
mod.Session.Events.Add("ble.device.connected", mod.currDevice)
|
||||
|
||||
if err := p.SetMTU(500); err != nil {
|
||||
d.Warning("Failed to set MTU: %s", err)
|
||||
mod.Warning("Failed to set MTU: %s", err)
|
||||
}
|
||||
|
||||
d.Info("Connected, enumerating all the things for %s!", p.ID())
|
||||
mod.Info("Connected, enumerating all the things for %s!", p.ID())
|
||||
services, err := p.DiscoverServices(nil)
|
||||
if err != nil {
|
||||
d.Error("Error discovering services: %s", err)
|
||||
mod.Error("Error discovering services: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
d.showServices(p, services)
|
||||
mod.showServices(p, services)
|
||||
}
|
||||
|
|
|
@ -23,7 +23,7 @@ var (
|
|||
blePresentInterval = time.Duration(30) * time.Second
|
||||
)
|
||||
|
||||
func (d *BLERecon) getRow(dev *network.BLEDevice) []string {
|
||||
func (mod *BLERecon) getRow(dev *network.BLEDevice) []string {
|
||||
address := network.NormalizeMac(dev.Device.ID())
|
||||
vendor := dev.Vendor
|
||||
sinceSeen := time.Since(dev.LastSeen)
|
||||
|
@ -51,14 +51,14 @@ func (d *BLERecon) getRow(dev *network.BLEDevice) []string {
|
|||
}
|
||||
}
|
||||
|
||||
func (d *BLERecon) Show() error {
|
||||
devices := d.Session.BLE.Devices()
|
||||
func (mod *BLERecon) Show() error {
|
||||
devices := mod.Session.BLE.Devices()
|
||||
|
||||
sort.Sort(ByBLERSSISorter(devices))
|
||||
|
||||
rows := make([][]string, 0)
|
||||
for _, dev := range devices {
|
||||
rows = append(rows, d.getRow(dev))
|
||||
rows = append(rows, mod.getRow(dev))
|
||||
}
|
||||
nrows := len(rows)
|
||||
|
||||
|
@ -68,7 +68,7 @@ func (d *BLERecon) Show() error {
|
|||
tui.Table(os.Stdout, columns, rows)
|
||||
}
|
||||
|
||||
d.Session.Refresh()
|
||||
mod.Session.Refresh()
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -124,15 +124,15 @@ func parseRawData(raw []byte) string {
|
|||
return tui.Yellow(s)
|
||||
}
|
||||
|
||||
func (d *BLERecon) showServices(p gatt.Peripheral, services []*gatt.Service) {
|
||||
func (mod *BLERecon) showServices(p gatt.Peripheral, services []*gatt.Service) {
|
||||
columns := []string{"Handles", "Service > Characteristics", "Properties", "Data"}
|
||||
rows := make([][]string, 0)
|
||||
|
||||
wantsToWrite := d.writeUUID != nil
|
||||
wantsToWrite := mod.writeUUID != nil
|
||||
foundToWrite := false
|
||||
|
||||
for _, svc := range services {
|
||||
d.Session.Events.Add("ble.device.service.discovered", svc)
|
||||
mod.Session.Events.Add("ble.device.service.discovered", svc)
|
||||
|
||||
name := svc.Name()
|
||||
if name == "" {
|
||||
|
@ -152,12 +152,12 @@ func (d *BLERecon) showServices(p gatt.Peripheral, services []*gatt.Service) {
|
|||
|
||||
chars, err := p.DiscoverCharacteristics(nil, svc)
|
||||
if err != nil {
|
||||
d.Error("Error while enumerating chars for service %s: %s", svc.UUID(), err)
|
||||
mod.Error("Error while enumerating chars for service %s: %s", svc.UUID(), err)
|
||||
continue
|
||||
}
|
||||
|
||||
for _, ch := range chars {
|
||||
d.Session.Events.Add("ble.device.characteristic.discovered", ch)
|
||||
mod.Session.Events.Add("ble.device.characteristic.discovered", ch)
|
||||
|
||||
name = ch.Name()
|
||||
if name == "" {
|
||||
|
@ -168,17 +168,17 @@ func (d *BLERecon) showServices(p gatt.Peripheral, services []*gatt.Service) {
|
|||
|
||||
props, isReadable, isWritable, withResponse := parseProperties(ch)
|
||||
|
||||
if wantsToWrite && d.writeUUID.Equal(ch.UUID()) {
|
||||
if wantsToWrite && mod.writeUUID.Equal(ch.UUID()) {
|
||||
foundToWrite = true
|
||||
if isWritable {
|
||||
d.Info("Writing %d bytes to characteristics %s ...", len(d.writeData), d.writeUUID)
|
||||
mod.Info("Writing %d bytes to characteristics %s ...", len(mod.writeData), mod.writeUUID)
|
||||
} else {
|
||||
d.Warning("Attempt to write %d bytes to non writable characteristics %s ...", len(d.writeData), d.writeUUID)
|
||||
mod.Warning("Attempt to write %d bytes to non writable characteristics %s ...", len(mod.writeData), mod.writeUUID)
|
||||
}
|
||||
|
||||
err := p.WriteCharacteristic(ch, d.writeData, !withResponse)
|
||||
err := p.WriteCharacteristic(ch, mod.writeData, !withResponse)
|
||||
if err != nil {
|
||||
d.Error("Error while writing: %s", err)
|
||||
mod.Error("Error while writing: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -204,9 +204,9 @@ func (d *BLERecon) showServices(p gatt.Peripheral, services []*gatt.Service) {
|
|||
}
|
||||
|
||||
if wantsToWrite && !foundToWrite {
|
||||
d.Error("Writable characteristics %s not found.", d.writeUUID)
|
||||
mod.Error("Writable characteristics %s not found.", mod.writeUUID)
|
||||
} else {
|
||||
tui.Table(os.Stdout, columns, rows)
|
||||
d.Session.Refresh()
|
||||
mod.Session.Refresh()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,45 +22,45 @@ var defaultBLEServerOptions = []gatt.Option{
|
|||
}
|
||||
*/
|
||||
func NewBLERecon(s *session.Session) *BLERecon {
|
||||
d := &BLERecon{
|
||||
mod := &BLERecon{
|
||||
SessionModule: session.NewSessionModule("ble.recon", s),
|
||||
}
|
||||
|
||||
d.AddHandler(session.NewModuleHandler("ble.recon on", "",
|
||||
mod.AddHandler(session.NewModuleHandler("ble.recon on", "",
|
||||
"Start Bluetooth Low Energy devices discovery.",
|
||||
func(args []string) error {
|
||||
return session.ErrNotSupported
|
||||
}))
|
||||
|
||||
d.AddHandler(session.NewModuleHandler("ble.recon off", "",
|
||||
mod.AddHandler(session.NewModuleHandler("ble.recon off", "",
|
||||
"Stop Bluetooth Low Energy devices discovery.",
|
||||
func(args []string) error {
|
||||
return session.ErrNotSupported
|
||||
}))
|
||||
|
||||
return d
|
||||
return mod
|
||||
}
|
||||
|
||||
func (d BLERecon) Name() string {
|
||||
func (mod BLERecon) Name() string {
|
||||
return "ble.recon"
|
||||
}
|
||||
|
||||
func (d BLERecon) Description() string {
|
||||
func (mod BLERecon) Description() string {
|
||||
return "Bluetooth Low Energy devices discovery."
|
||||
}
|
||||
|
||||
func (d BLERecon) Author() string {
|
||||
func (mod BLERecon) Author() string {
|
||||
return "Simone Margaritelli <evilsocket@gmail.com>"
|
||||
}
|
||||
|
||||
func (d *BLERecon) Configure() (err error) {
|
||||
func (mod *BLERecon) Configure() (err error) {
|
||||
return session.ErrNotSupported
|
||||
}
|
||||
|
||||
func (d *BLERecon) Start() error {
|
||||
func (mod *BLERecon) Start() error {
|
||||
return session.ErrNotSupported
|
||||
}
|
||||
|
||||
func (d *BLERecon) Stop() error {
|
||||
func (mod *BLERecon) Stop() error {
|
||||
return session.ErrNotSupported
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue