forked from naggie/dsnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reporttypes.go
179 lines (156 loc) · 4.56 KB
/
reporttypes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package dsnet
import (
"encoding/json"
"io/ioutil"
"net"
"os"
"time"
"github.com/go-playground/validator/v10"
"github.com/vishvananda/netlink"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
type DsnetReport struct {
ExternalIP net.IP
InterfaceName string
ListenPort int
// domain to append to hostnames. Relies on separate DNS server for
// resolution. Informational only.
Domain string
IP net.IP
IP6 net.IP
// IP network from which to allocate automatic sequential addresses
// Network is chosen randomly when not specified
Network JSONIPNet
Network6 JSONIPNet
DNS net.IP
PeersOnline int
PeersTotal int
Peers []PeerReport
ReceiveBytes uint64
TransmitBytes uint64
ReceiveBytesSI string
TransmitBytesSI string
// when the report was made
Timestamp time.Time
}
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
linkDev, err := netlink.LinkByName(conf.InterfaceName)
check(err)
stats := linkDev.Attrs().Statistics
for _, peer := range dev.Peers {
wgPeerIndex[peer.PublicKey] = peer
}
if oldReport != nil {
for _, report := range oldReport.Peers {
oldPeerReportIndex[report.Hostname] = report
}
}
for i, peer := range conf.Peers {
wgPeer, known := wgPeerIndex[peer.PublicKey.Key]
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 {
peersOnline++
}
externalIP := net.IP{}
if wgPeer.Endpoint != nil {
externalIP = wgPeer.Endpoint.IP
}
uReceiveBytes := uint64(wgPeer.ReceiveBytes)
uTransmitBytes := uint64(wgPeer.TransmitBytes)
peerReports[i] = PeerReport{
Hostname: peer.Hostname,
Online: online,
Dormant: dormant,
Owner: peer.Owner,
Description: peer.Description,
Added: peer.Added,
IP: peer.IP,
IP6: peer.IP6,
ExternalIP: externalIP,
Networks: peer.Networks,
LastHandshakeTime: wgPeer.LastHandshakeTime,
ReceiveBytes: uReceiveBytes,
TransmitBytes: uTransmitBytes,
ReceiveBytesSI: BytesToSI(uReceiveBytes),
TransmitBytesSI: BytesToSI(uTransmitBytes),
}
}
return DsnetReport{
ExternalIP: conf.ExternalIP,
InterfaceName: conf.InterfaceName,
ListenPort: conf.ListenPort,
Domain: conf.Domain,
IP: conf.IP,
IP6: conf.IP6,
Network: conf.Network,
Network6: conf.Network6,
DNS: conf.DNS,
Peers: peerReports,
PeersOnline: peersOnline,
PeersTotal: len(peerReports),
ReceiveBytes: stats.RxBytes,
TransmitBytes: stats.TxBytes,
ReceiveBytesSI: BytesToSI(stats.RxBytes),
TransmitBytesSI: BytesToSI(stats.TxBytes),
Timestamp: time.Now(),
}
}
func (report *DsnetReport) MustSave(filename string) {
_json, _ := json.MarshalIndent(report, "", " ")
err := ioutil.WriteFile(filename, _json, 0644)
check(err)
}
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
Hostname string
// username of person running this host/router
Owner string
// Description of what the host is and/or does
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
Added time.Time
// Internal VPN IP address. Added to AllowedIPs in server config as a /32
IP net.IP
IP6 net.IP
// Last known external IP
ExternalIP net.IP
// TODO ExternalIP support (Endpoint)
//ExternalIP net.UDPAddr `validate:"required,udp4_addr"`
// TODO support routing additional networks (AllowedIPs)
Networks []JSONIPNet
LastHandshakeTime time.Time
ReceiveBytes uint64
TransmitBytes uint64
ReceiveBytesSI string
TransmitBytesSI string
}