-
Notifications
You must be signed in to change notification settings - Fork 14
/
service_one_port.go
458 lines (390 loc) · 12.2 KB
/
service_one_port.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
package grpcinterface
import (
"bytes"
"context"
"crypto/tls"
"fmt"
"io/ioutil"
"net"
"net/http"
"path/filepath"
"github.com/tdex-network/tdex-daemon/internal/core/ports"
httpinterface "github.com/tdex-network/tdex-daemon/internal/interfaces/http"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
log "github.com/sirupsen/logrus"
daemonv1 "github.com/tdex-network/tdex-daemon/api-spec/protobuf/gen/tdex-daemon/v1"
tdexv1 "github.com/tdex-network/tdex-daemon/api-spec/protobuf/gen/tdex/v1"
"github.com/tdex-network/tdex-daemon/internal/interfaces"
grpchandler "github.com/tdex-network/tdex-daemon/internal/interfaces/grpc/handler"
"github.com/tdex-network/tdex-daemon/internal/interfaces/grpc/interceptor"
"github.com/tdex-network/tdex-daemon/internal/interfaces/grpc/permissions"
tdexold "github.com/tdex-network/tdex-protobuf/generated/go/trade"
"github.com/soheilhy/cmux"
"github.com/tdex-network/tdex-daemon/internal/core/application"
"github.com/tdex-network/tdex-daemon/pkg/macaroons"
"google.golang.org/grpc"
)
type ServiceOptsOnePort struct {
NoMacaroons bool
Datadir string
DBLocation string
MacaroonsLocation string
WalletUnlockPasswordFile string
Address string
WalletUnlockerSvc application.WalletUnlockerService
WalletSvc application.WalletService
OperatorSvc application.OperatorService
TradeSvc application.TradeService
RepoManager ports.RepoManager
TLSLocation string
NoTls bool
ExtraIPs []string
ExtraDomains []string
ConnectAddr string
ConnectProto string
}
type serviceOnePort struct {
opts ServiceOptsOnePort
macaroonSvc *macaroons.Service
grpcServer *grpc.Server
http1Server *http.Server
http2Server *http.Server
mux cmux.CMux
walletPassword string
passphraseChan chan application.PassphraseMsg
readyChan chan bool
}
func NewServiceOnePort(opts ServiceOptsOnePort) (interfaces.Service, error) {
if err := opts.validate(); err != nil {
return nil, fmt.Errorf("invalid opts: %s", err)
}
var macaroonSvc *macaroons.Service
if !opts.NoMacaroons {
macaroonSvc, _ = macaroons.NewService(
opts.dbDatadir(), Location, DBFile, false, macaroons.IPLockChecker,
)
if err := permissions.Validate(); err != nil {
return nil, err
}
}
if !opts.NoTls {
if err := generateOperatorTLSKeyCert(
opts.tlsDatadir(), opts.ExtraIPs, opts.ExtraDomains,
); err != nil {
return nil, err
}
}
var walletPassword string
if opts.WalletUnlockPasswordFile != "" {
walletPasswordBytes, err := ioutil.ReadFile(opts.WalletUnlockPasswordFile)
if err != nil {
return nil, err
}
trimmedPass := bytes.TrimFunc(walletPasswordBytes, func(r rune) bool {
return r == 10 || r == 32
})
walletPassword = string(trimmedPass)
}
return &serviceOnePort{
opts: opts,
macaroonSvc: macaroonSvc,
passphraseChan: opts.WalletUnlockerSvc.PassphraseChan(),
readyChan: opts.WalletUnlockerSvc.ReadyChan(),
walletPassword: walletPassword,
}, nil
}
func (s *serviceOnePort) Start() error {
walletUnlockerOnly := true
services, err := s.start(walletUnlockerOnly)
if err != nil {
return err
}
log.Infof("wallet unlocker interface is listening on %s", s.opts.Address)
go s.startListeningToPassphraseChan()
go s.startListeningToReadyChan()
s.grpcServer = services.grpcServer
s.http1Server = services.http1Server
s.http2Server = services.http2Server
s.mux = services.mux
return nil
}
func (s *serviceOnePort) startListeningToPassphraseChan() {
for msg := range s.passphraseChan {
if s.withMacaroons() {
switch msg.Method {
case application.UnlockWallet:
pwd := []byte(msg.CurrentPwd)
if err := s.macaroonSvc.CreateUnlock(&pwd); err != nil {
if err != macaroons.ErrAlreadyUnlocked {
log.WithError(err).Warn(
"an error occured while unlocking macaroon service",
)
}
}
ctx := context.Background()
if err := genMacaroons(
ctx, s.macaroonSvc, s.opts.macaroonsDatadir(),
); err != nil {
log.WithError(err).Warn("an error occured while creating macaroons")
}
case application.ChangePassphrase:
currentPwd := []byte(msg.CurrentPwd)
newPwd := []byte(msg.NewPwd)
if err := s.macaroonSvc.ChangePassword(currentPwd, newPwd); err != nil {
log.WithError(err).Warn(
"an error occured while changing password of macaroon service",
)
}
default:
pwd := []byte(msg.CurrentPwd)
if err := s.macaroonSvc.CreateUnlock(&pwd); err != nil {
log.WithError(err).Warn(
"an error occured while creating macaroon service",
)
}
ctx := context.Background()
if err := genMacaroons(
ctx, s.macaroonSvc, s.opts.macaroonsDatadir(),
); err != nil {
log.WithError(err).Warn("an error occured while creating macaroons")
}
}
}
}
}
func (s *serviceOnePort) startListeningToReadyChan() {
isReady := <-s.readyChan
dontStopMacaroonSvc := false
s.stop(dontStopMacaroonSvc)
if !isReady {
panic("failed to initialize wallet")
}
if s.walletPassword != "" {
if err := s.opts.WalletUnlockerSvc.UnlockWallet(context.Background(), s.walletPassword); err != nil {
panic(err)
}
s.walletPassword = ""
log.Infoln("wallet unlocked")
}
withoutUnlockerOnly := false
services, err := s.start(withoutUnlockerOnly)
if err != nil {
log.WithError(err).Warn(
"an error occured while enabling operator and trade interfaces. Shutting down",
)
panic(nil)
}
log.Infof("operator, trade interfaces is listening on %s", s.opts.Address)
s.grpcServer = services.grpcServer
s.http1Server = services.http1Server
s.http2Server = services.http2Server
s.mux = services.mux
}
func (s *serviceOnePort) stop(stopMacaroonSvc bool) {
if s.withMacaroons() && stopMacaroonSvc {
s.macaroonSvc.Close()
log.Debug("stopped macaroon service")
}
log.Debug("stop grpc-web server")
s.http1Server.Shutdown(context.Background())
s.http2Server.Shutdown(context.Background())
log.Debug("stop grpc server")
s.grpcServer.GracefulStop()
log.Debug("stop mux")
s.mux.Close()
}
func (s *serviceOnePort) Stop() {
stopMacaroonSvc := true
s.stop(stopMacaroonSvc)
}
func (s *serviceOnePort) start(withUnlockerOnly bool) (*serviceOnePort, error) {
unaryInterceptor := interceptor.UnaryInterceptor(s.macaroonSvc)
streamInterceptor := interceptor.StreamInterceptor(s.macaroonSvc)
var adminMacaroonPath string
if s.withMacaroons() {
adminMacaroonPath = filepath.Join(s.opts.macaroonsDatadir(), AdminMacaroonFile)
}
address := s.opts.Address
grpcServer := grpc.NewServer(
unaryInterceptor, streamInterceptor,
)
walletUnlockerHandler := grpchandler.NewWalletUnlockerHandler(
s.opts.WalletUnlockerSvc, adminMacaroonPath,
)
daemonv1.RegisterWalletUnlockerServiceServer(
grpcServer, walletUnlockerHandler,
)
var grpcGateway http.Handler
if !withUnlockerOnly {
walletHandler := grpchandler.NewWalletHandler(s.opts.WalletSvc)
operatorHandler := grpchandler.NewOperatorHandler(s.opts.OperatorSvc)
daemonv1.RegisterOperatorServiceServer(grpcServer, operatorHandler)
daemonv1.RegisterWalletServiceServer(grpcServer, walletHandler)
tradeHandler := grpchandler.NewTradeHandler(s.opts.TradeSvc)
tradeOldHandler := grpchandler.NewTradeOldHandler(s.opts.TradeSvc)
tdexv1.RegisterTradeServiceServer(grpcServer, tradeHandler)
tdexold.RegisterTradeServer(grpcServer, tradeOldHandler)
tradeGrpcGateway, err := s.tradeGrpcGateway(context.Background(), true)
if err != nil {
return nil, err
}
transportHandler := grpchandler.NewTransportHandler()
tdexv1.RegisterTransportServiceServer(grpcServer, transportHandler)
grpcGateway = tradeGrpcGateway
}
operatorTlsCert := s.opts.tlsCert()
operatorTlsKey := s.opts.tlsKey()
tdexConnectSvc, err := httpinterface.NewTdexConnectService(
s.opts.RepoManager,
s.opts.WalletUnlockerSvc,
adminMacaroonPath,
operatorTlsCert,
s.opts.ConnectAddr,
s.opts.ConnectProto,
)
if err != nil {
return nil, err
}
http1Server, http2Server := newGRPCWrappedServer(
address,
grpcServer,
grpcGateway,
map[string]func(w http.ResponseWriter, req *http.Request){
"/": tdexConnectSvc.RootHandler,
"/tdexdconnect": tdexConnectSvc.AuthHandler,
},
)
mux, err := serveMux(
s.opts.Address, operatorTlsKey, operatorTlsCert,
grpcServer, http1Server, http2Server,
)
if err != nil {
return nil, err
}
return &serviceOnePort{
opts: s.opts,
macaroonSvc: s.macaroonSvc,
grpcServer: grpcServer,
http1Server: http1Server,
http2Server: http2Server,
mux: mux,
walletPassword: s.walletPassword,
passphraseChan: s.passphraseChan,
readyChan: s.readyChan,
}, nil
}
func (s *serviceOnePort) withMacaroons() bool {
return s.macaroonSvc != nil
}
func (o ServiceOptsOnePort) macaroonsDatadir() string {
return filepath.Join(o.Datadir, o.MacaroonsLocation)
}
func (s *serviceOnePort) tradeGrpcGateway(
ctx context.Context, insecureConn bool,
) (http.Handler, error) {
creds := make([]grpc.DialOption, 0)
if insecureConn {
creds = append(creds, grpc.WithTransportCredentials(insecure.NewCredentials()))
} else {
// #nosec
creds = append(creds, grpc.WithTransportCredentials(
/* #nosec */
credentials.NewTLS(&tls.Config{
InsecureSkipVerify: true,
}),
))
}
conn, err := grpc.Dial(s.opts.Address, creds...)
if err != nil {
return nil, err
}
grpcGatewayMux := runtime.NewServeMux()
if err := tdexv1.RegisterTradeServiceHandler(ctx, grpcGatewayMux, conn); err != nil {
return nil, err
}
if err := tdexv1.RegisterTransportServiceHandler(ctx, grpcGatewayMux, conn); err != nil {
return nil, err
}
grpcGatewayHandler := http.Handler(grpcGatewayMux)
return grpcGatewayHandler, nil
}
func (o ServiceOptsOnePort) validate() error {
if !pathExists(o.Datadir) {
return fmt.Errorf("%s: datadir must be an existing directory", o.Datadir)
}
if !o.NoMacaroons {
macDir := o.macaroonsDatadir()
adminMacExists := pathExists(filepath.Join(macDir, AdminMacaroonFile))
roMacExists := pathExists(filepath.Join(macDir, ReadOnlyMacaroonFile))
marketMacExists := pathExists(filepath.Join(macDir, MarketMacaroonFile))
priceMacExists := pathExists(filepath.Join(macDir, PriceMacaroonFile))
if adminMacExists != roMacExists ||
adminMacExists != marketMacExists ||
adminMacExists != priceMacExists {
return fmt.Errorf(
"all macaroons must be either existing or not in path %s", macDir,
)
}
}
if !o.NoTls {
// TLS over operator interface is automatically enabled if macaroons auth
// is active.
tlsDir := o.tlsDatadir()
tlsKeyExists := pathExists(filepath.Join(tlsDir, OperatorTLSKeyFile))
tlsCertExists := pathExists(filepath.Join(tlsDir, OperatorTLSCertFile))
if !tlsKeyExists && tlsCertExists {
return fmt.Errorf(
"found %s file but %s is missing. Please delete %s to have the daemon recreate both in path %s",
OperatorTLSCertFile, OperatorTLSKeyFile, OperatorTLSCertFile, tlsDir,
)
}
if len(o.ExtraIPs) > 0 {
for _, ip := range o.ExtraIPs {
if net.ParseIP(ip) == nil {
return fmt.Errorf("invalid operator extra ip %s", ip)
}
}
}
}
if ok := isValidAddress(o.Address); !ok {
return fmt.Errorf("address is not valid: %s", o.Address)
}
if o.WalletUnlockPasswordFile != "" {
if !pathExists(o.WalletUnlockPasswordFile) {
return fmt.Errorf("wallet unlock password file not found")
}
}
if o.WalletUnlockerSvc == nil {
return fmt.Errorf("wallet unlocker app service must not be null")
}
if o.WalletSvc == nil {
return fmt.Errorf("wallet app service must not be null")
}
if o.OperatorSvc == nil {
return fmt.Errorf("operator app service must not be null")
}
if o.TradeSvc == nil {
return fmt.Errorf("trade app service must not be null")
}
return nil
}
func (o ServiceOptsOnePort) dbDatadir() string {
return filepath.Join(o.Datadir, o.DBLocation)
}
func (o ServiceOptsOnePort) tlsDatadir() string {
return filepath.Join(o.Datadir, o.TLSLocation)
}
func (o ServiceOptsOnePort) tlsKey() string {
if o.NoTls {
return ""
}
return filepath.Join(o.tlsDatadir(), OperatorTLSKeyFile)
}
func (o ServiceOptsOnePort) tlsCert() string {
if o.NoTls {
return ""
}
return filepath.Join(o.tlsDatadir(), OperatorTLSCertFile)
}