forked from berty/berty
-
Notifications
You must be signed in to change notification settings - Fork 3
/
api_push.go
313 lines (251 loc) · 9.81 KB
/
api_push.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
package bertyprotocol
import (
"bytes"
"context"
crand "crypto/rand"
"crypto/tls"
"encoding/base64"
"fmt"
"sync"
"time"
"github.com/ipfs/go-cid"
"github.com/libp2p/go-libp2p-core/crypto"
"go.uber.org/zap"
"golang.org/x/crypto/nacl/box"
"google.golang.org/grpc"
"google.golang.org/grpc/backoff"
"google.golang.org/grpc/credentials"
"berty.tech/berty/v2/go/internal/cryptoutil"
"berty.tech/berty/v2/go/internal/logutil"
"berty.tech/berty/v2/go/pkg/errcode"
"berty.tech/berty/v2/go/pkg/protocoltypes"
"berty.tech/berty/v2/go/pkg/pushtypes"
)
func PushSealTokenForServer(receiver *protocoltypes.PushServiceReceiver, server *protocoltypes.PushServer) (*protocoltypes.PushMemberTokenUpdate, error) {
if server == nil || len(server.ServerKey) != cryptoutil.KeySize {
return nil, errcode.ErrInvalidInput.Wrap(fmt.Errorf("expected a server key of %d bytes", cryptoutil.KeySize))
}
if receiver == nil {
return nil, errcode.ErrInvalidInput.Wrap(fmt.Errorf("expected the receiver value to be defined"))
}
serverKey := [cryptoutil.KeySize]byte{}
copy(serverKey[:], server.ServerKey)
opaqueToken, err := receiver.Marshal()
if err != nil {
return nil, errcode.ErrSerialization.Wrap(err)
}
opaqueToken, err = box.SealAnonymous(nil, opaqueToken, &serverKey, crand.Reader)
if err != nil {
return nil, err
}
return &protocoltypes.PushMemberTokenUpdate{
Token: opaqueToken,
Server: server,
}, nil
}
func (s *service) getPushClient(host string) (pushtypes.PushServiceClient, error) {
s.muPushClients.Lock()
defer s.muPushClients.Unlock()
if cc, ok := s.pushClients[host]; ok {
return pushtypes.NewPushServiceClient(cc), nil
}
var creds grpc.DialOption
if s.grpcInsecure {
creds = grpc.WithInsecure()
} else {
tlsconfig := credentials.NewTLS(&tls.Config{
MinVersion: tls.VersionTLS12,
})
creds = grpc.WithTransportCredentials(tlsconfig)
}
// retry policies
connectParams := grpc.WithConnectParams(grpc.ConnectParams{
Backoff: backoff.Config{
BaseDelay: 1.0 * time.Second,
Multiplier: 1.5,
Jitter: 0.2,
MaxDelay: 60 * time.Second,
},
MinConnectTimeout: time.Second * 10,
})
cc, err := grpc.DialContext(s.ctx, host, creds, connectParams)
if err != nil {
return nil, err
}
s.pushClients[host] = cc
// monitor push client state
go monitorPushServer(s.ctx, cc, s.logger)
return pushtypes.NewPushServiceClient(cc), err
}
func (s *service) PushReceive(ctx context.Context, request *protocoltypes.PushReceive_Request) (*protocoltypes.PushReceive_Reply, error) {
return s.pushHandler.PushReceive(ctx, request.Payload)
}
func (s *service) PushSend(ctx context.Context, request *protocoltypes.PushSend_Request) (*protocoltypes.PushSend_Reply, error) {
gc, err := s.GetContextGroupForID(request.GroupPublicKey)
if err != nil {
return nil, err
}
_, c, err := cid.CidFromBytes(request.CID)
if err != nil {
return nil, errcode.ErrInvalidInput.Wrap(err)
}
sealedMessageEnvelope, err := gc.messageStore.GetOutOfStoreMessageEnvelope(ctx, c)
if err != nil {
return nil, errcode.ErrInternal.Wrap(err)
}
pushTargets, err := s.getPushTargetsByServer(gc, request.GroupMembers)
if err != nil {
return nil, errcode.ErrInternal.Wrap(err)
}
if len(pushTargets) == 0 {
s.logger.Info("PushSend - pushing - no targets", logutil.PrivateString("cid", c.String()))
return &protocoltypes.PushSend_Reply{}, nil
}
wg := sync.WaitGroup{}
wg.Add(len(pushTargets))
for serverAddr, pushTokens := range pushTargets {
// @FIXME(gfanton): find a better way to get service token
go func(serverAddr string, pushTokens []*pushtypes.PushServiceOpaqueReceiver) {
s.logger.Info("PushSend - pushing", logutil.PrivateString("cid", c.String()), logutil.PrivateString("server", serverAddr))
defer wg.Done()
if len(pushTokens) == 0 {
s.logger.Info("no push receivers", logutil.PrivateString("push-server", serverAddr))
return
}
client, err := s.getPushClient(serverAddr)
if err != nil {
s.logger.Error("error while dialing push server", logutil.PrivateString("push-server", serverAddr), zap.Error(err))
return
}
_, err = client.Send(ctx, &pushtypes.PushServiceSend_Request{
Envelope: sealedMessageEnvelope,
Priority: pushtypes.PushServicePriority_PushPriorityNormal,
Receivers: pushTokens,
}, grpc.WaitForReady(true))
if err != nil {
s.logger.Error("error while dialing push server", logutil.PrivateString("push-server", serverAddr), zap.Error(err))
return
}
s.logger.Debug("send push notification successfully", logutil.PrivateString("cid", c.String()), logutil.PrivateString("endpoint", serverAddr))
}(serverAddr, pushTokens)
}
wg.Wait()
return &protocoltypes.PushSend_Reply{}, nil
}
func (s *service) getPushTargetsByServer(gc *GroupContext, targetGroupMembers []*protocoltypes.MemberWithDevices) (map[string][]*pushtypes.PushServiceOpaqueReceiver, error) {
pushTargets := []*protocoltypes.PushMemberTokenUpdate(nil)
serverTokens := map[string][]*pushtypes.PushServiceOpaqueReceiver{}
targetDevices := []crypto.PubKey(nil)
if len(targetGroupMembers) == 0 {
targetDevices = gc.metadataStore.ListOtherMembersDevices()
} else {
for _, memberAndDevices := range targetGroupMembers {
pk, err := crypto.UnmarshalEd25519PublicKey(memberAndDevices.MemberPK)
if err != nil {
return nil, errcode.ErrDeserialization.Wrap(err)
}
if len(memberAndDevices.DevicePKs) == 0 {
// If no devices provided push all devices
devices, err := gc.metadataStore.GetDevicesForMember(pk)
if err != nil {
return nil, errcode.ErrInternal.Wrap(err)
}
targetDevices = append(targetDevices, devices...)
continue
}
for _, pkB := range memberAndDevices.DevicePKs {
devPK, err := crypto.UnmarshalEd25519PublicKey(pkB)
if err != nil {
return nil, errcode.ErrDeserialization.Wrap(err)
}
member, err := gc.metadataStore.GetMemberByDevice(devPK)
if err != nil {
s.logger.Warn("error while retrieving member for device", zap.Error(err))
continue
}
if !member.Equals(devPK) {
s.logger.Warn("device does not belong to member")
continue
}
targetDevices = append(targetDevices, devPK)
}
}
}
for _, d := range targetDevices {
pushToken, err := gc.metadataStore.GetPushTokenForDevice(d)
if err != nil {
s.logger.Info("unable to get push token for device")
continue
}
pushTargets = append(pushTargets, pushToken)
}
for _, pushTarget := range pushTargets {
serverTokens[pushTarget.Server.ServiceAddr] = append(serverTokens[pushTarget.Server.ServiceAddr], &pushtypes.PushServiceOpaqueReceiver{OpaqueToken: pushTarget.Token})
}
return serverTokens, nil
}
func (s *service) PushShareToken(ctx context.Context, request *protocoltypes.PushShareToken_Request) (*protocoltypes.PushShareToken_Reply, error) {
gc, err := s.GetContextGroupForID(request.GroupPK)
if err != nil {
return nil, errcode.ErrInvalidInput.Wrap(err)
}
token, err := PushSealTokenForServer(request.Receiver, request.Server)
if err != nil {
return nil, errcode.ErrInternal.Wrap(err)
}
if _, err := gc.metadataStore.SendPushToken(ctx, token); err != nil {
return nil, err
}
s.logger.Debug("send push token done")
return &protocoltypes.PushShareToken_Reply{}, nil
}
func (s *service) PushSetDeviceToken(ctx context.Context, request *protocoltypes.PushSetDeviceToken_Request) (*protocoltypes.PushSetDeviceToken_Reply, error) {
s.lock.Lock()
defer s.lock.Unlock()
if request.Receiver == nil || request.Receiver.TokenType == pushtypes.PushServiceTokenType_PushTokenUndefined {
return nil, errcode.ErrInvalidInput.Wrap(fmt.Errorf("invalid push token provided"))
}
request.Receiver.RecipientPublicKey = s.pushHandler.PushPK()[:]
if currentReceiver := s.accountGroup.metadataStore.getCurrentDevicePushToken(); currentReceiver != nil && bytes.Equal(currentReceiver.Token, request.Receiver.Token) {
s.logger.Warn("push device token already set", logutil.PrivateString("b64 token", base64.StdEncoding.EncodeToString(request.Receiver.Token)))
return &protocoltypes.PushSetDeviceToken_Reply{}, nil
}
if _, err := s.accountGroup.metadataStore.RegisterDevicePushToken(ctx, request.Receiver); err != nil {
return nil, errcode.ErrInternal.Wrap(err)
}
s.logger.Debug("push token device set", zap.Int("token len", len(request.Receiver.Token)))
return &protocoltypes.PushSetDeviceToken_Reply{}, nil
}
func (s *service) PushSetServer(ctx context.Context, request *protocoltypes.PushSetServer_Request) (*protocoltypes.PushSetServer_Reply, error) {
s.lock.Lock()
defer s.lock.Unlock()
if request.Server == nil {
return nil, errcode.ErrInvalidInput.Wrap(fmt.Errorf("no push server provided"))
}
if currentServer := s.accountGroup.metadataStore.getCurrentDevicePushServer(); currentServer != nil &&
bytes.Equal(currentServer.ServerKey, request.Server.ServerKey) &&
currentServer.ServiceAddr == request.Server.ServiceAddr {
return &protocoltypes.PushSetServer_Reply{}, nil
}
if _, err := s.accountGroup.metadataStore.RegisterDevicePushServer(ctx, request.Server); err != nil {
return nil, errcode.ErrInternal.Wrap(err)
}
if err := s.pushHandler.UpdatePushServer(ctx, request.Server); err != nil {
return nil, errcode.ErrInternal.Wrap(err)
}
return &protocoltypes.PushSetServer_Reply{}, nil
}
func (s *service) GetCurrentDevicePushConfig() (*protocoltypes.PushServiceReceiver, *protocoltypes.PushServer) {
currentToken := s.accountGroup.metadataStore.getCurrentDevicePushToken()
currentServer := s.accountGroup.metadataStore.getCurrentDevicePushServer()
return currentToken, currentServer
}
func monitorPushServer(ctx context.Context, cc *grpc.ClientConn, logger *zap.Logger) {
currentState := cc.GetState()
for cc.WaitForStateChange(ctx, currentState) {
currentState = cc.GetState()
logger.Debug("push grpc client state updated",
logutil.PrivateString("target", cc.Target()),
logutil.PrivateString("state", currentState.String()))
}
}