initial adjustments for a proper concurrency model

This commit is contained in:
kayos 2021-05-11 02:20:41 -07:00
parent 5b6a18905b
commit 62d359446d
3 changed files with 117 additions and 98 deletions

52
main.go

@ -1,24 +1,31 @@
package main
import (
"flag"
//"flag"
"os"
"protomolecule/src/dust"
"protomolecule/src/eros"
"protomolecule/src/scanStuff"
"time"
projVars "protomolecule/src/vars"
//projVars "protomolecule/src/vars"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"tinygo.org/x/bluetooth"
//"tinygo.org/x/bluetooth"
)
var ScanMgr *scanStuff.Meta
func init() {
// initialize database engine
eros.Awaken()
ScanMgr = &scanStuff.Meta{
Count: 0,
Scans: make(map[int]*scanStuff.Scan),
}
// TODO: make this a commandline argument
// assure the log directory exists
var logDir string = "./.logs/"
@ -55,42 +62,15 @@ func init() {
}
func main() {
values := flag.Args()
var scanID int
var scan *scanStuff.Scan
scanStuff.Scanners()
//values := flag.Args()
log.Info().Strs("target", values).Msg("Searching")
scanID = ScanMgr.NewScan()
scan = ScanMgr.Scans[scanID]
/*
err := adapter.Enable()
if err != nil {
log.Fatal().Err(err).Msg("BLE stack unstable, could not enable")
return
}
*/
//var foundDevice bluetooth.ScanResult
//turn the stuff on
dust.Must("BLE stack", projVars.ScanAdapter.Enable())
log.Info().Msg("Scanning")
err := projVars.ScanAdapter.Scan(func(adapter *bluetooth.Adapter, result bluetooth.ScanResult) {
if result.Address.String() == *projVars.Target {
log.Info().Str("address", result.Address.String()).
Int16("RSSI", result.RSSI).Str("LocalName", result.LocalName())
}
/*
if err != nil {
log.Error().Err(err).Msg("could not enable the stack", err.Error())
return
}
*/
})
dust.Must("Scan", err)
dust.Must("Scan", scan.Start())
/*
connectToStuff := func(addr bluetooth.Addresser, device *bluetooth.Device) {

@ -9,11 +9,31 @@ package eros
import (
"encoding/json"
"time"
//"errors"
"github.com/prologic/bitcask"
//projVars "protomolecule/src/vars"
)
// Device will hold details about the discoverd device
type Device struct {
Name string
MAC string
// Services - see Service struct
Services []Service
// Discovered - timestamp from when the device was first discovered
Discovered time.Time
// Seen - timestamp from when the device was last seen
Seen time.Time
}
// Service - BLE service details to be marshalled into json before stored in bitcask
type Service struct {
Name string
UUID string
Category string
}
// deviceDb will hold details about devices discovered
var deviceDb *bitcask.Bitcask
@ -25,30 +45,6 @@ var err error
// DataDir - should be defined by config or cmd flag
var DataDir string = "./.eros-data/"
// Device - we use this to nest the populated Details struct associated with the MAC
type Device struct {
MAC string
Info Details
// Category potentially used for browsing devices or finding exploits
//// Category string
}
// Details - BLE device details to be marshalled into json before stored in bitcask
type Details struct {
LocalName string
Advertisement []byte
Services []Service
}
// Service - BLE service details to be marshalled into json before stored in bitcask
type Service struct {
Name string
UUID string
Category string
}
// Exploit - BLE service exploit details to be marshalled into json before stored in bitcask
type Exploit struct {
Name string
@ -83,28 +79,32 @@ func Awaken() {
}
}
// exists - check if a device is present in the Database
func exists(mac string) bool {
if deviceDb.Has([]byte(mac)) {
return true
}
return false
}
// Remember - store device details into the database
func Remember(mac string, name string, adv []byte /*, services []string*/) error {
func Remember(dev Device) error {
var err error
details := Details{
LocalName: name,
Advertisement: adv,
//Services: services,
if !exists(dev.MAC) {
dev.Discovered = time.Now()
}
device := Device{
MAC: mac,
Info: details,
}
dev.Seen = time.Now()
var jsonData []byte
jsonData, err = json.Marshal(device)
jsonData, err = json.Marshal(dev)
if err != nil {
return err
}
err = deviceDb.Put([]byte(mac), jsonData)
err = deviceDb.Put([]byte(dev.MAC), jsonData)
return err
}
@ -116,7 +116,9 @@ func Recall(mac string) (Device, error) {
var member Device
bytes, err = deviceDb.Get([]byte(mac))
if err != nil {
member = Device{}
return member, err
}

@ -8,21 +8,54 @@ import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"time"
"tinygo.org/x/bluetooth"
)
func Scanners() {
type Meta struct {
Count int
Scans map[int]*Scan
}
type Scan struct {
ID int
Started time.Time
Activity time.Time
Devices []eros.Device
}
func (mgr *Meta) NewScan() int {
var newid int = 0
if mgr.Count <= 0 {
newid = 0
} else {
newid = mgr.Count+1
}
mgr.Scans[newid] = &Scan{
ID: newid,
Started: time.Now(),
}
return newid
}
func (s *Scan) Start() error {
s.Started = time.Now()
//create list of devices in the AO
log.Debug().Msg("Creating Device Map")
dust.FirstList()
log.Info().Msg("Enabling Adapter")
// Enable BLE interface.
dust.Must("enable BLE stack", projVars.ScanAdapter.Enable())
err := projVars.ScanAdapter.Enable()
if err != nil {
return err
}
adapterId := projVars.ScanAdapter.ID
//TODO: Add device adapter MAC output
//println(bluetooth.Addresser.String())
@ -37,43 +70,49 @@ func Scanners() {
payload := result.AdvertisementPayload
addr := result.Address.String()
lname := result.LocalName()
adbytes := payload.Bytes()
//adbytes := payload.Bytes()
uuids := payload.ServiceUUIDOut()
rssi := result.RSSI
var sublog zerolog.Logger
//Trying to extract the service UUIDs from advertisment packet
//srvcPld := result.AdvertisementPayload.HasServiceUUID(projVars.SonosScanTest)
// ** List of common Service UUIDS to search for **
//bluetooth.ServiceUUIDDeviceInformation
//bluetooth.ServiceUUIDGenericAttribute
sublog = log.With().Str("LocalName", lname).Str("MAC", addr).
sublog = log.With().Int("Scan_ID", s.ID).Str("LocalName", lname).Str("MAC", addr).
Int16("RSSI", rssi).Logger()
// Skipping duplicate results
if lname == projVars.ScanList[result.Address.String()] {
//log.Debug().Str("lname", lname).Str("device_address", projVars.ScanList[device.Address.String()]).Msg("Skipping existing device")
return
}
//println(result.AdvertisementPayload.ServiceUUIDOut())
// Upon finding new and valid info we update the time for last activity
s.Activity = time.Now()
sublog.Info().Msg("NEW_DEVICE")
for _, uuid := range uuids {
sublog.Info().Str("UUID", uuid.String()).Msg("SERVICE")
// Initialize a Device struct to hold the incoming data for future serialization
dev := &eros.Device {
Name: lname,
MAC: addr,
}
//bluetooth.AdvertisementPayload.Bytes()
//println(uuids)
// Record all the services advertised, append them into the nested struct within Device
sublog.Debug().Msg("DEVICE_DISCOVERED")
for _, uuid := range uuids {
svc := &eros.Service{
UUID: uuid.String(),
}
//println(srvcPld)
dev.Services = append(dev.Services, *svc)
sublog.Debug().Str("UUID", svc.UUID).Msg("SERVICE_DISCOVERED")
}
// Append our defined device to the list of devices in the Scan struct
s.Devices = append(s.Devices, *dev)
//places captured devices into the eros DB
//log.Debug().Msg("Storing data with Eros")
eros.Remember(result.Address.String(), lname, adbytes)
sublog.Debug().Msg("Storing data with Eros")
eros.Remember(*dev)
//TODO: normalize this with the newly created ScanMgr
//TODO: create localname fort devices that dont broadcast one * dust.OhNameMeZaddy() *
projVars.ScanList[result.Address.String()] = result.LocalName()
@ -85,9 +124,6 @@ func Scanners() {
*/
//projVars.ScanAdapter.Connect(result.Address, bluetooth.ConnectionParams{})
//fucking sanity check
//i := projVars.ScanList[result.Address.String()]
//projVars.InitResults = append( projVars.ScanList[result.Address.String()], 1)
//test for data being placed
@ -103,5 +139,6 @@ func Scanners() {
//dust.Must("start scan", err)
return nil
}