-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
226 lines (196 loc) · 5.34 KB
/
main.go
File metadata and controls
226 lines (196 loc) · 5.34 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package main
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"flag"
"fmt"
"log"
"net"
"os"
"strings"
"time"
"github.com/atsevan/wireguard-grpc/client/testsetup"
pb "github.com/atsevan/wireguard-grpc/pb/wg"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"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")
confDevice = flag.Bool("configuretest", false, "configure 'wg0' device and add a peer")
)
const (
peerTmpl = `peer: %s
endpoint: %s
allowed ips: %s
latest handshake: %s
transfer: %d B received, %d B sent
`
deviceTmpl = `
interface: %s (%s)
public key: %s
private key: (hidden)
listening port: %d
`
peerConfigTmpl = `
## BEGIN(Client configugration)
[Interface]
PrivateKey = %s
Address = %s/24
[Peer]
PublicKey = %s
AllowedIPs = 0.0.0.0/0, ::/0
Endpoint = %s:%d
## END(Client configuration)
`
)
func stringPeer(p *pb.Peer) string {
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())
}
return fmt.Sprintf(
peerTmpl,
base64.StdEncoding.EncodeToString(p.GetPublicKey()),
endpoint.String(),
strings.Join(allowedIPs, ", "),
p.LastHandshakeTime.AsTime(),
p.RecievedBytes,
p.TransmitBytes,
)
}
// transportCredentialsFromTLS creates TransportCredentials based on TLS certificate
func transportCredentialsFromTLS(certPath string, keyPath string, caPath string, serverName string) (credentials.TransportCredentials, error) {
certificate, err := tls.LoadX509KeyPair(certPath, keyPath)
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)
if *confDevice == true {
ip := net.ParseIP("192.168.2.2")
devName := "wg0"
listenPort := int32(51820)
wgSetup, err := testsetup.NewTestWGSetup(client, devName, listenPort)
if err != nil {
log.Fatalf("create Wireguard setup: %v", err)
}
err = wgSetup.InitWGDevice(ctx)
if err != nil {
log.Fatalf("create Wireguard setup: %v", err)
}
peerPrivateKey, err := wgtypes.GeneratePrivateKey()
if err != nil {
log.Fatalf("generate peer key: %v", err)
}
peerPublicKey := peerPrivateKey.PublicKey()
peer := &pb.PeerConfig{
PublicKey: peerPublicKey[:],
AllowedIps: []*pb.IPNet{
{
Ip: ip,
IpMask: net.CIDRMask(32, 32), // /32 mask
},
},
ReplaceAllowedIps: true,
}
err = wgSetup.AddPeer(ctx, peer)
if err != nil {
log.Fatalf("add peer: %s", err)
}
log.Println("New peer has been added")
fmt.Printf(
peerConfigTmpl,
peerPrivateKey,
ip,
wgSetup.PublicKey,
*host,
listenPort,
)
fmt.Println()
}
log.Println("Wireguard configuration")
devices, err := client.Devices(ctx, &pb.DevicesRequest{})
if err != nil {
log.Fatalf("get devices: %v", err)
}
for _, dev := range devices.Devices {
fmt.Printf(
deviceTmpl,
dev.Name,
dev.Type,
base64.StdEncoding.EncodeToString(dev.GetPublicKey()),
dev.ListenPort)
for _, peer := range dev.Peers {
endpoint := &net.UDPAddr{
IP: peer.Endpoint.GetIp(),
Port: (int)(peer.Endpoint.GetPort()),
}
allowedIPs := make([]string, 0, len(peer.AllowedIps))
for _, ipn := range peer.AllowedIps {
ip := net.IPNet{IP: ipn.GetIp(), Mask: ipn.GetIpMask()}
allowedIPs = append(allowedIPs, ip.String())
}
fmt.Printf(
peerTmpl,
base64.StdEncoding.EncodeToString(peer.GetPublicKey()),
endpoint,
strings.Join(allowedIPs, ", "),
peer.LastHandshakeTime.AsTime(),
peer.RecievedBytes,
peer.TransmitBytes,
)
}
}
log.Println("Done")
}