diff --git a/main.go b/main.go index 20c9bde..29efe12 100644 --- a/main.go +++ b/main.go @@ -7,7 +7,8 @@ import ( "fmt" "io/ioutil" "log" - "os/exec" + "os" + "path/filepath" "strconv" "strings" "time" @@ -21,14 +22,13 @@ type WifiClient struct { Packets int64 } -const CSVDump = `dump.csv` +const CSVDumps = `/home/konrad/go/src/git.kolaente.de/konrad/wifi-statistics` const TheClientCSVHeader = `Station MAC, First time seen, Last time seen, Power, # packets, BSSID, Probed ESSIDs` const SecondsUntilInactive = 120 func main() { - clients := ParseCSVDump(CSVDump) - //clients := GetClientsFromBytes() + clients := ParseCSVDump(CSVDumps) var activeClients int64 for _, c := range clients { @@ -41,74 +41,63 @@ func main() { fmt.Println("Total Clients:", len(clients)) } -func GetClientsFromBytes() (clients []*WifiClient) { - - // Tooo much hassle, just output a csv dump and parse that and delete it afterwards should be good enough - - // -u für delay - argstr := []string{"-c", "airodump-ng wlp59s0 -u 9"} - command := exec.Command("/bin/bash", argstr...) - var buf bytes.Buffer - command.Stdout = &buf - - if err := command.Start(); err != nil { - log.Fatal(err) - } - - timer := time.AfterFunc(10 *time.Second, func() { - err := command.Process.Kill() +func ParseCSVDump(pathToDumps string) (clients []*WifiClient) { + err := filepath.Walk(pathToDumps, func(dumpPath string, info os.FileInfo, err error) error { if err != nil { - panic(err) + 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 }) - command.Wait() - timer.Stop() - ioutil.WriteFile("dump", buf.Bytes(), 0644) - - return -} - -func ParseCSVDump(pathToDump string) (clients []*WifiClient) { - bs, err := ioutil.ReadFile(pathToDump) 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 }