dsnet/reporttypes.go

180 lines
4.6 KiB
Go
Raw Permalink Normal View History

package dsnet
import (
2020-03-04 23:49:27 +01:00
"encoding/json"
"io/ioutil"
"net"
2020-03-06 00:56:24 +01:00
"os"
"time"
2020-03-04 23:23:32 +01:00
2020-03-06 00:56:24 +01:00
"github.com/go-playground/validator/v10"
2020-05-28 16:50:19 +02:00
"github.com/vishvananda/netlink"
2020-03-04 23:23:32 +01:00
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
type DsnetReport struct {
2020-03-06 00:56:24 +01:00
ExternalIP net.IP
InterfaceName string
2020-03-06 00:56:24 +01:00
ListenPort int
// domain to append to hostnames. Relies on separate DNS server for
// resolution. Informational only.
2020-03-06 00:56:24 +01:00
Domain string
IP net.IP
2020-11-02 19:24:09 +01:00
IP6 net.IP
// IP network from which to allocate automatic sequential addresses
// Network is chosen randomly when not specified
2020-05-28 16:50:19 +02:00
Network JSONIPNet
2020-11-02 19:24:09 +01:00
Network6 JSONIPNet
2020-05-28 16:50:19 +02:00
DNS net.IP
PeersOnline int
PeersTotal int
Peers []PeerReport
ReceiveBytes uint64
TransmitBytes uint64
ReceiveBytesSI string
TransmitBytesSI string
2020-05-30 15:40:07 +02:00
// when the report was made
Timestamp time.Time
}
2020-03-06 00:56:24 +01:00
func GenerateReport(dev *wgtypes.Device, conf *DsnetConfig, oldReport *DsnetReport) DsnetReport {
wgPeerIndex := make(map[wgtypes.Key]wgtypes.Peer)
peerReports := make([]PeerReport, len(conf.Peers))
2020-03-06 01:02:31 +01:00
oldPeerReportIndex := make(map[string]PeerReport)
2020-03-07 22:57:00 +01:00
peersOnline := 0
2020-05-28 16:50:19 +02:00
linkDev, err := netlink.LinkByName(conf.InterfaceName)
check(err)
stats := linkDev.Attrs().Statistics
for _, peer := range dev.Peers {
wgPeerIndex[peer.PublicKey] = peer
}
2020-03-06 01:02:31 +01:00
if oldReport != nil {
for _, report := range oldReport.Peers {
oldPeerReportIndex[report.Hostname] = report
}
}
for i, peer := range conf.Peers {
wgPeer, known := wgPeerIndex[peer.PublicKey.Key]
2020-03-05 23:05:47 +01:00
if !known {
// dangling peer, sync will remove. Dangling peers aren't such a
// problem now that add/remove performs a sync too.
continue
}
online := time.Since(wgPeer.LastHandshakeTime) < TIMEOUT
dormant := !wgPeer.LastHandshakeTime.IsZero() && time.Since(wgPeer.LastHandshakeTime) > EXPIRY
if online {
2020-05-28 16:50:19 +02:00
peersOnline++
2020-03-05 23:05:47 +01:00
}
2020-03-15 17:54:48 +01:00
externalIP := net.IP{}
if wgPeer.Endpoint != nil {
externalIP = wgPeer.Endpoint.IP
}
2020-05-28 16:50:19 +02:00
uReceiveBytes := uint64(wgPeer.ReceiveBytes)
uTransmitBytes := uint64(wgPeer.TransmitBytes)
peerReports[i] = PeerReport{
2020-03-06 00:56:24 +01:00
Hostname: peer.Hostname,
Online: online,
Dormant: dormant,
2020-03-06 00:56:24 +01:00
Owner: peer.Owner,
Description: peer.Description,
2020-03-19 21:12:42 +01:00
Added: peer.Added,
2020-03-06 00:56:24 +01:00
IP: peer.IP,
2020-11-02 19:24:09 +01:00
IP6: peer.IP6,
2020-03-15 17:54:48 +01:00
ExternalIP: externalIP,
2020-03-06 00:56:24 +01:00
Networks: peer.Networks,
LastHandshakeTime: wgPeer.LastHandshakeTime,
2020-05-28 16:50:19 +02:00
ReceiveBytes: uReceiveBytes,
TransmitBytes: uTransmitBytes,
ReceiveBytesSI: BytesToSI(uReceiveBytes),
TransmitBytesSI: BytesToSI(uTransmitBytes),
}
}
return DsnetReport{
2020-05-28 16:50:19 +02:00
ExternalIP: conf.ExternalIP,
InterfaceName: conf.InterfaceName,
ListenPort: conf.ListenPort,
Domain: conf.Domain,
IP: conf.IP,
2020-11-02 19:24:09 +01:00
IP6: conf.IP6,
2020-05-28 16:50:19 +02:00
Network: conf.Network,
2020-11-02 19:24:09 +01:00
Network6: conf.Network6,
2020-05-28 16:50:19 +02:00
DNS: conf.DNS,
Peers: peerReports,
PeersOnline: peersOnline,
PeersTotal: len(peerReports),
ReceiveBytes: stats.RxBytes,
TransmitBytes: stats.TxBytes,
ReceiveBytesSI: BytesToSI(stats.RxBytes),
TransmitBytesSI: BytesToSI(stats.TxBytes),
2020-05-30 15:40:07 +02:00
Timestamp: time.Now(),
}
2020-03-04 23:23:32 +01:00
}
2020-03-04 23:49:27 +01:00
func (report *DsnetReport) MustSave(filename string) {
2020-03-04 23:23:32 +01:00
_json, _ := json.MarshalIndent(report, "", " ")
2020-03-04 23:49:27 +01:00
err := ioutil.WriteFile(filename, _json, 0644)
2020-03-04 23:23:32 +01:00
check(err)
}
2020-03-06 00:56:24 +01:00
func MustLoadDsnetReport() *DsnetReport {
raw, err := ioutil.ReadFile(CONFIG_FILE)
if os.IsNotExist(err) {
return nil
} else if os.IsPermission(err) {
ExitFail("%s cannot be accessed. Check read permissions.", CONFIG_FILE)
} else {
check(err)
}
report := DsnetReport{}
err = json.Unmarshal(raw, &report)
check(err)
err = validator.New().Struct(report)
check(err)
return &report
}
type PeerReport struct {
// Used to update DNS
2020-03-04 00:09:54 +01:00
Hostname string
// username of person running this host/router
2020-03-04 00:09:54 +01:00
Owner string
// Description of what the host is and/or does
2020-03-04 00:09:54 +01:00
Description string
// Has a handshake occurred in the last 3 mins?
Online bool
// No handshake for 28 days
Dormant bool
2020-03-19 21:12:42 +01:00
// date peer was added to dsnet config
Added time.Time
// Internal VPN IP address. Added to AllowedIPs in server config as a /32
IP net.IP
2020-11-02 19:24:09 +01:00
IP6 net.IP
2020-03-15 17:54:48 +01:00
// Last known external IP
ExternalIP net.IP
// TODO ExternalIP support (Endpoint)
//ExternalIP net.UDPAddr `validate:"required,udp4_addr"`
// TODO support routing additional networks (AllowedIPs)
2020-03-04 00:09:54 +01:00
Networks []JSONIPNet
LastHandshakeTime time.Time
2020-05-28 16:50:19 +02:00
ReceiveBytes uint64
TransmitBytes uint64
2020-03-06 23:32:04 +01:00
ReceiveBytesSI string
TransmitBytesSI string
}