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 (
"fmt"
"os"
"time"
"text/template"
"time"
)
func Add() {

View File

@ -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,

View File

@ -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),
}
}

View File

@ -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 {