Skip to content

Commit 560ce8c

Browse files
committed
config+terminal: extract LND connection params into method
To prepare for distinguishing between a local and remote connection to lnd, the connection parameters are extracted into a method of the terminal config struct.
1 parent a6c0074 commit 560ce8c

File tree

2 files changed

+61
-47
lines changed

2 files changed

+61
-47
lines changed

config.go

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/jessevdk/go-flags"
1515
"github.com/lightninglabs/faraday"
16+
"github.com/lightninglabs/lndclient"
1617
"github.com/lightninglabs/loop/loopd"
1718
"github.com/lightningnetwork/lnd"
1819
"github.com/lightningnetwork/lnd/build"
@@ -54,13 +55,45 @@ type Config struct {
5455
Loop *loopd.Config `group:"loop" namespace:"loop"`
5556
}
5657

58+
// lndConnectParams returns the connection parameters to connect to the local
59+
// lnd instance.
60+
func (c *Config) lndConnectParams() (string, lndclient.Network, string, string,
61+
error) {
62+
63+
network, err := getNetwork(c.Lnd.Bitcoin)
64+
if err != nil {
65+
return "", "", "", "", err
66+
}
67+
68+
// When we start lnd internally, we take the listen address as
69+
// the client dial address. But with TLS enabled by default, we
70+
// cannot call 0.0.0.0 internally when dialing lnd as that IP
71+
// address isn't in the cert. We need to rewrite it to the
72+
// loopback address.
73+
lndDialAddr := c.Lnd.RPCListeners[0].String()
74+
switch {
75+
case strings.Contains(lndDialAddr, "0.0.0.0"):
76+
lndDialAddr = strings.Replace(
77+
lndDialAddr, "0.0.0.0", "127.0.0.1", 1,
78+
)
79+
80+
case strings.Contains(lndDialAddr, "[::]"):
81+
lndDialAddr = strings.Replace(
82+
lndDialAddr, "[::]", "[::1]", 1,
83+
)
84+
}
85+
86+
return lndDialAddr, lndclient.Network(network),
87+
c.Lnd.TLSCertPath, c.Lnd.AdminMacPath, nil
88+
}
89+
5790
// defaultConfig returns a configuration struct with all default values set.
5891
func defaultConfig() *Config {
5992
return &Config{
60-
HTTPSListen: defaultHTTPSListen,
61-
Lnd: &lndDefaultConfig,
62-
Faraday: &faradayDefaultConfig,
63-
Loop: &loopDefaultConfig,
93+
HTTPSListen: defaultHTTPSListen,
94+
Lnd: &lndDefaultConfig,
95+
Faraday: &faradayDefaultConfig,
96+
Loop: &loopDefaultConfig,
6497
}
6598
}
6699

terminal.go

