From 155b8fd1600d24a62e8fc3e336efaca53b9653e5 Mon Sep 17 00:00:00 2001 From: konrad Date: Thu, 29 Nov 2018 00:57:16 +0100 Subject: [PATCH] Restructured --- main.go | 134 +----------------------- pkg/config/config.go | 24 +++++ pkg/models/wifi_client.go | 46 ++++++++ pkg/processing/parse_dumps.go | 95 +++++++++++++++++ pkg/processing/run_airodump.go | 33 ++++++ pkg/processing/update_active_clients.go | 42 ++++++++ pkg/utils/parse_unixtime.go | 32 ++++++ 7 files changed, 275 insertions(+), 131 deletions(-) create mode 100644 pkg/config/config.go create mode 100644 pkg/models/wifi_client.go create mode 100644 pkg/processing/parse_dumps.go create mode 100644 pkg/processing/run_airodump.go create mode 100644 pkg/processing/update_active_clients.go create mode 100644 pkg/utils/parse_unixtime.go diff --git a/main.go b/main.go index 5b7787d..d4193b5 100644 --- a/main.go +++ b/main.go @@ -18,62 +18,16 @@ package main import ( - "bufio" - "bytes" - "encoding/csv" - "fmt" - "io/ioutil" - "log" + "git.kolaente.de/sofaraum/client/pkg/processing" "os" - "os/exec" - "path/filepath" - "strconv" - "strings" - "time" ) -type WifiClient struct { - MACAdress string - FirstSeen time.Time - LastSeen time.Time - Power int64 - Packets int64 -} - -const TheClientCSVHeader = `Station MAC, First time seen, Last time seen, Power, # packets, BSSID, Probed ESSIDs` -const CSVDumps = `/home/konrad/go/src/git.kolaente.de/sofaraum/client` -const SecondsUntilInactive = 120 -const UpdateSecondsInterval = 2 -const WifiInterface = `wlp59s0` - func main() { stop := make(chan int) - go func() { - err := exec.Command("/bin/bash", []string{"-c", "airodump-ng " + WifiInterface + " -w " + CSVDumps + "/dump --output-format csv"}...).Run() - if err != nil { - log.Println("Could not run airodump-ng. Please make sure it is installed and you have sufficent permissions. ", err) - stop <- 1 - } - }() + go processing.RunAirodumpNG(stop) - go func() { - for { - time.Sleep(UpdateSecondsInterval * time.Second) - - clients := ParseCSVDumps(CSVDumps) - - var activeClients int64 - for _, c := range clients { - //fmt.Println(fmt.Sprintf("Mac: %s | First seen: %s | Last seen: %s | Power: %d | Packets: %d | Active: %t", c.MACAdress, c.FirstSeen.String(), c.LastSeen.String(), c.Power, c.Packets, c.isActive())) - if c.isActive() { - activeClients++ - } - } - fmt.Println("Active Clients:", activeClients) - fmt.Println("Total Clients:", len(clients)) - } - }() + go processing.UpdateActiveClients() for exit := range stop { if exit == 0 { @@ -82,85 +36,3 @@ func main() { os.Exit(exit) } } - -func ParseCSVDumps(pathToDumps string) (clients []*WifiClient) { - err := filepath.Walk(pathToDumps, func(dumpPath string, info os.FileInfo, err error) error { - if err != nil { - return err - } - - // Only csv files - if info.IsDir() || filepath.Ext(dumpPath) != ".csv" { - return nil - } - - bs, err := ioutil.ReadFile(dumpPath) - if err != nil { - log.Fatal(err) - } - all := string(bs) - i := 0 - i = strings.Index(all, TheClientCSVHeader) - arefun := all[i+len(TheClientCSVHeader)+1:] - arefun = strings.Replace(arefun, " ", "", -1) - - scanner := bufio.NewScanner(strings.NewReader(arefun)) - for scanner.Scan() { - r := csv.NewReader(bytes.NewReader(scanner.Bytes())) - record, err := r.Read() - if err != nil { - if err.Error() == "EOF" { - continue - } - log.Fatal(err) - } - - power, err := strconv.ParseInt(record[3], 10, 64) - if err != nil { - log.Fatal(err) - } - - packets, err := strconv.ParseInt(record[4], 10, 64) - if err != nil { - log.Fatal(err) - } - - clients = append(clients, &WifiClient{ - MACAdress: record[0], - FirstSeen: parseDateToUnix(record[1]), - LastSeen: parseDateToUnix(record[2]), - Power: power, - Packets: packets, - }) - } - - return nil - }) - - if err != nil { - log.Fatal(err) - } - - return -} - -func parseDateToUnix(date string) (unix time.Time) { - unix, err := time.Parse("2006-01-0215:04:05", date) - if err != nil { - log.Fatal(err) - } - return -} - -func (c *WifiClient) isActive() bool { - // Should normally not be set here, should take the system time -> see below - current := time.Date(2018, 11, 14, 14, 31, 0, 0, &time.Location{}) - diff := current.Sub(c.LastSeen) - - //diff := time.Since(c.LastSeen) - if diff < SecondsUntilInactive*time.Second { - return true - } - - return false -} diff --git a/pkg/config/config.go b/pkg/config/config.go new file mode 100644 index 0000000..d1eec2a --- /dev/null +++ b/pkg/config/config.go @@ -0,0 +1,24 @@ +// Sofaraum client is the client software which collects statistics about +// wifi devices nearby and then sends them to the Sofaraum Server. +// Copyright (c) 2018. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +package config + +const TheClientCSVHeader = `Station MAC, First time seen, Last time seen, Power, # packets, BSSID, Probed ESSIDs` +const CSVDumps = `/home/konrad/go/src/git.kolaente.de/sofaraum/client` +const SecondsUntilInactive = 120 +const UpdateSecondsInterval = 2 +const WifiInterface = `wlp59s0` diff --git a/pkg/models/wifi_client.go b/pkg/models/wifi_client.go new file mode 100644 index 0000000..54633fe --- /dev/null +++ b/pkg/models/wifi_client.go @@ -0,0 +1,46 @@ +// Sofaraum client is the client software which collects statistics about +// wifi devices nearby and then sends them to the Sofaraum Server. +// Copyright (c) 2018. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +package models + +import ( + "git.kolaente.de/sofaraum/client/pkg/config" + "time" +) + +// WifiClient holds informations about a wifi client +type WifiClient struct { + MACAdress string + FirstSeen time.Time + LastSeen time.Time + Power int64 + Packets int64 +} + +// IsActive checks whether or the client has been seen within a configurable amount of time +func (c *WifiClient) IsActive() bool { + // Should normally not be set here, should take the system time -> see below + current := time.Date(2018, 11, 14, 14, 31, 0, 0, &time.Location{}) + diff := current.Sub(c.LastSeen) + + //diff := time.Since(c.LastSeen) + if diff < config.SecondsUntilInactive*time.Second { + return true + } + + return false +} diff --git a/pkg/processing/parse_dumps.go b/pkg/processing/parse_dumps.go new file mode 100644 index 0000000..4c43a14 --- /dev/null +++ b/pkg/processing/parse_dumps.go @@ -0,0 +1,95 @@ +// Sofaraum client is the client software which collects statistics about +// wifi devices nearby and then sends them to the Sofaraum Server. +// Copyright (c) 2018. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +package processing + +import ( + "bufio" + "bytes" + "encoding/csv" + "git.kolaente.de/sofaraum/client/pkg/config" + "git.kolaente.de/sofaraum/client/pkg/models" + "git.kolaente.de/sofaraum/client/pkg/utils" + "io/ioutil" + "log" + "os" + "path/filepath" + "strconv" + "strings" +) + +// ParseCSVDumps extracts data from airdump-ng's csv files in a format we can handle +func ParseCSVDumps(pathToDumps string) (clients []*models.WifiClient) { + err := filepath.Walk(pathToDumps, func(dumpPath string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + // Only csv files + if info.IsDir() || filepath.Ext(dumpPath) != ".csv" { + return nil + } + + bs, err := ioutil.ReadFile(dumpPath) + if err != nil { + log.Fatal(err) + } + all := string(bs) + i := 0 + i = strings.Index(all, config.TheClientCSVHeader) + arefun := all[i+len(config.TheClientCSVHeader)+1:] + arefun = strings.Replace(arefun, " ", "", -1) + + scanner := bufio.NewScanner(strings.NewReader(arefun)) + for scanner.Scan() { + r := csv.NewReader(bytes.NewReader(scanner.Bytes())) + record, err := r.Read() + if err != nil { + if err.Error() == "EOF" { + continue + } + log.Fatal(err) + } + + power, err := strconv.ParseInt(record[3], 10, 64) + if err != nil { + log.Fatal(err) + } + + packets, err := strconv.ParseInt(record[4], 10, 64) + if err != nil { + log.Fatal(err) + } + + clients = append(clients, &models.WifiClient{ + MACAdress: record[0], + FirstSeen: utils.ParseDateToUnix(record[1]), + LastSeen: utils.ParseDateToUnix(record[2]), + Power: power, + Packets: packets, + }) + } + + return nil + }) + + if err != nil { + log.Fatal(err) + } + + return +} diff --git a/pkg/processing/run_airodump.go b/pkg/processing/run_airodump.go new file mode 100644 index 0000000..ddcf6a6 --- /dev/null +++ b/pkg/processing/run_airodump.go @@ -0,0 +1,33 @@ +// Sofaraum client is the client software which collects statistics about +// wifi devices nearby and then sends them to the Sofaraum Server. +// Copyright (c) 2018. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +package processing + +import ( + "git.kolaente.de/sofaraum/client/pkg/config" + "log" + "os/exec" +) + +// RunAirodumpNG does what it says +func RunAirodumpNG(stop chan int) { + err := exec.Command("/bin/bash", []string{"-c", "airodump-ng " + config.WifiInterface + " -w " + config.CSVDumps + "/dump --output-format csv"}...).Run() + if err != nil { + log.Println("Could not run airodump-ng. Please make sure it is installed and you have sufficent permissions. ", err) + stop <- 1 + } +} diff --git a/pkg/processing/update_active_clients.go b/pkg/processing/update_active_clients.go new file mode 100644 index 0000000..7b6c7f6 --- /dev/null +++ b/pkg/processing/update_active_clients.go @@ -0,0 +1,42 @@ +// Sofaraum client is the client software which collects statistics about +// wifi devices nearby and then sends them to the Sofaraum Server. +// Copyright (c) 2018. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +package processing + +import ( + "fmt" + "git.kolaente.de/sofaraum/client/pkg/config" + "time" +) + +func UpdateActiveClients() { + for { + time.Sleep(config.UpdateSecondsInterval * time.Second) + + clients := ParseCSVDumps(config.CSVDumps) + + var activeClients int64 + for _, c := range clients { + //fmt.Println(fmt.Sprintf("Mac: %s | First seen: %s | Last seen: %s | Power: %d | Packets: %d | Active: %t", c.MACAdress, c.FirstSeen.String(), c.LastSeen.String(), c.Power, c.Packets, c.isActive())) + if c.IsActive() { + activeClients++ + } + } + fmt.Println("Active Clients:", activeClients) + fmt.Println("Total Clients:", len(clients)) + } +} diff --git a/pkg/utils/parse_unixtime.go b/pkg/utils/parse_unixtime.go new file mode 100644 index 0000000..27d3a5e --- /dev/null +++ b/pkg/utils/parse_unixtime.go @@ -0,0 +1,32 @@ +// Sofaraum client is the client software which collects statistics about +// wifi devices nearby and then sends them to the Sofaraum Server. +// Copyright (c) 2018. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +package utils + +import ( + "log" + "time" +) + +// ParseDateToUnix makes a go date from a unix timestamp +func ParseDateToUnix(date string) (unix time.Time) { + unix, err := time.Parse("2006-01-0215:04:05", date) + if err != nil { + log.Fatal(err) + } + return +}