peer online count in report

This commit is contained in:
Callan Bryant 2020-03-07 21:57:00 +00:00
parent 1fd9e03509
commit 17b0ad74ae
No known key found for this signature in database
GPG Key ID: C31FA9DF3ACBFFAA
4 changed files with 19 additions and 14 deletions

2
add.go
View File

@ -3,8 +3,8 @@ package dsnet
import ( import (
"fmt" "fmt"
"os" "os"
"time"
"text/template" "text/template"
"time"
) )
func Add() { func Add() {

View File

@ -180,12 +180,12 @@ func (conf DsnetConfig) GetWgPeerConfigs() []wgtypes.PeerConfig {
for _, peer := range conf.Peers { for _, peer := range conf.Peers {
wgPeers = append(wgPeers, wgtypes.PeerConfig{ wgPeers = append(wgPeers, wgtypes.PeerConfig{
PublicKey: peer.PublicKey.Key, PublicKey: peer.PublicKey.Key,
Remove: false, Remove: false,
UpdateOnly: false, UpdateOnly: false,
PresharedKey: &peer.PresharedKey.Key, PresharedKey: &peer.PresharedKey.Key,
Endpoint: nil, Endpoint: nil,
ReplaceAllowedIPs: true, ReplaceAllowedIPs: true,
AllowedIPs: []net.IPNet{ AllowedIPs: []net.IPNet{
net.IPNet{ net.IPNet{
IP: peer.IP, IP: peer.IP,

View File

@ -57,15 +57,18 @@ type DsnetReport struct {
IP net.IP IP net.IP
// IP network from which to allocate automatic sequential addresses // IP network from which to allocate automatic sequential addresses
// Network is chosen randomly when not specified // Network is chosen randomly when not specified
Network JSONIPNet Network JSONIPNet
DNS net.IP DNS net.IP
Peers []PeerReport Peers []PeerReport
PeersOnline int
PeersTotal int
} }
func GenerateReport(dev *wgtypes.Device, conf *DsnetConfig, oldReport *DsnetReport) DsnetReport { func GenerateReport(dev *wgtypes.Device, conf *DsnetConfig, oldReport *DsnetReport) DsnetReport {
wgPeerIndex := make(map[wgtypes.Key]wgtypes.Peer) wgPeerIndex := make(map[wgtypes.Key]wgtypes.Peer)
peerReports := make([]PeerReport, len(conf.Peers)) peerReports := make([]PeerReport, len(conf.Peers))
oldPeerReportIndex := make(map[string]PeerReport) oldPeerReportIndex := make(map[string]PeerReport)
peersOnline := 0
for _, peer := range dev.Peers { for _, peer := range dev.Peers {
wgPeerIndex[peer.PublicKey] = peer wgPeerIndex[peer.PublicKey] = peer
@ -86,6 +89,7 @@ func GenerateReport(dev *wgtypes.Device, conf *DsnetConfig, oldReport *DsnetRepo
status = StatusSyncRequired status = StatusSyncRequired
} else if time.Since(wgPeer.LastHandshakeTime) < TIMEOUT { } else if time.Since(wgPeer.LastHandshakeTime) < TIMEOUT {
status = StatusOnline status = StatusOnline
peersOnline += 1
// TODO same test but with rx byte data from last report (otherwise // TODO same test but with rx byte data from last report (otherwise
// peer can fake online status by disabling handshake) // peer can fake online status by disabling handshake)
} else if !wgPeer.LastHandshakeTime.IsZero() && time.Since(wgPeer.LastHandshakeTime) > EXPIRY { } else if !wgPeer.LastHandshakeTime.IsZero() && time.Since(wgPeer.LastHandshakeTime) > EXPIRY {
@ -118,6 +122,8 @@ func GenerateReport(dev *wgtypes.Device, conf *DsnetConfig, oldReport *DsnetRepo
Network: conf.Network, Network: conf.Network,
DNS: conf.DNS, DNS: conf.DNS,
Peers: peerReports, Peers: peerReports,
PeersOnline: peersOnline,
PeersTotal: len(peerReports),
} }
} }

View File

@ -37,14 +37,14 @@ func ConfigureDevice(conf *DsnetConfig) {
if !knownKeys[peer.PublicKey] { if !knownKeys[peer.PublicKey] {
peers = append(peers, wgtypes.PeerConfig{ peers = append(peers, wgtypes.PeerConfig{
PublicKey: peer.PublicKey, PublicKey: peer.PublicKey,
Remove: true, Remove: true,
}) })
} }
} }
wgConfig := wgtypes.Config{ wgConfig := wgtypes.Config{
PrivateKey: &conf.PrivateKey.Key, PrivateKey: &conf.PrivateKey.Key,
ListenPort: &conf.ListenPort, ListenPort: &conf.ListenPort,
// ReplacePeers with the same peers results in those peers losing // ReplacePeers with the same peers results in those peers losing
// connection, so it's not possible to do declarative configuration // connection, so it's not possible to do declarative configuration
// idempotently with ReplacePeers like I had assumed. Instead, peers // idempotently with ReplacePeers like I had assumed. Instead, peers
@ -54,7 +54,6 @@ func ConfigureDevice(conf *DsnetConfig) {
Peers: peers, Peers: peers,
} }
err = wg.ConfigureDevice(conf.InterfaceName, wgConfig) err = wg.ConfigureDevice(conf.InterfaceName, wgConfig)
if err != nil { if err != nil {