2020-03-03 23:08:52 +01:00
|
|
|
package dsnet
|
|
|
|
|
|
|
|
import (
|
2020-03-04 23:49:27 +01:00
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
2020-03-03 23:08:52 +01:00
|
|
|
"net"
|
2020-03-06 00:56:24 +01:00
|
|
|
"os"
|
2020-03-03 23:08:52 +01:00
|
|
|
"time"
|
2020-03-04 23:23:32 +01:00
|
|
|
|
2020-03-06 00:56:24 +01:00
|
|
|
"github.com/go-playground/validator/v10"
|
2020-03-04 23:23:32 +01:00
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
2020-03-03 23:08:52 +01:00
|
|
|
)
|
|
|
|
|
2020-03-04 00:32:07 +01:00
|
|
|
type Status int
|
|
|
|
|
|
|
|
const (
|
2020-03-05 23:05:47 +01:00
|
|
|
StatusUnknown = iota
|
2020-03-04 00:37:58 +01:00
|
|
|
// Host has not been loaded into wireguard yet
|
2020-03-05 23:05:47 +01:00
|
|
|
StatusSyncRequired
|
2020-03-08 22:52:36 +01:00
|
|
|
// No handshake in 3 minutes
|
2020-03-05 23:05:47 +01:00
|
|
|
StatusOffline
|
2020-03-08 22:52:36 +01:00
|
|
|
// Handshake in 3 minutes
|
2020-03-05 23:05:47 +01:00
|
|
|
StatusOnline
|
2020-03-04 00:37:58 +01:00
|
|
|
// Host has not connected for 28 days and may be removed
|
2020-03-05 23:05:47 +01:00
|
|
|
StatusExpired
|
2020-03-04 00:32:07 +01:00
|
|
|
)
|
|
|
|
|
2020-03-05 22:33:02 +01:00
|
|
|
// TODO pending/unknown
|
|
|
|
|
2020-03-04 00:32:07 +01:00
|
|
|
func (s Status) String() string {
|
|
|
|
switch s {
|
2020-03-05 23:05:47 +01:00
|
|
|
case StatusSyncRequired:
|
|
|
|
return "syncrequired"
|
|
|
|
case StatusOffline:
|
2020-03-04 00:41:55 +01:00
|
|
|
return "offline"
|
2020-03-05 23:05:47 +01:00
|
|
|
case StatusOnline:
|
2020-03-04 00:41:55 +01:00
|
|
|
return "online"
|
2020-03-05 23:05:47 +01:00
|
|
|
case StatusExpired:
|
2020-03-04 00:41:55 +01:00
|
|
|
return "expired"
|
|
|
|
default:
|
|
|
|
return "unknown"
|
2020-03-04 00:32:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// note unmarshal not required
|
|
|
|
func (s Status) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte("\"" + s.String() + "\""), nil
|
|
|
|
}
|
|
|
|
|
2020-03-03 23:08:52 +01:00
|
|
|
type DsnetReport struct {
|
2020-03-06 00:56:24 +01:00
|
|
|
ExternalIP net.IP
|
2020-03-05 22:33:02 +01:00
|
|
|
InterfaceName string
|
2020-03-06 00:56:24 +01:00
|
|
|
ListenPort int
|
2020-03-05 22:33:02 +01:00
|
|
|
// 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-03-03 22:06:36 +01:00
|
|
|
// IP network from which to allocate automatic sequential addresses
|
|
|
|
// Network is chosen randomly when not specified
|
2020-03-07 22:57:00 +01:00
|
|
|
Network JSONIPNet
|
|
|
|
DNS net.IP
|
|
|
|
PeersOnline int
|
|
|
|
PeersTotal int
|
2020-03-15 17:08:21 +01:00
|
|
|
Peers []PeerReport
|
2020-03-03 23:08:52 +01:00
|
|
|
}
|
|
|
|
|
2020-03-06 00:56:24 +01:00
|
|
|
func GenerateReport(dev *wgtypes.Device, conf *DsnetConfig, oldReport *DsnetReport) DsnetReport {
|
2020-03-05 22:33:02 +01:00
|
|
|
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-03-05 22:33:02 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-05 22:33:02 +01:00
|
|
|
for i, peer := range conf.Peers {
|
|
|
|
wgPeer, known := wgPeerIndex[peer.PublicKey.Key]
|
|
|
|
|
2020-03-05 23:05:47 +01:00
|
|
|
status := Status(StatusUnknown)
|
|
|
|
|
|
|
|
if !known {
|
|
|
|
status = StatusSyncRequired
|
2020-03-06 23:52:33 +01:00
|
|
|
} else if time.Since(wgPeer.LastHandshakeTime) < TIMEOUT {
|
2020-03-05 23:05:47 +01:00
|
|
|
status = StatusOnline
|
2020-03-07 22:57:00 +01:00
|
|
|
peersOnline += 1
|
2020-03-05 23:05:47 +01:00
|
|
|
// TODO same test but with rx byte data from last report (otherwise
|
|
|
|
// peer can fake online status by disabling handshake)
|
2020-03-06 23:57:05 +01:00
|
|
|
} else if !wgPeer.LastHandshakeTime.IsZero() && time.Since(wgPeer.LastHandshakeTime) > EXPIRY {
|
|
|
|
status = StatusExpired
|
2020-03-05 23:05:47 +01:00
|
|
|
} else {
|
|
|
|
status = StatusOffline
|
|
|
|
}
|
|
|
|
|
2020-03-15 17:54:48 +01:00
|
|
|
externalIP := net.IP{}
|
|
|
|
if wgPeer.Endpoint != nil {
|
|
|
|
externalIP = wgPeer.Endpoint.IP
|
|
|
|
}
|
|
|
|
|
2020-03-05 22:33:02 +01:00
|
|
|
peerReports[i] = PeerReport{
|
2020-03-06 00:56:24 +01:00
|
|
|
Hostname: peer.Hostname,
|
|
|
|
Owner: peer.Owner,
|
|
|
|
Description: peer.Description,
|
|
|
|
IP: peer.IP,
|
2020-03-15 17:54:48 +01:00
|
|
|
ExternalIP: externalIP,
|
2020-03-06 00:56:24 +01:00
|
|
|
Status: status,
|
|
|
|
Networks: peer.Networks,
|
2020-03-05 22:33:02 +01:00
|
|
|
LastHandshakeTime: wgPeer.LastHandshakeTime,
|
2020-03-06 00:56:24 +01:00
|
|
|
ReceiveBytes: wgPeer.ReceiveBytes,
|
|
|
|
TransmitBytes: wgPeer.TransmitBytes,
|
2020-03-06 23:32:04 +01:00
|
|
|
ReceiveBytesSI: BytesToSI(wgPeer.ReceiveBytes),
|
|
|
|
TransmitBytesSI: BytesToSI(wgPeer.TransmitBytes),
|
2020-03-05 22:33:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return DsnetReport{
|
2020-03-06 00:56:24 +01:00
|
|
|
ExternalIP: conf.ExternalIP,
|
2020-03-05 22:33:02 +01:00
|
|
|
InterfaceName: conf.InterfaceName,
|
2020-03-06 00:56:24 +01:00
|
|
|
ListenPort: conf.ListenPort,
|
|
|
|
Domain: conf.Domain,
|
|
|
|
IP: conf.IP,
|
|
|
|
Network: conf.Network,
|
|
|
|
DNS: conf.DNS,
|
|
|
|
Peers: peerReports,
|
2020-03-07 22:57:00 +01:00
|
|
|
PeersOnline: peersOnline,
|
|
|
|
PeersTotal: len(peerReports),
|
2020-03-05 22:33:02 +01:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2020-03-03 23:08:52 +01:00
|
|
|
type PeerReport struct {
|
|
|
|
// Used to update DNS
|
2020-03-04 00:09:54 +01:00
|
|
|
Hostname string
|
2020-03-03 22:06:36 +01:00
|
|
|
// username of person running this host/router
|
2020-03-04 00:09:54 +01:00
|
|
|
Owner string
|
2020-03-03 23:08:52 +01:00
|
|
|
// Description of what the host is and/or does
|
2020-03-04 00:09:54 +01:00
|
|
|
Description string
|
2020-03-03 22:06:36 +01:00
|
|
|
// Internal VPN IP address. Added to AllowedIPs in server config as a /32
|
2020-03-15 17:54:48 +01:00
|
|
|
IP net.IP
|
|
|
|
// Last known external IP
|
|
|
|
ExternalIP net.IP
|
|
|
|
Status Status
|
2020-03-03 22:06:36 +01:00
|
|
|
// 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
|
2020-03-03 23:08:52 +01:00
|
|
|
LastHandshakeTime time.Time
|
|
|
|
ReceiveBytes int64
|
|
|
|
TransmitBytes int64
|
2020-03-06 23:32:04 +01:00
|
|
|
ReceiveBytesSI string
|
|
|
|
TransmitBytesSI string
|
2020-03-03 23:08:52 +01:00
|
|
|
}
|