2018-11-28 22:20:12 +00:00
|
|
|
// 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/>.
|
|
|
|
|
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"
|
2018-11-28 23:20:03 +00:00
|
|
|
"os/exec"
|
2018-11-28 20:56:16 +00:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
|
|
|
const TheClientCSVHeader = `Station MAC, First time seen, Last time seen, Power, # packets, BSSID, Probed ESSIDs`
|
2018-11-28 23:20:03 +00:00
|
|
|
const CSVDumps = `/home/konrad/go/src/git.kolaente.de/sofaraum/client`
|
2018-11-14 22:14:43 +00:00
|
|
|
const SecondsUntilInactive = 120
|
2018-11-28 23:20:03 +00:00
|
|
|
const UpdateSecondsInterval = 2
|
|
|
|
const WifiInterface = `wlp59s0`
|
2018-11-14 22:14:43 +00:00
|
|
|
|
2018-11-14 14:24:48 +00:00
|
|
|
func main() {
|
|
|
|
|
2018-11-28 23:20:03 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}()
|
2018-11-14 22:14:43 +00:00
|
|
|
|
2018-11-28 23:20:03 +00:00
|
|
|
for exit := range stop {
|
|
|
|
if exit == 0 {
|
|
|
|
continue
|
2018-11-14 22:14:43 +00:00
|
|
|
}
|
2018-11-28 23:20:03 +00:00
|
|
|
os.Exit(exit)
|
2018-11-14 22:14:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|