client/main.go

167 lines
4.1 KiB
Go

// 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 <https://www.gnu.org/licenses/>.
package main
import (
"bufio"
"bytes"
"encoding/csv"
"fmt"
"io/ioutil"
"log"
"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 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))
}
}()
for exit := range stop {
if exit == 0 {
continue
}
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
}