-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
138 lines (118 loc) · 3.34 KB
/
main.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
package main
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"flag"
"fmt"
"log"
"net"
"os"
"strings"
"time"
pb "github.com/atsevan/wireguard-grpc/pb/wg"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
)
var (
host = flag.String("host", "localhost", "Wireguard GRPC server host")
port = flag.Int("port", 8080, "Wireguard GRPC server port")
certFile = flag.String("cert", "certs/client.crt", "path to RSA certificate")
keyFile = flag.String("key", "certs/client.key", "path to RSA Private key")
caFile = flag.String("ca", "certs/ca.crt", "path to CA certificate")
insecureFlag = flag.Bool("insecure", false, "no credentials in use")
)
func printPeer(p *pb.Peer) {
endpoint := net.UDPAddr{IP: p.Endpoint.GetIp(), Port: (int)(p.Endpoint.GetPort())}
allowedIPs := make([]string, 0, len(p.AllowedIps))
for _, ipn := range p.AllowedIps {
ip := net.IPNet{IP: ipn.GetIp(), Mask: ipn.GetIpMask()}
allowedIPs = append(allowedIPs, ip.String())
}
const f = `peer: %s
endpoint: %s
allowed ips: %s
latest handshake: %s
transfer: %d B received, %d B sent
`
fmt.Printf(
f,
base64.StdEncoding.EncodeToString(p.GetPublicKey()),
endpoint.String(),
strings.Join(allowedIPs, ", "),
p.LastHandshakeTime.AsTime(),
p.RecievedBytes,
p.TransmitBytes,
)
}
func printDevice(d *pb.Device) {
const f = `interface: %s (%s)
public key: %s
private key: (hidden)
listening port: %d
`
fmt.Printf(
f,
d.Name,
d.Type.String(),
base64.StdEncoding.EncodeToString(d.GetPublicKey()),
d.ListenPort)
}
func transportCredentialsFromTLS(certPath string, keyPath string, caPath string, serverName string) (credentials.TransportCredentials, error) {
certificate, err := tls.LoadX509KeyPair(*certFile, *keyFile)
if err != nil {
return nil, fmt.Errorf("read RSA key pair: %s", err)
}
ca, err := os.ReadFile(caPath)
if err != nil {
return nil, fmt.Errorf("read CA certificate: %s", err)
}
// Create a certificate pool and append the client certificates from the CA
certPool := x509.NewCertPool()
if ok := certPool.AppendCertsFromPEM(ca); !ok {
return nil, fmt.Errorf("failed to append client certs")
}
return credentials.NewTLS(&tls.Config{
ServerName: serverName,
Certificates: []tls.Certificate{certificate},
RootCAs: certPool,
}), nil
}
func main() {
flag.Parse()
var err error
var creds credentials.TransportCredentials
if *insecureFlag == true {
log.Println("No transport security in use")
creds = insecure.NewCredentials()
} else {
creds, err = transportCredentialsFromTLS(*certFile, *keyFile, *caFile, *host)
if err != nil {
log.Fatalf("trasport credentials from TLS: %s", err)
}
}
opts := []grpc.DialOption{
grpc.WithTransportCredentials(creds),
}
ctx, cancelCtx := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelCtx()
conn, err := grpc.DialContext(ctx, fmt.Sprintf("%s:%d", *host, *port), opts...)
if err != nil {
log.Fatalf("gRPC client connection: %v", err)
}
defer conn.Close()
client := pb.NewWireGuardClient(conn)
devices, err := client.Devices(ctx, &pb.DevicesRequest{})
if err != nil {
log.Fatalf("get devices: %v", err)
}
for _, dev := range devices.Devices {
printDevice(dev)
for _, peer := range dev.Peers {
printPeer(peer)
}
}
log.Println("done")
}