forked from mattn/tailscale-systray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.go
63 lines (49 loc) · 1.2 KB
/
json.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
package main
import (
"encoding/json"
"strings"
)
type Status struct {
TailscaleUp bool
Self Machine
Peers map[string]Machine
}
type Machine struct {
RawMachine
DisplayName Name
}
func (s *Status) UnmarshalJSON(data []byte) error {
rawStatus := new(rawStatus)
if err := json.Unmarshal(data, &rawStatus); err != nil {
return err
}
isRunning := strings.Contains(rawStatus.BackendState, "Running")
peers := map[string]Machine{}
for name, rawPeer := range rawStatus.Peers {
peers[name] = rawPeer.ToMachine(rawStatus.MagicDNSSuffix)
}
self := rawStatus.Self.ToMachine(rawStatus.MagicDNSSuffix)
*s = Status{
TailscaleUp: isRunning,
Self: self,
Peers: peers,
}
return nil
}
type rawStatus struct {
BackendState string
Self RawMachine `json:"Self"`
Peers map[string]RawMachine `json:"Peer"`
MagicDNSSuffix string `json:"MagicDNSSuffix"`
}
type RawMachine struct {
DNSName string `json:"DNSName"`
HostName string `json:"HostName"`
TailscaleIPs []string `json:"TailscaleIPs"`
}
func (rm RawMachine) ToMachine(dnsSuffix string) Machine {
return Machine{
RawMachine: rm,
DisplayName: dnsOrQuoteHostname(dnsSuffix, rm),
}
}