Lines changed: 24 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ var (
6565
// start an lnd node then start and register external subservers to it.
6666
type LightningTerminal struct {
6767
cfg *Config
68-
lndAddr string
6968
listenerCfg lnd.ListenerCfg
7069

7170
wg sync.WaitGroup
@@ -148,33 +147,6 @@ func (g *LightningTerminal) Run() error {
148147
},
149148
}
150149

151-
// With TLS enabled by default, we cannot call 0.0.0.0 internally when
152-
// dialing lnd as that IP address isn't in the cert. We need to rewrite
153-
// it to the loopback address.
154-
lndDialAddr := rpcAddr.String()
155-
switch {
156-
case strings.Contains(lndDialAddr, "0.0.0.0"):
157-
lndDialAddr = strings.Replace(
158-
lndDialAddr, "0.0.0.0", "127.0.0.1", 1,
159-
)
160-
161-
case strings.Contains(lndDialAddr, "[::]"):
162-
lndDialAddr = strings.Replace(
163-
lndDialAddr, "[::]", "[::1]", 1,
164-
)
165-
}
166-
g.lndAddr = lndDialAddr
167-
168-
// Some of the subservers' configuration options won't have any effect
169-
// (like the log or lnd options) as they will be taken from lnd's config
170-
// struct. Others we want to force to be the same as lnd so the user
171-
// doesn't have to set them manually, like the network for example.
172-
network, err := getNetwork(g.cfg.Lnd.Bitcoin)
173-
if err != nil {
174-
return err
175-
}
176-
g.cfg.Loop.Network = network
177-
178150
// Create the instances of our subservers now so we can hook them up to
179151
// lnd once it's fully started.
180152
g.faradayServer = frdrpc.NewRPCServer(&frdrpc.Config{})
@@ -216,7 +188,7 @@ func (g *LightningTerminal) Run() error {
216188
case <-signal.ShutdownChannel():
217189
return errors.New("shutting down")
218190
}
219-
err = g.startSubservers(network)
191+
err = g.startSubservers()
220192
if err != nil {
221193
log.Errorf("Could not start subservers: %v", err)
222194
return err
@@ -253,25 +225,36 @@ func (g *LightningTerminal) Run() error {
253225
// startSubservers creates an internal connection to lnd and then starts all
254226
// embedded daemons as external subservers that hook into the same gRPC and REST
255227
// servers that lnd started.
256-
func (g *LightningTerminal) startSubservers(network string) error {
228+
func (g *LightningTerminal) startSubservers() error {
257229
var basicClient lnrpc.LightningClient
258230

231+
host, network, tlsPath, macPath, err := g.cfg.lndConnectParams()
232+
if err != nil {
233+
return err
234+
}
235+
236+
// Some of the subservers' configuration options won't have any effect
237+
// (like the log or lnd options) as they will be taken from lnd's config
238+
// struct. Others we want to force to be the same as lnd so the user
239+
// doesn't have to set them manually, like the network for example.
240+
g.cfg.Loop.Network = string(network)
241+
if err := loopd.Validate(g.cfg.Loop); err != nil {
242+
return err
243+
}
244+
259245
// The main RPC listener of lnd might need some time to start, it could
260246
// be that we run into a connection refused a few times. We use the
261247
// basic client connection to find out if the RPC server is started yet
262248
// because that doesn't do anything else than just connect. We'll check
263249
// if lnd is also ready to be used in the next step.
264-
err := wait.NoError(func() error {
250+
err = wait.NoError(func() error {
265251
// Create an lnd client now that we have the full configuration.
266252
// We'll need a basic client and a full client because not all
267253
// subservers have the same requirements.
268254
var err error
269255
basicClient, err = lndclient.NewBasicClient(
270-
g.lndAddr, g.cfg.Lnd.TLSCertPath,
271-
filepath.Dir(g.cfg.Lnd.AdminMacPath), network,
272-
lndclient.MacFilename(filepath.Base(
273-
g.cfg.Lnd.AdminMacPath,
274-
)),
256+
host, tlsPath, filepath.Dir(macPath), string(network),
257+
lndclient.MacFilename(filepath.Base(macPath)),
275258
)
276259
return err
277260
}, defaultStartupTimeout)
@@ -304,12 +287,10 @@ func (g *LightningTerminal) startSubservers(network string) error {
304287
}()
305288
g.lndClient, err = lndclient.NewLndServices(
306289
&lndclient.LndServicesConfig{
307-
LndAddress: g.lndAddr,
308-
Network: lndclient.Network(network),
309-
MacaroonDir: filepath.Dir(
310-
g.cfg.Lnd.AdminMacPath,
311-
),
312-
TLSPath: g.cfg.Lnd.TLSCertPath,
290+
LndAddress: host,
291+
Network: network,
292+
MacaroonDir: filepath.Dir(macPath),
293+
TLSPath: tlsPath,
313294
BlockUntilChainSynced: true,
314295
ChainSyncCtx: ctxc,
315296
},
@@ -429,7 +410,7 @@ func (g *LightningTerminal) startGrpcWebProxy() error {
429410
// admin macaroon and converts the browser's gRPC web calls into native
430411
// gRPC.
431412
lndGrpcServer, grpcServer, err := buildGrpcWebProxyServer(
432-
g.lndAddr, g.cfg.UIPassword, g.cfg.Lnd,
413+
g.cfg.Lnd.RPCListeners[0].String(), g.cfg.UIPassword, g.cfg.Lnd,
433414
)
434415
if err != nil {
435416
return fmt.Errorf("could not create gRPC web proxy: %v", err)

0 commit comments

Comments
 (0)