2017-11-28 01:12:09 +01:00
|
|
|
// Copyright (c) 2013-2017 The btcsuite developers
|
|
|
|
// Copyright (c) 2015-2016 The Decred developers
|
2022-02-06 21:16:47 +01:00
|
|
|
// Copyright (C) 2015-2022 The Lightning Network Developers
|
2017-11-28 01:12:09 +01:00
|
|
|
|
2019-01-24 14:28:25 +01:00
|
|
|
package lnd
|
2015-12-17 01:42:52 +01:00
|
|
|
|
2015-12-30 01:23:27 +01:00
|
|
|
import (
|
2019-09-29 01:07:37 +02:00
|
|
|
"context"
|
2021-06-20 11:16:03 +02:00
|
|
|
"errors"
|
2015-12-30 01:23:27 +01:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2016-01-17 04:09:41 +01:00
|
|
|
"net/http"
|
2023-06-20 19:44:43 +02:00
|
|
|
"net/http/pprof"
|
2015-12-30 03:59:16 +01:00
|
|
|
"os"
|
2023-09-13 20:12:26 +02:00
|
|
|
"runtime"
|
2023-06-20 19:44:43 +02:00
|
|
|
runtimePprof "runtime/pprof"
|
2018-03-15 10:04:17 +01:00
|
|
|
"strings"
|
2017-12-17 18:28:38 +01:00
|
|
|
"sync"
|
2017-06-06 00:18:06 +02:00
|
|
|
"time"
|
2015-12-30 01:23:27 +01:00
|
|
|
|
2022-02-23 14:48:00 +01:00
|
|
|
"github.com/btcsuite/btcd/btcutil"
|
2021-07-27 12:59:59 +02:00
|
|
|
proxy "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
2018-12-13 12:26:29 +01:00
|
|
|
"github.com/lightningnetwork/lnd/autopilot"
|
2018-09-20 12:26:58 +02:00
|
|
|
"github.com/lightningnetwork/lnd/build"
|
2019-08-08 04:17:50 +02:00
|
|
|
"github.com/lightningnetwork/lnd/chanacceptor"
|
2016-03-23 02:50:11 +01:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2024-07-25 18:21:47 +02:00
|
|
|
"github.com/lightningnetwork/lnd/cluster"
|
2018-02-18 00:40:10 +01:00
|
|
|
"github.com/lightningnetwork/lnd/keychain"
|
2018-05-23 15:41:16 +02:00
|
|
|
"github.com/lightningnetwork/lnd/lncfg"
|
2016-01-16 19:38:48 +01:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc"
|
2017-06-06 00:18:06 +02:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
2017-08-18 03:50:57 +02:00
|
|
|
"github.com/lightningnetwork/lnd/macaroons"
|
2021-02-02 13:45:29 +01:00
|
|
|
"github.com/lightningnetwork/lnd/monitoring"
|
2020-10-13 11:24:40 +02:00
|
|
|
"github.com/lightningnetwork/lnd/rpcperms"
|
2018-06-15 05:16:20 +02:00
|
|
|
"github.com/lightningnetwork/lnd/signal"
|
2020-03-17 15:58:36 +01:00
|
|
|
"github.com/lightningnetwork/lnd/tor"
|
2017-10-12 11:37:37 +02:00
|
|
|
"github.com/lightningnetwork/lnd/walletunlocker"
|
2019-06-14 02:31:55 +02:00
|
|
|
"github.com/lightningnetwork/lnd/watchtower"
|
2022-02-07 13:58:21 +01:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials"
|
2023-05-25 14:09:14 +02:00
|
|
|
"google.golang.org/grpc/keepalive"
|
2022-02-07 13:58:21 +01:00
|
|
|
"gopkg.in/macaroon-bakery.v2/bakery"
|
|
|
|
"gopkg.in/macaroon.v2"
|
2015-12-30 01:23:27 +01:00
|
|
|
)
|
|
|
|
|
2021-07-17 10:45:17 +02:00
|
|
|
const (
|
|
|
|
// adminMacaroonFilePermissions is the file permission that is used for
|
|
|
|
// creating the admin macaroon file.
|
|
|
|
//
|
|
|
|
// Why 640 is safe:
|
|
|
|
// Assuming a reasonably secure Linux system, it will have a
|
|
|
|
// separate group for each user. E.g. a new user lnd gets assigned group
|
|
|
|
// lnd which nothing else belongs to. A system that does not do this is
|
|
|
|
// inherently broken already.
|
|
|
|
//
|
|
|
|
// Since there is no other user in the group, no other user can read
|
|
|
|
// admin macaroon unless the administrator explicitly allowed it. Thus
|
|
|
|
// there's no harm allowing group read.
|
|
|
|
adminMacaroonFilePermissions = 0640
|
2024-07-25 18:21:47 +02:00
|
|
|
|
|
|
|
// leaderResignTimeout is the timeout used when resigning from the
|
|
|
|
// leader role. This is kept short so LND can shut down quickly in case
|
|
|
|
// of a system failure or network partition making the cluster
|
|
|
|
// unresponsive. The cluster itself should ensure that the leader is not
|
|
|
|
// elected again until the previous leader has resigned or the leader
|
|
|
|
// election timeout has passed.
|
|
|
|
leaderResignTimeout = 5 * time.Second
|
2021-07-17 10:45:17 +02:00
|
|
|
)
|
|
|
|
|
2019-12-02 13:23:20 +01:00
|
|
|
// AdminAuthOptions returns a list of DialOptions that can be used to
|
|
|
|
// authenticate with the RPC server with admin capabilities.
|
2021-02-11 13:53:30 +01:00
|
|
|
// skipMacaroons=true should be set if we don't want to include macaroons with
|
|
|
|
// the auth options. This is needed for instance for the WalletUnlocker
|
|
|
|
// service, which must be usable also before macaroons are created.
|
2019-12-02 13:23:20 +01:00
|
|
|
//
|
|
|
|
// NOTE: This should only be called after the RPCListener has signaled it is
|
|
|
|
// ready.
|
2021-11-08 11:58:52 +01:00
|
|
|
func AdminAuthOptions(cfg *Config, skipMacaroons bool) ([]grpc.DialOption,
|
|
|
|
error) {
|
|
|
|
|
2019-12-02 13:23:20 +01:00
|
|
|
creds, err := credentials.NewClientTLSFromFile(cfg.TLSCertPath, "")
|
|
|
|
if err != nil {
|
2024-02-26 12:19:38 +01:00
|
|
|
return nil, fmt.Errorf("unable to read TLS cert: %w", err)
|
2019-12-02 13:23:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a dial options array.
|
|
|
|
opts := []grpc.DialOption{
|
|
|
|
grpc.WithTransportCredentials(creds),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the admin macaroon if macaroons are active.
|
2021-02-11 13:53:30 +01:00
|
|
|
if !skipMacaroons && !cfg.NoMacaroons {
|
2022-10-28 01:26:47 +02:00
|
|
|
// Load the admin macaroon file.
|
2023-06-13 16:22:44 +02:00
|
|
|
macBytes, err := os.ReadFile(cfg.AdminMacPath)
|
2019-12-02 13:23:20 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to read macaroon "+
|
|
|
|
"path (check the network setting!): %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mac := &macaroon.Macaroon{}
|
|
|
|
if err = mac.UnmarshalBinary(macBytes); err != nil {
|
2024-03-07 13:19:28 +01:00
|
|
|
return nil, fmt.Errorf("unable to decode macaroon: %w",
|
2019-12-02 13:23:20 +01:00
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we append the macaroon credentials to the dial options.
|
2021-08-12 16:07:18 +02:00
|
|
|
cred, err := macaroons.NewMacaroonCredential(mac)
|
|
|
|
if err != nil {
|
2024-02-26 12:19:38 +01:00
|
|
|
return nil, fmt.Errorf("error cloning mac: %w", err)
|
2021-08-12 16:07:18 +02:00
|
|
|
}
|
2019-12-02 13:23:20 +01:00
|
|
|
opts = append(opts, grpc.WithPerRPCCredentials(cred))
|
|
|
|
}
|
|
|
|
|
|
|
|
return opts, nil
|
|
|
|
}
|
|
|
|
|
2021-11-08 11:58:52 +01:00
|
|
|
// ListenerWithSignal is a net.Listener that has an additional Ready channel
|
|
|
|
// that will be closed when a server starts listening.
|
2019-11-29 11:35:37 +01:00
|
|
|
type ListenerWithSignal struct {
|
|
|
|
net.Listener
|
|
|
|
|
|
|
|
// Ready will be closed by the server listening on Listener.
|
|
|
|
Ready chan struct{}
|
2021-09-22 21:31:39 +02:00
|
|
|
|
|
|
|
// MacChan is an optional way to pass the admin macaroon to the program
|
|
|
|
// that started lnd. The channel should be buffered to avoid lnd being
|
|
|
|
// blocked on sending to the channel.
|
|
|
|
MacChan chan []byte
|
2019-11-29 11:35:37 +01:00
|
|
|
}
|
|
|
|
|
2019-07-09 14:04:51 +02:00
|
|
|
// ListenerCfg is a wrapper around custom listeners that can be passed to lnd
|
|
|
|
// when calling its main method.
|
|
|
|
type ListenerCfg struct {
|
2021-09-22 21:31:39 +02:00
|
|
|
// RPCListeners can be set to the listeners to use for the RPC server.
|
|
|
|
// If empty a regular network listener will be created.
|
|
|
|
RPCListeners []*ListenerWithSignal
|
2019-07-09 14:04:51 +02:00
|
|
|
}
|
|
|
|
|
2021-08-01 14:28:32 +02:00
|
|
|
var errStreamIsolationWithProxySkip = errors.New(
|
|
|
|
"while stream isolation is enabled, the TOR proxy may not be skipped",
|
2021-06-20 11:16:03 +02:00
|
|
|
)
|
|
|
|
|
2020-04-30 09:42:28 +02:00
|
|
|
// Main is the true entry point for lnd. It accepts a fully populated and
|
|
|
|
// validated main configuration struct and an optional listener config struct.
|
|
|
|
// This function starts all main system components then blocks until a signal
|
|
|
|
// is received on the shutdownChan at which point everything is shut down again.
|
2021-09-23 16:54:39 +02:00
|
|
|
func Main(cfg *Config, lisCfg ListenerCfg, implCfg *ImplementationCfg,
|
|
|
|
interceptor signal.Interceptor) error {
|
|
|
|
|
2017-06-21 17:07:44 +02:00
|
|
|
defer func() {
|
2020-11-23 14:37:42 +01:00
|
|
|
ltndLog.Info("Shutdown complete\n")
|
2024-09-10 18:21:35 +02:00
|
|
|
err := cfg.LogRotator.Close()
|
2019-09-20 16:22:02 +02:00
|
|
|
if err != nil {
|
|
|
|
ltndLog.Errorf("Could not close log rotator: %v", err)
|
|
|
|
}
|
2017-06-21 17:07:44 +02:00
|
|
|
}()
|
2016-03-23 02:50:11 +01:00
|
|
|
|
2021-11-08 11:58:52 +01:00
|
|
|
mkErr := func(format string, args ...interface{}) error {
|
|
|
|
ltndLog.Errorf("Shutting down because error in main "+
|
|
|
|
"method: "+format, args...)
|
|
|
|
return fmt.Errorf(format, args...)
|
|
|
|
}
|
|
|
|
|
2016-03-23 02:50:11 +01:00
|
|
|
// Show version at startup.
|
2021-11-08 11:58:52 +01:00
|
|
|
ltndLog.Infof("Version: %s commit=%s, build=%s, logging=%s, "+
|
|
|
|
"debuglevel=%s", build.Version(), build.Commit,
|
|
|
|
build.Deployment, build.LoggingType, cfg.DebugLevel)
|
2016-02-23 03:24:56 +01:00
|
|
|
|
2018-03-15 10:04:17 +01:00
|
|
|
var network string
|
|
|
|
switch {
|
2023-08-03 17:25:13 +02:00
|
|
|
case cfg.Bitcoin.TestNet3:
|
2018-03-15 10:04:17 +01:00
|
|
|
network = "testnet"
|
|
|
|
|
2023-08-03 17:25:13 +02:00
|
|
|
case cfg.Bitcoin.MainNet:
|
2018-03-15 10:04:17 +01:00
|
|
|
network = "mainnet"
|
|
|
|
|
2023-08-03 17:25:13 +02:00
|
|
|
case cfg.Bitcoin.SimNet:
|
2018-05-24 03:24:01 +02:00
|
|
|
network = "simnet"
|
2018-03-15 10:04:17 +01:00
|
|
|
|
2023-08-03 17:25:13 +02:00
|
|
|
case cfg.Bitcoin.RegTest:
|
2018-03-15 10:04:17 +01:00
|
|
|
network = "regtest"
|
2020-12-24 16:46:13 +01:00
|
|
|
|
|
|
|
case cfg.Bitcoin.SigNet:
|
|
|
|
network = "signet"
|
2018-03-15 10:04:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ltndLog.Infof("Active chain: %v (network=%v)",
|
2023-08-03 18:12:49 +02:00
|
|
|
strings.Title(BitcoinChainName), network,
|
2018-03-15 10:04:17 +01:00
|
|
|
)
|
|
|
|
|
2023-04-19 11:06:09 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
2016-06-21 06:42:07 +02:00
|
|
|
// Enable http profiling server if requested.
|
2024-09-25 12:56:18 +02:00
|
|
|
if cfg.Pprof.Profile != "" {
|
2023-04-19 11:06:09 +02:00
|
|
|
// Create the http handler.
|
2023-06-20 19:44:43 +02:00
|
|
|
pprofMux := http.NewServeMux()
|
|
|
|
pprofMux.HandleFunc("/debug/pprof/", pprof.Index)
|
|
|
|
pprofMux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
|
|
|
|
pprofMux.HandleFunc("/debug/pprof/profile", pprof.Profile)
|
|
|
|
pprofMux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
|
|
|
|
pprofMux.HandleFunc("/debug/pprof/trace", pprof.Trace)
|
|
|
|
|
2024-09-25 12:56:18 +02:00
|
|
|
if cfg.Pprof.BlockingProfile != 0 {
|
|
|
|
runtime.SetBlockProfileRate(cfg.Pprof.BlockingProfile)
|
2023-09-13 20:12:26 +02:00
|
|
|
}
|
2024-09-25 12:56:18 +02:00
|
|
|
if cfg.Pprof.MutexProfile != 0 {
|
|
|
|
runtime.SetMutexProfileFraction(cfg.Pprof.MutexProfile)
|
2023-09-13 20:12:26 +02:00
|
|
|
}
|
|
|
|
|
2023-06-20 19:44:43 +02:00
|
|
|
// Redirect all requests to the pprof handler, thus visiting
|
|
|
|
// `127.0.0.1:6060` will be redirected to
|
|
|
|
// `127.0.0.1:6060/debug/pprof`.
|
|
|
|
pprofMux.Handle("/", http.RedirectHandler(
|
|
|
|
"/debug/pprof/", http.StatusSeeOther,
|
|
|
|
))
|
|
|
|
|
2024-09-25 12:56:18 +02:00
|
|
|
ltndLog.Infof("Pprof listening on %v", cfg.Pprof.Profile)
|
2023-04-19 11:06:09 +02:00
|
|
|
|
|
|
|
// Create the pprof server.
|
|
|
|
pprofServer := &http.Server{
|
2024-09-25 12:56:18 +02:00
|
|
|
Addr: cfg.Pprof.Profile,
|
2023-06-20 19:44:43 +02:00
|
|
|
Handler: pprofMux,
|
2023-10-10 21:21:04 +02:00
|
|
|
ReadHeaderTimeout: cfg.HTTPHeaderTimeout,
|
2023-04-19 11:06:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Shut the server down when lnd is shutting down.
|
|
|
|
defer func() {
|
|
|
|
ltndLog.Info("Stopping pprof server...")
|
|
|
|
err := pprofServer.Shutdown(ctx)
|
|
|
|
if err != nil {
|
|
|
|
ltndLog.Errorf("Stop pprof server got err: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Start the pprof server.
|
2016-06-21 06:42:07 +02:00
|
|
|
go func() {
|
2023-04-19 11:06:09 +02:00
|
|
|
err := pprofServer.ListenAndServe()
|
|
|
|
if err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
|
|
ltndLog.Errorf("Serving pprof got err: %v", err)
|
|
|
|
}
|
2016-06-21 06:42:07 +02:00
|
|
|
}()
|
|
|
|
}
|
2016-01-17 04:01:06 +01:00
|
|
|
|
2017-10-15 02:08:27 +02:00
|
|
|
// Write cpu profile if requested.
|
2024-09-25 12:56:18 +02:00
|
|
|
if cfg.Pprof.CPUProfile != "" {
|
|
|
|
f, err := os.Create(cfg.Pprof.CPUProfile)
|
2017-10-15 02:08:27 +02:00
|
|
|
if err != nil {
|
2021-11-08 11:58:52 +01:00
|
|
|
return mkErr("unable to create CPU profile: %v", err)
|
2017-10-15 02:08:27 +02:00
|
|
|
}
|
2023-06-20 19:44:43 +02:00
|
|
|
_ = runtimePprof.StartCPUProfile(f)
|
|
|
|
defer func() {
|
|
|
|
_ = f.Close()
|
|
|
|
}()
|
|
|
|
defer runtimePprof.StopCPUProfile()
|
2017-10-15 02:08:27 +02:00
|
|
|
}
|
|
|
|
|
2021-02-09 17:44:43 +01:00
|
|
|
// Run configuration dependent DB pre-initialization. Note that this
|
|
|
|
// needs to be done early and once during the startup process, before
|
|
|
|
// any DB access.
|
2021-08-03 09:57:26 +02:00
|
|
|
if err := cfg.DB.Init(ctx, cfg.graphDatabaseDir()); err != nil {
|
2021-11-08 11:58:52 +01:00
|
|
|
return mkErr("error initializing DBs: %v", err)
|
2021-02-09 17:44:43 +01:00
|
|
|
}
|
|
|
|
|
2022-11-15 23:10:27 +01:00
|
|
|
tlsManagerCfg := &TLSManagerCfg{
|
|
|
|
TLSCertPath: cfg.TLSCertPath,
|
|
|
|
TLSKeyPath: cfg.TLSKeyPath,
|
|
|
|
TLSEncryptKey: cfg.TLSEncryptKey,
|
|
|
|
TLSExtraIPs: cfg.TLSExtraIPs,
|
|
|
|
TLSExtraDomains: cfg.TLSExtraDomains,
|
|
|
|
TLSAutoRefresh: cfg.TLSAutoRefresh,
|
|
|
|
TLSDisableAutofill: cfg.TLSDisableAutofill,
|
|
|
|
TLSCertDuration: cfg.TLSCertDuration,
|
|
|
|
|
|
|
|
LetsEncryptDir: cfg.LetsEncryptDir,
|
|
|
|
LetsEncryptDomain: cfg.LetsEncryptDomain,
|
|
|
|
LetsEncryptListen: cfg.LetsEncryptListen,
|
|
|
|
|
|
|
|
DisableRestTLS: cfg.DisableRestTLS,
|
2023-10-10 21:21:04 +02:00
|
|
|
|
|
|
|
HTTPHeaderTimeout: cfg.HTTPHeaderTimeout,
|
2022-11-15 23:10:27 +01:00
|
|
|
}
|
|
|
|
tlsManager := NewTLSManager(tlsManagerCfg)
|
2022-02-08 01:05:29 +01:00
|
|
|
serverOpts, restDialOpts, restListen, cleanUp,
|
2022-11-15 23:10:27 +01:00
|
|
|
err := tlsManager.SetCertificateBeforeUnlock()
|
2017-10-12 11:37:37 +02:00
|
|
|
if err != nil {
|
2022-11-15 23:10:27 +01:00
|
|
|
return mkErr("error setting cert before unlock: %v", err)
|
|
|
|
}
|
|
|
|
if cleanUp != nil {
|
|
|
|
defer cleanUp()
|
2017-10-12 11:37:37 +02:00
|
|
|
}
|
2019-01-07 18:48:02 +01:00
|
|
|
|
2021-02-02 13:45:29 +01:00
|
|
|
// If we have chosen to start with a dedicated listener for the
|
|
|
|
// rpc server, we set it directly.
|
2021-11-04 15:20:34 +01:00
|
|
|
grpcListeners := append([]*ListenerWithSignal{}, lisCfg.RPCListeners...)
|
|
|
|
if len(grpcListeners) == 0 {
|
2021-02-02 13:45:29 +01:00
|
|
|
// Otherwise we create listeners from the RPCListeners defined
|
|
|
|
// in the config.
|
2019-07-09 11:09:19 +02:00
|
|
|
for _, grpcEndpoint := range cfg.RPCListeners {
|
|
|
|
// Start a gRPC server listening for HTTP/2
|
|
|
|
// connections.
|
|
|
|
lis, err := lncfg.ListenOnAddress(grpcEndpoint)
|
|
|
|
if err != nil {
|
2021-11-08 11:58:52 +01:00
|
|
|
return mkErr("unable to listen on %s: %v",
|
|
|
|
grpcEndpoint, err)
|
2019-07-09 11:09:19 +02:00
|
|
|
}
|
2021-02-02 13:45:29 +01:00
|
|
|
defer lis.Close()
|
|
|
|
|
2019-11-29 11:35:37 +01:00
|
|
|
grpcListeners = append(
|
|
|
|
grpcListeners, &ListenerWithSignal{
|
|
|
|
Listener: lis,
|
|
|
|
Ready: make(chan struct{}),
|
2021-11-04 15:20:34 +01:00
|
|
|
},
|
|
|
|
)
|
2019-07-09 11:09:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-19 15:53:06 +01:00
|
|
|
// Create a new RPC interceptor that we'll add to the GRPC server. This
|
|
|
|
// will be used to log the API calls invoked on the GRPC server.
|
|
|
|
interceptorChain := rpcperms.NewInterceptorChain(
|
2021-08-12 16:07:24 +02:00
|
|
|
rpcsLog, cfg.NoMacaroons, cfg.RPCMiddleware.Mandatory,
|
2021-03-19 15:53:06 +01:00
|
|
|
)
|
|
|
|
if err := interceptorChain.Start(); err != nil {
|
2021-11-08 11:58:52 +01:00
|
|
|
return mkErr("error starting interceptor chain: %v", err)
|
2021-03-19 15:53:06 +01:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err := interceptorChain.Stop()
|
|
|
|
if err != nil {
|
|
|
|
ltndLog.Warnf("error stopping RPC interceptor "+
|
|
|
|
"chain: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-05-25 14:09:14 +02:00
|
|
|
// Allow the user to overwrite some defaults of the gRPC library related
|
|
|
|
// to connection keepalive (server side and client side pings).
|
|
|
|
serverKeepalive := keepalive.ServerParameters{
|
|
|
|
Time: cfg.GRPC.ServerPingTime,
|
|
|
|
Timeout: cfg.GRPC.ServerPingTimeout,
|
|
|
|
}
|
|
|
|
clientKeepalive := keepalive.EnforcementPolicy{
|
|
|
|
MinTime: cfg.GRPC.ClientPingMinWait,
|
|
|
|
PermitWithoutStream: cfg.GRPC.ClientAllowPingWithoutStream,
|
|
|
|
}
|
|
|
|
|
2021-03-19 15:53:06 +01:00
|
|
|
rpcServerOpts := interceptorChain.CreateServerOpts()
|
|
|
|
serverOpts = append(serverOpts, rpcServerOpts...)
|
2022-01-10 23:11:42 +01:00
|
|
|
serverOpts = append(
|
|
|
|
serverOpts, grpc.MaxRecvMsgSize(lnrpc.MaxGrpcMsgSize),
|
2023-05-25 14:09:14 +02:00
|
|
|
grpc.KeepaliveParams(serverKeepalive),
|
|
|
|
grpc.KeepaliveEnforcementPolicy(clientKeepalive),
|
2022-01-10 23:11:42 +01:00
|
|
|
)
|
2021-03-19 15:53:06 +01:00
|
|
|
|
|
|
|
grpcServer := grpc.NewServer(serverOpts...)
|
|
|
|
defer grpcServer.Stop()
|
|
|
|
|
|
|
|
// We'll also register the RPC interceptor chain as the StateServer, as
|
|
|
|
// it can be used to query for the current state of the wallet.
|
|
|
|
lnrpc.RegisterStateServer(grpcServer, interceptorChain)
|
|
|
|
|
|
|
|
// Initialize, and register our implementation of the gRPC interface
|
|
|
|
// exported by the rpcServer.
|
2021-09-23 16:54:39 +02:00
|
|
|
rpcServer := newRPCServer(cfg, interceptorChain, implCfg, interceptor)
|
2021-03-19 15:53:06 +01:00
|
|
|
err = rpcServer.RegisterWithGrpcServer(grpcServer)
|
|
|
|
if err != nil {
|
2021-11-08 11:58:52 +01:00
|
|
|
return mkErr("error registering gRPC server: %v", err)
|
2021-03-19 15:53:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now that both the WalletUnlocker and LightningService have been
|
|
|
|
// registered with the GRPC server, we can start listening.
|
|
|
|
err = startGrpcListen(cfg, grpcServer, grpcListeners)
|
|
|
|
if err != nil {
|
2021-11-08 11:58:52 +01:00
|
|
|
return mkErr("error starting gRPC listener: %v", err)
|
2021-03-19 15:53:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now start the REST proxy for our gRPC server above. We'll ensure
|
|
|
|
// we direct LND to connect to its loopback address rather than a
|
|
|
|
// wildcard to prevent certificate issues when accessing the proxy
|
|
|
|
// externally.
|
|
|
|
stopProxy, err := startRestProxy(
|
|
|
|
cfg, rpcServer, restDialOpts, restListen,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2021-11-08 11:58:52 +01:00
|
|
|
return mkErr("error starting REST proxy: %v", err)
|
2021-03-19 15:53:06 +01:00
|
|
|
}
|
|
|
|
defer stopProxy()
|
|
|
|
|
2021-02-08 21:56:27 +01:00
|
|
|
// Start leader election if we're running on etcd. Continuation will be
|
|
|
|
// blocked until this instance is elected as the current leader or
|
|
|
|
// shutting down.
|
|
|
|
elected := false
|
2024-07-25 18:21:47 +02:00
|
|
|
var leaderElector cluster.LeaderElector
|
2021-02-08 21:56:27 +01:00
|
|
|
if cfg.Cluster.EnableLeaderElection {
|
|
|
|
electionCtx, cancelElection := context.WithCancel(ctx)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-interceptor.ShutdownChannel()
|
|
|
|
cancelElection()
|
|
|
|
}()
|
|
|
|
|
|
|
|
ltndLog.Infof("Using %v leader elector",
|
|
|
|
cfg.Cluster.LeaderElector)
|
|
|
|
|
2024-07-25 18:21:47 +02:00
|
|
|
leaderElector, err = cfg.Cluster.MakeLeaderElector(
|
2021-02-08 21:56:27 +01:00
|
|
|
electionCtx, cfg.DB,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if !elected {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ltndLog.Infof("Attempting to resign from leader role "+
|
|
|
|
"(%v)", cfg.Cluster.ID)
|
|
|
|
|
2024-07-25 18:21:47 +02:00
|
|
|
// Ensure that we don't block the shutdown process if
|
|
|
|
// the leader resigning process takes too long. The
|
|
|
|
// cluster will ensure that the leader is not elected
|
|
|
|
// again until the previous leader has resigned or the
|
|
|
|
// leader election timeout has passed.
|
|
|
|
timeoutCtx, cancel := context.WithTimeout(
|
|
|
|
ctx, leaderResignTimeout,
|
|
|
|
)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if err := leaderElector.Resign(timeoutCtx); err != nil {
|
2021-02-08 21:56:27 +01:00
|
|
|
ltndLog.Errorf("Leader elector failed to "+
|
|
|
|
"resign: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
ltndLog.Infof("Starting leadership campaign (%v)",
|
|
|
|
cfg.Cluster.ID)
|
|
|
|
|
|
|
|
if err := leaderElector.Campaign(electionCtx); err != nil {
|
2021-11-08 11:58:52 +01:00
|
|
|
return mkErr("leadership campaign failed: %v", err)
|
2021-02-08 21:56:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
elected = true
|
|
|
|
ltndLog.Infof("Elected as leader (%v)", cfg.Cluster.ID)
|
|
|
|
}
|
|
|
|
|
2021-09-23 16:54:41 +02:00
|
|
|
dbs, cleanUp, err := implCfg.DatabaseBuilder.BuildDatabase(ctx)
|
2021-02-08 21:56:27 +01:00
|
|
|
switch {
|
|
|
|
case err == channeldb.ErrDryRunMigrationOK:
|
|
|
|
ltndLog.Infof("%v, exiting", err)
|
|
|
|
return nil
|
|
|
|
case err != nil:
|
2021-11-08 11:58:52 +01:00
|
|
|
return mkErr("unable to open databases: %v", err)
|
2021-02-08 21:56:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
defer cleanUp()
|
|
|
|
|
2021-10-14 15:42:47 +02:00
|
|
|
partialChainControl, walletConfig, cleanUp, err := implCfg.BuildWalletConfig(
|
2024-04-25 19:00:42 +02:00
|
|
|
ctx, dbs, &implCfg.AuxComponents, interceptorChain,
|
|
|
|
grpcListeners,
|
2021-09-23 16:54:38 +02:00
|
|
|
)
|
2021-10-14 15:42:47 +02:00
|
|
|
if err != nil {
|
2021-11-08 11:58:52 +01:00
|
|
|
return mkErr("error creating wallet config: %v", err)
|
2021-10-14 15:42:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
activeChainControl, cleanUp, err := implCfg.BuildChainControl(
|
|
|
|
partialChainControl, walletConfig,
|
|
|
|
)
|
2021-09-23 16:54:38 +02:00
|
|
|
if err != nil {
|
2021-11-08 11:58:52 +01:00
|
|
|
return mkErr("error loading chain control: %v", err)
|
2021-03-29 15:23:46 +02:00
|
|
|
}
|
|
|
|
|
2021-09-23 16:54:43 +02:00
|
|
|
defer cleanUp()
|
2017-03-27 19:25:44 +02:00
|
|
|
|
2018-02-18 00:40:10 +01:00
|
|
|
// TODO(roasbeef): add rotation
|
2020-10-06 17:03:42 +02:00
|
|
|
idKeyDesc, err := activeChainControl.KeyRing.DeriveKey(
|
2020-04-28 10:06:27 +02:00
|
|
|
keychain.KeyLocator{
|
|
|
|
Family: keychain.KeyFamilyNodeKey,
|
|
|
|
Index: 0,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
2021-11-08 11:58:52 +01:00
|
|
|
return mkErr("error deriving node key: %v", err)
|
2020-04-28 10:06:27 +02:00
|
|
|
}
|
|
|
|
|
2021-08-01 14:28:32 +02:00
|
|
|
if cfg.Tor.StreamIsolation && cfg.Tor.SkipProxyForClearNetTargets {
|
|
|
|
return errStreamIsolationWithProxySkip
|
2021-06-20 11:16:03 +02:00
|
|
|
}
|
|
|
|
|
2018-04-27 22:54:35 +02:00
|
|
|
if cfg.Tor.Active {
|
2021-08-01 14:28:32 +02:00
|
|
|
if cfg.Tor.SkipProxyForClearNetTargets {
|
2021-11-08 11:31:02 +01:00
|
|
|
srvrLog.Info("Onion services are accessible via Tor! " +
|
|
|
|
"NOTE: Traffic to clearnet services is not " +
|
|
|
|
"routed via Tor.")
|
2021-06-20 11:16:03 +02:00
|
|
|
} else {
|
|
|
|
srvrLog.Infof("Proxying all network traffic via Tor "+
|
2021-11-08 11:31:02 +01:00
|
|
|
"(stream_isolation=%v)! NOTE: Ensure the "+
|
|
|
|
"backend node is proxying over Tor as well",
|
|
|
|
cfg.Tor.StreamIsolation)
|
2021-06-20 11:16:03 +02:00
|
|
|
}
|
2018-02-06 03:36:11 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 11:58:52 +01:00
|
|
|
// If tor is active and either v2 or v3 onion services have been
|
|
|
|
// specified, make a tor controller and pass it into both the watchtower
|
|
|
|
// server and the regular lnd server.
|
2020-03-17 15:58:36 +01:00
|
|
|
var torController *tor.Controller
|
|
|
|
if cfg.Tor.Active && (cfg.Tor.V2 || cfg.Tor.V3) {
|
|
|
|
torController = tor.NewController(
|
2021-11-08 11:58:52 +01:00
|
|
|
cfg.Tor.Control, cfg.Tor.TargetIPAddress,
|
|
|
|
cfg.Tor.Password,
|
2020-03-17 15:58:36 +01:00
|
|
|
)
|
|
|
|
|
2021-11-08 11:58:52 +01:00
|
|
|
// Start the tor controller before giving it to any other
|
|
|
|
// subsystems.
|
2020-03-17 15:58:36 +01:00
|
|
|
if err := torController.Start(); err != nil {
|
2021-11-08 11:58:52 +01:00
|
|
|
return mkErr("unable to initialize tor controller: %v",
|
|
|
|
err)
|
2020-03-17 15:58:36 +01:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := torController.Stop(); err != nil {
|
2021-11-08 11:31:02 +01:00
|
|
|
ltndLog.Errorf("error stopping tor "+
|
|
|
|
"controller: %v", err)
|
2020-03-17 15:58:36 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2019-06-14 02:31:55 +02:00
|
|
|
var tower *watchtower.Standalone
|
|
|
|
if cfg.Watchtower.Active {
|
2020-10-06 17:03:42 +02:00
|
|
|
towerKeyDesc, err := activeChainControl.KeyRing.DeriveKey(
|
2020-04-28 10:06:31 +02:00
|
|
|
keychain.KeyLocator{
|
|
|
|
Family: keychain.KeyFamilyTowerID,
|
|
|
|
Index: 0,
|
2019-06-21 01:55:52 +02:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
2021-11-08 11:58:52 +01:00
|
|
|
return mkErr("error deriving tower key: %v", err)
|
2019-06-21 01:55:52 +02:00
|
|
|
}
|
|
|
|
|
2020-03-17 15:58:36 +01:00
|
|
|
wtCfg := &watchtower.Config{
|
2020-10-06 17:03:42 +02:00
|
|
|
BlockFetcher: activeChainControl.ChainIO,
|
2021-09-23 16:54:41 +02:00
|
|
|
DB: dbs.TowerServerDB,
|
2020-10-06 17:03:42 +02:00
|
|
|
EpochRegistrar: activeChainControl.ChainNotifier,
|
2019-06-14 02:31:55 +02:00
|
|
|
Net: cfg.net,
|
|
|
|
NewAddress: func() (btcutil.Address, error) {
|
2020-10-06 17:03:42 +02:00
|
|
|
return activeChainControl.Wallet.NewAddress(
|
2022-08-11 03:31:07 +02:00
|
|
|
lnwallet.TaprootPubkey, false,
|
2021-02-20 02:41:45 +01:00
|
|
|
lnwallet.DefaultAccountName,
|
2019-06-14 02:31:55 +02:00
|
|
|
)
|
|
|
|
},
|
2020-04-28 10:06:31 +02:00
|
|
|
NodeKeyECDH: keychain.NewPubKeyECDH(
|
2020-10-06 17:03:42 +02:00
|
|
|
towerKeyDesc, activeChainControl.KeyRing,
|
2020-04-28 10:06:31 +02:00
|
|
|
),
|
2020-10-06 17:03:42 +02:00
|
|
|
PublishTx: activeChainControl.Wallet.PublishTransaction,
|
2020-08-04 20:56:31 +02:00
|
|
|
ChainHash: *cfg.ActiveNetParams.GenesisHash,
|
2020-03-17 15:58:36 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 11:58:52 +01:00
|
|
|
// If there is a tor controller (user wants auto hidden
|
|
|
|
// services), then store a pointer in the watchtower config.
|
2020-03-17 15:58:36 +01:00
|
|
|
if torController != nil {
|
|
|
|
wtCfg.TorController = torController
|
|
|
|
wtCfg.WatchtowerKeyPath = cfg.Tor.WatchtowerKeyPath
|
2022-05-11 03:11:19 +02:00
|
|
|
wtCfg.EncryptKey = cfg.Tor.EncryptKey
|
|
|
|
wtCfg.KeyRing = activeChainControl.KeyRing
|
2020-03-17 15:58:36 +01:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case cfg.Tor.V2:
|
|
|
|
wtCfg.Type = tor.V2
|
|
|
|
case cfg.Tor.V3:
|
|
|
|
wtCfg.Type = tor.V3
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-08 11:58:52 +01:00
|
|
|
wtConfig, err := cfg.Watchtower.Apply(
|
|
|
|
wtCfg, lncfg.NormalizeAddresses,
|
|
|
|
)
|
2019-06-14 02:31:55 +02:00
|
|
|
if err != nil {
|
2021-11-08 11:58:52 +01:00
|
|
|
return mkErr("unable to configure watchtower: %v", err)
|
2019-06-14 02:31:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
tower, err = watchtower.New(wtConfig)
|
|
|
|
if err != nil {
|
2021-11-08 11:58:52 +01:00
|
|
|
return mkErr("unable to create watchtower: %v", err)
|
2019-06-14 02:31:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-08 23:18:14 +02:00
|
|
|
// Initialize the MultiplexAcceptor. If lnd was started with the
|
|
|
|
// zero-conf feature bit, then this will be a ZeroConfAcceptor.
|
|
|
|
// Otherwise, this will be a ChainedAcceptor.
|
|
|
|
var multiAcceptor chanacceptor.MultiplexAcceptor
|
|
|
|
if cfg.ProtocolOptions.ZeroConf() {
|
|
|
|
multiAcceptor = chanacceptor.NewZeroConfAcceptor()
|
|
|
|
} else {
|
|
|
|
multiAcceptor = chanacceptor.NewChainedAcceptor()
|
|
|
|
}
|
2019-08-08 04:17:50 +02:00
|
|
|
|
2017-05-18 20:49:32 +02:00
|
|
|
// Set up the core server which will listen for incoming peer
|
|
|
|
// connections.
|
2018-02-18 00:40:10 +01:00
|
|
|
server, err := newServer(
|
2021-08-03 09:57:33 +02:00
|
|
|
cfg, cfg.Listeners, dbs, activeChainControl, &idKeyDesc,
|
2021-09-23 16:54:43 +02:00
|
|
|
activeChainControl.Cfg.WalletUnlockParams.ChansToRestore,
|
2024-07-25 18:21:47 +02:00
|
|
|
multiAcceptor, torController, tlsManager, leaderElector,
|
2024-04-05 01:13:30 +02:00
|
|
|
implCfg,
|
2018-02-18 00:40:10 +01:00
|
|
|
)
|
2016-01-17 04:09:41 +01:00
|
|
|
if err != nil {
|
2021-11-08 11:58:52 +01:00
|
|
|
return mkErr("unable to create server: %v", err)
|
2016-01-17 04:09:41 +01:00
|
|
|
}
|
2017-06-06 00:18:06 +02:00
|
|
|
|
2019-01-09 09:14:45 +01:00
|
|
|
// Set up an autopilot manager from the current config. This will be
|
2018-12-13 12:26:29 +01:00
|
|
|
// used to manage the underlying autopilot agent, starting and stopping
|
|
|
|
// it at will.
|
2021-09-23 16:54:35 +02:00
|
|
|
atplCfg, err := initAutoPilot(
|
|
|
|
server, cfg.Autopilot, activeChainControl.MinHtlcIn,
|
|
|
|
cfg.ActiveNetParams,
|
|
|
|
)
|
2019-01-09 09:14:45 +01:00
|
|
|
if err != nil {
|
2021-11-08 11:58:52 +01:00
|
|
|
return mkErr("unable to initialize autopilot: %v", err)
|
2019-01-09 09:14:45 +01:00
|
|
|
}
|
|
|
|
|
2018-12-13 12:26:29 +01:00
|
|
|
atplManager, err := autopilot.NewManager(atplCfg)
|
|
|
|
if err != nil {
|
2021-11-08 11:58:52 +01:00
|
|
|
return mkErr("unable to create autopilot manager: %v", err)
|
2018-12-13 12:26:29 +01:00
|
|
|
}
|
|
|
|
if err := atplManager.Start(); err != nil {
|
2021-11-08 11:58:52 +01:00
|
|
|
return mkErr("unable to start autopilot manager: %v", err)
|
2018-12-13 12:26:29 +01:00
|
|
|
}
|
|
|
|
defer atplManager.Stop()
|
|
|
|
|
2022-11-15 23:10:27 +01:00
|
|
|
err = tlsManager.LoadPermanentCertificate(activeChainControl.KeyRing)
|
|
|
|
if err != nil {
|
|
|
|
return mkErr("unable to load permanent TLS certificate: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
2021-02-02 13:45:29 +01:00
|
|
|
// Now we have created all dependencies necessary to populate and
|
|
|
|
// start the RPC server.
|
|
|
|
err = rpcServer.addDeps(
|
2021-09-23 16:54:33 +02:00
|
|
|
server, interceptorChain.MacaroonService(), cfg.SubRPCServers,
|
2022-07-08 23:18:14 +02:00
|
|
|
atplManager, server.invoices, tower, multiAcceptor,
|
2024-04-23 15:48:42 +02:00
|
|
|
server.invoiceHtlcModifier,
|
2018-05-23 15:38:19 +02:00
|
|
|
)
|
2016-10-15 23:38:47 +02:00
|
|
|
if err != nil {
|
2021-11-08 11:58:52 +01:00
|
|
|
return mkErr("unable to add deps to RPC server: %v", err)
|
2016-10-15 23:38:47 +02:00
|
|
|
}
|
lnd+rpc: modify rpcServer to fully manaage listeners and gRPC, handle sub-servers
In this commit, we modify the existing rpcServer to fully manage the
macaroons, gRPC server, and also seek out and create all sub-servers.
With this change, the RPC server gains more responsibility, as it
becomes the "root" server in the hierarchy of gRPC sub-servers.
In addition to creating each sub-server, it will also merge the set of
macaroon permissions for each sub-server, with the permissions of the
rest of the RPC infra. As a result, each sub-server is able to
independently specify what it needs w.r.t macaroon permissions and have
that taken care of by the RPC server. In order to achieve this, we need
to unify the creation of the RPC interceptors, and also fully manage the
gRPC server ourselves.
Some examples with various build configs:
```
⛰i make build
Building debug lnd and lncli.
go build -v -tags="dev" -o lnd-debug -ldflags "-X github.com/lightningnetwork/lnd/build.Commit=v0.5-beta-143-gb2069914c4b76109b7c59320dc48f8a5f30deb75-dirty" github.com/lightningnetwork/lnd
go build -v -tags="dev" -o lncli-debug -ldflags "-X github.com/lightningnetwork/lnd/build.Commit=v0.5-beta-143-gb2069914c4b76109b7c59320dc48f8a5f30deb75-dirty" github.com/lightningnetwork/lnd/cmd/lncli
⛰i ./lnd-debug --debuglevel=debug --signrpc.signermacaroonpath=~/sign.macaroon
unknown flag `signrpc.signermacaroonpath'
unknown flag `signrpc.signermacaroonpath'
⛰i make build tags=signerrpc
Building debug lnd and lncli.
go build -v -tags="dev signerrpc" -o lnd-debug -ldflags "-X github.com/lightningnetwork/lnd/build.Commit=v0.5-beta-143-gb2069914c4b76109b7c59320dc48f8a5f30deb75-dirty" github.com/lightningnetwork/lnd
go build -v -tags="dev signerrpc" -o lncli-debug -ldflags "-X github.com/lightningnetwork/lnd/build.Commit=v0.5-beta-143-gb2069914c4b76109b7c59320dc48f8a5f30deb75-dirty" github.com/lightningnetwork/lnd/cmd/lncli
⛰i ./lnd-debug --debuglevel=debug --signrpc.signermacaroonpath=~/sign.macaroon
2018-10-22 17:31:01.132 [INF] LTND: Version: 0.5.0-beta commit=v0.5-beta-143-gb2069914c4b76109b7c59320dc48f8a5f30deb75-dirty, build=development, logging=default
2018-10-22 17:31:01.133 [INF] LTND: Active chain: Bitcoin (network=simnet)
2018-10-22 17:31:01.140 [INF] CHDB: Checking for schema update: latest_version=6, db_version=6
2018-10-22 17:31:01.236 [INF] LTND: Primary chain is set to: bitcoin
2018-10-22 17:31:02.391 [INF] LNWL: Opened wallet
2018-10-22 17:31:03.315 [INF] LNWL: The wallet has been unlocked without a time limit
2018-10-22 17:31:03.315 [INF] LTND: LightningWallet opened
2018-10-22 17:31:03.319 [INF] LNWL: Catching up block hashes to height 3060, this will take a while...
2018-10-22 17:31:03.320 [INF] HSWC: Restoring in-memory circuit state from disk
2018-10-22 17:31:03.320 [INF] LNWL: Done catching up block hashes
2018-10-22 17:31:03.320 [INF] HSWC: Payment circuits loaded: num_pending=0, num_open=0
2018-10-22 17:31:03.322 [DBG] LTND: Populating dependencies for sub RPC server: Signrpc
```
As for the config, an example is:
```
[signrpc]
signrpc.signermacaroonpath=~/signer.macaroon
```
2018-10-23 03:03:07 +02:00
|
|
|
if err := rpcServer.Start(); err != nil {
|
2021-11-08 11:58:52 +01:00
|
|
|
return mkErr("unable to start RPC server: %v", err)
|
2017-12-17 18:28:38 +01:00
|
|
|
}
|
lnd+rpc: modify rpcServer to fully manaage listeners and gRPC, handle sub-servers
In this commit, we modify the existing rpcServer to fully manage the
macaroons, gRPC server, and also seek out and create all sub-servers.
With this change, the RPC server gains more responsibility, as it
becomes the "root" server in the hierarchy of gRPC sub-servers.
In addition to creating each sub-server, it will also merge the set of
macaroon permissions for each sub-server, with the permissions of the
rest of the RPC infra. As a result, each sub-server is able to
independently specify what it needs w.r.t macaroon permissions and have
that taken care of by the RPC server. In order to achieve this, we need
to unify the creation of the RPC interceptors, and also fully manage the
gRPC server ourselves.
Some examples with various build configs:
```
⛰i make build
Building debug lnd and lncli.
go build -v -tags="dev" -o lnd-debug -ldflags "-X github.com/lightningnetwork/lnd/build.Commit=v0.5-beta-143-gb2069914c4b76109b7c59320dc48f8a5f30deb75-dirty" github.com/lightningnetwork/lnd
go build -v -tags="dev" -o lncli-debug -ldflags "-X github.com/lightningnetwork/lnd/build.Commit=v0.5-beta-143-gb2069914c4b76109b7c59320dc48f8a5f30deb75-dirty" github.com/lightningnetwork/lnd/cmd/lncli
⛰i ./lnd-debug --debuglevel=debug --signrpc.signermacaroonpath=~/sign.macaroon
unknown flag `signrpc.signermacaroonpath'
unknown flag `signrpc.signermacaroonpath'
⛰i make build tags=signerrpc
Building debug lnd and lncli.
go build -v -tags="dev signerrpc" -o lnd-debug -ldflags "-X github.com/lightningnetwork/lnd/build.Commit=v0.5-beta-143-gb2069914c4b76109b7c59320dc48f8a5f30deb75-dirty" github.com/lightningnetwork/lnd
go build -v -tags="dev signerrpc" -o lncli-debug -ldflags "-X github.com/lightningnetwork/lnd/build.Commit=v0.5-beta-143-gb2069914c4b76109b7c59320dc48f8a5f30deb75-dirty" github.com/lightningnetwork/lnd/cmd/lncli
⛰i ./lnd-debug --debuglevel=debug --signrpc.signermacaroonpath=~/sign.macaroon
2018-10-22 17:31:01.132 [INF] LTND: Version: 0.5.0-beta commit=v0.5-beta-143-gb2069914c4b76109b7c59320dc48f8a5f30deb75-dirty, build=development, logging=default
2018-10-22 17:31:01.133 [INF] LTND: Active chain: Bitcoin (network=simnet)
2018-10-22 17:31:01.140 [INF] CHDB: Checking for schema update: latest_version=6, db_version=6
2018-10-22 17:31:01.236 [INF] LTND: Primary chain is set to: bitcoin
2018-10-22 17:31:02.391 [INF] LNWL: Opened wallet
2018-10-22 17:31:03.315 [INF] LNWL: The wallet has been unlocked without a time limit
2018-10-22 17:31:03.315 [INF] LTND: LightningWallet opened
2018-10-22 17:31:03.319 [INF] LNWL: Catching up block hashes to height 3060, this will take a while...
2018-10-22 17:31:03.320 [INF] HSWC: Restoring in-memory circuit state from disk
2018-10-22 17:31:03.320 [INF] LNWL: Done catching up block hashes
2018-10-22 17:31:03.320 [INF] HSWC: Payment circuits loaded: num_pending=0, num_open=0
2018-10-22 17:31:03.322 [DBG] LTND: Populating dependencies for sub RPC server: Signrpc
```
As for the config, an example is:
```
[signrpc]
signrpc.signermacaroonpath=~/signer.macaroon
```
2018-10-23 03:03:07 +02:00
|
|
|
defer rpcServer.Stop()
|
2016-10-15 23:38:47 +02:00
|
|
|
|
2021-02-05 13:33:01 +01:00
|
|
|
// We transition the RPC state to Active, as the RPC server is up.
|
|
|
|
interceptorChain.SetRPCActive()
|
|
|
|
|
2021-07-17 13:54:41 +02:00
|
|
|
if err := interceptor.Notifier.NotifyReady(true); err != nil {
|
2021-11-08 11:58:52 +01:00
|
|
|
return mkErr("error notifying ready: %v", err)
|
2021-07-17 13:54:41 +02:00
|
|
|
}
|
|
|
|
|
2021-08-17 20:55:00 +02:00
|
|
|
// We'll wait until we're fully synced to continue the start up of the
|
|
|
|
// remainder of the daemon. This ensures that we don't accept any
|
|
|
|
// possibly invalid state transitions, or accept channels with spent
|
|
|
|
// funds.
|
|
|
|
_, bestHeight, err := activeChainControl.ChainIO.GetBestBlock()
|
|
|
|
if err != nil {
|
2021-11-08 11:58:52 +01:00
|
|
|
return mkErr("unable to determine chain tip: %v", err)
|
2021-08-17 20:55:00 +02:00
|
|
|
}
|
2017-06-06 00:18:06 +02:00
|
|
|
|
2021-08-17 20:55:00 +02:00
|
|
|
ltndLog.Infof("Waiting for chain backend to finish sync, "+
|
|
|
|
"start_height=%v", bestHeight)
|
2017-06-06 00:18:06 +02:00
|
|
|
|
2024-09-24 17:58:08 +02:00
|
|
|
type syncResult struct {
|
|
|
|
synced bool
|
|
|
|
bestBlockTime int64
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
var syncedResChan = make(chan syncResult, 1)
|
|
|
|
|
2021-08-17 20:55:00 +02:00
|
|
|
for {
|
2024-09-24 17:58:08 +02:00
|
|
|
// We check if the wallet is synced in a separate goroutine as
|
|
|
|
// the call is blocking, and we want to be able to interrupt it
|
|
|
|
// if the daemon is shutting down.
|
|
|
|
go func() {
|
|
|
|
synced, bestBlockTime, err := activeChainControl.Wallet.
|
|
|
|
IsSynced()
|
|
|
|
syncedResChan <- syncResult{synced, bestBlockTime, err}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-interceptor.ShutdownChannel():
|
2021-08-17 20:55:00 +02:00
|
|
|
return nil
|
2017-06-06 00:18:06 +02:00
|
|
|
|
2024-09-24 17:58:08 +02:00
|
|
|
case res := <-syncedResChan:
|
|
|
|
if res.err != nil {
|
|
|
|
return mkErr("unable to determine if wallet "+
|
|
|
|
"is synced: %v", res.err)
|
|
|
|
}
|
2017-06-06 00:18:06 +02:00
|
|
|
|
2024-09-24 17:58:08 +02:00
|
|
|
ltndLog.Debugf("Syncing to block timestamp: %v, is "+
|
|
|
|
"synced=%v", time.Unix(res.bestBlockTime, 0),
|
|
|
|
res.synced)
|
2023-06-19 08:00:21 +02:00
|
|
|
|
2024-09-24 17:58:08 +02:00
|
|
|
if res.synced {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're not yet synced, we'll wait for a second
|
|
|
|
// before checking again.
|
|
|
|
select {
|
|
|
|
case <-interceptor.ShutdownChannel():
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
continue
|
|
|
|
}
|
2021-08-17 20:55:00 +02:00
|
|
|
}
|
|
|
|
|
2024-09-24 17:58:08 +02:00
|
|
|
break
|
2017-06-06 00:18:06 +02:00
|
|
|
}
|
|
|
|
|
2021-08-17 20:55:00 +02:00
|
|
|
_, bestHeight, err = activeChainControl.ChainIO.GetBestBlock()
|
|
|
|
if err != nil {
|
2021-11-08 11:58:52 +01:00
|
|
|
return mkErr("unable to determine chain tip: %v", err)
|
2021-08-17 20:55:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ltndLog.Infof("Chain backend is fully synced (end_height=%v)!",
|
|
|
|
bestHeight)
|
|
|
|
|
2017-06-06 00:18:06 +02:00
|
|
|
// With all the relevant chains initialized, we can finally start the
|
2024-05-08 21:25:49 +02:00
|
|
|
// server itself. We start the server in an asynchronous goroutine so
|
|
|
|
// that we are able to interrupt and shutdown the daemon gracefully in
|
|
|
|
// case the startup of the subservers do not behave as expected.
|
|
|
|
errChan := make(chan error)
|
|
|
|
go func() {
|
|
|
|
errChan <- server.Start()
|
|
|
|
}()
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err := server.Stop()
|
|
|
|
if err != nil {
|
|
|
|
ltndLog.Warnf("Stopping the server including all "+
|
|
|
|
"its subsystems failed with %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-errChan:
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2021-11-08 11:58:52 +01:00
|
|
|
return mkErr("unable to start server: %v", err)
|
2024-05-08 21:25:49 +02:00
|
|
|
|
|
|
|
case <-interceptor.ShutdownChannel():
|
|
|
|
return nil
|
2017-06-06 00:18:06 +02:00
|
|
|
}
|
|
|
|
|
2021-09-11 19:26:43 +02:00
|
|
|
// We transition the server state to Active, as the server is up.
|
|
|
|
interceptorChain.SetServerActive()
|
|
|
|
|
2017-08-11 06:40:15 +02:00
|
|
|
// Now that the server has started, if the autopilot mode is currently
|
2018-12-13 12:26:29 +01:00
|
|
|
// active, then we'll start the autopilot agent immediately. It will be
|
|
|
|
// stopped together with the autopilot service.
|
2017-08-11 06:40:15 +02:00
|
|
|
if cfg.Autopilot.Active {
|
2018-12-13 12:26:29 +01:00
|
|
|
if err := atplManager.StartAgent(); err != nil {
|
2021-11-08 11:58:52 +01:00
|
|
|
return mkErr("unable to start autopilot agent: %v", err)
|
2017-08-11 06:40:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 02:31:55 +02:00
|
|
|
if cfg.Watchtower.Active {
|
|
|
|
if err := tower.Start(); err != nil {
|
2021-11-08 11:58:52 +01:00
|
|
|
return mkErr("unable to start watchtower: %v", err)
|
2019-06-14 02:31:55 +02:00
|
|
|
}
|
|
|
|
defer tower.Stop()
|
|
|
|
}
|
|
|
|
|
2016-03-23 02:50:11 +01:00
|
|
|
// Wait for shutdown signal from either a graceful server stop or from
|
|
|
|
// the interrupt handler.
|
2020-08-28 11:18:15 +02:00
|
|
|
<-interceptor.ShutdownChannel()
|
2016-07-13 02:03:29 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-06 17:23:38 +02:00
|
|
|
// bakeMacaroon creates a new macaroon with newest version and the given
|
|
|
|
// permissions then returns it binary serialized.
|
|
|
|
func bakeMacaroon(ctx context.Context, svc *macaroons.Service,
|
|
|
|
permissions []bakery.Op) ([]byte, error) {
|
|
|
|
|
|
|
|
mac, err := svc.NewMacaroon(
|
|
|
|
ctx, macaroons.DefaultRootKeyID, permissions...,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return mac.M().MarshalBinary()
|
|
|
|
}
|
|
|
|
|
2023-04-11 19:17:15 +02:00
|
|
|
// saveMacaroon bakes a macaroon with the specified macaroon permissions and
|
|
|
|
// writes it to a file with the given filename and file permissions.
|
|
|
|
func saveMacaroon(ctx context.Context, svc *macaroons.Service, filename string,
|
|
|
|
macaroonPermissions []bakery.Op, filePermissions os.FileMode) error {
|
2018-03-21 00:50:08 +01:00
|
|
|
|
2023-04-11 19:17:15 +02:00
|
|
|
macaroonBytes, err := bakeMacaroon(ctx, svc, macaroonPermissions)
|
2018-03-21 00:50:08 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-11 19:17:15 +02:00
|
|
|
err = os.WriteFile(filename, macaroonBytes, filePermissions)
|
2018-03-21 00:50:08 +01:00
|
|
|
if err != nil {
|
2023-04-11 19:17:15 +02:00
|
|
|
_ = os.Remove(filename)
|
2018-03-21 00:50:08 +01:00
|
|
|
return err
|
|
|
|
}
|
2018-01-16 17:18:41 +01:00
|
|
|
|
2023-04-11 19:17:15 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// genDefaultMacaroons checks for three default macaroon files and generates
|
|
|
|
// them if they do not exist; one admin-level, one for invoice access and one
|
|
|
|
// read-only. Each macaroon is checked and created independently to ensure all
|
|
|
|
// three exist. The admin macaroon can also be used to generate more granular
|
|
|
|
// macaroons.
|
|
|
|
func genDefaultMacaroons(ctx context.Context, svc *macaroons.Service,
|
|
|
|
admFile, roFile, invoiceFile string) error {
|
|
|
|
|
|
|
|
// First, we'll generate a macaroon that only allows the caller to
|
|
|
|
// access invoice related calls. This is useful for merchants and other
|
|
|
|
// services to allow an isolated instance that can only query and
|
|
|
|
// modify invoices.
|
|
|
|
if !lnrpc.FileExists(invoiceFile) {
|
|
|
|
err := saveMacaroon(
|
|
|
|
ctx, svc, invoiceFile, invoicePermissions, 0644,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-18 03:50:57 +02:00
|
|
|
}
|
|
|
|
|
2023-04-11 19:17:15 +02:00
|
|
|
// Generate the read-only macaroon and write it to a file.
|
|
|
|
if !lnrpc.FileExists(roFile) {
|
|
|
|
err := saveMacaroon(
|
|
|
|
ctx, svc, roFile, readPermissions, 0644,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-18 03:50:57 +02:00
|
|
|
}
|
2021-07-17 10:45:17 +02:00
|
|
|
|
2023-04-11 19:17:15 +02:00
|
|
|
// Generate the admin macaroon and write it to a file.
|
|
|
|
if !lnrpc.FileExists(admFile) {
|
|
|
|
err := saveMacaroon(
|
|
|
|
ctx, svc, admFile, adminPermissions(),
|
|
|
|
adminMacaroonFilePermissions,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-18 03:50:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2017-10-12 11:37:37 +02:00
|
|
|
|
2020-10-06 17:23:38 +02:00
|
|
|
// adminPermissions returns a list of all permissions in a safe way that doesn't
|
|
|
|
// modify any of the source lists.
|
|
|
|
func adminPermissions() []bakery.Op {
|
|
|
|
admin := make([]bakery.Op, len(readPermissions)+len(writePermissions))
|
|
|
|
copy(admin[:len(readPermissions)], readPermissions)
|
|
|
|
copy(admin[len(readPermissions):], writePermissions)
|
|
|
|
return admin
|
|
|
|
}
|
|
|
|
|
2021-02-02 13:45:29 +01:00
|
|
|
// createWalletUnlockerService creates a WalletUnlockerService from the passed
|
|
|
|
// config.
|
2021-03-19 15:53:06 +01:00
|
|
|
func createWalletUnlockerService(cfg *Config) *walletunlocker.UnlockerService {
|
2020-10-06 17:23:38 +02:00
|
|
|
// The macaroonFiles are passed to the wallet unlocker so they can be
|
|
|
|
// deleted and recreated in case the root macaroon key is also changed
|
|
|
|
// during the change password operation.
|
2018-04-20 09:14:41 +02:00
|
|
|
macaroonFiles := []string{
|
|
|
|
cfg.AdminMacPath, cfg.ReadMacPath, cfg.InvoiceMacPath,
|
|
|
|
}
|
2021-03-04 23:13:06 +01:00
|
|
|
|
2021-02-02 13:45:29 +01:00
|
|
|
return walletunlocker.New(
|
2021-08-03 09:57:30 +02:00
|
|
|
cfg.ActiveNetParams.Params, macaroonFiles,
|
2021-03-19 15:53:06 +01:00
|
|
|
cfg.ResetWalletTransactions, nil,
|
2018-04-20 09:06:06 +02:00
|
|
|
)
|
2021-02-02 13:45:29 +01:00
|
|
|
}
|
2020-10-06 17:23:34 +02:00
|
|
|
|
2021-02-02 13:45:29 +01:00
|
|
|
// startGrpcListen starts the GRPC server on the passed listeners.
|
|
|
|
func startGrpcListen(cfg *Config, grpcServer *grpc.Server,
|
|
|
|
listeners []*ListenerWithSignal) error {
|
2020-10-06 17:23:34 +02:00
|
|
|
|
2017-12-17 18:28:38 +01:00
|
|
|
// Use a WaitGroup so we can be sure the instructions on how to input the
|
|
|
|
// password is the last thing to be printed to the console.
|
|
|
|
var wg sync.WaitGroup
|
2017-10-12 11:37:37 +02:00
|
|
|
|
2019-07-09 11:09:19 +02:00
|
|
|
for _, lis := range listeners {
|
2017-12-17 18:28:38 +01:00
|
|
|
wg.Add(1)
|
2019-11-29 11:35:37 +01:00
|
|
|
go func(lis *ListenerWithSignal) {
|
2021-02-02 13:45:29 +01:00
|
|
|
rpcsLog.Infof("RPC server listening on %s", lis.Addr())
|
2019-11-29 11:35:37 +01:00
|
|
|
|
|
|
|
// Close the ready chan to indicate we are listening.
|
|
|
|
close(lis.Ready)
|
|
|
|
|
2017-12-17 18:28:38 +01:00
|
|
|
wg.Done()
|
2020-10-06 17:23:36 +02:00
|
|
|
_ = grpcServer.Serve(lis)
|
2019-07-09 11:09:19 +02:00
|
|
|
}(lis)
|
2017-12-17 18:28:38 +01:00
|
|
|
}
|
2017-10-12 11:37:37 +02:00
|
|
|
|
2021-02-02 13:45:29 +01:00
|
|
|
// If Prometheus monitoring is enabled, start the Prometheus exporter.
|
|
|
|
if cfg.Prometheus.Enabled() {
|
|
|
|
err := monitoring.ExportPrometheusMetrics(
|
|
|
|
grpcServer, cfg.Prometheus,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for gRPC servers to be up running.
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// startRestProxy starts the given REST proxy on the listeners found in the
|
|
|
|
// config.
|
|
|
|
func startRestProxy(cfg *Config, rpcServer *rpcServer, restDialOpts []grpc.DialOption,
|
|
|
|
restListen func(net.Addr) (net.Listener, error)) (func(), error) {
|
|
|
|
|
|
|
|
// We use the first RPC listener as the destination for our REST proxy.
|
|
|
|
// If the listener is set to listen on all interfaces, we replace it
|
|
|
|
// with localhost, as we cannot dial it directly.
|
|
|
|
restProxyDest := cfg.RPCListeners[0].String()
|
|
|
|
switch {
|
|
|
|
case strings.Contains(restProxyDest, "0.0.0.0"):
|
|
|
|
restProxyDest = strings.Replace(
|
|
|
|
restProxyDest, "0.0.0.0", "127.0.0.1", 1,
|
|
|
|
)
|
|
|
|
|
|
|
|
case strings.Contains(restProxyDest, "[::]"):
|
|
|
|
restProxyDest = strings.Replace(
|
|
|
|
restProxyDest, "[::]", "[::1]", 1,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
var shutdownFuncs []func()
|
|
|
|
shutdown := func() {
|
|
|
|
for _, shutdownFn := range shutdownFuncs {
|
|
|
|
shutdownFn()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start a REST proxy for our gRPC server.
|
2017-10-12 11:37:37 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2020-10-06 17:23:36 +02:00
|
|
|
shutdownFuncs = append(shutdownFuncs, cancel)
|
2017-10-12 11:37:37 +02:00
|
|
|
|
2021-02-02 13:45:29 +01:00
|
|
|
// We'll set up a proxy that will forward REST calls to the GRPC
|
|
|
|
// server.
|
|
|
|
//
|
|
|
|
// The default JSON marshaler of the REST proxy only sets OrigName to
|
|
|
|
// true, which instructs it to use the same field names as specified in
|
|
|
|
// the proto file and not switch to camel case. What we also want is
|
|
|
|
// that the marshaler prints all values, even if they are falsey.
|
|
|
|
customMarshalerOption := proxy.WithMarshalerOption(
|
|
|
|
proxy.MIMEWildcard, &proxy.JSONPb{
|
2023-05-08 14:54:09 +02:00
|
|
|
MarshalOptions: *lnrpc.RESTJsonMarshalOpts,
|
|
|
|
UnmarshalOptions: *lnrpc.RESTJsonUnmarshalOpts,
|
2021-02-02 13:45:29 +01:00
|
|
|
},
|
|
|
|
)
|
2022-03-23 17:40:35 +01:00
|
|
|
mux := proxy.NewServeMux(
|
|
|
|
customMarshalerOption,
|
|
|
|
|
|
|
|
// Don't allow falling back to other HTTP methods, we want exact
|
|
|
|
// matches only. The actual method to be used can be overwritten
|
|
|
|
// by setting X-HTTP-Method-Override so there should be no
|
|
|
|
// reason for not specifying the correct method in the first
|
|
|
|
// place.
|
|
|
|
proxy.WithDisablePathLengthFallback(),
|
|
|
|
)
|
2017-12-17 18:28:38 +01:00
|
|
|
|
2021-02-09 15:12:21 +01:00
|
|
|
// Register our services with the REST proxy.
|
2023-08-08 11:43:25 +02:00
|
|
|
err := rpcServer.RegisterWithRestProxy(
|
2021-02-02 13:45:29 +01:00
|
|
|
ctx, mux, restDialOpts, restProxyDest,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-10-12 11:37:37 +02:00
|
|
|
}
|
2017-12-17 18:28:38 +01:00
|
|
|
|
2021-02-02 13:45:29 +01:00
|
|
|
// Wrap the default grpc-gateway handler with the WebSocket handler.
|
2021-04-27 15:47:30 +02:00
|
|
|
restHandler := lnrpc.NewWebSocketProxy(
|
2021-04-27 15:47:34 +02:00
|
|
|
mux, rpcsLog, cfg.WSPingInterval, cfg.WSPongWait,
|
2021-04-27 15:47:32 +02:00
|
|
|
lnrpc.LndClientStreamingURIs,
|
2021-04-27 15:47:30 +02:00
|
|
|
)
|
2017-10-12 11:37:37 +02:00
|
|
|
|
2021-02-02 13:45:29 +01:00
|
|
|
// Use a WaitGroup so we can be sure the instructions on how to input the
|
|
|
|
// password is the last thing to be printed to the console.
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
// Now spin up a network listener for each requested port and start a
|
|
|
|
// goroutine that serves REST with the created mux there.
|
|
|
|
for _, restEndpoint := range cfg.RESTListeners {
|
2020-09-24 13:14:43 +02:00
|
|
|
lis, err := restListen(restEndpoint)
|
2017-10-12 11:37:37 +02:00
|
|
|
if err != nil {
|
2021-02-02 13:45:29 +01:00
|
|
|
ltndLog.Errorf("gRPC proxy unable to listen on %s",
|
|
|
|
restEndpoint)
|
|
|
|
return nil, err
|
2017-10-12 11:37:37 +02:00
|
|
|
}
|
2021-02-02 13:45:29 +01:00
|
|
|
|
2020-10-06 17:23:36 +02:00
|
|
|
shutdownFuncs = append(shutdownFuncs, func() {
|
|
|
|
err := lis.Close()
|
|
|
|
if err != nil {
|
|
|
|
rpcsLog.Errorf("Error closing listener: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
})
|
2017-12-17 18:28:38 +01:00
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
2021-02-02 13:45:29 +01:00
|
|
|
rpcsLog.Infof("gRPC proxy started at %s", lis.Addr())
|
|
|
|
|
|
|
|
// Create our proxy chain now. A request will pass
|
|
|
|
// through the following chain:
|
|
|
|
// req ---> CORS handler --> WS proxy --->
|
|
|
|
// REST proxy --> gRPC endpoint
|
|
|
|
corsHandler := allowCORS(restHandler, cfg.RestCORS)
|
|
|
|
|
2017-12-17 18:28:38 +01:00
|
|
|
wg.Done()
|
2021-02-02 13:45:29 +01:00
|
|
|
err := http.Serve(lis, corsHandler)
|
|
|
|
if err != nil && !lnrpc.IsClosedConnError(err) {
|
|
|
|
rpcsLog.Error(err)
|
|
|
|
}
|
2017-12-17 18:28:38 +01:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2021-02-02 13:45:29 +01:00
|
|
|
// Wait for REST servers to be up running.
|
2017-12-17 18:28:38 +01:00
|
|
|
wg.Wait()
|
2017-10-12 11:37:37 +02:00
|
|
|
|
2021-02-02 13:45:29 +01:00
|
|
|
return shutdown, nil
|
|
|
|
}
|