-
Notifications
You must be signed in to change notification settings - Fork 2
/
status.go
91 lines (77 loc) · 1.57 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
package cmd
import (
"fmt"
"log"
"strings"
"github.com/spf13/cobra"
"golang.zx2c4.com/wireguard/wgctrl"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
func statusCmd() *cobra.Command {
return &cobra.Command{
Use: "status",
Aliases: []string{"s"},
Short: "Display SORACOM Arc interface status",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
c, err := wgctrl.New()
if err != nil {
log.Fatalf("failed to open wgctrl: %v", err)
}
defer func() {
err := c.Close()
if err != nil {
log.Printf("failed to close wgctrl: %v ", err)
}
}()
var devices []*wgtypes.Device
devices, err = c.Devices()
if err != nil {
log.Fatalf("failed to get devices: %v", err)
}
if len(devices) == 0 {
fmt.Println("no SORACOM Arc device found")
}
for _, d := range devices {
printDevice(d)
for _, p := range d.Peers {
printPeer(p)
}
}
},
}
}
func printDevice(d *wgtypes.Device) {
const f = `interface: %s (%s)
public key: %s
private key: (hidden)
listening port: %d
`
fmt.Printf(
f,
d.Name,
d.Type.String(),
d.PublicKey.String(),
d.ListenPort)
}
func printPeer(p wgtypes.Peer) {
const f = `peer: %s
endpoint: %s
allowed ips: %s
latest handshake: %s
transfer: %d B received, %d B sent
`
ips := make([]string, 0, len(p.AllowedIPs))
for _, ip := range p.AllowedIPs {
ips = append(ips, ip.String())
}
fmt.Printf(
f,
p.PublicKey.String(),
p.Endpoint.String(),
strings.Join(ips, ", "),
p.LastHandshakeTime.String(),
p.ReceiveBytes,
p.TransmitBytes,
)
}