compute peer status (incomplete)
This commit is contained in:
parent
f7123be3a4
commit
8c5aaee793
@ -5,7 +5,6 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/go-playground/validator/v10"
|
"github.com/go-playground/validator/v10"
|
||||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||||
@ -180,7 +179,7 @@ func (conf DsnetConfig) MustAllocateIP() net.IP {
|
|||||||
func (conf DsnetConfig) GetWgPeerConfigs() []wgtypes.PeerConfig {
|
func (conf DsnetConfig) GetWgPeerConfigs() []wgtypes.PeerConfig {
|
||||||
wgPeers := make([]wgtypes.PeerConfig, 0, len(conf.Peers))
|
wgPeers := make([]wgtypes.PeerConfig, 0, len(conf.Peers))
|
||||||
|
|
||||||
interval := time.Second * KEEPALIVE_SECONDS
|
interval := KEEPALIVE
|
||||||
|
|
||||||
for _, peer := range conf.Peers {
|
for _, peer := range conf.Peers {
|
||||||
wgPeers = append(wgPeers, wgtypes.PeerConfig{
|
wgPeers = append(wgPeers, wgtypes.PeerConfig{
|
||||||
|
8
const.go
8
const.go
@ -1,5 +1,9 @@
|
|||||||
package dsnet
|
package dsnet
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// could be overridden in future via env
|
// could be overridden in future via env
|
||||||
CONFIG_FILE = "/etc/dsnetconfig.json"
|
CONFIG_FILE = "/etc/dsnetconfig.json"
|
||||||
@ -11,7 +15,9 @@ const (
|
|||||||
|
|
||||||
// keepalive always configured for everything. Set to a value likely to
|
// keepalive always configured for everything. Set to a value likely to
|
||||||
// stop most NATs from dropping the connection.
|
// stop most NATs from dropping the connection.
|
||||||
KEEPALIVE_SECONDS = 21
|
KEEPALIVE = 21 * time.Second
|
||||||
|
// allow missing a single keepalive + margin. Received data resets timeout, too.
|
||||||
|
TIMEOUT = 50 * time.Second
|
||||||
|
|
||||||
// when is a peer considered gone forever? (could remove)
|
// when is a peer considered gone forever? (could remove)
|
||||||
EXPIRY_DAYS = 28
|
EXPIRY_DAYS = 28
|
||||||
|
@ -12,27 +12,28 @@ import (
|
|||||||
type Status int
|
type Status int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
StatusUnknown = iota
|
||||||
// Host has not been loaded into wireguard yet
|
// Host has not been loaded into wireguard yet
|
||||||
Pending = iota
|
StatusSyncRequired
|
||||||
// Host has not transferred anything (not even a keepalive) for 30 seconds
|
// Host has not transferred anything (not even a keepalive) for 30 seconds
|
||||||
Offline
|
StatusOffline
|
||||||
// Host has transferred something in the last 30 seconds, keepalive counts
|
// Host has transferred something in the last 30 seconds, keepalive counts
|
||||||
Online
|
StatusOnline
|
||||||
// Host has not connected for 28 days and may be removed
|
// Host has not connected for 28 days and may be removed
|
||||||
Expired
|
StatusExpired
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO pending/unknown
|
// TODO pending/unknown
|
||||||
|
|
||||||
func (s Status) String() string {
|
func (s Status) String() string {
|
||||||
switch s {
|
switch s {
|
||||||
case Pending:
|
case StatusSyncRequired:
|
||||||
return "pending"
|
return "syncrequired"
|
||||||
case Offline:
|
case StatusOffline:
|
||||||
return "offline"
|
return "offline"
|
||||||
case Online:
|
case StatusOnline:
|
||||||
return "online"
|
return "online"
|
||||||
case Expired:
|
case StatusExpired:
|
||||||
return "expired"
|
return "expired"
|
||||||
default:
|
default:
|
||||||
return "unknown"
|
return "unknown"
|
||||||
@ -70,12 +71,24 @@ func GenerateReport(dev *wgtypes.Device, conf *DsnetConfig) DsnetReport {
|
|||||||
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 {
|
||||||
|
status = StatusSyncRequired
|
||||||
|
} else if wgPeer.LastHandshakeTime.After(time.Now().Add(-TIMEOUT)) {
|
||||||
|
status = StatusOnline
|
||||||
|
// TODO same test but with rx byte data from last report (otherwise
|
||||||
|
// peer can fake online status by disabling handshake)
|
||||||
|
} else {
|
||||||
|
status = StatusOffline
|
||||||
|
}
|
||||||
|
|
||||||
peerReports[i] = PeerReport{
|
peerReports[i] = PeerReport{
|
||||||
Hostname: peer.Hostname,
|
Hostname: peer.Hostname,
|
||||||
Owner: peer.Owner,
|
Owner: peer.Owner,
|
||||||
Description: peer.Description,
|
Description: peer.Description,
|
||||||
IP: peer.IP,
|
IP: peer.IP,
|
||||||
// TODO Status
|
Status: status,
|
||||||
Networks: peer.Networks,
|
Networks: peer.Networks,
|
||||||
LastHandshakeTime: wgPeer.LastHandshakeTime,
|
LastHandshakeTime: wgPeer.LastHandshakeTime,
|
||||||
ReceiveBytes: wgPeer.ReceiveBytes,
|
ReceiveBytes: wgPeer.ReceiveBytes,
|
||||||
|
Loading…
Reference in New Issue
Block a user