[BUGFIX] store lastseen and not forward respondd of blacklisted nodes to websocket

This commit is contained in:
Martin/Geno 2019-06-05 21:37:43 +02:00 committed by genofire
parent b27c452fab
commit 63087001d3
4 changed files with 19 additions and 12 deletions

View File

@ -7,7 +7,6 @@ import (
"time"
yanicData "github.com/FreifunkBremen/yanic/data"
"github.com/FreifunkBremen/yanic/lib/jsontime"
yanicRuntime "github.com/FreifunkBremen/yanic/runtime"
)
@ -57,10 +56,10 @@ func GetWirelessSettings(node *yanicRuntime.Node) *WirelessSettings {
}
type Node struct {
Lastseen jsontime.Time `json:"lastseen" mapstructure:"-" gorm:"-"`
NodeID string `json:"node_id" gorm:"primary_key" mapstructure:"node_id"`
Blacklist *time.Time `json:"-"`
Address string `json:"ip"`
Lastseen time.Time `json:"lastseen" mapstructure:"-" gorm:"lastseen"`
NodeID string `json:"node_id" gorm:"primary_key" mapstructure:"node_id"`
Blacklist *time.Time `json:"-"`
Address string `json:"ip"`
Hostname string `json:"hostname"`
HostnameRespondd string `json:"hostname_respondd" gorm:"-"`
@ -104,7 +103,7 @@ func (n *Node) Update(node *yanicRuntime.Node, ipPrefix string) {
if node == nil {
return
}
n.Lastseen = jsontime.Now()
n.Lastseen = time.Now()
if nodeinfo := node.Nodeinfo; nodeinfo != nil {
n.HostnameRespondd = nodeinfo.Hostname

View File

@ -68,6 +68,7 @@ func (pinger *Pinger) Stop() {
func (pinger *Pinger) run() {
result := &data.PingResult{}
now := time.Now()
before := now.Add(-pinger.blacklistFor)
count := 0
var nodes []*Node
@ -80,7 +81,7 @@ func (pinger *Pinger) run() {
for _, node := range nodes {
go func(n *Node) {
defer wg.Done()
if n.Blacklist.After(now.Add(-pinger.blacklistFor)) {
if n.Blacklist != nil && n.Blacklist.After(before) {
return
}

View File

@ -46,23 +46,26 @@ func (conn *YanicDB) InsertNode(n *runtimeYanic.Node) {
}
if conn.db.First(&lNode).Error == nil {
lNode.Update(n, conn.prefix)
conn.db.Model(&lNode).Update(map[string]interface{}{"address": lNode.Address})
conn.db.Model(&lNode).Update(map[string]interface{}{
"address": lNode.Address,
"lastseen": now,
})
if lNode.Blacklist != nil && lNode.Blacklist.After(now.Add(-conn.blacklistFor)) {
logger.Debug("on blacklist")
return
}
conn.sendNode(&lNode)
if !lNode.CheckRespondd() {
if !lNode.SSHUpdate(conn.ssh) {
conn.db.Model(&lNode).Update(map[string]interface{}{"blacklist": &now})
logger.Warn("yanic trigger sshupdate failed - set blacklist")
} else {
logger.Debug("yanic trigger sshupdate again")
return
}
logger.Debug("yanic trigger sshupdate again")
} else {
logger.Debug("yanic update")
}
conn.sendNode(&lNode)
return
}
node := NewNode(n, conn.prefix)

View File

@ -18,13 +18,17 @@ func (ws *WebsocketServer) connectHandler(logger *log.Entry, msg *wsLib.Message)
var count int
now := time.Now()
before := now.Add(-ws.blacklistFor)
ws.db.Find(&nodes).Count(&count)
ws.nodes.Lock()
i := 0
for _, node := range nodes {
if node.Blacklist != nil && node.Blacklist.After(now.Add(-ws.blacklistFor)) {
if node.Blacklist != nil && node.Blacklist.After(before) {
continue
}
if node.Lastseen.Before(before) {
continue
}
node.Update(ws.nodes.List[node.NodeID], ws.ipPrefix)