2018-11-14 14:24:48 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-11-14 16:16:17 +00:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"encoding/csv"
|
2018-11-14 14:24:48 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2018-11-14 22:14:43 +00:00
|
|
|
"log"
|
2018-11-28 20:56:16 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-11-14 22:14:43 +00:00
|
|
|
"strconv"
|
2018-11-14 14:24:48 +00:00
|
|
|
"strings"
|
2018-11-14 22:14:43 +00:00
|
|
|
"time"
|
2018-11-14 14:24:48 +00:00
|
|
|
)
|
|
|
|
|
2018-11-14 22:14:43 +00:00
|
|
|
type WifiClient struct {
|
|
|
|
MACAdress string
|
|
|
|
FirstSeen time.Time
|
|
|
|
LastSeen time.Time
|
|
|
|
Power int64
|
|
|
|
Packets int64
|
|
|
|
}
|
|
|
|
|
2018-11-28 20:56:16 +00:00
|
|
|
const CSVDumps = `/home/konrad/go/src/git.kolaente.de/konrad/wifi-statistics`
|
2018-11-14 22:14:43 +00:00
|
|
|
const TheClientCSVHeader = `Station MAC, First time seen, Last time seen, Power, # packets, BSSID, Probed ESSIDs`
|
|
|
|
const SecondsUntilInactive = 120
|
|
|
|
|
2018-11-14 14:24:48 +00:00
|
|
|
func main() {
|
|
|
|
|
2018-11-28 21:59:21 +00:00
|
|
|
clients := ParseCSVDumps(CSVDumps)
|
2018-11-14 22:14:43 +00:00
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2018-11-28 21:59:21 +00:00
|
|
|
func ParseCSVDumps(pathToDumps string) (clients []*WifiClient) {
|
2018-11-28 20:56:16 +00:00
|
|
|
err := filepath.Walk(pathToDumps, func(dumpPath string, info os.FileInfo, err error) error {
|
2018-11-28 20:22:02 +00:00
|
|
|
if err != nil {
|
2018-11-28 20:56:16 +00:00
|
|
|
return err
|
2018-11-28 20:22:02 +00:00
|
|
|
}
|
|
|
|
|
2018-11-28 20:56:16 +00:00
|
|
|
// Only csv files
|
|
|
|
if info.IsDir() || filepath.Ext(dumpPath) != ".csv" {
|
|
|
|
return nil
|
2018-11-14 22:14:43 +00:00
|
|
|
}
|
|
|
|
|
2018-11-28 20:56:16 +00:00
|
|
|
bs, err := ioutil.ReadFile(dumpPath)
|
2018-11-14 22:14:43 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2018-11-28 20:56:16 +00:00
|
|
|
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)
|
|
|
|
}
|
2018-11-14 22:14:43 +00:00
|
|
|
|
2018-11-28 20:56:16 +00:00
|
|
|
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,
|
|
|
|
})
|
2018-11-14 16:16:17 +00:00
|
|
|
}
|
2018-11-14 22:14:43 +00:00
|
|
|
|
2018-11-28 20:56:16 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2018-11-14 16:16:17 +00:00
|
|
|
}
|
2018-11-14 14:24:48 +00:00
|
|
|
|
2018-11-14 22:14:43 +00:00
|
|
|
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
|
|
|
|
}
|
2018-11-14 14:24:48 +00:00
|
|
|
|
2018-11-14 22:14:43 +00:00
|
|
|
return false
|
2018-11-14 14:24:48 +00:00
|
|
|
}
|