switch from status to online + dormant bool

This commit is contained in:
Callan Bryant 2020-03-21 08:38:41 +00:00
parent c95e225de8
commit fb9cc3d4e3
No known key found for this signature in database
GPG Key ID: C31FA9DF3ACBFFAA

View File

@ -11,39 +11,6 @@ import (
"golang.zx2c4.com/wireguard/wgctrl/wgtypes" "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
) )
type Status int
const (
// Host has not been loaded into wireguard yet
StatusUnknown = iota
// No handshake in 3 minutes
StatusOffline
// Handshake in 3 minutes
StatusOnline
// Host has not connected for 28 days and may be removed
StatusDormant
)
func (s Status) String() string {
switch s {
case StatusUnknown:
return "unknown"
case StatusOffline:
return "offline"
case StatusOnline:
return "online"
case StatusDormant:
return "dormant"
default:
return "";
}
}
// note unmarshal not required
func (s Status) MarshalJSON() ([]byte, error) {
return []byte("\"" + s.String() + "\""), nil
}
type DsnetReport struct { type DsnetReport struct {
ExternalIP net.IP ExternalIP net.IP
InterfaceName string InterfaceName string
@ -80,17 +47,17 @@ func GenerateReport(dev *wgtypes.Device, conf *DsnetConfig, oldReport *DsnetRepo
for i, peer := range conf.Peers { for i, peer := range conf.Peers {
wgPeer, known := wgPeerIndex[peer.PublicKey.Key] wgPeer, known := wgPeerIndex[peer.PublicKey.Key]
status := Status(StatusUnknown)
if !known { if !known {
status = StatusUnknown // dangling peer, sync will remove. Dangling peers aren't such a
} else if time.Since(wgPeer.LastHandshakeTime) < TIMEOUT { // problem now that add/remove performs a sync too.
status = StatusOnline continue
}
online := time.Since(wgPeer.LastHandshakeTime) < TIMEOUT
dormant := !wgPeer.LastHandshakeTime.IsZero() && time.Since(wgPeer.LastHandshakeTime) > EXPIRY
if online {
peersOnline += 1 peersOnline += 1
} else if !wgPeer.LastHandshakeTime.IsZero() && time.Since(wgPeer.LastHandshakeTime) > EXPIRY {
status = StatusDormant
} else {
status = StatusOffline
} }
externalIP := net.IP{} externalIP := net.IP{}
@ -100,12 +67,13 @@ func GenerateReport(dev *wgtypes.Device, conf *DsnetConfig, oldReport *DsnetRepo
peerReports[i] = PeerReport{ peerReports[i] = PeerReport{
Hostname: peer.Hostname, Hostname: peer.Hostname,
Online: online,
Dormant: dormant,
Owner: peer.Owner, Owner: peer.Owner,
Description: peer.Description, Description: peer.Description,
Added: peer.Added, Added: peer.Added,
IP: peer.IP, IP: peer.IP,
ExternalIP: externalIP, ExternalIP: externalIP,
Status: status,
Networks: peer.Networks, Networks: peer.Networks,
LastHandshakeTime: wgPeer.LastHandshakeTime, LastHandshakeTime: wgPeer.LastHandshakeTime,
ReceiveBytes: wgPeer.ReceiveBytes, ReceiveBytes: wgPeer.ReceiveBytes,
@ -163,13 +131,16 @@ type PeerReport struct {
Owner string Owner string
// Description of what the host is and/or does // Description of what the host is and/or does
Description string Description string
// Has a handshake occurred in the last 3 mins?
Online bool
// No handshake for 28 days
Dormant bool
// date peer was added to dsnet config // date peer was added to dsnet config
Added time.Time Added time.Time
// Internal VPN IP address. Added to AllowedIPs in server config as a /32 // Internal VPN IP address. Added to AllowedIPs in server config as a /32
IP net.IP IP net.IP
// Last known external IP // Last known external IP
ExternalIP net.IP ExternalIP net.IP
Status Status
// TODO ExternalIP support (Endpoint) // TODO ExternalIP support (Endpoint)
//ExternalIP net.UDPAddr `validate:"required,udp4_addr"` //ExternalIP net.UDPAddr `validate:"required,udp4_addr"`
// TODO support routing additional networks (AllowedIPs) // TODO support routing additional networks (AllowedIPs)