From 607e996ad6173419c10c397a8683382bbf0f1b3d Mon Sep 17 00:00:00 2001 From: kayos Date: Fri, 28 May 2021 01:54:20 -0700 Subject: [PATCH] lots of documentation and continuing work on scan manager --- main.go | 21 +++---- src/dust/dust.go | 14 +++-- src/scanStuff/scanStuff.go | 109 +++++++++++++++++++++++-------------- 3 files changed, 87 insertions(+), 57 deletions(-) diff --git a/main.go b/main.go index ece5e2f..ccf804d 100644 --- a/main.go +++ b/main.go @@ -18,12 +18,7 @@ import ( var ScanMgr *scanStuff.Meta -//const banner = 'IOKWhOKWhOKWhMK34paE4paE4paEICAgICAgICDiloTiloTiloTiloTiloQgICAgICDigKIg4paMIOKWhCDCty4gICAgICAg4paE4paE4paMICDiloTiloTiloQgLiDiloTiloTCtyDiloTigKIg4paE4paM4paE4paE4paMICDiloTiloTiloQgLgrilpDilogg4paE4paI4paA4paEIOKWiMK34paqICAgICDigKLilojiloggIOKWqiAgICAgwrfilojilogg4paQ4paI4paI4paI4paq4paqICAgICDilojilojigKIgIOKWgOKWhC7iloDCt+KWkOKWiCDilozilqrilojilqrilojilojilozilojilojigKIgIOKWgOKWhC7iloDCtwog4paI4paI4paAwrfilpDiloDiloDiloQgIOKWhOKWiOKWgOKWhCAg4paQ4paILuKWqiDiloTilojiloDiloQg4paQ4paIIOKWjOKWkOKWjOKWkOKWiMK3IOKWhOKWiOKWgOKWhCDilojilojilqogIOKWkOKWgOKWgOKWquKWhOKWiOKWiCDiloTiloTilojilozilpDilojilozilojilojilqogIOKWkOKWgOKWgOKWquKWhArilpDilojilqrCt+KAouKWkOKWiOKAouKWiOKWjOKWkOKWiOKWjC7ilpDilowg4paQ4paI4paMwrfilpDilojilowu4paQ4paM4paI4paIIOKWiOKWiOKWjOKWkOKWiOKWjOKWkOKWiOKWjC7ilpDilozilpDilojilozilpDilozilpDilojiloTiloTilozilpDilojilojilojilozilpDilojiloTilojilozilpDilojilozilpDilozilpDilojiloTiloTilowKLuKWgCAgIC7iloAgIOKWgCDiloDilojiloTiloDilqog4paA4paA4paAICDiloDilojiloTiloDilqriloDiloAgIOKWiOKWquKWgOKWgOKWgCDiloDilojiloTiloDilqou4paA4paA4paAICDiloDiloDiloAgwrfiloDiloDiloAgIOKWgOKWgOKWgCAu4paA4paA4paAICDiloDiloDiloAgCg==' - func init() { - // print banner for style points - // dust.Splash() - flag.Parse() if *projVars.AFlag { @@ -38,15 +33,20 @@ func init() { projVars.TrackingMode = true } + // Initializing an instance of our scan manager to keep track of concurrent scans + // NOTE: Devices are kept track of per Scan via a nested map of eros.Device instances + // + // TODO: Get all of this management stuff into its own package (structs and maps and initialization of the former) + // TODO: Name these structs and maps and instances more uniformly and idiomatically ScanMgr = &scanStuff.Meta{ Count: 0, Scans: make(map[int]*scanStuff.Scan), } - // TODO: make this a commandline argument - // assure the log directory exists + // TODO: make this a command line argument var logDir string = "./.logs/" + // assure the log directory exists err := os.MkdirAll(logDir, 0755) if err != nil { panic(err.Error()) @@ -73,18 +73,15 @@ func init() { log.Debug().Msg("Logging initialized") + // see ./src/eros log.Debug().Msg("Initializing database engine") - // initialize database engine eros.Awaken() } func main() { - - var scanID int var scan *scanStuff.Scan - scanID = ScanMgr.NewScan() - scan = ScanMgr.Scans[scanID] + scan = ScanMgr.NewScan() //time.Sleep(30 * time.Millisecond) dust.Must("Scan", scan.Start()) diff --git a/src/dust/dust.go b/src/dust/dust.go index dbdc25a..8b14059 100644 --- a/src/dust/dust.go +++ b/src/dust/dust.go @@ -4,7 +4,7 @@ import ( "encoding/base64" //pt "github.com/pterm/pterm" "github.com/rs/zerolog/log" - projVars "protomolecule/src/vars" + //projVars "protomolecule/src/vars" //"bufio" //"strings" ) @@ -15,15 +15,19 @@ func Must(action string, err error) { } } -func FirstList() { - projVars.ScanList = make(map[string]string) -} +/* DID: Name devices that do not have a Local Device name + + We solve this with our scan manager by assigning them an ID as a key in our map(s) + See: eros.Device & scanStuff.Meta & scanStuff.Scan + + TODO: we should consider making a package to contain the concurrency managment mechanisms -//TODO: Name devices that do not have a Local Device name func OhNameMeZaddy() { } +*/ + func b64d(str string) string { data, err := base64.StdEncoding.DecodeString(str) if err != nil { diff --git a/src/scanStuff/scanStuff.go b/src/scanStuff/scanStuff.go index 5b39a5f..31089d1 100644 --- a/src/scanStuff/scanStuff.go +++ b/src/scanStuff/scanStuff.go @@ -1,7 +1,7 @@ package scanStuff import ( - "protomolecule/src/dust" + //"protomolecule/src/dust" "protomolecule/src/eros" projVars "protomolecule/src/vars" @@ -14,51 +14,94 @@ import ( "time" ) +/* TODO: + Meta and Scan should be in the same place as our Device struct + either in eros or in a new package + Why the Meta struct? This shit is a fucking maze. + Well in theory we will have multiple types of scans in the future, + if that's the case this is going to become necessary very quickly + + I've mocked up some commented out examples below. +*/ type Meta struct { Count int Scans map[int]*Scan + + // // // Future Concepts // // // + // BLEScans map[int]*BLEScan + // LoraScans map[int]*LoraScan + // ZigScans map[int]*ZigScan } type Scan struct { + Count int ID int Started time.Time Activity time.Time - Devices []eros.Device + Devices map[int]*eros.Device } -func (mgr *Meta) NewScan() int { - var newid int = 0 +func (m *Meta) NewScan() *Scan { + // Here we are creating an "anonymous" instance of a Scan struct + // You'll notice it doesn't contain most of the data that actually is supposed to live within that struct - if mgr.Count <= 0 { - newid = 0 - } else { - newid = mgr.Count + 1 - } + // This works because the integer ID is being used as a key that will have a global scope + // + // See: Remember function as it is defined in eros/eros.go + // Remember usage in this file (scanStuff/scanStuff.go + // + // In these examples we are sending an instance of a struct with some data to eros; allowing it to finish filling out the rest + // This can be done with just structs, but becomes problematic when trying to refer to them later + // + //TODO: implement Mutex locking from sync in most of these structs to prevent concurrent read/write operations from causing a race w̶a̶r̶ condition + // ref: https://gobyexample.com/mutexes + // ref: https://golang.org/pkg/sync/#Mutex + // ref: https://www.geeksforgeeks.org/mutex-in-golang-with-examples/ - mgr.Scans[newid] = &Scan{ + newid := len(m.Scans) - 1 + + m.Scans[newid] = &Scan{ ID: newid, Started: time.Now(), } - return newid + scan := m.Scans[newid] + scan.Devices = make(map[int]*eros.Device) + + m.Count = len(m.Scans) + return scan } +func (s *Scan) NewDevice(name string, addr string, rssi int16) *eros.Device { + newid := len(s.Devices) - 1 + s.Devices[newid] = &eros.Device{ + Name: name, + MAC: addr, + RSSInow: rssi, + } + + s.Count = len(s.Devices) + return s.Devices[newid] +} + + func (s *Scan) Start() error { s.Started = time.Now() - //create list of devices in the AO + // create list of devices in the Area of Operation log.Debug().Msg("Creating Device Map") - dust.FirstList() + log.Info().Msg("Enabling Adapter") if projVars.TrackingMode == true { log.Info().Str("Mode", "Tracking Mode Enabled").Msg("PROTO_MODE") } + if projVars.AttackMode == true { log.Info().Str("Mode", "Attack Mode Enabled").Msg("PROTO_MODE") - } - // Enable BLE interface. + + // Enable BLE interface err := projVars.ScanAdapter.Enable() if err != nil { return err @@ -66,15 +109,10 @@ func (s *Scan) Start() error { adapterId := projVars.ScanAdapter.ID - //TODO: Add device adapter MAC output - //println(bluetooth.Addresser.String()) - - //TODO: only scan a device once, skip if its in the map - // Start scanning. - // redundant(?): log.Info().Msg("scanning...") - //results := new(bluetooth.ScanResult) - log.Info().Str("ID", adapterId).Msg("Adapter in Use") + log.Info(). + Str("ID", adapterId). + Msg("Adapter in Use") projVars.ScanAdapter.Scan(func(scanAdapter *bluetooth.Adapter, result bluetooth.ScanResult) { payload := result.AdvertisementPayload @@ -86,7 +124,8 @@ func (s *Scan) Start() error { var sublog zerolog.Logger - sublog = log.With().Int("Scan_ID", s.ID).Str("LocalName", lname).Str("MAC", addr). + sublog = log.With().Int("Scan_ID", s.ID). + Str("LocalName", lname).Str("MAC", addr). Int16("RSSI", rssi).Logger() //could add -t option to enable this feature for active tracking @@ -100,12 +139,7 @@ func (s *Scan) Start() error { // Upon finding new and valid info we update the time for last activity s.Activity = time.Now() - // Initialize a Device struct to hold the incoming data for future serialization - dev := &eros.Device{ - Name: lname, - MAC: addr, - RSSInow: rssi, - } + dev := s.NewDevice(lname, addr, rssi) AdvServBuf := make([]byte, 255) @@ -126,10 +160,8 @@ func (s *Scan) Start() error { 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 + // eros.Remember places captured devices into the eros DB + // and assigns it an ID that we can attack to our Map that we defined within the Scan struct sublog.Debug().Msg("Storing data with Eros") eros.Remember(*dev) @@ -173,6 +205,7 @@ func (s *Scan) Start() error { sublog.Info().Str("Adapter", "Connection Timeout").Msg("ADAPTER_STATUS") TargetDevice.Disconnect() endTime = nil + default: sublog.Info().Str("Adapter", "Working...").Msg("ADAPTER_STATUS") } @@ -244,16 +277,12 @@ func (s *Scan) Start() error { projVars.ConnectedToTarget = false } } - //time.Sleep(5 * time.Second) - //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() + dev.MAC = result.Address.String() + dev.Name = result.LocalName() //projVars.ScanAdapter.Connect(result.Address, bluetooth.ConnectionParams{}) - //projVars.InitResults = append( projVars.ScanList[result.Address.String()], 1) - //TODO: stop scan and call bluestuff func and pass it the local name value *********** //scanAdapter.Scan()