lnd.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. // Copyright (c) 2013-2017 The btcsuite developers
  2. // Copyright (c) 2015-2016 The Decred developers
  3. // Copyright (C) 2015-2022 The Lightning Network Developers
  4. package lnd
  5. import (
  6. "context"
  7. "errors"
  8. "fmt"
  9. "net"
  10. "net/http"
  11. "net/http/pprof"
  12. "os"
  13. "runtime"
  14. runtimePprof "runtime/pprof"
  15. "strings"
  16. "sync"
  17. "time"
  18. "github.com/btcsuite/btcd/btcutil"
  19. proxy "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
  20. "github.com/lightningnetwork/lnd/autopilot"
  21. "github.com/lightningnetwork/lnd/build"
  22. "github.com/lightningnetwork/lnd/chanacceptor"
  23. "github.com/lightningnetwork/lnd/channeldb"
  24. "github.com/lightningnetwork/lnd/keychain"
  25. "github.com/lightningnetwork/lnd/lncfg"
  26. "github.com/lightningnetwork/lnd/lnrpc"
  27. "github.com/lightningnetwork/lnd/lnwallet"
  28. "github.com/lightningnetwork/lnd/macaroons"
  29. "github.com/lightningnetwork/lnd/monitoring"
  30. "github.com/lightningnetwork/lnd/rpcperms"
  31. "github.com/lightningnetwork/lnd/signal"
  32. "github.com/lightningnetwork/lnd/tor"
  33. "github.com/lightningnetwork/lnd/walletunlocker"
  34. "github.com/lightningnetwork/lnd/watchtower"
  35. "google.golang.org/grpc"
  36. "google.golang.org/grpc/credentials"
  37. "google.golang.org/grpc/keepalive"
  38. "gopkg.in/macaroon-bakery.v2/bakery"
  39. "gopkg.in/macaroon.v2"
  40. )
  41. const (
  42. // adminMacaroonFilePermissions is the file permission that is used for
  43. // creating the admin macaroon file.
  44. //
  45. // Why 640 is safe:
  46. // Assuming a reasonably secure Linux system, it will have a
  47. // separate group for each user. E.g. a new user lnd gets assigned group
  48. // lnd which nothing else belongs to. A system that does not do this is
  49. // inherently broken already.
  50. //
  51. // Since there is no other user in the group, no other user can read
  52. // admin macaroon unless the administrator explicitly allowed it. Thus
  53. // there's no harm allowing group read.
  54. adminMacaroonFilePermissions = 0640
  55. )
  56. // AdminAuthOptions returns a list of DialOptions that can be used to
  57. // authenticate with the RPC server with admin capabilities.
  58. // skipMacaroons=true should be set if we don't want to include macaroons with
  59. // the auth options. This is needed for instance for the WalletUnlocker
  60. // service, which must be usable also before macaroons are created.
  61. //
  62. // NOTE: This should only be called after the RPCListener has signaled it is
  63. // ready.
  64. func AdminAuthOptions(cfg *Config, skipMacaroons bool) ([]grpc.DialOption,
  65. error) {
  66. creds, err := credentials.NewClientTLSFromFile(cfg.TLSCertPath, "")
  67. if err != nil {
  68. return nil, fmt.Errorf("unable to read TLS cert: %w", err)
  69. }
  70. // Create a dial options array.
  71. opts := []grpc.DialOption{
  72. grpc.WithTransportCredentials(creds),
  73. }
  74. // Get the admin macaroon if macaroons are active.
  75. if !skipMacaroons && !cfg.NoMacaroons {
  76. // Load the admin macaroon file.
  77. macBytes, err := os.ReadFile(cfg.AdminMacPath)
  78. if err != nil {
  79. return nil, fmt.Errorf("unable to read macaroon "+
  80. "path (check the network setting!): %v", err)
  81. }
  82. mac := &macaroon.Macaroon{}
  83. if err = mac.UnmarshalBinary(macBytes); err != nil {
  84. return nil, fmt.Errorf("unable to decode macaroon: %w",
  85. err)
  86. }
  87. // Now we append the macaroon credentials to the dial options.
  88. cred, err := macaroons.NewMacaroonCredential(mac)
  89. if err != nil {
  90. return nil, fmt.Errorf("error cloning mac: %w", err)
  91. }
  92. opts = append(opts, grpc.WithPerRPCCredentials(cred))
  93. }
  94. return opts, nil
  95. }
  96. // ListenerWithSignal is a net.Listener that has an additional Ready channel
  97. // that will be closed when a server starts listening.
  98. type ListenerWithSignal struct {
  99. net.Listener
  100. // Ready will be closed by the server listening on Listener.
  101. Ready chan struct{}
  102. // MacChan is an optional way to pass the admin macaroon to the program
  103. // that started lnd. The channel should be buffered to avoid lnd being
  104. // blocked on sending to the channel.
  105. MacChan chan []byte
  106. }
  107. // ListenerCfg is a wrapper around custom listeners that can be passed to lnd
  108. // when calling its main method.
  109. type ListenerCfg struct {
  110. // RPCListeners can be set to the listeners to use for the RPC server.
  111. // If empty a regular network listener will be created.
  112. RPCListeners []*ListenerWithSignal
  113. }
  114. var errStreamIsolationWithProxySkip = errors.New(
  115. "while stream isolation is enabled, the TOR proxy may not be skipped",
  116. )
  117. // Main is the true entry point for lnd. It accepts a fully populated and
  118. // validated main configuration struct and an optional listener config struct.
  119. // This function starts all main system components then blocks until a signal
  120. // is received on the shutdownChan at which point everything is shut down again.
  121. func Main(cfg *Config, lisCfg ListenerCfg, implCfg *ImplementationCfg,
  122. interceptor signal.Interceptor) error {
  123. defer func() {
  124. ltndLog.Info("Shutdown complete\n")
  125. err := cfg.LogWriter.Close()
  126. if err != nil {
  127. ltndLog.Errorf("Could not close log rotator: %v", err)
  128. }
  129. }()
  130. mkErr := func(format string, args ...interface{}) error {
  131. ltndLog.Errorf("Shutting down because error in main "+
  132. "method: "+format, args...)
  133. return fmt.Errorf(format, args...)
  134. }
  135. // Show version at startup.
  136. ltndLog.Infof("Version: %s commit=%s, build=%s, logging=%s, "+
  137. "debuglevel=%s", build.Version(), build.Commit,
  138. build.Deployment, build.LoggingType, cfg.DebugLevel)
  139. var network string
  140. switch {
  141. case cfg.Bitcoin.TestNet3:
  142. network = "testnet"
  143. case cfg.Bitcoin.MainNet:
  144. network = "mainnet"
  145. case cfg.Bitcoin.SimNet:
  146. network = "simnet"
  147. case cfg.Bitcoin.RegTest:
  148. network = "regtest"
  149. case cfg.Bitcoin.SigNet:
  150. network = "signet"
  151. }
  152. ltndLog.Infof("Active chain: %v (network=%v)",
  153. strings.Title(BitcoinChainName), network,
  154. )
  155. ctx := context.Background()
  156. ctx, cancel := context.WithCancel(ctx)
  157. defer cancel()
  158. // Enable http profiling server if requested.
  159. if cfg.Profile != "" {
  160. // Create the http handler.
  161. pprofMux := http.NewServeMux()
  162. pprofMux.HandleFunc("/debug/pprof/", pprof.Index)
  163. pprofMux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
  164. pprofMux.HandleFunc("/debug/pprof/profile", pprof.Profile)
  165. pprofMux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
  166. pprofMux.HandleFunc("/debug/pprof/trace", pprof.Trace)
  167. if cfg.BlockingProfile != 0 {
  168. runtime.SetBlockProfileRate(cfg.BlockingProfile)
  169. }
  170. if cfg.MutexProfile != 0 {
  171. runtime.SetMutexProfileFraction(cfg.MutexProfile)
  172. }
  173. // Redirect all requests to the pprof handler, thus visiting
  174. // `127.0.0.1:6060` will be redirected to
  175. // `127.0.0.1:6060/debug/pprof`.
  176. pprofMux.Handle("/", http.RedirectHandler(
  177. "/debug/pprof/", http.StatusSeeOther,
  178. ))
  179. ltndLog.Infof("Pprof listening on %v", cfg.Profile)
  180. // Create the pprof server.
  181. pprofServer := &http.Server{
  182. Addr: cfg.Profile,
  183. Handler: pprofMux,
  184. ReadHeaderTimeout: cfg.HTTPHeaderTimeout,
  185. }
  186. // Shut the server down when lnd is shutting down.
  187. defer func() {
  188. ltndLog.Info("Stopping pprof server...")
  189. err := pprofServer.Shutdown(ctx)
  190. if err != nil {
  191. ltndLog.Errorf("Stop pprof server got err: %v",
  192. err)
  193. }
  194. }()
  195. // Start the pprof server.
  196. go func() {
  197. err := pprofServer.ListenAndServe()
  198. if err != nil && !errors.Is(err, http.ErrServerClosed) {
  199. ltndLog.Errorf("Serving pprof got err: %v", err)
  200. }
  201. }()
  202. }
  203. // Write cpu profile if requested.
  204. if cfg.CPUProfile != "" {
  205. f, err := os.Create(cfg.CPUProfile)
  206. if err != nil {
  207. return mkErr("unable to create CPU profile: %v", err)
  208. }
  209. _ = runtimePprof.StartCPUProfile(f)
  210. defer func() {
  211. _ = f.Close()
  212. }()
  213. defer runtimePprof.StopCPUProfile()
  214. }
  215. // Run configuration dependent DB pre-initialization. Note that this
  216. // needs to be done early and once during the startup process, before
  217. // any DB access.
  218. if err := cfg.DB.Init(ctx, cfg.graphDatabaseDir()); err != nil {
  219. return mkErr("error initializing DBs: %v", err)
  220. }
  221. tlsManagerCfg := &TLSManagerCfg{
  222. TLSCertPath: cfg.TLSCertPath,
  223. TLSKeyPath: cfg.TLSKeyPath,
  224. TLSEncryptKey: cfg.TLSEncryptKey,
  225. TLSExtraIPs: cfg.TLSExtraIPs,
  226. TLSExtraDomains: cfg.TLSExtraDomains,
  227. TLSAutoRefresh: cfg.TLSAutoRefresh,
  228. TLSDisableAutofill: cfg.TLSDisableAutofill,
  229. TLSCertDuration: cfg.TLSCertDuration,
  230. LetsEncryptDir: cfg.LetsEncryptDir,
  231. LetsEncryptDomain: cfg.LetsEncryptDomain,
  232. LetsEncryptListen: cfg.LetsEncryptListen,
  233. DisableRestTLS: cfg.DisableRestTLS,
  234. HTTPHeaderTimeout: cfg.HTTPHeaderTimeout,
  235. }
  236. tlsManager := NewTLSManager(tlsManagerCfg)
  237. serverOpts, restDialOpts, restListen, cleanUp,
  238. err := tlsManager.SetCertificateBeforeUnlock()
  239. if err != nil {
  240. return mkErr("error setting cert before unlock: %v", err)
  241. }
  242. if cleanUp != nil {
  243. defer cleanUp()
  244. }
  245. // If we have chosen to start with a dedicated listener for the
  246. // rpc server, we set it directly.
  247. grpcListeners := append([]*ListenerWithSignal{}, lisCfg.RPCListeners...)
  248. if len(grpcListeners) == 0 {
  249. // Otherwise we create listeners from the RPCListeners defined
  250. // in the config.
  251. for _, grpcEndpoint := range cfg.RPCListeners {
  252. // Start a gRPC server listening for HTTP/2
  253. // connections.
  254. lis, err := lncfg.ListenOnAddress(grpcEndpoint)
  255. if err != nil {
  256. return mkErr("unable to listen on %s: %v",
  257. grpcEndpoint, err)
  258. }
  259. defer lis.Close()
  260. grpcListeners = append(
  261. grpcListeners, &ListenerWithSignal{
  262. Listener: lis,
  263. Ready: make(chan struct{}),
  264. },
  265. )
  266. }
  267. }
  268. // Create a new RPC interceptor that we'll add to the GRPC server. This
  269. // will be used to log the API calls invoked on the GRPC server.
  270. interceptorChain := rpcperms.NewInterceptorChain(
  271. rpcsLog, cfg.NoMacaroons, cfg.RPCMiddleware.Mandatory,
  272. )
  273. if err := interceptorChain.Start(); err != nil {
  274. return mkErr("error starting interceptor chain: %v", err)
  275. }
  276. defer func() {
  277. err := interceptorChain.Stop()
  278. if err != nil {
  279. ltndLog.Warnf("error stopping RPC interceptor "+
  280. "chain: %v", err)
  281. }
  282. }()
  283. // Allow the user to overwrite some defaults of the gRPC library related
  284. // to connection keepalive (server side and client side pings).
  285. serverKeepalive := keepalive.ServerParameters{
  286. Time: cfg.GRPC.ServerPingTime,
  287. Timeout: cfg.GRPC.ServerPingTimeout,
  288. }
  289. clientKeepalive := keepalive.EnforcementPolicy{
  290. MinTime: cfg.GRPC.ClientPingMinWait,
  291. PermitWithoutStream: cfg.GRPC.ClientAllowPingWithoutStream,
  292. }
  293. rpcServerOpts := interceptorChain.CreateServerOpts()
  294. serverOpts = append(serverOpts, rpcServerOpts...)
  295. serverOpts = append(
  296. serverOpts, grpc.MaxRecvMsgSize(lnrpc.MaxGrpcMsgSize),
  297. grpc.KeepaliveParams(serverKeepalive),
  298. grpc.KeepaliveEnforcementPolicy(clientKeepalive),
  299. )
  300. grpcServer := grpc.NewServer(serverOpts...)
  301. defer grpcServer.Stop()
  302. // We'll also register the RPC interceptor chain as the StateServer, as
  303. // it can be used to query for the current state of the wallet.
  304. lnrpc.RegisterStateServer(grpcServer, interceptorChain)
  305. // Initialize, and register our implementation of the gRPC interface
  306. // exported by the rpcServer.
  307. rpcServer := newRPCServer(cfg, interceptorChain, implCfg, interceptor)
  308. err = rpcServer.RegisterWithGrpcServer(grpcServer)
  309. if err != nil {
  310. return mkErr("error registering gRPC server: %v", err)
  311. }
  312. // Now that both the WalletUnlocker and LightningService have been
  313. // registered with the GRPC server, we can start listening.
  314. err = startGrpcListen(cfg, grpcServer, grpcListeners)
  315. if err != nil {
  316. return mkErr("error starting gRPC listener: %v", err)
  317. }
  318. // Now start the REST proxy for our gRPC server above. We'll ensure
  319. // we direct LND to connect to its loopback address rather than a
  320. // wildcard to prevent certificate issues when accessing the proxy
  321. // externally.
  322. stopProxy, err := startRestProxy(
  323. cfg, rpcServer, restDialOpts, restListen,
  324. )
  325. if err != nil {
  326. return mkErr("error starting REST proxy: %v", err)
  327. }
  328. defer stopProxy()
  329. // Start leader election if we're running on etcd. Continuation will be
  330. // blocked until this instance is elected as the current leader or
  331. // shutting down.
  332. elected := false
  333. if cfg.Cluster.EnableLeaderElection {
  334. electionCtx, cancelElection := context.WithCancel(ctx)
  335. go func() {
  336. <-interceptor.ShutdownChannel()
  337. cancelElection()
  338. }()
  339. ltndLog.Infof("Using %v leader elector",
  340. cfg.Cluster.LeaderElector)
  341. leaderElector, err := cfg.Cluster.MakeLeaderElector(
  342. electionCtx, cfg.DB,
  343. )
  344. if err != nil {
  345. return err
  346. }
  347. defer func() {
  348. if !elected {
  349. return
  350. }
  351. ltndLog.Infof("Attempting to resign from leader role "+
  352. "(%v)", cfg.Cluster.ID)
  353. if err := leaderElector.Resign(); err != nil {
  354. ltndLog.Errorf("Leader elector failed to "+
  355. "resign: %v", err)
  356. }
  357. }()
  358. ltndLog.Infof("Starting leadership campaign (%v)",
  359. cfg.Cluster.ID)
  360. if err := leaderElector.Campaign(electionCtx); err != nil {
  361. return mkErr("leadership campaign failed: %v", err)
  362. }
  363. elected = true
  364. ltndLog.Infof("Elected as leader (%v)", cfg.Cluster.ID)
  365. }
  366. dbs, cleanUp, err := implCfg.DatabaseBuilder.BuildDatabase(ctx)
  367. switch {
  368. case err == channeldb.ErrDryRunMigrationOK:
  369. ltndLog.Infof("%v, exiting", err)
  370. return nil
  371. case err != nil:
  372. return mkErr("unable to open databases: %v", err)
  373. }
  374. defer cleanUp()
  375. partialChainControl, walletConfig, cleanUp, err := implCfg.BuildWalletConfig(
  376. ctx, dbs, interceptorChain, grpcListeners,
  377. )
  378. if err != nil {
  379. return mkErr("error creating wallet config: %v", err)
  380. }
  381. defer cleanUp()
  382. activeChainControl, cleanUp, err := implCfg.BuildChainControl(
  383. partialChainControl, walletConfig,
  384. )
  385. if err != nil {
  386. return mkErr("error loading chain control: %v", err)
  387. }
  388. defer cleanUp()
  389. // TODO(roasbeef): add rotation
  390. idKeyDesc, err := activeChainControl.KeyRing.DeriveKey(
  391. keychain.KeyLocator{
  392. Family: keychain.KeyFamilyNodeKey,
  393. Index: 0,
  394. },
  395. )
  396. if err != nil {
  397. return mkErr("error deriving node key: %v", err)
  398. }
  399. if cfg.Tor.StreamIsolation && cfg.Tor.SkipProxyForClearNetTargets {
  400. return errStreamIsolationWithProxySkip
  401. }
  402. if cfg.Tor.Active {
  403. if cfg.Tor.SkipProxyForClearNetTargets {
  404. srvrLog.Info("Onion services are accessible via Tor! " +
  405. "NOTE: Traffic to clearnet services is not " +
  406. "routed via Tor.")
  407. } else {
  408. srvrLog.Infof("Proxying all network traffic via Tor "+
  409. "(stream_isolation=%v)! NOTE: Ensure the "+
  410. "backend node is proxying over Tor as well",
  411. cfg.Tor.StreamIsolation)
  412. }
  413. }
  414. // If tor is active and either v2 or v3 onion services have been
  415. // specified, make a tor controller and pass it into both the watchtower
  416. // server and the regular lnd server.
  417. var torController *tor.Controller
  418. if cfg.Tor.Active && (cfg.Tor.V2 || cfg.Tor.V3) {
  419. torController = tor.NewController(
  420. cfg.Tor.Control, cfg.Tor.TargetIPAddress,
  421. cfg.Tor.Password,
  422. )
  423. // Start the tor controller before giving it to any other
  424. // subsystems.
  425. if err := torController.Start(); err != nil {
  426. return mkErr("unable to initialize tor controller: %v",
  427. err)
  428. }
  429. defer func() {
  430. if err := torController.Stop(); err != nil {
  431. ltndLog.Errorf("error stopping tor "+
  432. "controller: %v", err)
  433. }
  434. }()
  435. }
  436. var tower *watchtower.Standalone
  437. if cfg.Watchtower.Active {
  438. towerKeyDesc, err := activeChainControl.KeyRing.DeriveKey(
  439. keychain.KeyLocator{
  440. Family: keychain.KeyFamilyTowerID,
  441. Index: 0,
  442. },
  443. )
  444. if err != nil {
  445. return mkErr("error deriving tower key: %v", err)
  446. }
  447. wtCfg := &watchtower.Config{
  448. BlockFetcher: activeChainControl.ChainIO,
  449. DB: dbs.TowerServerDB,
  450. EpochRegistrar: activeChainControl.ChainNotifier,
  451. Net: cfg.net,
  452. NewAddress: func() (btcutil.Address, error) {
  453. return activeChainControl.Wallet.NewAddress(
  454. lnwallet.TaprootPubkey, false,
  455. lnwallet.DefaultAccountName,
  456. )
  457. },
  458. NodeKeyECDH: keychain.NewPubKeyECDH(
  459. towerKeyDesc, activeChainControl.KeyRing,
  460. ),
  461. PublishTx: activeChainControl.Wallet.PublishTransaction,
  462. ChainHash: *cfg.ActiveNetParams.GenesisHash,
  463. }
  464. // If there is a tor controller (user wants auto hidden
  465. // services), then store a pointer in the watchtower config.
  466. if torController != nil {
  467. wtCfg.TorController = torController
  468. wtCfg.WatchtowerKeyPath = cfg.Tor.WatchtowerKeyPath
  469. wtCfg.EncryptKey = cfg.Tor.EncryptKey
  470. wtCfg.KeyRing = activeChainControl.KeyRing
  471. switch {
  472. case cfg.Tor.V2:
  473. wtCfg.Type = tor.V2
  474. case cfg.Tor.V3:
  475. wtCfg.Type = tor.V3
  476. }
  477. }
  478. wtConfig, err := cfg.Watchtower.Apply(
  479. wtCfg, lncfg.NormalizeAddresses,
  480. )
  481. if err != nil {
  482. return mkErr("unable to configure watchtower: %v", err)
  483. }
  484. tower, err = watchtower.New(wtConfig)
  485. if err != nil {
  486. return mkErr("unable to create watchtower: %v", err)
  487. }
  488. }
  489. // Initialize the MultiplexAcceptor. If lnd was started with the
  490. // zero-conf feature bit, then this will be a ZeroConfAcceptor.
  491. // Otherwise, this will be a ChainedAcceptor.
  492. var multiAcceptor chanacceptor.MultiplexAcceptor
  493. if cfg.ProtocolOptions.ZeroConf() {
  494. multiAcceptor = chanacceptor.NewZeroConfAcceptor()
  495. } else {
  496. multiAcceptor = chanacceptor.NewChainedAcceptor()
  497. }
  498. // Set up the core server which will listen for incoming peer
  499. // connections.
  500. server, err := newServer(
  501. cfg, cfg.Listeners, dbs, activeChainControl, &idKeyDesc,
  502. activeChainControl.Cfg.WalletUnlockParams.ChansToRestore,
  503. multiAcceptor, torController, tlsManager,
  504. )
  505. if err != nil {
  506. return mkErr("unable to create server: %v", err)
  507. }
  508. // Set up an autopilot manager from the current config. This will be
  509. // used to manage the underlying autopilot agent, starting and stopping
  510. // it at will.
  511. atplCfg, err := initAutoPilot(
  512. server, cfg.Autopilot, activeChainControl.MinHtlcIn,
  513. cfg.ActiveNetParams,
  514. )
  515. if err != nil {
  516. return mkErr("unable to initialize autopilot: %v", err)
  517. }
  518. atplManager, err := autopilot.NewManager(atplCfg)
  519. if err != nil {
  520. return mkErr("unable to create autopilot manager: %v", err)
  521. }
  522. if err := atplManager.Start(); err != nil {
  523. return mkErr("unable to start autopilot manager: %v", err)
  524. }
  525. defer atplManager.Stop()
  526. err = tlsManager.LoadPermanentCertificate(activeChainControl.KeyRing)
  527. if err != nil {
  528. return mkErr("unable to load permanent TLS certificate: %v",
  529. err)
  530. }
  531. // Now we have created all dependencies necessary to populate and
  532. // start the RPC server.
  533. err = rpcServer.addDeps(
  534. server, interceptorChain.MacaroonService(), cfg.SubRPCServers,
  535. atplManager, server.invoices, tower, multiAcceptor,
  536. )
  537. if err != nil {
  538. return mkErr("unable to add deps to RPC server: %v", err)
  539. }
  540. if err := rpcServer.Start(); err != nil {
  541. return mkErr("unable to start RPC server: %v", err)
  542. }
  543. defer rpcServer.Stop()
  544. // We transition the RPC state to Active, as the RPC server is up.
  545. interceptorChain.SetRPCActive()
  546. if err := interceptor.Notifier.NotifyReady(true); err != nil {
  547. return mkErr("error notifying ready: %v", err)
  548. }
  549. // We'll wait until we're fully synced to continue the start up of the
  550. // remainder of the daemon. This ensures that we don't accept any
  551. // possibly invalid state transitions, or accept channels with spent
  552. // funds.
  553. _, bestHeight, err := activeChainControl.ChainIO.GetBestBlock()
  554. if err != nil {
  555. return mkErr("unable to determine chain tip: %v", err)
  556. }
  557. ltndLog.Infof("Waiting for chain backend to finish sync, "+
  558. "start_height=%v", bestHeight)
  559. for {
  560. if !interceptor.Alive() {
  561. return nil
  562. }
  563. synced, ts, err := activeChainControl.Wallet.IsSynced()
  564. if err != nil {
  565. return mkErr("unable to determine if wallet is "+
  566. "synced: %v", err)
  567. }
  568. ltndLog.Debugf("Syncing to block timestamp: %v, is synced=%v",
  569. time.Unix(ts, 0), synced)
  570. if synced {
  571. break
  572. }
  573. time.Sleep(time.Second * 1)
  574. }
  575. _, bestHeight, err = activeChainControl.ChainIO.GetBestBlock()
  576. if err != nil {
  577. return mkErr("unable to determine chain tip: %v", err)
  578. }
  579. ltndLog.Infof("Chain backend is fully synced (end_height=%v)!",
  580. bestHeight)
  581. // With all the relevant chains initialized, we can finally start the
  582. // server itself.
  583. if err := server.Start(); err != nil {
  584. return mkErr("unable to start server: %v", err)
  585. }
  586. defer server.Stop()
  587. // We transition the server state to Active, as the server is up.
  588. interceptorChain.SetServerActive()
  589. // Now that the server has started, if the autopilot mode is currently
  590. // active, then we'll start the autopilot agent immediately. It will be
  591. // stopped together with the autopilot service.
  592. if cfg.Autopilot.Active {
  593. if err := atplManager.StartAgent(); err != nil {
  594. return mkErr("unable to start autopilot agent: %v", err)
  595. }
  596. }
  597. if cfg.Watchtower.Active {
  598. if err := tower.Start(); err != nil {
  599. return mkErr("unable to start watchtower: %v", err)
  600. }
  601. defer tower.Stop()
  602. }
  603. // Wait for shutdown signal from either a graceful server stop or from
  604. // the interrupt handler.
  605. <-interceptor.ShutdownChannel()
  606. return nil
  607. }
  608. // bakeMacaroon creates a new macaroon with newest version and the given
  609. // permissions then returns it binary serialized.
  610. func bakeMacaroon(ctx context.Context, svc *macaroons.Service,
  611. permissions []bakery.Op) ([]byte, error) {
  612. mac, err := svc.NewMacaroon(
  613. ctx, macaroons.DefaultRootKeyID, permissions...,
  614. )
  615. if err != nil {
  616. return nil, err
  617. }
  618. return mac.M().MarshalBinary()
  619. }
  620. // saveMacaroon bakes a macaroon with the specified macaroon permissions and
  621. // writes it to a file with the given filename and file permissions.
  622. func saveMacaroon(ctx context.Context, svc *macaroons.Service, filename string,
  623. macaroonPermissions []bakery.Op, filePermissions os.FileMode) error {
  624. macaroonBytes, err := bakeMacaroon(ctx, svc, macaroonPermissions)
  625. if err != nil {
  626. return err
  627. }
  628. err = os.WriteFile(filename, macaroonBytes, filePermissions)
  629. if err != nil {
  630. _ = os.Remove(filename)
  631. return err
  632. }
  633. return nil
  634. }
  635. // genDefaultMacaroons checks for three default macaroon files and generates
  636. // them if they do not exist; one admin-level, one for invoice access and one
  637. // read-only. Each macaroon is checked and created independently to ensure all
  638. // three exist. The admin macaroon can also be used to generate more granular
  639. // macaroons.
  640. func genDefaultMacaroons(ctx context.Context, svc *macaroons.Service,
  641. admFile, roFile, invoiceFile string) error {
  642. // First, we'll generate a macaroon that only allows the caller to
  643. // access invoice related calls. This is useful for merchants and other
  644. // services to allow an isolated instance that can only query and
  645. // modify invoices.
  646. if !lnrpc.FileExists(invoiceFile) {
  647. err := saveMacaroon(
  648. ctx, svc, invoiceFile, invoicePermissions, 0644,
  649. )
  650. if err != nil {
  651. return err
  652. }
  653. }
  654. // Generate the read-only macaroon and write it to a file.
  655. if !lnrpc.FileExists(roFile) {
  656. err := saveMacaroon(
  657. ctx, svc, roFile, readPermissions, 0644,
  658. )
  659. if err != nil {
  660. return err
  661. }
  662. }
  663. // Generate the admin macaroon and write it to a file.
  664. if !lnrpc.FileExists(admFile) {
  665. err := saveMacaroon(
  666. ctx, svc, admFile, adminPermissions(),
  667. adminMacaroonFilePermissions,
  668. )
  669. if err != nil {
  670. return err
  671. }
  672. }
  673. return nil
  674. }
  675. // adminPermissions returns a list of all permissions in a safe way that doesn't
  676. // modify any of the source lists.
  677. func adminPermissions() []bakery.Op {
  678. admin := make([]bakery.Op, len(readPermissions)+len(writePermissions))
  679. copy(admin[:len(readPermissions)], readPermissions)
  680. copy(admin[len(readPermissions):], writePermissions)
  681. return admin
  682. }
  683. // createWalletUnlockerService creates a WalletUnlockerService from the passed
  684. // config.
  685. func createWalletUnlockerService(cfg *Config) *walletunlocker.UnlockerService {
  686. // The macaroonFiles are passed to the wallet unlocker so they can be
  687. // deleted and recreated in case the root macaroon key is also changed
  688. // during the change password operation.
  689. macaroonFiles := []string{
  690. cfg.AdminMacPath, cfg.ReadMacPath, cfg.InvoiceMacPath,
  691. }
  692. return walletunlocker.New(
  693. cfg.ActiveNetParams.Params, macaroonFiles,
  694. cfg.ResetWalletTransactions, nil,
  695. )
  696. }
  697. // startGrpcListen starts the GRPC server on the passed listeners.
  698. func startGrpcListen(cfg *Config, grpcServer *grpc.Server,
  699. listeners []*ListenerWithSignal) error {
  700. // Use a WaitGroup so we can be sure the instructions on how to input the
  701. // password is the last thing to be printed to the console.
  702. var wg sync.WaitGroup
  703. for _, lis := range listeners {
  704. wg.Add(1)
  705. go func(lis *ListenerWithSignal) {
  706. rpcsLog.Infof("RPC server listening on %s", lis.Addr())
  707. // Close the ready chan to indicate we are listening.
  708. close(lis.Ready)
  709. wg.Done()
  710. _ = grpcServer.Serve(lis)
  711. }(lis)
  712. }
  713. // If Prometheus monitoring is enabled, start the Prometheus exporter.
  714. if cfg.Prometheus.Enabled() {
  715. err := monitoring.ExportPrometheusMetrics(
  716. grpcServer, cfg.Prometheus,
  717. )
  718. if err != nil {
  719. return err
  720. }
  721. }
  722. // Wait for gRPC servers to be up running.
  723. wg.Wait()
  724. return nil
  725. }
  726. // startRestProxy starts the given REST proxy on the listeners found in the
  727. // config.
  728. func startRestProxy(cfg *Config, rpcServer *rpcServer, restDialOpts []grpc.DialOption,
  729. restListen func(net.Addr) (net.Listener, error)) (func(), error) {
  730. // We use the first RPC listener as the destination for our REST proxy.
  731. // If the listener is set to listen on all interfaces, we replace it
  732. // with localhost, as we cannot dial it directly.
  733. restProxyDest := cfg.RPCListeners[0].String()
  734. switch {
  735. case strings.Contains(restProxyDest, "0.0.0.0"):
  736. restProxyDest = strings.Replace(
  737. restProxyDest, "0.0.0.0", "127.0.0.1", 1,
  738. )
  739. case strings.Contains(restProxyDest, "[::]"):
  740. restProxyDest = strings.Replace(
  741. restProxyDest, "[::]", "[::1]", 1,
  742. )
  743. }
  744. var shutdownFuncs []func()
  745. shutdown := func() {
  746. for _, shutdownFn := range shutdownFuncs {
  747. shutdownFn()
  748. }
  749. }
  750. // Start a REST proxy for our gRPC server.
  751. ctx := context.Background()
  752. ctx, cancel := context.WithCancel(ctx)
  753. shutdownFuncs = append(shutdownFuncs, cancel)
  754. // We'll set up a proxy that will forward REST calls to the GRPC
  755. // server.
  756. //
  757. // The default JSON marshaler of the REST proxy only sets OrigName to
  758. // true, which instructs it to use the same field names as specified in
  759. // the proto file and not switch to camel case. What we also want is
  760. // that the marshaler prints all values, even if they are falsey.
  761. customMarshalerOption := proxy.WithMarshalerOption(
  762. proxy.MIMEWildcard, &proxy.JSONPb{
  763. MarshalOptions: *lnrpc.RESTJsonMarshalOpts,
  764. UnmarshalOptions: *lnrpc.RESTJsonUnmarshalOpts,
  765. },
  766. )
  767. mux := proxy.NewServeMux(
  768. customMarshalerOption,
  769. // Don't allow falling back to other HTTP methods, we want exact
  770. // matches only. The actual method to be used can be overwritten
  771. // by setting X-HTTP-Method-Override so there should be no
  772. // reason for not specifying the correct method in the first
  773. // place.
  774. proxy.WithDisablePathLengthFallback(),
  775. )
  776. // Register our services with the REST proxy.
  777. err := rpcServer.RegisterWithRestProxy(
  778. ctx, mux, restDialOpts, restProxyDest,
  779. )
  780. if err != nil {
  781. return nil, err
  782. }
  783. // Wrap the default grpc-gateway handler with the WebSocket handler.
  784. restHandler := lnrpc.NewWebSocketProxy(
  785. mux, rpcsLog, cfg.WSPingInterval, cfg.WSPongWait,
  786. lnrpc.LndClientStreamingURIs,
  787. )
  788. // Use a WaitGroup so we can be sure the instructions on how to input the
  789. // password is the last thing to be printed to the console.
  790. var wg sync.WaitGroup
  791. // Now spin up a network listener for each requested port and start a
  792. // goroutine that serves REST with the created mux there.
  793. for _, restEndpoint := range cfg.RESTListeners {
  794. lis, err := restListen(restEndpoint)
  795. if err != nil {
  796. ltndLog.Errorf("gRPC proxy unable to listen on %s",
  797. restEndpoint)
  798. return nil, err
  799. }
  800. shutdownFuncs = append(shutdownFuncs, func() {
  801. err := lis.Close()
  802. if err != nil {
  803. rpcsLog.Errorf("Error closing listener: %v",
  804. err)
  805. }
  806. })
  807. wg.Add(1)
  808. go func() {
  809. rpcsLog.Infof("gRPC proxy started at %s", lis.Addr())
  810. // Create our proxy chain now. A request will pass
  811. // through the following chain:
  812. // req ---> CORS handler --> WS proxy --->
  813. // REST proxy --> gRPC endpoint
  814. corsHandler := allowCORS(restHandler, cfg.RestCORS)
  815. wg.Done()
  816. err := http.Serve(lis, corsHandler)
  817. if err != nil && !lnrpc.IsClosedConnError(err) {
  818. rpcsLog.Error(err)
  819. }
  820. }()
  821. }
  822. // Wait for REST servers to be up running.
  823. wg.Wait()
  824. return shutdown, nil
  825. }