From 17ac7397535edf29711330772536ef7c5b6bf055 Mon Sep 17 00:00:00 2001 From: Martin/Geno Date: Mon, 16 Jul 2018 13:00:37 +0200 Subject: [PATCH] add lastseen + cleanup --- controller/handler.go | 2 +- controller/main.go | 11 +++++------ database/ap.go | 19 +++++++++++++++++-- database/client.go | 26 ++++++++++++++++---------- database/main.go | 8 ++++++++ example.conf | 2 +- 6 files changed, 48 insertions(+), 20 deletions(-) diff --git a/controller/handler.go b/controller/handler.go index ac0e04a..d981efd 100644 --- a/controller/handler.go +++ b/controller/handler.go @@ -22,7 +22,7 @@ func (c *Controller) Handler(addr *net.UDPAddr, msg *data.SocketMSG) (*data.Sock Client: c.db.GetClient(msg.Client.Addr), } - if c.central || !ignore { + if !ignore { return msg, nil } return nil, nil diff --git a/controller/main.go b/controller/main.go index de15da5..b967ba5 100644 --- a/controller/main.go +++ b/controller/main.go @@ -11,11 +11,10 @@ import ( ) type Controller struct { - SendTo func(addr *net.UDPAddr, msg *data.SocketMSG) - Send func(msg *data.SocketMSG) - db *database.DB - ticker *time.Ticker - central bool + SendTo func(addr *net.UDPAddr, msg *data.SocketMSG) + Send func(msg *data.SocketMSG) + db *database.DB + ticker *time.Ticker } func NewController(db *database.DB) *Controller { @@ -33,6 +32,6 @@ func (c *Controller) Close() { func (c *Controller) Repeated() { for range c.ticker.C { - log.Debug("lerned: %d APs, %d Clients", len(c.db.APs), len(c.db.Clients)) + log.Debugf("lerned: %d APs, %d Clients", len(c.db.APs), len(c.db.Clients)) } } diff --git a/database/ap.go b/database/ap.go index 2b0712a..d9ab148 100644 --- a/database/ap.go +++ b/database/ap.go @@ -1,7 +1,22 @@ package database -import "net" +import ( + "net" + + "github.com/FreifunkBremen/yanic/lib/jsontime" +) type AP struct { - IP *net.IP + IP *net.IP `json:"ip"` + Lastseen jsontime.Time `json:"lastseen"` +} + +func (db *DB) GetClients(ap *AP) []*Client { + var clients []*Client + for _, client := range db.Clients { + if client.AP == ap { + clients = append(clients, client) + } + } + return clients } diff --git a/database/client.go b/database/client.go index a775d35..d2c8d10 100644 --- a/database/client.go +++ b/database/client.go @@ -5,20 +5,23 @@ import ( "time" // log "github.com/sirupsen/logrus" + "github.com/FreifunkBremen/yanic/lib/jsontime" "dev.sum7.eu/wifictld/analyzer/data" ) type Client struct { - Addr net.HardwareAddr - Time time.Time - TryProbe uint16 - TryAuth uint16 - Connected bool - Authed bool - FreqHighest uint16 - SignalLowFreq int16 - SignalHighFreq int16 + AP *AP `json:"-"` + APAddr string `json:"ap"` + Addr net.HardwareAddr `json:"-"` + TryProbe uint16 `json:"try_probe"` + TryAuth uint16 `json:"try_auth"` + Connected bool `json:"connected"` + Authed bool `json:"authed"` + FreqHighest uint16 `json:"freq_highest"` + SignalLowFreq int16 `json:"signal_low_freq"` + SignalHighFreq int16 `json:"signal_high_freq"` + Lastseen jsontime.Time `json:"lastseen"` } func (db *DB) LearnClient(apIP net.IP, clientWifictl *data.WifiClient) bool { @@ -32,6 +35,7 @@ func (db *DB) LearnClient(apIP net.IP, clientWifictl *data.WifiClient) bool { db.APs[apAddr] = ap } ap.IP = &apIP + ap.Lastseen = jsontime.Now() // learn client clientAddr := clientWifictl.Addr.String() @@ -42,7 +46,9 @@ func (db *DB) LearnClient(apIP net.IP, clientWifictl *data.WifiClient) bool { } db.Clients[clientAddr] = client } - client.Time = time.Now() + client.Lastseen = jsontime.Now() + client.AP = ap + client.APAddr = apAddr if client.FreqHighest < clientWifictl.FreqHighest { ret = (client.FreqHighest != 0) diff --git a/database/main.go b/database/main.go index 6d06000..77e0cdd 100644 --- a/database/main.go +++ b/database/main.go @@ -1,6 +1,7 @@ package database import ( + "net" "time" log "github.com/sirupsen/logrus" @@ -23,6 +24,13 @@ func NewDB(path string) *DB { file.ReadJSON(path, db) + for addr, client := range db.Clients { + client.Addr, _ = net.ParseMAC(addr) + if ap, ok := db.APs[client.APAddr]; ok { + client.AP = ap + } + } + db.worker = worker.NewWorker(time.Minute, func() { file.SaveJSON(path, db) log.Debug("save db state") diff --git a/example.conf b/example.conf index f7151a0..999bf8c 100644 --- a/example.conf +++ b/example.conf @@ -1,6 +1,6 @@ state_path = "/tmp/wifictld.json" [[interfaces]] -ifname = "mmfd0" +ifname = "wlp4s0" #port = 1000 #ip_address = "ff02::31f1"