use duration for blacklist

This commit is contained in:
Martin/Geno 2018-08-10 17:17:00 +02:00
parent df5a229fc8
commit b5989710e8
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
8 changed files with 71 additions and 56 deletions

View File

@ -6,6 +6,8 @@ webroot = "./webroot/"
secret = "passw0rd" secret = "passw0rd"
blacklist_for = "1w"
ssh_key = "~/.ssh/id_rsa" ssh_key = "~/.ssh/id_rsa"
ssh_ipaddress_prefix = "fd2f:" ssh_ipaddress_prefix = "fd2f:"
ssh_timeout = "1m" ssh_timeout = "1m"

View File

@ -53,9 +53,9 @@ func main() {
sshmanager := ssh.NewManager(config.SSHPrivateKey, config.SSHTimeout.Duration) sshmanager := ssh.NewManager(config.SSHPrivateKey, config.SSHTimeout.Duration)
nodesYanic := runtimeYanic.NewNodes(&runtimeYanic.NodesConfig{}) nodesYanic := runtimeYanic.NewNodes(&runtimeYanic.NodesConfig{})
ws := websocket.NewWebsocketServer(config.Secret, config.SSHIPAddressPrefix, db, nodesYanic) ws := websocket.NewWebsocketServer(config.Secret, config.SSHIPAddressPrefix, db, config.BlacklistFor.Duration, nodesYanic)
yanic := runtime.NewYanicDB(db, sshmanager, ws.SendNode, ws.SendStats, config.SSHIPAddressPrefix) yanic := runtime.NewYanicDB(db, sshmanager, config.BlacklistFor.Duration, ws.SendNode, ws.SendStats, config.SSHIPAddressPrefix)
if config.YanicEnable { if config.YanicEnable {
if duration := config.YanicSynchronize.Duration; duration > 0 { if duration := config.YanicSynchronize.Duration; duration > 0 {

View File

@ -17,6 +17,8 @@ type Config struct {
// path to deliver static content // path to deliver static content
Webroot string `toml:"webroot"` Webroot string `toml:"webroot"`
BlacklistFor duration.Duration `toml:"blacklist_for"`
// auth secret // auth secret
Secret string `toml:"secret"` Secret string `toml:"secret"`

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"net" "net"
"strings" "strings"
"time"
yanicData "github.com/FreifunkBremen/yanic/data" yanicData "github.com/FreifunkBremen/yanic/data"
"github.com/FreifunkBremen/yanic/lib/jsontime" "github.com/FreifunkBremen/yanic/lib/jsontime"
@ -11,19 +12,19 @@ import (
) )
type Node struct { type Node struct {
Lastseen jsontime.Time `json:"lastseen" mapstructure:"-"` Lastseen jsontime.Time `json:"lastseen" gorm:"-"`
NodeID string `json:"node_id" gorm:"primary_key" mapstructure:"node_id"` NodeID string `json:"node_id" gorm:"primary_key" mapstructure:"node_id"`
Blacklist bool `json:"blacklist"` Blacklist *time.Time `json:"-"`
Address string `json:"ip"` Address string `json:"ip"`
Hostname string `json:"hostname"` Hostname string `json:"hostname"`
HostnameRespondd string `json:"hostname_Respondd" gorm:"-"` HostnameRespondd string `json:"hostname_respondd" gorm:"-"`
Owner string `json:"owner"` Owner string `json:"owner"`
OwnerRespondd string `json:"owner_Respondd" gorm:"-"` OwnerRespondd string `json:"owner_respondd" gorm:"-"`
Location yanicData.Location `json:"location" gorm:"embedded;embedded_prefix:location_"` Location yanicData.Location `json:"location" gorm:"embedded;embedded_prefix:location_"`
LocationRespondd yanicData.Location `json:"location_Respondd" gorm:"-"` LocationRespondd yanicData.Location `json:"location_respondd" gorm:"-"`
Wireless yanicData.Wireless `json:"wireless" gorm:"embedded;embedded_prefix:wireless_"` Wireless yanicData.Wireless `json:"wireless" gorm:"embedded;embedded_prefix:wireless_"`
WirelessRespondd yanicData.Wireless `json:"wireless_Respondd" gorm:"-"` WirelessRespondd yanicData.Wireless `json:"wireless_respondd" gorm:"-"`
StatisticsRespondd struct { StatisticsRespondd struct {
Wireless yanicData.WirelessStatistics `json:"wireless"` Wireless yanicData.WirelessStatistics `json:"wireless"`
@ -58,6 +59,7 @@ func (n *Node) Update(node *yanicRuntime.Node, ipPrefix string) {
if node == nil { if node == nil {
return return
} }
n.Lastseen = jsontime.Now()
if nodeinfo := node.Nodeinfo; nodeinfo != nil { if nodeinfo := node.Nodeinfo; nodeinfo != nil {
n.HostnameRespondd = nodeinfo.Hostname n.HostnameRespondd = nodeinfo.Hostname

View File

@ -9,10 +9,10 @@ import (
"github.com/FreifunkBremen/freifunkmanager/ssh" "github.com/FreifunkBremen/freifunkmanager/ssh"
) )
func (n *Node) SSHUpdate(sshmgmt *ssh.Manager) { func (n *Node) SSHUpdate(sshmgmt *ssh.Manager) bool {
client, err := sshmgmt.ConnectTo(n.GetAddress()) client, err := sshmgmt.ConnectTo(n.GetAddress())
if err != nil { if err != nil {
return return false
} }
defer client.Close() defer client.Close()
@ -53,7 +53,7 @@ func (n *Node) SSHUpdate(sshmgmt *ssh.Manager) {
echo "radio1"; echo "radio1";
fi;`) fi;`)
if err != nil { if err != nil {
return return true
} }
radio := ssh.SSHResultToString(result) radio := ssh.SSHResultToString(result)
ch := GetChannel(n.Wireless.Channel24) ch := GetChannel(n.Wireless.Channel24)
@ -82,7 +82,7 @@ func (n *Node) SSHUpdate(sshmgmt *ssh.Manager) {
echo "radio1"; echo "radio1";
fi;`) fi;`)
if err != nil { if err != nil {
return return true
} }
radio = ssh.SSHResultToString(result) radio = ssh.SSHResultToString(result)
ch = GetChannel(n.Wireless.Channel5) ch = GetChannel(n.Wireless.Channel5)
@ -103,4 +103,5 @@ func (n *Node) SSHUpdate(sshmgmt *ssh.Manager) {
radio, n.Wireless.Channel5)) radio, n.Wireless.Channel5))
} }
} }
return true
} }

View File

@ -7,7 +7,6 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
databaseYanic "github.com/FreifunkBremen/yanic/database" databaseYanic "github.com/FreifunkBremen/yanic/database"
"github.com/FreifunkBremen/yanic/lib/jsontime"
runtimeYanic "github.com/FreifunkBremen/yanic/runtime" runtimeYanic "github.com/FreifunkBremen/yanic/runtime"
"github.com/FreifunkBremen/freifunkmanager/ssh" "github.com/FreifunkBremen/freifunkmanager/ssh"
@ -15,6 +14,7 @@ import (
type YanicDB struct { type YanicDB struct {
databaseYanic.Connection databaseYanic.Connection
blacklistFor time.Duration
db *gorm.DB db *gorm.DB
ssh *ssh.Manager ssh *ssh.Manager
sendNode func(*Node) sendNode func(*Node)
@ -22,10 +22,11 @@ type YanicDB struct {
prefix string prefix string
} }
func NewYanicDB(db *gorm.DB, ssh *ssh.Manager, sendNode func(*Node), sendStats func(*runtimeYanic.GlobalStats), prefix string) *YanicDB { func NewYanicDB(db *gorm.DB, ssh *ssh.Manager, blacklistFor time.Duration, sendNode func(*Node), sendStats func(*runtimeYanic.GlobalStats), prefix string) *YanicDB {
return &YanicDB{ return &YanicDB{
db: db, db: db,
ssh: ssh, ssh: ssh,
blacklistFor: blacklistFor,
sendNode: sendNode, sendNode: sendNode,
sendStats: sendStats, sendStats: sendStats,
prefix: prefix, prefix: prefix,
@ -33,32 +34,32 @@ func NewYanicDB(db *gorm.DB, ssh *ssh.Manager, sendNode func(*Node), sendStats f
} }
func (conn *YanicDB) InsertNode(n *runtimeYanic.Node) { func (conn *YanicDB) InsertNode(n *runtimeYanic.Node) {
nodeid := "" if n.Nodeinfo == nil {
if nodeinfo := n.Nodeinfo; nodeinfo != nil {
nodeid = nodeinfo.NodeID
} else {
return return
} }
logger := log.WithField("method", "LearnNode").WithField("node_id", nodeid) now := time.Now()
logger := log.WithField("method", "LearnNode").WithField("node_id", n.Nodeinfo.NodeID)
lNode := Node{ lNode := Node{
NodeID: nodeid, NodeID: n.Nodeinfo.NodeID,
} }
if conn.db.First(&lNode).Error == nil { if conn.db.First(&lNode).Error == nil {
lNode.Update(n, conn.prefix) lNode.Update(n, conn.prefix)
conn.db.Model(&lNode).Update(map[string]interface{}{ conn.db.Model(&lNode).Update(map[string]interface{}{"address": lNode.Address})
"Lastseen": jsontime.Now(),
//"StatsWireless": node.StatsWireless, if lNode.Blacklist != nil && lNode.Blacklist.After(now.Add(-conn.blacklistFor)) {
//"StatsClients": node.StatsClients,
"Address": lNode.Address,
})
if lNode.Blacklist {
logger.Debug("on blacklist") logger.Debug("on blacklist")
return return
} }
conn.sendNode(&lNode) conn.sendNode(&lNode)
if !lNode.CheckRespondd() { if !lNode.CheckRespondd() {
lNode.SSHUpdate(conn.ssh) 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") logger.Debug("yanic trigger sshupdate again")
}
} else { } else {
logger.Debug("yanic update") logger.Debug("yanic update")
} }
@ -68,15 +69,14 @@ func (conn *YanicDB) InsertNode(n *runtimeYanic.Node) {
if node == nil { if node == nil {
return return
} }
node.Lastseen = jsontime.Now()
_, err := conn.ssh.RunOn(node.GetAddress(), "uptime") _, err := conn.ssh.RunOn(node.GetAddress(), "uptime")
if err != nil { if err != nil {
logger.Debugf("set on blacklist: %s", err.Error()) logger.Debugf("set on blacklist: %s", err.Error())
node.Blacklist = true node.Blacklist = &now
} }
conn.db.Create(&node) conn.db.Create(&node)
if !node.Blacklist { if node.Blacklist == nil {
conn.sendNode(node) conn.sendNode(node)
} }
} }

View File

@ -1,6 +1,8 @@
package websocket package websocket
import ( import (
"time"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
wsLib "dev.sum7.eu/genofire/golang-lib/websocket" wsLib "dev.sum7.eu/genofire/golang-lib/websocket"
@ -12,16 +14,19 @@ var wifi24Channels []uint32
var wifi5Channels []uint32 var wifi5Channels []uint32
func (ws *WebsocketServer) connectHandler(logger *log.Entry, msg *wsLib.Message) error { func (ws *WebsocketServer) connectHandler(logger *log.Entry, msg *wsLib.Message) error {
//msg.From.Write(&wsLib.Message{Subject: MessageTypeStats, Body: ws.nodes.Statistics})
var nodes []*runtime.Node var nodes []*runtime.Node
var count int var count int
ws.db.Where("blacklist = false").Find(&nodes).Count(&count) now := time.Now()
ws.db.Find(&nodes).Count(&count)
ws.nodes.Lock() ws.nodes.Lock()
i := 0 i := 0
for _, node := range nodes { for _, node := range nodes {
//TODO skip blacklist if node.Blacklist != nil && node.Blacklist.After(now.Add(-ws.blacklistFor)) {
continue
}
node.Update(ws.nodes.List[node.NodeID], ws.ipPrefix) node.Update(ws.nodes.List[node.NodeID], ws.ipPrefix)
msg.From.Write(&wsLib.Message{Subject: MessageTypeNode, Body: node}) msg.From.Write(&wsLib.Message{Subject: MessageTypeNode, Body: node})
i++ i++

View File

@ -2,6 +2,7 @@ package websocket
import ( import (
"net/http" "net/http"
"time"
wsLib "dev.sum7.eu/genofire/golang-lib/websocket" wsLib "dev.sum7.eu/genofire/golang-lib/websocket"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
@ -12,6 +13,7 @@ import (
type WebsocketServer struct { type WebsocketServer struct {
nodes *runtime.Nodes nodes *runtime.Nodes
db *gorm.DB db *gorm.DB
blacklistFor time.Duration
secret string secret string
ipPrefix string ipPrefix string
@ -20,10 +22,11 @@ type WebsocketServer struct {
handlers map[string]WebsocketHandlerFunc handlers map[string]WebsocketHandlerFunc
} }
func NewWebsocketServer(secret string, ipPrefix string, db *gorm.DB, nodes *runtime.Nodes) *WebsocketServer { func NewWebsocketServer(secret string, ipPrefix string, db *gorm.DB, blacklistFor time.Duration, nodes *runtime.Nodes) *WebsocketServer {
ownWS := WebsocketServer{ ownWS := WebsocketServer{
nodes: nodes, nodes: nodes,
db: db, db: db,
blacklistFor: blacklistFor,
handlers: make(map[string]WebsocketHandlerFunc), handlers: make(map[string]WebsocketHandlerFunc),
inputMSG: make(chan *wsLib.Message), inputMSG: make(chan *wsLib.Message),
secret: secret, secret: secret,