From 17b0ad74ae59600d5da79e71ad66dff87c1d510c Mon Sep 17 00:00:00 2001 From: Callan Bryant Date: Sat, 7 Mar 2020 21:57:00 +0000 Subject: [PATCH] peer online count in report --- add.go | 2 +- configtypes.go | 12 ++++++------ reporttypes.go | 12 +++++++++--- sync.go | 7 +++---- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/add.go b/add.go index 93ee38c..85a42b8 100644 --- a/add.go +++ b/add.go @@ -3,8 +3,8 @@ package dsnet import ( "fmt" "os" - "time" "text/template" + "time" ) func Add() { diff --git a/configtypes.go b/configtypes.go index 211d51c..f0d9a09 100644 --- a/configtypes.go +++ b/configtypes.go @@ -180,12 +180,12 @@ func (conf DsnetConfig) GetWgPeerConfigs() []wgtypes.PeerConfig { for _, peer := range conf.Peers { wgPeers = append(wgPeers, wgtypes.PeerConfig{ - PublicKey: peer.PublicKey.Key, - Remove: false, - UpdateOnly: false, - PresharedKey: &peer.PresharedKey.Key, - Endpoint: nil, - ReplaceAllowedIPs: true, + PublicKey: peer.PublicKey.Key, + Remove: false, + UpdateOnly: false, + PresharedKey: &peer.PresharedKey.Key, + Endpoint: nil, + ReplaceAllowedIPs: true, AllowedIPs: []net.IPNet{ net.IPNet{ IP: peer.IP, diff --git a/reporttypes.go b/reporttypes.go index 3305438..d6d629a 100644 --- a/reporttypes.go +++ b/reporttypes.go @@ -57,15 +57,18 @@ type DsnetReport struct { IP net.IP // IP network from which to allocate automatic sequential addresses // Network is chosen randomly when not specified - Network JSONIPNet - DNS net.IP - Peers []PeerReport + Network JSONIPNet + DNS net.IP + Peers []PeerReport + PeersOnline int + PeersTotal int } func GenerateReport(dev *wgtypes.Device, conf *DsnetConfig, oldReport *DsnetReport) DsnetReport { wgPeerIndex := make(map[wgtypes.Key]wgtypes.Peer) peerReports := make([]PeerReport, len(conf.Peers)) oldPeerReportIndex := make(map[string]PeerReport) + peersOnline := 0 for _, peer := range dev.Peers { wgPeerIndex[peer.PublicKey] = peer @@ -86,6 +89,7 @@ func GenerateReport(dev *wgtypes.Device, conf *DsnetConfig, oldReport *DsnetRepo status = StatusSyncRequired } else if time.Since(wgPeer.LastHandshakeTime) < TIMEOUT { status = StatusOnline + peersOnline += 1 // TODO same test but with rx byte data from last report (otherwise // peer can fake online status by disabling handshake) } 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, DNS: conf.DNS, Peers: peerReports, + PeersOnline: peersOnline, + PeersTotal: len(peerReports), } } diff --git a/sync.go b/sync.go index 896fccc..05ec725 100644 --- a/sync.go +++ b/sync.go @@ -37,14 +37,14 @@ func ConfigureDevice(conf *DsnetConfig) { if !knownKeys[peer.PublicKey] { peers = append(peers, wgtypes.PeerConfig{ PublicKey: peer.PublicKey, - Remove: true, + Remove: true, }) } } wgConfig := wgtypes.Config{ - PrivateKey: &conf.PrivateKey.Key, - ListenPort: &conf.ListenPort, + PrivateKey: &conf.PrivateKey.Key, + ListenPort: &conf.ListenPort, // ReplacePeers with the same peers results in those peers losing // connection, so it's not possible to do declarative configuration // idempotently with ReplacePeers like I had assumed. Instead, peers @@ -54,7 +54,6 @@ func ConfigureDevice(conf *DsnetConfig) { Peers: peers, } - err = wg.ConfigureDevice(conf.InterfaceName, wgConfig) if err != nil {