-
Notifications
You must be signed in to change notification settings - Fork 28
/
service_outofstoremessage.go
271 lines (224 loc) · 7.11 KB
/
service_outofstoremessage.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
package weshnet
import (
"context"
"fmt"
"io"
"time"
ds "github.com/ipfs/go-datastore"
ds_sync "github.com/ipfs/go-datastore/sync"
coreiface "github.com/ipfs/kubo/core/coreiface"
"go.uber.org/zap"
"google.golang.org/grpc"
"berty.tech/weshnet/v2/pkg/errcode"
"berty.tech/weshnet/v2/pkg/grpcutil"
"berty.tech/weshnet/v2/pkg/outofstoremessagetypes"
"berty.tech/weshnet/v2/pkg/protocoltypes"
"berty.tech/weshnet/v2/pkg/secretstore"
)
type OOSMService interface {
outofstoremessagetypes.OutOfStoreMessageServiceServer
}
var _ OOSMService = (*oosmService)(nil)
type oosmService struct {
logger *zap.Logger
rootDatastore ds.Datastore
secretStore secretstore.SecretStore
outofstoremessagetypes.UnimplementedOutOfStoreMessageServiceServer
}
type OOSMServiceClient interface {
outofstoremessagetypes.OutOfStoreMessageServiceClient
io.Closer
}
type oosmServiceClient struct {
OOSMServiceClient
service OOSMService
server *grpc.Server
}
type OOSMOption func(*oosmService) error
// NewOutOfStoreMessageServiceClient creates a new Wesh protocol service and returns a gRPC
// ServiceClient which uses a direct in-memory connection. When finished, you must call Close().
// This opens or creates a Wesh account where the datastore location is specified by the path argument.
// The service will not start any network stuff, it will only use the filesystem to store or get data.
func NewOutOfStoreMessageServiceClient(opts ...OOSMOption) (OOSMServiceClient, error) {
svc, err := NewOutOfStoreMessageService(opts...)
if err != nil {
return nil, err
}
s := grpc.NewServer()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
c, err := newClientFromService(ctx, s, svc)
if err != nil {
return nil, fmt.Errorf("uanble to create client from server: %w", err)
}
return &oosmServiceClient{
OOSMServiceClient: c,
server: s,
service: svc,
}, nil
}
type oosmClient struct {
outofstoremessagetypes.OutOfStoreMessageServiceClient
l *grpcutil.BufListener
cc *grpc.ClientConn
}
func (c *oosmClient) Close() error {
err := c.cc.Close()
_ = c.l.Close()
return err
}
func newClientFromService(ctx context.Context, s *grpc.Server, svc OOSMService, opts ...grpc.DialOption) (OOSMServiceClient, error) {
bl := grpcutil.NewBufListener(ClientBufferSize)
cc, err := bl.NewClientConn(ctx, opts...)
if err != nil {
return nil, err
}
outofstoremessagetypes.RegisterOutOfStoreMessageServiceServer(s, svc)
go func() {
// we dont need to log the error
_ = s.Serve(bl)
}()
return &oosmClient{
OutOfStoreMessageServiceClient: outofstoremessagetypes.NewOutOfStoreMessageServiceClient(cc),
cc: cc,
l: bl,
}, nil
}
func NewOutOfStoreMessageService(opts ...OOSMOption) (OOSMService, error) {
svc := &oosmService{}
withDefaultOpts := make([]OOSMOption, len(opts))
copy(withDefaultOpts, opts)
withDefaultOpts = append(withDefaultOpts, WithFallbackDefaults)
for _, opt := range withDefaultOpts {
if err := opt(svc); err != nil {
return nil, err
}
}
return svc, nil
}
func (s *oosmService) Close() error {
return nil
}
func (s *oosmService) Status() (Status, error) {
return Status{}, nil
}
func (s *oosmService) IpfsCoreAPI() coreiface.CoreAPI {
return nil
}
func (s *oosmService) OutOfStoreReceive(ctx context.Context, request *protocoltypes.OutOfStoreReceive_Request) (*protocoltypes.OutOfStoreReceive_Reply, error) {
outOfStoreMessage, group, clearPayload, alreadyDecrypted, err := s.secretStore.OpenOutOfStoreMessage(ctx, request.Payload)
if err != nil {
return nil, errcode.ErrCode_ErrCryptoDecrypt.Wrap(err)
}
return &protocoltypes.OutOfStoreReceive_Reply{
Message: outOfStoreMessage,
Cleartext: clearPayload,
GroupPublicKey: group.PublicKey,
AlreadyReceived: alreadyDecrypted,
}, nil
}
// FallBackOption is a structure that permit to fallback to a default option if the option is not set.
type FallBackOption struct {
fallback func(s *oosmService) bool
opt OOSMOption
}
// WithLogger set the given logger.
var WithLogger = func(l *zap.Logger) OOSMOption {
return func(s *oosmService) error {
s.logger = l
return nil
}
}
// WithDefaultLogger init a noop logger.
var WithDefaultLogger OOSMOption = func(s *oosmService) error {
s.logger = zap.NewNop()
return nil
}
var fallbackLogger = FallBackOption{
fallback: func(s *oosmService) bool { return s.logger == nil },
opt: WithDefaultLogger,
}
// WithFallbackLogger set the logger if no logger is set.
var WithFallbackLogger OOSMOption = func(s *oosmService) error {
if fallbackLogger.fallback(s) {
return fallbackLogger.opt(s)
}
return nil
}
// WithRootDatastore set the root datastore.
var WithRootDatastore = func(ds ds.Datastore) OOSMOption {
return func(s *oosmService) error {
s.rootDatastore = ds
return nil
}
}
// WithDefaultRootDatastore init a in-memory datastore.
var WithDefaultRootDatastore OOSMOption = func(s *oosmService) error {
s.rootDatastore = ds_sync.MutexWrap(ds.NewMapDatastore())
return nil
}
var fallbackRootDatastore = FallBackOption{
fallback: func(s *oosmService) bool { return s.rootDatastore == nil },
opt: WithDefaultRootDatastore,
}
// WithFallbackRootDatastore set the root datastore if no root datastore is set.
var WithFallbackRootDatastore OOSMOption = func(s *oosmService) error {
if fallbackRootDatastore.fallback(s) {
return fallbackRootDatastore.opt(s)
}
return nil
}
// WithSecretStore set the secret store.
var WithSecretStore = func(ss secretstore.SecretStore) OOSMOption {
return func(s *oosmService) error {
s.secretStore = ss
return nil
}
}
// WithDefaultSecretStore init a new secret store.
// Call WithRootDatastore before this option if you want to use your datastore.
// Call WithLogger before this option if you want to use your logger.
var WithDefaultSecretStore OOSMOption = func(s *oosmService) error {
// dependency
if err := WithFallbackRootDatastore(s); err != nil {
return err
}
if err := WithFallbackLogger(s); err != nil {
return err
}
var err error
s.secretStore, err = secretstore.NewSecretStore(s.rootDatastore, &secretstore.NewSecretStoreOptions{
Logger: s.logger,
})
return err
}
var fallbackSecretStore = FallBackOption{
fallback: func(s *oosmService) bool { return s.secretStore == nil },
opt: WithDefaultSecretStore,
}
// WithFallbackSecretStore set the secret store if no secret store is set.
// Call WithRootDatastore before this option if you want to use your datastore if a new secret store is created.
// Call WithLogger before this option if you want to use your logger if a new secret store is created.
var WithFallbackSecretStore OOSMOption = func(s *oosmService) error {
if fallbackSecretStore.fallback(s) {
return fallbackSecretStore.opt(s)
}
return nil
}
var defaults = []FallBackOption{
fallbackLogger,
fallbackRootDatastore,
fallbackSecretStore,
}
// WithFallbackDefaults set the default options if no option is set.
var WithFallbackDefaults OOSMOption = func(s *oosmService) error {
for _, def := range defaults {
if !def.fallback(s) {
continue
}
if err := def.opt(s); err != nil {
return err
}
}
return nil
}