-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathterminal.go
1407 lines (1230 loc) · 44.1 KB
/
terminal.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package terminal
import (
"context"
"crypto/tls"
"embed"
"encoding/hex"
"errors"
"fmt"
"io/fs"
"net"
"net/http"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"sync"
"time"
restProxy "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/jessevdk/go-flags"
"github.com/lightninglabs/faraday/frdrpc"
"github.com/lightninglabs/lightning-terminal/litrpc"
"github.com/lightninglabs/lightning-terminal/session"
"github.com/lightninglabs/lndclient"
"github.com/lightninglabs/loop"
"github.com/lightninglabs/loop/loopd"
"github.com/lightninglabs/loop/looprpc"
"github.com/lightninglabs/pool"
"github.com/lightninglabs/pool/poolrpc"
"github.com/lightningnetwork/lnd"
"github.com/lightningnetwork/lnd/build"
"github.com/lightningnetwork/lnd/chainreg"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnrpc/autopilotrpc"
"github.com/lightningnetwork/lnd/lnrpc/chainrpc"
"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
"github.com/lightningnetwork/lnd/lnrpc/signrpc"
"github.com/lightningnetwork/lnd/lnrpc/verrpc"
"github.com/lightningnetwork/lnd/lnrpc/walletrpc"
"github.com/lightningnetwork/lnd/lnrpc/watchtowerrpc"
"github.com/lightningnetwork/lnd/lnrpc/wtclientrpc"
"github.com/lightningnetwork/lnd/lntest/wait"
"github.com/lightningnetwork/lnd/lnwallet/btcwallet"
"github.com/lightningnetwork/lnd/macaroons"
"github.com/lightningnetwork/lnd/rpcperms"
"github.com/lightningnetwork/lnd/signal"
grpcProxy "github.com/mwitkow/grpc-proxy/proxy"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/test/bufconn"
"google.golang.org/protobuf/encoding/protojson"
"gopkg.in/macaroon-bakery.v2/bakery"
"gopkg.in/macaroon.v2"
)
const (
defaultServerTimeout = 10 * time.Second
defaultConnectTimeout = 15 * time.Second
defaultStartupTimeout = 5 * time.Second
)
// restRegistration is a function type that represents a REST proxy
// registration.
type restRegistration func(context.Context, *restProxy.ServeMux, string,
[]grpc.DialOption) error
var (
// maxMsgRecvSize is the largest message our REST proxy will receive. We
// set this to 200MiB atm.
maxMsgRecvSize = grpc.MaxCallRecvMsgSize(1 * 1024 * 1024 * 200)
// appBuildFS is an in-memory file system that contains all the static
// HTML/CSS/JS files of the UI. It is compiled into the binary with the
// go 1.16 embed directive below. Because the path is relative to the
// root package, all assets will have a path prefix of /app/build/ which
// we'll strip by giving a sub directory to the HTTP server.
//
//go:embed app/build/*
appBuildFS embed.FS
// appFilesDir is the sub directory of the above build directory which
// we pass to the HTTP server.
appFilesDir = "app/build"
// appFilesPrefix is the path prefix the static assets of the UI are
// exposed under. This variable can be overwritten during build time if
// a different deployment path should be used.
appFilesPrefix = ""
// patternRESTRequest is the regular expression that matches all REST
// URIs that are currently used by lnd, faraday, loop and pool.
patternRESTRequest = regexp.MustCompile(`^/v\d/.*`)
// lndRESTRegistrations is the list of all lnd REST handler registration
// functions we want to call when creating our REST proxy. We include
// all lnd subserver packages here, even though some might not be active
// in a remote lnd node. That will result in an "UNIMPLEMENTED" error
// instead of a 404 which should be an okay tradeoff vs. connecting
// first and querying all enabled subservers to dynamically populate
// this list.
lndRESTRegistrations = []restRegistration{
lnrpc.RegisterLightningHandlerFromEndpoint,
lnrpc.RegisterWalletUnlockerHandlerFromEndpoint,
autopilotrpc.RegisterAutopilotHandlerFromEndpoint,
chainrpc.RegisterChainNotifierHandlerFromEndpoint,
invoicesrpc.RegisterInvoicesHandlerFromEndpoint,
routerrpc.RegisterRouterHandlerFromEndpoint,
signrpc.RegisterSignerHandlerFromEndpoint,
verrpc.RegisterVersionerHandlerFromEndpoint,
walletrpc.RegisterWalletKitHandlerFromEndpoint,
watchtowerrpc.RegisterWatchtowerHandlerFromEndpoint,
wtclientrpc.RegisterWatchtowerClientHandlerFromEndpoint,
}
// minimalCompatibleVersion is the minimal lnd version that is required
// to run LiT in remote mode.
minimalCompatibleVersion = &verrpc.Version{
AppMajor: 0,
AppMinor: 13,
AppPatch: 3,
BuildTags: []string{
"signrpc", "walletrpc", "chainrpc", "invoicesrpc",
},
}
)
// LightningTerminal is the main grand unified binary instance. Its task is to
// start an lnd node then start and register external subservers to it.
type LightningTerminal struct {
cfg *Config
defaultImplCfg *lnd.ImplementationCfg
// lndInterceptorChain is a reference to lnd's interceptor chain that
// guards all incoming calls. This is only set in integrated mode!
lndInterceptorChain *rpcperms.InterceptorChain
wg sync.WaitGroup
lndErrChan chan error
lndClient *lndclient.GrpcLndServices
basicClient lnrpc.LightningClient
faradayServer *frdrpc.RPCServer
faradayStarted bool
loopServer *loopd.Daemon
loopStarted bool
poolServer *pool.Server
poolStarted bool
rpcProxy *rpcProxy
httpServer *http.Server
sessionDB *session.DB
sessionServer *session.Server
sessionRpcServer *sessionRpcServer
restHandler http.Handler
restCancel func()
}
// New creates a new instance of the lightning-terminal daemon.
func New() *LightningTerminal {
return &LightningTerminal{
lndErrChan: make(chan error, 1),
}
}
// Run starts everything and then blocks until either the application is shut
// down or a critical error happens.
func (g *LightningTerminal) Run() error {
// Hook interceptor for os signals.
shutdownInterceptor, err := signal.Intercept()
if err != nil {
return fmt.Errorf("could not intercept signals: %v", err)
}
cfg, err := loadAndValidateConfig(shutdownInterceptor)
if err != nil {
return fmt.Errorf("could not load config: %w", err)
}
g.cfg = cfg
g.defaultImplCfg = g.cfg.Lnd.ImplementationConfig(shutdownInterceptor)
// Show version at startup.
log.Infof("LiT version: %s", Version())
// Create the instances of our subservers now so we can hook them up to
// lnd once it's fully started.
bufRpcListener := bufconn.Listen(100)
g.faradayServer = frdrpc.NewRPCServer(g.cfg.faradayRpcConfig)
g.loopServer = loopd.New(g.cfg.Loop, nil)
g.poolServer = pool.NewServer(g.cfg.Pool)
g.rpcProxy = newRpcProxy(
g.cfg, g, g.validateSuperMacaroon, getAllMethodPermissions(),
bufRpcListener,
)
// Create an instance of the local Terminal Connect session store DB.
networkDir := path.Join(g.cfg.LitDir, g.cfg.Network)
g.sessionDB, err = session.NewDB(networkDir, session.DBFilename)
if err != nil {
return fmt.Errorf("error creating session DB: %v", err)
}
// Create the gRPC server that handles adding/removing sessions and the
// actual mailbox server that spins up the Terminal Connect server
// interface.
g.sessionServer = session.NewServer(
func(opts ...grpc.ServerOption) *grpc.Server {
allOpts := []grpc.ServerOption{
grpc.CustomCodec(grpcProxy.Codec()), // nolint: staticcheck,
grpc.ChainStreamInterceptor(
g.rpcProxy.StreamServerInterceptor,
),
grpc.ChainUnaryInterceptor(
g.rpcProxy.UnaryServerInterceptor,
),
grpc.UnknownServiceHandler(
grpcProxy.TransparentHandler(
// Don't allow calls to litrpc.
g.rpcProxy.makeDirector(false),
),
),
}
allOpts = append(allOpts, opts...)
grpcServer := grpc.NewServer(allOpts...)
g.registerSubDaemonGrpcServers(grpcServer, false)
return grpcServer
},
)
g.sessionRpcServer = &sessionRpcServer{
basicAuth: g.rpcProxy.basicAuth,
db: g.sessionDB,
sessionServer: g.sessionServer,
quit: make(chan struct{}),
superMacBaker: func(ctx context.Context, rootKeyID uint64,
recipe *session.MacaroonRecipe) (string, error) {
return BakeSuperMacaroon(
ctx, g.basicClient, rootKeyID,
recipe.Permissions, recipe.Caveats,
)
},
}
// Overwrite the loop and pool daemon's user agent name so it sends
// "litd" instead of "loopd" and "poold" respectively.
loop.AgentName = "litd"
pool.SetAgentName("litd")
// Call the "real" main in a nested manner so the defers will properly
// be executed in the case of a graceful shutdown.
readyChan := make(chan struct{})
bufReadyChan := make(chan struct{})
unlockChan := make(chan struct{})
macChan := make(chan []byte, 1)
if g.cfg.LndMode == ModeIntegrated {
lisCfg := lnd.ListenerCfg{
RPCListeners: []*lnd.ListenerWithSignal{{
Listener: &onDemandListener{
addr: g.cfg.Lnd.RPCListeners[0],
},
Ready: readyChan,
}, {
Listener: bufRpcListener,
Ready: bufReadyChan,
MacChan: macChan,
}},
}
implCfg := &lnd.ImplementationCfg{
GrpcRegistrar: g,
RestRegistrar: g,
ExternalValidator: g,
DatabaseBuilder: g.defaultImplCfg.DatabaseBuilder,
WalletConfigBuilder: g,
ChainControlBuilder: g.defaultImplCfg.ChainControlBuilder,
}
g.wg.Add(1)
go func() {
defer g.wg.Done()
err := lnd.Main(
g.cfg.Lnd, lisCfg, implCfg, shutdownInterceptor,
)
if e, ok := err.(*flags.Error); err != nil &&
(!ok || e.Type != flags.ErrHelp) {
log.Errorf("Error running main lnd: %v", err)
g.lndErrChan <- err
return
}
close(g.lndErrChan)
}()
} else {
close(unlockChan)
close(readyChan)
close(bufReadyChan)
_ = g.RegisterGrpcSubserver(g.rpcProxy.grpcServer)
}
// We'll also create a REST proxy that'll convert any REST calls to gRPC
// calls and forward them to the internal listener.
if g.cfg.EnableREST {
if err := g.createRESTProxy(); err != nil {
return fmt.Errorf("error creating REST proxy: %v", err)
}
}
// Wait for lnd to be started up so we know we have a TLS cert.
select {
// If lnd needs to be unlocked we get the signal that it's ready to do
// so. We then go ahead and start the UI so we can unlock it there as
// well.
case <-unlockChan:
// If lnd is running with --noseedbackup and doesn't need unlocking, we
// get the ready signal immediately.
case <-readyChan:
case err := <-g.lndErrChan:
return err
case <-shutdownInterceptor.ShutdownChannel():
return errors.New("shutting down")
}
// We now know that starting lnd was successful. If we now run into an
// error, we must shut down lnd correctly.
defer func() {
err := g.shutdown()
if err != nil {
log.Errorf("Error shutting down: %v", err)
}
}()
// Now start the RPC proxy that will handle all incoming gRPC, grpc-web
// and REST requests. We also start the main web server that dispatches
// requests either to the static UI file server or the RPC proxy. This
// makes it possible to unlock lnd through the UI.
if err := g.rpcProxy.Start(); err != nil {
return fmt.Errorf("error starting lnd gRPC proxy server: %v",
err)
}
if err := g.startMainWebServer(); err != nil {
return fmt.Errorf("error starting UI HTTP server: %v", err)
}
// Now that we have started the main UI web server, show some useful
// information to the user so they can access the web UI easily.
if err := g.showStartupInfo(); err != nil {
return fmt.Errorf("error displaying startup info: %v", err)
}
// Wait for lnd to be unlocked, then start all clients.
select {
case <-readyChan:
case err := <-g.lndErrChan:
return err
case <-shutdownInterceptor.ShutdownChannel():
return errors.New("shutting down")
}
// If we're in integrated mode, we'll need to wait for lnd to send the
// macaroon after unlock before going any further.
if g.cfg.LndMode == ModeIntegrated {
<-bufReadyChan
g.cfg.lndAdminMacaroon = <-macChan
}
err = g.startSubservers()
if err != nil {
log.Errorf("Could not start subservers: %v", err)
return err
}
// Now start up all previously created sessions. Since the sessions
// require a lnd connection in order to bake macaroons, we can only
// start up the sessions once the connection to lnd has been
// established.
sessions, err := g.sessionDB.ListSessions()
if err != nil {
return fmt.Errorf("error listing sessions: %v", err)
}
for _, sess := range sessions {
if err := g.sessionRpcServer.resumeSession(sess); err != nil {
return fmt.Errorf("error resuming sesion: %v", err)
}
}
// Now block until we receive an error or the main shutdown signal.
select {
case err := <-g.loopServer.ErrChan:
// Loop will shut itself down if an error happens. We don't need
// to try to stop it again.
g.loopStarted = false
log.Errorf("Received critical error from loop, shutting down: "+
"%v", err)
case err := <-g.lndErrChan:
if err != nil {
log.Errorf("Received critical error from lnd, "+
"shutting down: %v", err)
}
case <-shutdownInterceptor.ShutdownChannel():
log.Infof("Shutdown signal received")
}
return nil
}
// startSubservers creates an internal connection to lnd and then starts all
// embedded daemons as external subservers that hook into the same gRPC and REST
// servers that lnd started.
func (g *LightningTerminal) startSubservers() error {
var (
insecure bool
clientOptions []lndclient.BasicClientOption
)
host, network, tlsPath, macPath, macData := g.cfg.lndConnectParams()
clientOptions = append(clientOptions, lndclient.MacaroonData(
hex.EncodeToString(macData),
))
clientOptions = append(
clientOptions, lndclient.MacFilename(path.Base(macPath)),
)
// If we're in integrated mode, we can retrieve the macaroon string
// from lnd directly, rather than grabbing it from disk.
if g.cfg.LndMode == ModeIntegrated {
// Set to true in integrated mode, since we will not require tls
// when communicating with lnd via a bufconn.
insecure = true
clientOptions = append(clientOptions, lndclient.Insecure())
}
// The main RPC listener of lnd might need some time to start, it could
// be that we run into a connection refused a few times. We use the
// basic client connection to find out if the RPC server is started yet
// because that doesn't do anything else than just connect. We'll check
// if lnd is also ready to be used in the next step.
err := wait.NoError(func() error {
// Create an lnd client now that we have the full configuration.
// We'll need a basic client and a full client because not all
// subservers have the same requirements.
var err error
g.basicClient, err = lndclient.NewBasicClient(
host, tlsPath, path.Dir(macPath), string(network),
clientOptions...,
)
return err
}, defaultStartupTimeout)
if err != nil {
return err
}
// Now we know that the connection itself is ready. But we also need to
// wait for two things: The chain notifier to be ready and the lnd
// wallet being fully synced to its chain backend. The chain notifier
// will always be ready first so if we instruct the lndclient to wait
// for the wallet sync, we should be fully ready to start all our
// subservers. This will just block until lnd signals readiness. But we
// still want to react to shutdown requests, so we need to listen for
// those.
ctxc, cancel := context.WithCancel(context.Background())
defer cancel()
// Make sure the context is canceled if the user requests shutdown.
go func() {
select {
// Client requests shutdown, cancel the wait.
case <-interceptor.ShutdownChannel():
cancel()
// The check was completed and the above defer canceled the
// context. We can just exit the goroutine, nothing more to do.
case <-ctxc.Done():
}
}()
g.lndClient, err = lndclient.NewLndServices(
&lndclient.LndServicesConfig{
LndAddress: host,
Network: network,
TLSPath: tlsPath,
Insecure: insecure,
CustomMacaroonPath: macPath,
CustomMacaroonHex: hex.EncodeToString(macData),
BlockUntilChainSynced: true,
BlockUntilUnlocked: true,
CallerCtx: ctxc,
CheckVersion: minimalCompatibleVersion,
},
)
if err != nil {
return err
}
// In the integrated mode, we received an admin macaroon once lnd was
// ready. We can now bake a "super macaroon" that contains all
// permissions of all daemons that we can use for any internal calls.
if g.cfg.LndMode == ModeIntegrated {
// Create a super macaroon that can be used to control lnd,
// faraday, loop, and pool, all at the same time.
ctx := context.Background()
superMacaroon, err := BakeSuperMacaroon(
ctx, g.basicClient, session.NewSuperMacaroonRootKeyID(
[4]byte{},
),
GetAllPermissions(false), nil,
)
if err != nil {
return err
}
g.rpcProxy.superMacaroon = superMacaroon
}
// If we're in integrated and stateless init mode, we won't create
// macaroon files in any of the subserver daemons.
createDefaultMacaroons := true
if g.cfg.LndMode == ModeIntegrated && g.lndInterceptorChain != nil &&
g.lndInterceptorChain.MacaroonService() != nil {
// If the wallet was initialized in stateless mode, we don't
// want any macaroons lying around on the filesystem. In that
// case only the UI will be able to access any of the integrated
// daemons. In all other cases we want default macaroons so we
// can use the CLI tools to interact with loop/pool/faraday.
macService := g.lndInterceptorChain.MacaroonService()
createDefaultMacaroons = !macService.StatelessInit
}
// Both connection types are ready now, let's start our subservers if
// they should be started locally as an integrated service.
if !g.cfg.faradayRemote {
err = g.faradayServer.StartAsSubserver(
g.lndClient.LndServices, createDefaultMacaroons,
)
if err != nil {
return err
}
g.faradayStarted = true
}
if !g.cfg.loopRemote {
err = g.loopServer.StartAsSubserver(
g.lndClient, createDefaultMacaroons,
)
if err != nil {
return err
}
g.loopStarted = true
}
if !g.cfg.poolRemote {
err = g.poolServer.StartAsSubserver(
g.basicClient, g.lndClient, createDefaultMacaroons,
)
if err != nil {
return err
}
g.poolStarted = true
}
return nil
}
// RegisterGrpcSubserver is a callback on the lnd.SubserverConfig struct that is
// called once lnd has initialized its main gRPC server instance. It gives the
// daemons (or external subservers) the possibility to register themselves to
// the same server instance.
//
// NOTE: This is part of the lnd.GrpcRegistrar interface.
func (g *LightningTerminal) RegisterGrpcSubserver(server *grpc.Server) error {
if err := g.defaultImplCfg.RegisterGrpcSubserver(server); err != nil {
return err
}
// Register all other daemon RPC servers that are running in-process.
// The LiT session server should be enabled on the main interface.
g.registerSubDaemonGrpcServers(server, true)
return nil
}
// registerSubDaemonGrpcServers registers the sub daemon (Faraday, Loop, Pool
// and LiT session) servers to a given gRPC server, given they are running in
// the local process. The lit session server is gated by its own boolean because
// we don't necessarily want to expose it on all listeners, given its security
// implications.
func (g *LightningTerminal) registerSubDaemonGrpcServers(server *grpc.Server,
withLitRPC bool) {
// In remote mode the "director" of the RPC proxy will act as a catch-
// all for any gRPC request that isn't known because we didn't register
// any server for it. The director will then forward the request to the
// remote service.
if !g.cfg.faradayRemote {
frdrpc.RegisterFaradayServerServer(server, g.faradayServer)
}
if !g.cfg.loopRemote {
looprpc.RegisterSwapClientServer(server, g.loopServer)
}
if !g.cfg.poolRemote {
poolrpc.RegisterTraderServer(server, g.poolServer)
}
if withLitRPC {
litrpc.RegisterSessionsServer(server, g.sessionRpcServer)
}
}
// RegisterRestSubserver is a callback on the lnd.SubserverConfig struct that is
// called once lnd has initialized its main REST server instance. It gives the
// daemons (or external subservers) the possibility to register themselves to
// the same server instance.
//
// NOTE: This is part of the lnd.RestRegistrar interface.
func (g *LightningTerminal) RegisterRestSubserver(ctx context.Context,
mux *restProxy.ServeMux, endpoint string,
dialOpts []grpc.DialOption) error {
err := g.defaultImplCfg.RegisterRestSubserver(
ctx, mux, endpoint, dialOpts,
)
if err != nil {
return err
}
err = frdrpc.RegisterFaradayServerHandlerFromEndpoint(
ctx, mux, endpoint, dialOpts,
)
if err != nil {
return err
}
err = looprpc.RegisterSwapClientHandlerFromEndpoint(
ctx, mux, endpoint, dialOpts,
)
if err != nil {
return err
}
return poolrpc.RegisterTraderHandlerFromEndpoint(
ctx, mux, endpoint, dialOpts,
)
}
// ValidateMacaroon extracts the macaroon from the context's gRPC metadata,
// checks its signature, makes sure all specified permissions for the called
// method are contained within and finally ensures all caveat conditions are
// met. A non-nil error is returned if any of the checks fail.
//
// NOTE: This is part of the lnd.ExternalValidator interface.
func (g *LightningTerminal) ValidateMacaroon(ctx context.Context,
requiredPermissions []bakery.Op, fullMethod string) error {
macHex, err := macaroons.RawMacaroonFromContext(ctx)
if err != nil {
return err
}
// If we're using a super macaroon, we just make sure it is valid and
// contains all the permissions needed. If we get to this point, we're
// either in integrated lnd mode where this is the only macaroon
// validation function, and we're done after the check. Or we're in
// remote lnd mode but the request is for an in-process daemon which we
// can validate here. Any request for a remote sub-daemon goes through
// the proxy and its director and any super macaroon will be converted
// to a daemon specific macaroon before directing the call to the remote
// daemon. Those calls don't land here.
if session.IsSuperMacaroon(macHex) {
macBytes, err := hex.DecodeString(macHex)
if err != nil {
return err
}
return g.validateSuperMacaroon(
ctx, macBytes, requiredPermissions, fullMethod,
)
}
// Validate all macaroons for services that are running in the local
// process. Calls that we proxy to a remote host don't need to be
// checked as they'll have their own interceptor.
switch {
case isFaradayURI(fullMethod):
// In remote mode we just pass through the request, the remote
// daemon will check the macaroon.
if g.cfg.faradayRemote {
return nil
}
if !g.faradayStarted {
return fmt.Errorf("faraday is not yet ready for " +
"requests, lnd possibly still starting or " +
"syncing")
}
err = g.faradayServer.ValidateMacaroon(
ctx, requiredPermissions, fullMethod,
)
if err != nil {
return &proxyErr{
proxyContext: "faraday",
wrapped: fmt.Errorf("invalid macaroon: %v",
err),
}
}
case isLoopURI(fullMethod):
// In remote mode we just pass through the request, the remote
// daemon will check the macaroon.
if g.cfg.loopRemote {
return nil
}
if !g.loopStarted {
return fmt.Errorf("loop is not yet ready for " +
"requests, lnd possibly still starting or " +
"syncing")
}
err = g.loopServer.ValidateMacaroon(
ctx, requiredPermissions, fullMethod,
)
if err != nil {
return &proxyErr{
proxyContext: "loop",
wrapped: fmt.Errorf("invalid macaroon: %v",
err),
}
}
case isPoolURI(fullMethod):
// In remote mode we just pass through the request, the remote
// daemon will check the macaroon.
if g.cfg.poolRemote {
return nil
}
if !g.poolStarted {
return fmt.Errorf("pool is not yet ready for " +
"requests, lnd possibly still starting or " +
"syncing")
}
err = g.poolServer.ValidateMacaroon(
ctx, requiredPermissions, fullMethod,
)
if err != nil {
return &proxyErr{
proxyContext: "pool",
wrapped: fmt.Errorf("invalid macaroon: %v",
err),
}
}
case isLitURI(fullMethod):
wrap := fmt.Errorf("invalid basic auth")
_, err := g.rpcProxy.convertBasicAuth(ctx, fullMethod, wrap)
if err != nil {
return &proxyErr{
proxyContext: "lit",
wrapped: fmt.Errorf("invalid auth: %v",
err),
}
}
}
// Because lnd will spin up its own gRPC server with macaroon
// interceptors if it is running in this process, it will check its
// macaroons there. If lnd is running remotely, that process will check
// the macaroons. So we don't need to worry about anything other than
// the subservers that are running in the local process.
return nil
}
// Permissions returns all permissions for which the external validator of the
// terminal is responsible.
//
// NOTE: This is part of the lnd.ExternalValidator interface.
func (g *LightningTerminal) Permissions() map[string][]bakery.Op {
return getSubserverPermissions()
}
// BuildWalletConfig is responsible for creating or unlocking and then
// fully initializing a wallet.
//
// NOTE: This is only implemented in order for us to intercept the setup call
// and store a reference to the interceptor chain.
//
// NOTE: This is part of the lnd.WalletConfigBuilder interface.
func (g *LightningTerminal) BuildWalletConfig(ctx context.Context,
dbs *lnd.DatabaseInstances, interceptorChain *rpcperms.InterceptorChain,
grpcListeners []*lnd.ListenerWithSignal) (*chainreg.PartialChainControl,
*btcwallet.Config, func(), error) {
g.lndInterceptorChain = interceptorChain
return g.defaultImplCfg.WalletConfigBuilder.BuildWalletConfig(
ctx, dbs, interceptorChain, grpcListeners,
)
}
// shutdown stops all subservers that were started and attached to lnd.
func (g *LightningTerminal) shutdown() error {
var returnErr error
if g.faradayStarted {
if err := g.faradayServer.Stop(); err != nil {
log.Errorf("Error stopping faraday: %v", err)
returnErr = err
}
}
if g.loopStarted {
g.loopServer.Stop()
if err := <-g.loopServer.ErrChan; err != nil {
log.Errorf("Error stopping loop: %v", err)
returnErr = err
}
}
if g.poolStarted {
if err := g.poolServer.Stop(); err != nil {
log.Errorf("Error stopping pool: %v", err)
returnErr = err
}
}
g.sessionRpcServer.stop()
if err := g.sessionDB.Close(); err != nil {
log.Errorf("Error closing session DB: %v", err)
returnErr = err
}
g.sessionServer.Stop()
if g.lndClient != nil {
g.lndClient.Close()
}
if g.restCancel != nil {
g.restCancel()
}
if g.rpcProxy != nil {
if err := g.rpcProxy.Stop(); err != nil {
log.Errorf("Error stopping lnd proxy: %v", err)
returnErr = err
}
}
if g.httpServer != nil {
if err := g.httpServer.Close(); err != nil {
log.Errorf("Error stopping UI server: %v", err)
returnErr = err
}
}
// In case the error wasn't thrown by lnd, make sure we stop it too.
interceptor.RequestShutdown()
g.wg.Wait()
// The lnd error channel is only used if we are actually running lnd in
// the same process.
if g.cfg.LndMode == ModeIntegrated {
err := <-g.lndErrChan
if err != nil {
log.Errorf("Error stopping lnd: %v", err)
returnErr = err
}
}
return returnErr
}
// startMainWebServer creates the main web HTTP server that delegates requests
// between the embedded HTTP server and the RPC proxy. An incoming request will
// go through the following chain of components:
//
// Request on port 8443 <------------------------------------+
// | converted gRPC request |
// v |
// +---+----------------------+ other +----------------+ |
// | Main web HTTP server +------->+ Embedded HTTP | |
// +---+----------------------+____+ +----------------+ |
// | | |
// v any RPC or grpc-web call | any REST call |
// +---+----------------------+ |->+----------------+ |
// | grpc-web proxy | + grpc-gateway +-----------+
// +---+----------------------+ +----------------+
// |
// v native gRPC call with basic auth
// +---+----------------------+
// | interceptors |
// +---+----------------------+
// |
// v native gRPC call with macaroon
// +---+----------------------+
// | gRPC server |
// +---+----------------------+
// |
// v unknown authenticated call, gRPC server is just a wrapper
// +---+----------------------+
// | director |
// +---+----------------------+
// |
// v authenticated call
// +---+----------------------+ call to lnd or integrated daemon
// | lnd (remote or local) +---------------+
// | faraday remote | |
// | loop remote | +----------v----------+
// | pool remote | | lnd local subserver |
// +--------------------------+ | - faraday |
// | - loop |
// | - pool |
// +---------------------+
//
func (g *LightningTerminal) startMainWebServer() error {
// Initialize the in-memory file server from the content compiled by
// the go:embed directive. Since everything's relative to the root dir,
// we need to create an FS of the sub directory app/build.
buildDir, err := fs.Sub(appBuildFS, appFilesDir)
if err != nil {
return err
}
staticFileServer := http.FileServer(&ClientRouteWrapper{
assets: http.FS(buildDir),
})
// Both gRPC (web) and static file requests will come into through the
// main UI HTTP server. We use this simple switching handler to send the
// requests to the correct implementation.
httpHandler := func(resp http.ResponseWriter, req *http.Request) {
// If this is some kind of gRPC, gRPC Web or REST call that
// should go to lnd or one of the daemons, pass it to the proxy
// that handles all those calls.
if g.rpcProxy.isHandling(resp, req) {
return
}
// REST requests aren't that easy to identify, we have to look
// at the URL itself. If this is a REST request, we give it
// directly to our REST handler which will then forward it to
// us again but converted to a gRPC request.
if g.cfg.EnableREST && isRESTRequest(req) {
log.Infof("Handling REST request: %s", req.URL.Path)
g.restHandler.ServeHTTP(resp, req)
return
}
// If we got here, it's a static file the browser wants, or
// something we don't know in which case the static file server
// will answer with a 404.
log.Infof("Handling static file request: %s", req.URL.Path)
// Add 1-year cache header for static files. React uses content-
// based hashes in file names, so when any file is updated, the
// url will change causing the browser cached version to be
// invalidated.
var re = regexp.MustCompile(`^/(static|fonts|icons)/.*`)
if re.MatchString(req.URL.Path) {
resp.Header().Set("Cache-Control", "max-age=31536000")
}
// Transfer static files using gzip to save up to 70% of
// bandwidth.
gzipHandler := makeGzipHandler(staticFileServer.ServeHTTP)
gzipHandler(resp, req)
}
// Create and start our HTTPS server now that will handle both gRPC web
// and static file requests.
g.httpServer = &http.Server{
// To make sure that long-running calls and indefinitely opened
// streaming connections aren't terminated by the internal
// proxy, we need to disable all timeouts except the one for
// reading the HTTP headers. That timeout shouldn't be removed
// as we would otherwise be prone to the slowloris attack where