Restructured

This commit is contained in:
konrad 2018-11-29 00:57:16 +01:00
parent ae7734248d
commit 155b8fd160
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
7 changed files with 275 additions and 131 deletions

134
main.go
View File

@ -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
}

24
pkg/config/config.go Normal file
View File

@ -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 <https://www.gnu.org/licenses/>.
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`

46
pkg/models/wifi_client.go Normal file
View File

@ -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 <https://www.gnu.org/licenses/>.
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
}

View File

@ -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 <https://www.gnu.org/licenses/>.
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
}

View File

@ -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 <https://www.gnu.org/licenses/>.
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
}
}

View File

@ -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 <https://www.gnu.org/licenses/>.
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))
}
}

View File

@ -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 <https://www.gnu.org/licenses/>.
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
}