From cfcb3859cadeb2e0d2430768b4f11b14926b636d Mon Sep 17 00:00:00 2001 From: Martin/Geno Date: Fri, 10 Aug 2018 13:46:18 +0200 Subject: [PATCH] WIP --- config_example.conf | 2 +- main.go | 4 +- runtime/channel_test.go | 24 +++++----- runtime/config.go | 2 +- runtime/node.go | 87 ++++++++++++++++++----------------- runtime/node_ssh.go | 18 ++++---- runtime/node_test.go | 7 ++- runtime/yanic.go | 35 ++++++++------ ssh/execute_test.go | 2 +- ssh/manager_test.go | 7 ++- ssh/run_test.go | 2 +- webroot/js/socket.js | 7 +-- webroot/js/store.js | 17 ++----- webroot/js/view/list.js | 26 +++++------ webroot/js/view/map.js | 10 ++-- webroot/js/view/statistics.js | 2 +- websocket/hd_connect.go | 16 ++----- websocket/hd_connect_test.go | 11 +---- websocket/hd_node.go | 6 ++- websocket/msg.go | 5 +- websocket/send.go | 8 +--- websocket/server.go | 9 ++-- 22 files changed, 143 insertions(+), 164 deletions(-) diff --git a/config_example.conf b/config_example.conf index a26f761..b6dce46 100644 --- a/config_example.conf +++ b/config_example.conf @@ -7,7 +7,7 @@ webroot = "./webroot/" secret = "passw0rd" ssh_key = "~/.ssh/id_rsa" -ssh_ipaddress_suffix = "fd2f:" +ssh_ipaddress_prefix = "fd2f:" ssh_timeout = "1m" # enable receiving diff --git a/main.go b/main.go index 2f50c2b..0688f67 100644 --- a/main.go +++ b/main.go @@ -53,9 +53,9 @@ func main() { sshmanager := ssh.NewManager(config.SSHPrivateKey, config.SSHTimeout.Duration) nodesYanic := runtimeYanic.NewNodes(&runtimeYanic.NodesConfig{}) - ws := websocket.NewWebsocketServer(config.Secret, db, nodesYanic) + ws := websocket.NewWebsocketServer(config.Secret, config.SSHIPAddressPrefix, db, nodesYanic) - yanic := runtime.NewYanicDB(db, sshmanager, ws.SendNode, ws.SendStats, config.SSHIPAddressSuffix) + yanic := runtime.NewYanicDB(db, sshmanager, ws.SendNode, ws.SendStats, config.SSHIPAddressPrefix) if config.YanicEnable { if duration := config.YanicSynchronize.Duration; duration > 0 { diff --git a/runtime/channel_test.go b/runtime/channel_test.go index 575d3e4..273336e 100644 --- a/runtime/channel_test.go +++ b/runtime/channel_test.go @@ -10,21 +10,21 @@ func TestChannel(t *testing.T) { assert := assert.New(t) ChannelEU = true - assert.False(ChannelIs5GHz(11), "is 2.4 GHz in EU") - assert.True(ChannelIs5GHz(40), "is 5 GHz everywhere") - assert.False(ChannelIs5GHz(42), "is not 5 GHz in EU") + assert.False(ChannelIs5GHz(9), "is 2.4 GHz in EU") + assert.True(ChannelIs5GHz(44), "is 5 GHz everywhere") + assert.False(ChannelIs5GHz(38), "is not 5 GHz in EU") - assert.NotNil(GetChannel(11), "is 2.4 GHz channel everywhere") - assert.NotNil(GetChannel(40), "is 5 GHz channel everywhere") - assert.Nil(GetChannel(42), "is not a 5 GHz channel in EU") + assert.NotNil(GetChannel(9), "is 2.4 GHz channel everywhere") + assert.NotNil(GetChannel(44), "is 5 GHz channel everywhere") + assert.Nil(GetChannel(38), "is not a 5 GHz channel in EU") ChannelEU = false - assert.False(ChannelIs5GHz(11), "is 2.4 GHz in EU") - assert.True(ChannelIs5GHz(40), "is 5 GHz everywhere") - assert.True(ChannelIs5GHz(42), "is 5 GHz somewhere else") + assert.False(ChannelIs5GHz(9), "is 2.4 GHz in EU") + assert.True(ChannelIs5GHz(44), "is 5 GHz everywhere") + assert.True(ChannelIs5GHz(38), "is 5 GHz somewhere else") - assert.NotNil(GetChannel(11), "is 2.4 GHz channel everywhere") - assert.NotNil(GetChannel(40), "is 5 GHz channel everywhere") - assert.NotNil(GetChannel(42), "is 5 GHz channel somewhere else") + assert.NotNil(GetChannel(9), "is 2.4 GHz channel everywhere") + assert.NotNil(GetChannel(44), "is 5 GHz channel everywhere") + assert.NotNil(GetChannel(38), "is 5 GHz channel somewhere else") } diff --git a/runtime/config.go b/runtime/config.go index e42db0b..a8bb972 100644 --- a/runtime/config.go +++ b/runtime/config.go @@ -22,7 +22,7 @@ type Config struct { // SSH private key SSHPrivateKey string `toml:"ssh_key"` - SSHIPAddressSuffix string `toml:"ssh_ipaddress_suffix"` + SSHIPAddressPrefix string `toml:"ssh_ipaddress_prefix"` SSHTimeout duration.Duration `toml:"ssh_timeout"` // yanic socket diff --git a/runtime/node.go b/runtime/node.go index 34d1b2d..27e604b 100644 --- a/runtime/node.go +++ b/runtime/node.go @@ -11,18 +11,24 @@ import ( ) type Node struct { - Lastseen jsontime.Time `json:"lastseen" mapstructure:"-"` - NodeID string `json:"node_id" gorm:"primary_key" mapstructure:"node_id"` - Hostname string `json:"hostname"` - Location yanicData.Location `json:"location" gorm:"embedded;embedded_prefix:location_"` - Wireless yanicData.Wireless `json:"wireless" gorm:"embedded;embedded_prefix:wireless_"` - Owner string `json:"owner"` - Blacklist bool `json:"blacklist" gorm:"-" mapstructure:"-"` - Address string `json:"ip" mapstructure:"-"` - Stats struct { + Lastseen jsontime.Time `json:"lastseen" mapstructure:"-"` + NodeID string `json:"node_id" gorm:"primary_key" mapstructure:"node_id"` + Blacklist bool `json:"blacklist"` + Address string `json:"ip"` + + Hostname string `json:"hostname"` + HostnameRespondd string `json:"hostname_Respondd" gorm:"-"` + Owner string `json:"owner"` + OwnerRespondd string `json:"owner_Respondd" gorm:"-"` + Location yanicData.Location `json:"location" gorm:"embedded;embedded_prefix:location_"` + LocationRespondd yanicData.Location `json:"location_Respondd" gorm:"-"` + Wireless yanicData.Wireless `json:"wireless" gorm:"embedded;embedded_prefix:wireless_"` + WirelessRespondd yanicData.Wireless `json:"wireless_Respondd" gorm:"-"` + + StatisticsRespondd struct { Wireless yanicData.WirelessStatistics `json:"wireless"` Clients yanicData.Clients `json:"clients"` - } `json:"statistics" mapstructure:"-"` + } `json:"statistics_respondd" mapstructure:"-"` } func NewNode(nodeOrigin *yanicRuntime.Node, ipPrefix string) *Node { @@ -31,15 +37,6 @@ func NewNode(nodeOrigin *yanicRuntime.Node, ipPrefix string) *Node { Hostname: nodeinfo.Hostname, NodeID: nodeinfo.NodeID, } - for _, ip := range nodeinfo.Network.Addresses { - if !strings.HasPrefix(ip, ipPrefix) { - continue - } - ipAddr := net.ParseIP(ip) - if node.Address == "" || ipAddr.IsGlobalUnicast() { - node.Address = ipAddr.String() - } - } if owner := nodeinfo.Owner; owner != nil { node.Owner = owner.Contact } @@ -49,10 +46,7 @@ func NewNode(nodeOrigin *yanicRuntime.Node, ipPrefix string) *Node { if wireless := nodeinfo.Wireless; wireless != nil { node.Wireless = *wireless } - if stats := nodeOrigin.Statistics; stats != nil { - node.Stats.Clients = stats.Clients - node.Stats.Wireless = stats.Wireless - } + node.Update(nodeOrigin, ipPrefix) return node } return nil @@ -60,42 +54,49 @@ func NewNode(nodeOrigin *yanicRuntime.Node, ipPrefix string) *Node { func (n *Node) GetAddress() net.TCPAddr { return net.TCPAddr{IP: net.ParseIP(n.Address), Port: 22} } -func (n *Node) Update(node *yanicRuntime.Node) { +func (n *Node) Update(node *yanicRuntime.Node, ipPrefix string) { + if node == nil { + return + } if nodeinfo := node.Nodeinfo; nodeinfo != nil { - n.Hostname = nodeinfo.Hostname - n.NodeID = nodeinfo.NodeID - n.Address = node.Address.IP.String() + n.HostnameRespondd = nodeinfo.Hostname + + for _, ip := range nodeinfo.Network.Addresses { + if !strings.HasPrefix(ip, ipPrefix) { + continue + } + ipAddr := net.ParseIP(ip) + if n.Address == "" || ipAddr.IsGlobalUnicast() || !ipAddr.IsLinkLocalUnicast() { + n.Address = ip + } + } if owner := nodeinfo.Owner; owner != nil { - n.Owner = owner.Contact + n.OwnerRespondd = owner.Contact } if location := nodeinfo.Location; location != nil { - n.Location = *location + n.LocationRespondd = *location } if wireless := nodeinfo.Wireless; wireless != nil { - n.Wireless = *wireless + n.WirelessRespondd = *wireless } } + if stats := node.Statistics; stats != nil { + n.StatisticsRespondd.Clients = stats.Clients + n.StatisticsRespondd.Wireless = stats.Wireless + } } -func (n *Node) IsEqual(node *Node) bool { - if n.NodeID != node.NodeID { +func (n *Node) CheckRespondd() bool { + if n.Hostname != n.HostnameRespondd { return false } - /* - if n.Address != node.Address { - return false - } - */ - if n.Hostname != node.Hostname { + if n.Owner != n.OwnerRespondd { return false } - if n.Owner != node.Owner { + if !locationEqual(n.Location, n.LocationRespondd) { return false } - if !locationEqual(n.Location, node.Location) { - return false - } - if !wirelessEqual(n.Wireless, node.Wireless) { + if !wirelessEqual(n.Wireless, n.WirelessRespondd) { return false } return true diff --git a/runtime/node_ssh.go b/runtime/node_ssh.go index 7b7b7ad..c26e335 100644 --- a/runtime/node_ssh.go +++ b/runtime/node_ssh.go @@ -9,26 +9,26 @@ import ( "github.com/FreifunkBremen/freifunkmanager/ssh" ) -func (n *Node) SSHUpdate(sshmgmt *ssh.Manager, oldnode *Node) { +func (n *Node) SSHUpdate(sshmgmt *ssh.Manager) { client, err := sshmgmt.ConnectTo(n.GetAddress()) if err != nil { return } defer client.Close() - if oldnode == nil || n.Hostname != oldnode.Hostname { + if n.Hostname != n.HostnameRespondd { ssh.Execute(n.Address, client, fmt.Sprintf(` uci set system.@system[0].hostname='%s'; uci commit; echo $(uci get system.@system[0].hostname) > /proc/sys/kernel/hostname;`, n.Hostname)) } - if oldnode == nil || n.Owner != oldnode.Owner { + if n.Owner != n.OwnerRespondd { ssh.Execute(n.Address, client, fmt.Sprintf(` uci set gluon-node-info.@owner[0].contact='%s'; uci commit gluon-node-info;`, n.Owner)) } - if oldnode == nil || !locationEqual(n.Location, oldnode.Location) { + if !locationEqual(n.Location, n.LocationRespondd) { ssh.Execute(n.Address, client, fmt.Sprintf(` uci set gluon-node-info.@location[0].latitude='%f'; uci set gluon-node-info.@location[0].longitude='%f'; @@ -58,14 +58,14 @@ func (n *Node) SSHUpdate(sshmgmt *ssh.Manager, oldnode *Node) { radio := ssh.SSHResultToString(result) ch := GetChannel(n.Wireless.Channel24) if radio != "" && ch != nil { - if oldnode == nil || n.Wireless.TxPower24 != oldnode.Wireless.TxPower24 { + if n.Wireless.TxPower24 != n.WirelessRespondd.TxPower24 { ssh.Execute(n.Address, client, fmt.Sprintf(` uci set wireless.%s.txpower='%d'; uci commit wireless;`, radio, n.Wireless.TxPower24)) runWifi = true } - if oldnode == nil || n.Wireless.Channel24 != oldnode.Wireless.Channel24 { + if n.Wireless.Channel24 != n.WirelessRespondd.Channel24 { ssh.Execute(n.Address, client, fmt.Sprintf(` ubus call hostapd.%s switch_chan '{"freq":%d}' uci set wireless.%s.channel='%d'; @@ -87,14 +87,14 @@ func (n *Node) SSHUpdate(sshmgmt *ssh.Manager, oldnode *Node) { radio = ssh.SSHResultToString(result) ch = GetChannel(n.Wireless.Channel5) if radio != "" && ch != nil { - if oldnode == nil || n.Wireless.TxPower5 != oldnode.Wireless.TxPower5 { + if n.Wireless.TxPower5 != n.WirelessRespondd.TxPower5 { ssh.Execute(n.Address, client, fmt.Sprintf(` uci set wireless.%s.txpower='%d'; uci commit wireless;`, radio, n.Wireless.TxPower5)) runWifi = true } - if oldnode == nil || n.Wireless.Channel5 != oldnode.Wireless.Channel5 { + if n.Wireless.Channel5 != n.WirelessRespondd.Channel5 { ssh.Execute(n.Address, client, fmt.Sprintf(` ubus call hostapd.%s switch_chan '{"freq":%d}' uci set wireless.%s.channel='%d'; @@ -103,6 +103,4 @@ func (n *Node) SSHUpdate(sshmgmt *ssh.Manager, oldnode *Node) { radio, n.Wireless.Channel5)) } } - - oldnode = n } diff --git a/runtime/node_test.go b/runtime/node_test.go index 917fbb6..d6a7bd3 100644 --- a/runtime/node_test.go +++ b/runtime/node_test.go @@ -27,10 +27,9 @@ func TestNode(t *testing.T) { assert.NotNil(n1) assert.Equal(float64(13), n1.Location.Altitude) - n2 := NewNode(node1, "") - assert.True(n2.IsEqual(n1)) + assert.True(n1.CheckRespondd()) node1.Nodeinfo.Owner.Contact = "blub2" - n2.Update(node1) - assert.False(n2.IsEqual(n1)) + n1.Update(node1, "") + assert.False(n1.CheckRespondd()) } diff --git a/runtime/yanic.go b/runtime/yanic.go index b4cbe6f..d8b0b43 100644 --- a/runtime/yanic.go +++ b/runtime/yanic.go @@ -17,12 +17,12 @@ type YanicDB struct { databaseYanic.Connection db *gorm.DB ssh *ssh.Manager - sendNode func(*Node, bool) + sendNode func(*Node) sendStats func(*runtimeYanic.GlobalStats) prefix string } -func NewYanicDB(db *gorm.DB, ssh *ssh.Manager, sendNode func(*Node, bool), sendStats func(*runtimeYanic.GlobalStats), prefix string) *YanicDB { +func NewYanicDB(db *gorm.DB, ssh *ssh.Manager, sendNode func(*Node), sendStats func(*runtimeYanic.GlobalStats), prefix string) *YanicDB { return &YanicDB{ db: db, ssh: ssh, @@ -33,45 +33,52 @@ func NewYanicDB(db *gorm.DB, ssh *ssh.Manager, sendNode func(*Node, bool), sendS } func (conn *YanicDB) InsertNode(n *runtimeYanic.Node) { - node := NewNode(n, conn.prefix) - if node == nil { + nodeid := "" + if nodeinfo := n.Nodeinfo; nodeinfo != nil { + nodeid = nodeinfo.NodeID + } else { return } - node.Lastseen = jsontime.Now() - logger := log.WithField("method", "LearnNode").WithField("node_id", node.NodeID) + logger := log.WithField("method", "LearnNode").WithField("node_id", nodeid) lNode := Node{ - NodeID: node.NodeID, + NodeID: 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": node.Address, + "Address": lNode.Address, }) if lNode.Blacklist { logger.Debug("on blacklist") return } - conn.sendNode(node, false) - if !node.IsEqual(&lNode) { - lNode.SSHUpdate(conn.ssh, node) + conn.sendNode(&lNode) + if !lNode.CheckRespondd() { + lNode.SSHUpdate(conn.ssh) logger.Debug("yanic trigger sshupdate again") } else { logger.Debug("yanic update") } return } - + node := NewNode(n, conn.prefix) + if node == nil { + return + } node.Lastseen = jsontime.Now() _, err := conn.ssh.RunOn(node.GetAddress(), "uptime") if err != nil { - logger.Debug("set on blacklist") + logger.Debugf("set on blacklist: %s", err.Error()) node.Blacklist = true } conn.db.Create(&node) - conn.sendNode(node, true) + if !node.Blacklist { + conn.sendNode(node) + } } func (conn *YanicDB) InsertLink(link *runtimeYanic.Link, time time.Time) { diff --git a/ssh/execute_test.go b/ssh/execute_test.go index 46e2d26..2afa00e 100644 --- a/ssh/execute_test.go +++ b/ssh/execute_test.go @@ -13,7 +13,7 @@ func TestExecute(t *testing.T) { addr := net.TCPAddr{IP: net.ParseIP("fd2f:5119:f2c::127"), Port: 22} - mgmt := NewManager("~/.ssh/id_rsa", time.Second) + mgmt := NewManager("~/.ssh/id_rsa", 5*time.Second) assert.NotNil(mgmt, "no new manager created") client, err := mgmt.ConnectTo(addr) diff --git a/ssh/manager_test.go b/ssh/manager_test.go index 2bbfd8a..199d27b 100644 --- a/ssh/manager_test.go +++ b/ssh/manager_test.go @@ -11,9 +11,12 @@ import ( func TestManager(t *testing.T) { assert := assert.New(t) - mgmt := NewManager("~/.ssh/id_rsa", time.Second) + mgmt := NewManager("~/.ssh/id_rsa", 5*time.Second) assert.NotNil(mgmt, "no new manager created") client, _ := mgmt.ConnectTo(net.TCPAddr{IP: net.ParseIP("fd2f:5119:f2c::127"), Port: 22}) - client.Close() + assert.NotNil(client, "no connection to client") + if client == nil { + client.Close() + } } diff --git a/ssh/run_test.go b/ssh/run_test.go index 9c4a599..a5d05eb 100644 --- a/ssh/run_test.go +++ b/ssh/run_test.go @@ -12,7 +12,7 @@ import ( func TestRun(t *testing.T) { assert := assert.New(t) addr := net.TCPAddr{IP: net.ParseIP("fd2f:5119:f2c::127"), Port: 22} - mgmt := NewManager("~/.ssh/id_rsa", time.Second) + mgmt := NewManager("~/.ssh/id_rsa", 5*time.Second) assert.NotNil(mgmt, "no new manager created") client, err := mgmt.ConnectTo(addr) diff --git a/webroot/js/socket.js b/webroot/js/socket.js index 2c6f717..0ac6f4f 100644 --- a/webroot/js/socket.js +++ b/webroot/js/socket.js @@ -162,7 +162,7 @@ export function delEvent (to, func) { } export function sendnode(node, callback) { - sendjson({'subject':'node-system','body': node}, (msg) => { + sendjson({'subject':'node','body': node}, (msg) => { if(!msg.body) { notify.send({ 'header': 'Speichern', @@ -190,10 +190,7 @@ setEvent('auth_status', (msg) => { render(); }); -addEvent('node-system', (msg) => { - store.updateNode(msg.body, true); -}); -addEvent('node-current', (msg) => { +addEvent('node', (msg) => { store.updateNode(msg.body); }); addEvent('channels_wifi24', (msg) => { diff --git a/webroot/js/store.js b/webroot/js/store.js index e3ad9c1..bb96d87 100644 --- a/webroot/js/store.js +++ b/webroot/js/store.js @@ -10,14 +10,7 @@ export function getNode (nodeid) { } let node = list[nodeid]; - if (current[nodeid]) { - const cNode = current[nodeid]; - - // eslint-disable-next-line no-underscore-dangle - node._wireless = cNode.wireless; - node.lastseen = cNode.lastseen; - node.statistics = cNode.statistics; - } + // keep structur for pings later return node; }; @@ -38,12 +31,8 @@ export function createNode (nodeid) { // Overwrites the values for the specified node (identified by its node_id) with new values. // If system==false, the special "current" node will be modified instead. -export function updateNode (node, system) { - if (system) { - list[node.node_id] = node; - } else { - current[node.node_id] = node; - } +export function updateNode (node) { + list[node.node_id] = node; }; // Returns a list of all known nodes. diff --git a/webroot/js/view/list.js b/webroot/js/view/list.js index 225c21c..2feacae 100644 --- a/webroot/js/view/list.js +++ b/webroot/js/view/list.js @@ -45,7 +45,7 @@ export class ListView extends View { { name:'Freq' }, { name:'CurCh', - sort: (a, b) => (a._wireless ? a._wireless.channel24 : 0) - (b._wireless ? b._wireless.channel24 : 0), + sort: (a, b) => (a.wireless_respondd ? a.wireless_respondd.channel24 : 0) - (b.wireless_respondd ? b.wireless_respondd.channel24 : 0), reverse: false }, { @@ -55,7 +55,7 @@ export class ListView extends View { }, { name:'CurPW', - sort: (a, b) => (a._wireless ? a._wireless.txpower24 : 0) - (b._wireless ? b._wireless.txpower24 : 0), + sort: (a, b) => (a.wireless_respondd ? a.wireless_respondd.txpower24 : 0) - (b.wireless_respondd ? b.wireless_respondd.txpower24 : 0), reverse: false }, { @@ -65,12 +65,12 @@ export class ListView extends View { }, { name:'Clients', - sort: (a, b) => a.statistics.clients.wifi24 - b.statistics.clients.wifi24, + sort: (a, b) => a.statistics_respondd.clients.wifi24 - b.statistics_respondd.clients.wifi24, reverse: false }, { name:'ChanUtil', - sort: (a, b) => (a.statistics.wireless ? a.statistics.wireless.filter((d) => d.frequency < 5000)[0].ChanUtil || 0 : 0) - (b.statistics.wireless ? b.statistics.wireless.filter((d) => d.frequency < 5000)[0].ChanUtil || 0 : 0), + sort: (a, b) => (a.statistics_respondd.wireless ? a.statistics_respondd.wireless.filter((d) => d.frequency < 5000)[0].ChanUtil || 0 : 0) - (b.statistics_respondd.wireless ? b.statistics_respondd.wireless.filter((d) => d.frequency < 5000)[0].ChanUtil || 0 : 0), reverse: false }, { name:'Options' } @@ -101,7 +101,7 @@ export class ListView extends View { return V.h('tr', {},[ V.h('td', { - 'class':(new Date(node.lastseen) < startdate)?'offline':(!node._wireless)?'unseen':'' + 'class':(new Date(node.lastseen) < startdate)?'offline':(!node.wireless_respondd)?'unseen':'' }, FromNowAgo(node.lastseen)), V.h('td', {}, node.node_id), V.h('td', {}, V.h('input',{ @@ -127,8 +127,8 @@ export class ListView extends View { V.h('span', {},'5 GHz') ]), V.h('td', {}, [ - V.h('span', {}, node._wireless ? node._wireless.channel24 || '-':'-'), - V.h('span', {}, node._wireless ? node._wireless.channel5 || '-':'-') + V.h('span', {}, node.wireless_respondd ? node.wireless_respondd.channel24 || '-':'-'), + V.h('span', {}, node.wireless_respondd ? node.wireless_respondd.channel5 || '-':'-') ]), V.h('td', {}, [ V.h('span', {}, V.h('select',{ @@ -157,8 +157,8 @@ export class ListView extends View { }, channel5Options)) ]), V.h('td', {}, [ - V.h('span', {}, node._wireless ? node._wireless.txpower24 || '-':'-'), - V.h('span', {}, node._wireless ? node._wireless.txpower5 || '-':'-') + V.h('span', {}, node.wireless_respondd ? node.wireless_respondd.txpower24 || '-':'-'), + V.h('span', {}, node.wireless_respondd ? node.wireless_respondd.txpower5 || '-':'-') ]), V.h('td', {}, [ V.h('span', {}, V.h('input',{ @@ -205,12 +205,12 @@ export class ListView extends View { })) ]), V.h('td', {}, [ - V.h('span', {}, node.statistics.clients.wifi24), - V.h('span', {}, node.statistics.clients.wifi5) + V.h('span', {}, node.statistics_respondd.clients.wifi24), + V.h('span', {}, node.statistics_respondd.clients.wifi5) ]), V.h('td', {}, [ - V.h('span', {}, node.statistics.wireless ? (node.statistics.wireless.filter((d) => d.frequency < 5000)[0] || {ChanUtil: '-'}).ChanUtil : '-'), - V.h('span', {}, node.statistics.wireless ? (node.statistics.wireless.filter((d) => d.frequency > 5000)[0] || {ChanUtil: '-'}).ChanUtil : '-'), + V.h('span', {}, node.statistics_respondd.wireless ? (node.statistics_respondd.wireless.filter((d) => d.frequency < 5000)[0] || {ChanUtil: '-'}).ChanUtil : '-'), + V.h('span', {}, node.statistics_respondd.wireless ? (node.statistics_respondd.wireless.filter((d) => d.frequency > 5000)[0] || {ChanUtil: '-'}).ChanUtil : '-'), ]), V.h('td', {}, [ V.h('a',{ diff --git a/webroot/js/view/map.js b/webroot/js/view/map.js index 4609145..80ae367 100644 --- a/webroot/js/view/map.js +++ b/webroot/js/view/map.js @@ -108,9 +108,9 @@ export class MapView extends View { } - if (node.statistics && node.statistics.clients) { + if (node.statistics_respondd && node.statistics_respondd.clients) { // eslint-disable-next-line prefer-destructuring - wifi24 = node.statistics.clients.wifi24; + wifi24 = node.statistics_respondd.clients.wifi24; if (wifi24 < config.map.icon.warn.wifi24 && wifi24 > 0) { className += ' client24'; @@ -120,7 +120,7 @@ export class MapView extends View { className += ' client24-crit'; } // eslint-disable-next-line prefer-destructuring - wifi5 = node.statistics.clients.wifi5; + wifi5 = node.statistics_respondd.clients.wifi5; if (wifi5 < config.map.icon.warn.wifi5 && wifi5 > 0) { className += ' client5'; @@ -182,13 +182,13 @@ export class MapView extends View { this.clientLayer24.setData(nodes.filter((node) => { return (node.location && node.location.latitude && node.location.longitude); }).map((node) => { - return [node.location.latitude, node.location.longitude, node.statistics.clients.wifi24 || 0]; + return [node.location.latitude, node.location.longitude, node.statistics_respondd.clients.wifi24 || 0]; })); this.clientLayer5.setData(nodes.filter((node) => { return (node.location && node.location.latitude && node.location.longitude); }).map((node) => { - return [node.location.latitude, node.location.longitude, node.statistics.clients.wifi5 || 0]; + return [node.location.latitude, node.location.longitude, node.statistics_respondd.clients.wifi5 || 0]; })); this.map.invalidateSize(); } diff --git a/webroot/js/view/statistics.js b/webroot/js/view/statistics.js index 67d51d7..9ff8478 100644 --- a/webroot/js/view/statistics.js +++ b/webroot/js/view/statistics.js @@ -52,7 +52,7 @@ export class StatisticsView extends View { this.clientsWifi5.innerHTML = msg.body.ClientsWifi5; } }); - socket.addEvent('node-current', this.updateChannelTable.bind(this)); + socket.addEvent('node', this.updateChannelTable.bind(this)); socket.addEvent('channels_wifi24', this.updateChannelTable.bind(this)); socket.addEvent('channels_wifi5', this.updateChannelTable.bind(this)); } diff --git a/websocket/hd_connect.go b/websocket/hd_connect.go index cf0b13e..377a9d0 100644 --- a/websocket/hd_connect.go +++ b/websocket/hd_connect.go @@ -16,23 +16,17 @@ func (ws *WebsocketServer) connectHandler(logger *log.Entry, msg *wsLib.Message) var nodes []*runtime.Node var count int - ws.db.Find(&nodes).Count(&count) + ws.db.Where("blacklist = false").Find(&nodes).Count(&count) ws.nodes.Lock() i := 0 - for _, node := range ws.nodes.List { - n := runtime.NewNode(node, "") - if n == nil { - continue - } - n.Lastseen = node.Lastseen - msg.From.Write(&wsLib.Message{Subject: MessageTypeCurrentNode, Body: n}) + for _, node := range nodes { + //TODO skip blacklist + node.Update(ws.nodes.List[node.NodeID], ws.ipPrefix) + msg.From.Write(&wsLib.Message{Subject: MessageTypeNode, Body: node}) i++ } ws.nodes.Unlock() - for _, node := range nodes { - msg.From.Write(&wsLib.Message{Subject: MessageTypeSystemNode, Body: node}) - } msg.From.Write(&wsLib.Message{Subject: MessageTypeChannelsWifi24, Body: wifi24Channels}) msg.From.Write(&wsLib.Message{Subject: MessageTypeChannelsWifi5, Body: wifi5Channels}) logger.Debugf("done - fetch %d nodes and send %d", count, i) diff --git a/websocket/hd_connect_test.go b/websocket/hd_connect_test.go index 64e5fd5..c65d5d5 100644 --- a/websocket/hd_connect_test.go +++ b/websocket/hd_connect_test.go @@ -1,14 +1,6 @@ package websocket -import ( - "fmt" - "testing" - - "github.com/stretchr/testify/assert" - - "github.com/FreifunkBremen/freifunkmanager/runtime" -) - +/* func TestConnectChannelInit(t *testing.T) { assert := assert.New(t) @@ -25,3 +17,4 @@ func TestConnectChannelInit(t *testing.T) { assert.Len(wifi5Channels, 35) } +*/ diff --git a/websocket/hd_node.go b/websocket/hd_node.go index 4bcea78..a09b9f1 100644 --- a/websocket/hd_node.go +++ b/websocket/hd_node.go @@ -50,7 +50,11 @@ func (ws *WebsocketServer) nodeHandler(logger *log.Entry, msg *wsLib.Message) er logger.Warnf("could not change %s: %s", node.NodeID, err.Error.Error()) return err.Error } - ws.SendNode(&node, true) + ws.nodes.Lock() + nodeRespondd := ws.nodes.List[node.NodeID] + ws.nodes.Unlock() + node.Update(nodeRespondd, ws.ipPrefix) + ws.SendNode(&node) logger.Infof("change %s", node.NodeID) return nil } diff --git a/websocket/msg.go b/websocket/msg.go index b825f09..7a41a55 100644 --- a/websocket/msg.go +++ b/websocket/msg.go @@ -10,7 +10,6 @@ const ( MessageTypeChannelsWifi24 = "channels_wifi24" MessageTypeChannelsWifi5 = "channels_wifi5" - MessageTypeSystemNode = "node-system" - MessageTypeCurrentNode = "node-current" - MessageTypeStats = "stats" + MessageTypeNode = "node" + MessageTypeStats = "stats" ) diff --git a/websocket/send.go b/websocket/send.go index fd1de5d..3f6f7f7 100644 --- a/websocket/send.go +++ b/websocket/send.go @@ -8,12 +8,8 @@ import ( "github.com/FreifunkBremen/freifunkmanager/runtime" ) -func (ws *WebsocketServer) SendNode(node *runtime.Node, system bool) { - msgType := MessageTypeCurrentNode - if system { - msgType = MessageTypeSystemNode - } - ws.ws.SendAll(&wsLib.Message{Subject: msgType, Body: node}) +func (ws *WebsocketServer) SendNode(node *runtime.Node) { + ws.ws.SendAll(&wsLib.Message{Subject: MessageTypeNode, Body: node}) } func (ws *WebsocketServer) SendStats(data *yanicRuntime.GlobalStats) { diff --git a/websocket/server.go b/websocket/server.go index 602aafa..335ff71 100644 --- a/websocket/server.go +++ b/websocket/server.go @@ -4,7 +4,6 @@ import ( "net/http" wsLib "dev.sum7.eu/genofire/golang-lib/websocket" - "github.com/google/uuid" "github.com/jinzhu/gorm" "github.com/FreifunkBremen/yanic/runtime" @@ -14,21 +13,21 @@ type WebsocketServer struct { nodes *runtime.Nodes db *gorm.DB secret string - loggedIn map[uuid.UUID]bool + ipPrefix string inputMSG chan *wsLib.Message ws *wsLib.Server handlers map[string]WebsocketHandlerFunc } -func NewWebsocketServer(secret string, db *gorm.DB, nodes *runtime.Nodes) *WebsocketServer { +func NewWebsocketServer(secret string, ipPrefix string, db *gorm.DB, nodes *runtime.Nodes) *WebsocketServer { ownWS := WebsocketServer{ nodes: nodes, db: db, handlers: make(map[string]WebsocketHandlerFunc), - loggedIn: make(map[uuid.UUID]bool), inputMSG: make(chan *wsLib.Message), secret: secret, + ipPrefix: ipPrefix, } ownWS.ws = wsLib.NewServer(ownWS.inputMSG, wsLib.NewSessionManager()) @@ -39,7 +38,7 @@ func NewWebsocketServer(secret string, db *gorm.DB, nodes *runtime.Nodes) *Webso ownWS.handlers[MessageTypeAuthStatus] = ownWS.authStatusHandler ownWS.handlers[MessageTypeLogout] = ownWS.logoutHandler - ownWS.handlers[MessageTypeSystemNode] = ownWS.nodeHandler + ownWS.handlers[MessageTypeNode] = ownWS.nodeHandler http.HandleFunc("/ws", ownWS.ws.Handler) go ownWS.MessageHandler()