/
server_daemon.go
426 lines (339 loc) · 10.2 KB
/
server_daemon.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// SPDX-FileCopyrightText: 2023 Steffen Vogel <post@steffenvogel.de>
// SPDX-License-Identifier: Apache-2.0
package rpc
import (
"context"
"encoding"
"errors"
"fmt"
"io"
"net"
"reflect"
"regexp"
"strings"
"github.com/mitchellh/mapstructure"
"github.com/spf13/cobra"
"go.uber.org/zap"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"cunicu.li/cunicu/pkg/buildinfo"
"cunicu.li/cunicu/pkg/crypto"
"cunicu.li/cunicu/pkg/daemon"
"cunicu.li/cunicu/pkg/daemon/feature/epdisc"
osx "cunicu.li/cunicu/pkg/os"
"cunicu.li/cunicu/pkg/proto"
coreproto "cunicu.li/cunicu/pkg/proto/core"
rpcproto "cunicu.li/cunicu/pkg/proto/rpc"
slicesx "cunicu.li/cunicu/pkg/types/slices"
)
var errNoSettingChanged = errors.New("no setting was changed")
type DaemonServer struct {
rpcproto.UnimplementedDaemonServer
*Server
*daemon.Daemon
}
func NewDaemonServer(s *Server, d *daemon.Daemon) *DaemonServer {
ds := &DaemonServer{
Server: s,
Daemon: d,
}
rpcproto.RegisterDaemonServer(s.grpc, ds)
d.AddInterfaceHandler(ds)
return ds
}
func (s *DaemonServer) StreamEvents(_ *proto.Empty, stream rpcproto.Daemon_StreamEventsServer) error {
// Send initial connection state of all peers
s.SendPeerStates(stream)
events := s.events.Add()
defer s.events.Remove(events)
out:
for {
select {
case event, ok := <-events:
if !ok {
break out
}
if err := stream.Send(event); errors.Is(err, io.EOF) {
break out
} else if err != nil {
return fmt.Errorf("failed to send event: %w", err)
}
case <-stream.Context().Done():
break out
}
}
return nil
}
func (s *DaemonServer) GetBuildInfo(context.Context, *proto.Empty) (*proto.BuildInfo, error) {
return buildinfo.BuildInfo(), nil
}
func (s *DaemonServer) UnWait(_ context.Context, _ *proto.Empty) (*proto.Empty, error) {
err := status.Error(codes.AlreadyExists, "RPC socket has already been unwaited")
s.waitOnce.Do(func() {
s.waitGroup.Done()
err = nil
})
return &proto.Empty{}, err
}
func (s *DaemonServer) Shutdown(_ context.Context, params *rpcproto.ShutdownParams) (*proto.Empty, error) {
if params.Restart && !osx.ReexecSelfSupported {
return nil, status.Error(codes.Unimplemented, "not supported on this platform")
}
s.Daemon.Shutdown(params.Restart)
return &proto.Empty{}, nil
}
func (s *DaemonServer) Sync(_ context.Context, _ *proto.Empty) (*proto.Empty, error) {
if err := s.Daemon.Sync(); err != nil {
return nil, status.Errorf(codes.Unknown, "failed to sync: %s", err)
}
return &proto.Empty{}, nil
}
func (s *DaemonServer) GetStatus(_ context.Context, p *rpcproto.GetStatusParams) (*rpcproto.GetStatusResp, error) { //nolint:gocognit
var err error
var pk crypto.Key
if p.Peer != nil {
if pk, err = crypto.ParseKeyBytes(p.Peer); err != nil {
return nil, status.Error(codes.InvalidArgument, "invalid peer key")
}
}
qis := []*coreproto.Interface{}
if err := s.daemon.ForEachInterface(func(i *daemon.Interface) error {
epi := epdisc.Get(i)
if p.Interface == "" || i.Name() == p.Interface {
qi := i.MarshalWithPeers(func(cp *daemon.Peer) *coreproto.Peer {
if pk.IsSet() && pk != cp.PublicKey() {
return nil
}
qp := cp.Marshal()
if epi != nil {
if epp, ok := epi.Peers[cp]; ok {
qp.Ice = epp.Marshal()
qp.Reachability = epp.Reachability()
}
}
return qp
})
if epi != nil {
qi.Ice = epi.Marshal()
}
qis = append(qis, qi)
}
return nil
}); err != nil {
return nil, status.Errorf(codes.Internal, "failed to marshal interface: %s", err)
}
// Check if filters matched anything
if p.Interface != "" && len(qis) == 0 {
return nil, status.Errorf(codes.NotFound, "no such interface '%s'", p.Interface)
} else if pk.IsSet() && len(qis[0].Peers) == 0 {
return nil, status.Errorf(codes.NotFound, "no such peer '%s' for interface '%s'", pk, p.Interface)
}
return &rpcproto.GetStatusResp{
Interfaces: qis,
}, nil
}
func (s *DaemonServer) SetConfig(_ context.Context, p *rpcproto.SetConfigParams) (*proto.Empty, error) {
settings := map[string]any{}
for key, value := range p.Settings {
switch {
case value.Scalar != "":
settings[key] = value.Scalar
case len(value.List) > 0:
settings[key] = value.List
default:
settings[key] = nil // Unset
}
}
if changes, err := s.Config.Update(settings); err != nil {
return nil, decodeError(err)
} else if len(changes) == 0 {
return nil, status.Error(codes.InvalidArgument, errNoSettingChanged.Error())
}
return &proto.Empty{}, nil
}
func (s *DaemonServer) GetConfig(_ context.Context, p *rpcproto.GetConfigParams) (*rpcproto.GetConfigResp, error) {
settings := map[string]*rpcproto.ConfigValue{}
for key, value := range s.Config.All() {
if p.KeyFilter != "" && !strings.HasPrefix(key, p.KeyFilter) {
continue
}
str, err := settingToValue(value)
if err != nil {
return nil, status.Errorf(codes.Internal, "Failed to marshal: %s", err)
}
settings[key] = str
}
return &rpcproto.GetConfigResp{
Settings: settings,
}, nil
}
func (s *DaemonServer) GetCompletion(_ context.Context, params *rpcproto.GetCompletionParams) (*rpcproto.GetCompletionResp, error) {
var options []string
var flags cobra.ShellCompDirective
switch {
case len(params.Cmd) < 2 || params.Cmd[0] != "cunicu":
flags = cobra.ShellCompDirectiveError
case params.Cmd[1] == "config":
flags = cobra.ShellCompDirectiveNoFileComp
options = s.getConfigCompletion(params.Cmd[2], params.Args, params.ToComplete)
default:
flags = cobra.ShellCompDirectiveNoFileComp
}
return &rpcproto.GetCompletionResp{
Options: options,
Flags: int32(flags),
}, nil
}
func (s *DaemonServer) getConfigCompletion(cmd string, args []string, toComplete string) []string {
var options []string
if isValueCompletion := len(args) > 0; isValueCompletion {
if cmd != "set" {
return nil
}
if meta := s.Config.Meta.Lookup(args[0]); meta != nil {
options = meta.CompletionOptions()
}
} else {
options = s.Config.Meta.Keys()
}
if toComplete != "" {
options = slicesx.Filter(options, func(s string) bool {
return strings.HasPrefix(s, toComplete)
})
}
return options
}
func (s *DaemonServer) ReloadConfig(_ context.Context, _ *proto.Empty) (*proto.Empty, error) {
if _, err := s.Config.ReloadAllSources(); err != nil {
return nil, decodeError(err)
}
return &proto.Empty{}, nil
}
func (s *DaemonServer) AddPeer(_ context.Context, params *rpcproto.AddPeerParams) (*rpcproto.AddPeerResp, error) {
i := s.InterfaceByName(params.Interface)
if i == nil {
return nil, status.Errorf(codes.NotFound, "Interface %s does not exist", params.Interface)
}
// Add peer to running daemon
pk, err := crypto.ParseKeyBytes(params.PublicKey)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "Invalid public key")
}
if err := i.AddPeer(&wgtypes.PeerConfig{
PublicKey: wgtypes.Key(pk),
}); err != nil {
return nil, status.Errorf(codes.Internal, "Failed to add peer: %s", err)
}
// TODO: Persist new peer in runtime config
// Create response
resp := &rpcproto.AddPeerResp{
Invitation: &rpcproto.Invitation{},
Interface: i.Marshal(),
}
if community := crypto.Key(i.Settings.Community); community.IsSet() {
resp.Invitation.Community = community.Bytes()
}
// Detect our own endpoint
if epi := epdisc.Get(i); epi != nil {
if ep, err := epi.Endpoint(); err != nil {
s.logger.Warn("Failed to determine our own endpoint address", zap.Error(err))
} else if ep != nil {
resp.Invitation.Endpoint = ep.String()
// Perform reverse lookup of our own endpoint
if names, err := net.LookupAddr(resp.Invitation.Endpoint); err == nil && len(names) >= 1 {
epName := names[0]
// Do not use auto-generated IPv4 rDNS names
if match, _ := regexp.MatchString(`\d{1,3}-\d{1,3}-\d{1,3}-\d{1,3}`, epName); !match {
resp.Invitation.Endpoint = fmt.Sprintf("%s:%d", epName, ep.Port)
}
}
}
}
return resp, nil
}
func (s *DaemonServer) OnInterfaceAdded(i *daemon.Interface) {
i.AddPeerStateChangeHandler(s)
}
func (s *DaemonServer) OnInterfaceRemoved(_ *daemon.Interface) {
}
func (s *DaemonServer) SendPeerStates(stream rpcproto.Daemon_StreamEventsServer) {
if err := s.daemon.ForEachInterface(func(di *daemon.Interface) error {
if i := epdisc.Get(di); i != nil {
for _, p := range i.Peers {
e := &rpcproto.Event{
Type: rpcproto.EventType_PEER_STATE_CHANGED,
Interface: p.Interface.Name(),
Peer: p.Peer.PublicKey().Bytes(),
Event: &rpcproto.Event_PeerStateChange{
PeerStateChange: &rpcproto.PeerStateChangeEvent{
NewState: p.State(),
},
},
}
if err := stream.Send(e); errors.Is(err, io.EOF) {
continue
} else if err != nil {
s.logger.Error("Failed to send connection states", zap.Error(err))
}
}
}
return nil
}); err != nil {
s.logger.Error("Failed to send connection states", zap.Error(err))
}
}
func (s *DaemonServer) OnPeerStateChanged(p *daemon.Peer, newState, prevState daemon.PeerState) {
s.events.Send(&rpcproto.Event{
Type: rpcproto.EventType_PEER_STATE_CHANGED,
Interface: p.Interface.Name(),
Peer: p.PublicKey().Bytes(),
Event: &rpcproto.Event_PeerStateChange{
PeerStateChange: &rpcproto.PeerStateChangeEvent{
NewState: newState,
PrevState: prevState,
},
},
})
}
func serializeToString(in any) (string, error) {
if tm, ok := in.(encoding.TextMarshaler); ok {
b, err := tm.MarshalText()
if err != nil {
return "", err
}
return string(b), nil
}
return fmt.Sprint(in), nil
}
func settingToValue(val any) (*rpcproto.ConfigValue, error) {
cval := &rpcproto.ConfigValue{}
if val == nil {
return cval, nil
} else if rval := reflect.ValueOf(val); rval.Kind() == reflect.Slice {
for i := 0; i < rval.Len(); i++ {
e := rval.Index(i)
s, err := serializeToString(e.Interface())
if err != nil {
return nil, err
}
cval.List = append(cval.List, s)
}
} else {
cval.Scalar = fmt.Sprint(val)
}
return cval, nil
}
func decodeError(err error) error {
var msErr *mapstructure.Error
if errors.As(err, &msErr) {
sts := status.New(codes.InvalidArgument, "Failed to decode")
for _, err := range msErr.Errors {
sts, _ = sts.WithDetails(&proto.Error{
Message: err,
})
}
return sts.Err()
}
return status.Error(codes.InvalidArgument, err.Error())
}