-
Notifications
You must be signed in to change notification settings - Fork 3
/
status.go
119 lines (101 loc) · 2.98 KB
/
status.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
package main
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/apognu/wgctl/lib"
"github.com/apognu/wgctl/wireguard"
"github.com/sirupsen/logrus"
nl "github.com/vishvananda/netlink"
)
func status(instance string, short bool, all bool) {
if instance == "" {
statusAll(short)
return
}
l, err := nl.LinkByName(instance)
if err != nil {
if !short {
Down("tunnel '%s' is down", instance)
}
if !all {
os.Exit(1)
}
return
}
if l.Type() == wireguard.NetlinkName {
if short {
fmt.Printf("%s\n", instance)
} else {
Up("tunnel '%s' is up and running", instance)
}
} else {
if !short {
Down("interface '%s' does not seem to be a WireGuard device", instance)
}
if !all {
os.Exit(1)
}
}
}
func statusAll(short bool) {
instances, err := filepath.Glob(fmt.Sprintf("%s/*.yml", lib.GetConfigPath()))
if err != nil {
logrus.Fatalf("could not enumerate your configurations: %s", err.Error())
}
for _, path := range instances {
i := lib.GetInstanceFromArg(path)
status(i, short, true)
}
}
func info(instance string) {
config, err := lib.ParseConfig(instance)
if err != nil {
logrus.Fatalf("could not parse configuration: %s", err.Error())
}
dev, _, err := wireguard.GetDevice(instance)
if err != nil {
logrus.Fatalf("could not retrieve device information: %s", err.Error())
}
description := "<no description provided>"
if len(config.Description) > 0 {
description = config.Description
}
PrintSection(0, "tunnel", description, tunnelColor)
PrintAttr(1, "interface", dev.Name, true)
PrintAttr(1, "public key", dev.PublicKey.String(), true)
PrintAttr(1, "port", strconv.Itoa(dev.ListenPort), true)
PrintAttr(1, "fwmark", strconv.Itoa(dev.FirewallMark), dev.FirewallMark > 0)
if len(dev.Peers) > 0 {
for _, p := range dev.Peers {
description := "<no description provided>"
if peerSpec := config.GetPeer(p.PublicKey.String()); peerSpec != nil {
if len(peerSpec.Description) > 0 {
description = peerSpec.Description
}
}
PrintSection(1, "peer", description, peerColor)
PrintAttr(2, "public key", p.PublicKey.String(), true)
if p.Endpoint != nil {
if p.Endpoint.IP.To4() != nil {
PrintAttr(2, "endpoint", "%s:%d", true, p.Endpoint.IP, p.Endpoint.Port)
} else {
PrintAttr(2, "endpoint", "[%s]:%d", true, p.Endpoint.IP, p.Endpoint.Port)
}
}
PrintAttr(2, "pre-shared key", FormatPSK(p.PresharedKey), p.PresharedKey != lib.EmptyPSK)
if len(p.AllowedIPs) > 0 {
ips := make([]string, len(p.AllowedIPs))
for idx, ip := range p.AllowedIPs {
ips[idx] = FormatSubnet(ip)
}
PrintAttr(2, "allowed ips", strings.Join(ips, ", "), true)
}
PrintAttr(2, "last handshake", FormatInterval(p.LastHandshakeTime), p.LastHandshakeTime.Year() > 1970)
PrintAttr(2, "keepalive", fmt.Sprintf("every %.0f seconds", p.PersistentKeepaliveInterval.Seconds()), p.PersistentKeepaliveInterval > 0)
PrintAttr(2, "transfer", FormatTransfer(p.ReceiveBytes, p.TransmitBytes), true)
}
}
}