Skip to content

Commit

Permalink
use duration for blacklist
Browse files Browse the repository at this point in the history
  • Loading branch information
genofire committed Aug 10, 2018
1 parent df5a229 commit 6dde5bd
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 46 deletions.
2 changes: 2 additions & 0 deletions config_example.conf
Expand Up @@ -6,6 +6,8 @@ webroot = "./webroot/"

secret = "passw0rd"

blacklist_for = "1w"

ssh_key = "~/.ssh/id_rsa"
ssh_ipaddress_prefix = "fd2f:"
ssh_timeout = "1m"
Expand Down
4 changes: 2 additions & 2 deletions main.go
Expand Up @@ -53,9 +53,9 @@ func main() {
sshmanager := ssh.NewManager(config.SSHPrivateKey, config.SSHTimeout.Duration)
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 duration := config.YanicSynchronize.Duration; duration > 0 {
Expand Down
2 changes: 2 additions & 0 deletions runtime/config.go
Expand Up @@ -17,6 +17,8 @@ type Config struct {
// path to deliver static content
Webroot string `toml:"webroot"`

BlacklistFor duration.Duration `toml:"blacklist_for"`

// auth secret
Secret string `toml:"secret"`

Expand Down
6 changes: 4 additions & 2 deletions runtime/node.go
Expand Up @@ -4,16 +4,17 @@ import (
"fmt"
"net"
"strings"
"time"

yanicData "github.com/FreifunkBremen/yanic/data"
"github.com/FreifunkBremen/yanic/lib/jsontime"
yanicRuntime "github.com/FreifunkBremen/yanic/runtime"
)

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"`
Blacklist bool `json:"blacklist"`
Blacklist *time.Time `json:"-"`
Address string `json:"ip"`

Hostname string `json:"hostname"`
Expand Down Expand Up @@ -58,6 +59,7 @@ func (n *Node) Update(node *yanicRuntime.Node, ipPrefix string) {
if node == nil {
return
}
n.Lastseen = jsontime.Now()
if nodeinfo := node.Nodeinfo; nodeinfo != nil {
n.HostnameRespondd = nodeinfo.Hostname

Expand Down
9 changes: 5 additions & 4 deletions runtime/node_ssh.go
Expand Up @@ -9,10 +9,10 @@ import (
"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())
if err != nil {
return
return false
}
defer client.Close()

Expand Down Expand Up @@ -53,7 +53,7 @@ func (n *Node) SSHUpdate(sshmgmt *ssh.Manager) {
echo "radio1";
fi;`)
if err != nil {
return
return true
}
radio := ssh.SSHResultToString(result)
ch := GetChannel(n.Wireless.Channel24)
Expand Down Expand Up @@ -82,7 +82,7 @@ func (n *Node) SSHUpdate(sshmgmt *ssh.Manager) {
echo "radio1";
fi;`)
if err != nil {
return
return true
}
radio = ssh.SSHResultToString(result)
ch = GetChannel(n.Wireless.Channel5)
Expand All @@ -103,4 +103,5 @@ func (n *Node) SSHUpdate(sshmgmt *ssh.Manager) {
radio, n.Wireless.Channel5))
}
}
return true
}
51 changes: 25 additions & 26 deletions runtime/yanic.go
Expand Up @@ -7,57 +7,57 @@ import (
log "github.com/sirupsen/logrus"

databaseYanic "github.com/FreifunkBremen/yanic/database"
"github.com/FreifunkBremen/yanic/lib/jsontime"
runtimeYanic "github.com/FreifunkBremen/yanic/runtime"

"github.com/FreifunkBremen/freifunkmanager/ssh"
)

type YanicDB struct {
databaseYanic.Connection
db *gorm.DB
ssh *ssh.Manager
sendNode func(*Node)
sendStats func(*runtimeYanic.GlobalStats)
prefix string
blacklistFor time.Duration
db *gorm.DB
ssh *ssh.Manager
sendNode func(*Node)
sendStats func(*runtimeYanic.GlobalStats)
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{
db: db,
ssh: ssh,
sendNode: sendNode,
sendStats: sendStats,
prefix: prefix,
db: db,
ssh: ssh,
blacklistFor: blacklistFor,
sendNode: sendNode,
sendStats: sendStats,
prefix: prefix,
}
}

func (conn *YanicDB) InsertNode(n *runtimeYanic.Node) {
nodeid := ""
if nodeinfo := n.Nodeinfo; nodeinfo != nil {
nodeid = nodeinfo.NodeID
} else {
if n.Nodeinfo == nil {
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{
NodeID: nodeid,
NodeID: n.Nodeinfo.NodeID,
}
if conn.db.First(&lNode).Error == nil {
lNode.Update(n, conn.prefix)
conn.db.Model(&lNode).Update(map[string]interface{}{
"Lastseen": jsontime.Now(),
//"StatsWireless": node.StatsWireless,
//"StatsClients": node.StatsClients,
"Address": lNode.Address,
})
if lNode.Blacklist {
if lNode.Blacklist.Before(now.Add(-conn.blacklistFor)) {
logger.Debug("on blacklist")
return
}
conn.sendNode(&lNode)
if !lNode.CheckRespondd() {
lNode.SSHUpdate(conn.ssh)
if !lNode.SSHUpdate(conn.ssh) {
lNode.Blacklist = &now
conn.db.Save(&lNode)
}
logger.Debug("yanic trigger sshupdate again")
} else {
logger.Debug("yanic update")
Expand All @@ -68,15 +68,14 @@ func (conn *YanicDB) InsertNode(n *runtimeYanic.Node) {
if node == nil {
return
}
node.Lastseen = jsontime.Now()

_, err := conn.ssh.RunOn(node.GetAddress(), "uptime")
if err != nil {
logger.Debugf("set on blacklist: %s", err.Error())
node.Blacklist = true
node.Blacklist = &now
}
conn.db.Create(&node)
if !node.Blacklist {
if !node.Blacklist.Before(now.Add(-conn.blacklistFor)) {
conn.sendNode(node)
}
}
Expand Down
9 changes: 8 additions & 1 deletion websocket/hd_connect.go
@@ -1,6 +1,8 @@
package websocket

import (
"time"

log "github.com/sirupsen/logrus"

wsLib "dev.sum7.eu/genofire/golang-lib/websocket"
Expand All @@ -16,11 +18,16 @@ func (ws *WebsocketServer) connectHandler(logger *log.Entry, msg *wsLib.Message)
var nodes []*runtime.Node
var count int

ws.db.Where("blacklist = false").Find(&nodes).Count(&count)
now := time.Now()

ws.db.Find(&nodes).Count(&count)

ws.nodes.Lock()
i := 0
for _, node := range nodes {
if node.Blacklist.Before(now.Add(-ws.blacklistFor)) {
continue
}
//TODO skip blacklist
node.Update(ws.nodes.List[node.NodeID], ws.ipPrefix)
msg.From.Write(&wsLib.Message{Subject: MessageTypeNode, Body: node})
Expand Down
25 changes: 14 additions & 11 deletions websocket/server.go
Expand Up @@ -2,6 +2,7 @@ package websocket

import (
"net/http"
"time"

wsLib "dev.sum7.eu/genofire/golang-lib/websocket"
"github.com/jinzhu/gorm"
Expand All @@ -10,24 +11,26 @@ import (
)

type WebsocketServer struct {
nodes *runtime.Nodes
db *gorm.DB
secret string
ipPrefix string
nodes *runtime.Nodes
db *gorm.DB
blacklistFor time.Duration
secret string
ipPrefix string

inputMSG chan *wsLib.Message
ws *wsLib.Server
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{
nodes: nodes,
db: db,
handlers: make(map[string]WebsocketHandlerFunc),
inputMSG: make(chan *wsLib.Message),
secret: secret,
ipPrefix: ipPrefix,
nodes: nodes,
db: db,
blacklistFor: blacklistFor,
handlers: make(map[string]WebsocketHandlerFunc),
inputMSG: make(chan *wsLib.Message),
secret: secret,
ipPrefix: ipPrefix,
}
ownWS.ws = wsLib.NewServer(ownWS.inputMSG, wsLib.NewSessionManager())

Expand Down

0 comments on commit 6dde5bd

Please sign in to comment.