Skip to content

Commit f316bfe

Browse files
authored
Merge pull request #516 from positiveblue/subservers
Add new `subservers` package
2 parents 73c36ac + f4ca465 commit f316bfe

17 files changed

+1552
-772
lines changed

config.go

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/lightninglabs/lightning-terminal/autopilotserver"
2121
"github.com/lightninglabs/lightning-terminal/firewall"
2222
mid "github.com/lightninglabs/lightning-terminal/rpcmiddleware"
23+
"github.com/lightninglabs/lightning-terminal/subservers"
2324
"github.com/lightninglabs/lndclient"
2425
"github.com/lightninglabs/loop/loopd"
2526
"github.com/lightninglabs/pool"
@@ -169,7 +170,7 @@ type Config struct {
169170
// That way only one global network flag is needed.
170171
Network string `long:"network" description:"The network the UI and all its components run on" choice:"regtest" choice:"testnet" choice:"mainnet" choice:"simnet"`
171172

172-
Remote *RemoteConfig `group:"Remote mode options (use when lnd-mode=remote)" namespace:"remote"`
173+
Remote *subservers.RemoteConfig `group:"Remote mode options (use when lnd-mode=remote)" namespace:"remote"`
173174

174175
// LndMode is the selected mode to run lnd in. The supported modes are
175176
// 'integrated' and 'remote'. We only use a string instead of a bool
@@ -212,40 +213,6 @@ type Config struct {
212213
lndAdminMacaroon []byte
213214
}
214215

215-
// RemoteConfig holds the configuration parameters that are needed when running
216-
// LiT in the "remote" lnd mode.
217-
type RemoteConfig struct {
218-
LitLogDir string `long:"lit-logdir" description:"For lnd remote mode only: Directory to log output."`
219-
LitMaxLogFiles int `long:"lit-maxlogfiles" description:"For lnd remote mode only: Maximum logfiles to keep (0 for no rotation)"`
220-
LitMaxLogFileSize int `long:"lit-maxlogfilesize" description:"For lnd remote mode only: Maximum logfile size in MB"`
221-
222-
LitDebugLevel string `long:"lit-debuglevel" description:"For lnd remote mode only: Logging level for all subsystems {trace, debug, info, warn, error, critical} -- You may also specify <subsystem>=<level>,<subsystem2>=<level>,... to set the log level for individual subsystems."`
223-
224-
Lnd *RemoteDaemonConfig `group:"Remote lnd (use when lnd-mode=remote)" namespace:"lnd"`
225-
Faraday *RemoteDaemonConfig `group:"Remote faraday (use when faraday-mode=remote)" namespace:"faraday"`
226-
Loop *RemoteDaemonConfig `group:"Remote loop (use when loop-mode=remote)" namespace:"loop"`
227-
Pool *RemoteDaemonConfig `group:"Remote pool (use when pool-mode=remote)" namespace:"pool"`
228-
}
229-
230-
// RemoteDaemonConfig holds the configuration parameters that are needed to
231-
// connect to a remote daemon like lnd for example.
232-
type RemoteDaemonConfig struct {
233-
// RPCServer is host:port that the remote daemon's RPC server is
234-
// listening on.
235-
RPCServer string `long:"rpcserver" description:"The host:port that the remote daemon is listening for RPC connections on."`
236-
237-
// MacaroonPath is the path to the single macaroon that should be used
238-
// instead of needing to specify the macaroon directory that contains
239-
// all of the daemon's macaroons. The specified macaroon MUST have all
240-
// permissions that all the subservers use, otherwise permission errors
241-
// will occur.
242-
MacaroonPath string `long:"macaroonpath" description:"The full path to the single macaroon to use, either the main (admin.macaroon in lnd's case) or a custom baked one. A custom macaroon must contain ALL permissions required for all subservers to work, otherwise permission errors will occur."`
243-
244-
// TLSCertPath is the path to the tls cert of the remote daemon that
245-
// should be used to verify the TLS identity of the remote RPC server.
246-
TLSCertPath string `long:"tlscertpath" description:"The full path to the remote daemon's TLS cert to use for RPC connection verification."`
247-
}
248-
249216
// lndConnectParams returns the connection parameters to connect to the local
250217
// lnd instance.
251218
func (c *Config) lndConnectParams() (string, lndclient.Network, string,
@@ -289,27 +256,27 @@ func defaultConfig() *Config {
289256
HTTPSListen: defaultHTTPSListen,
290257
TLSCertPath: DefaultTLSCertPath,
291258
TLSKeyPath: defaultTLSKeyPath,
292-
Remote: &RemoteConfig{
259+
Remote: &subservers.RemoteConfig{
293260
LitDebugLevel: defaultLogLevel,
294261
LitLogDir: defaultLogDir,
295262
LitMaxLogFiles: defaultMaxLogFiles,
296263
LitMaxLogFileSize: defaultMaxLogFileSize,
297-
Lnd: &RemoteDaemonConfig{
264+
Lnd: &subservers.RemoteDaemonConfig{
298265
RPCServer: defaultRemoteLndRpcServer,
299266
MacaroonPath: DefaultRemoteLndMacaroonPath,
300267
TLSCertPath: lndDefaultConfig.TLSCertPath,
301268
},
302-
Faraday: &RemoteDaemonConfig{
269+
Faraday: &subservers.RemoteDaemonConfig{
303270
RPCServer: defaultRemoteFaradayRpcServer,
304271
MacaroonPath: faradayDefaultConfig.MacaroonPath,
305272
TLSCertPath: faradayDefaultConfig.TLSCertPath,
306273
},
307-
Loop: &RemoteDaemonConfig{
274+
Loop: &subservers.RemoteDaemonConfig{
308275
RPCServer: defaultRemoteLoopRpcServer,
309276
MacaroonPath: loopDefaultConfig.MacaroonPath,
310277
TLSCertPath: loopDefaultConfig.TLSCertPath,
311278
},
312-
Pool: &RemoteDaemonConfig{
279+
Pool: &subservers.RemoteDaemonConfig{
313280
RPCServer: defaultRemotePoolRpcServer,
314281
MacaroonPath: poolDefaultConfig.MacaroonPath,
315282
TLSCertPath: poolDefaultConfig.TLSCertPath,

lnd_connection.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package terminal
2+
3+
import (
4+
"context"
5+
"crypto/tls"
6+
"fmt"
7+
"net"
8+
9+
grpcProxy "github.com/mwitkow/grpc-proxy/proxy"
10+
"google.golang.org/grpc"
11+
"google.golang.org/grpc/backoff"
12+
"google.golang.org/grpc/credentials"
13+
"google.golang.org/grpc/test/bufconn"
14+
)
15+
16+
// connectLND sets up LiT's LND connection.
17+
func connectLND(cfg *Config, bufListener *bufconn.Listener) (*grpc.ClientConn,
18+
error) {
19+
20+
if cfg.lndRemote {
21+
host, _, tlsPath, _, _ := cfg.lndConnectParams()
22+
return dialBackend("lnd", host, tlsPath)
23+
}
24+
25+
// If LND is running in integrated mode, then we use a bufconn to
26+
// connect to lnd in integrated mode.
27+
return dialBufConnBackend(bufListener)
28+
}
29+
30+
// dialBackend connects to a gRPC backend through the given address and uses the
31+
// given TLS certificate to authenticate the connection.
32+
func dialBackend(name, dialAddr, tlsCertPath string) (*grpc.ClientConn, error) {
33+
tlsConfig, err := credentials.NewClientTLSFromFile(tlsCertPath, "")
34+
if err != nil {
35+
return nil, fmt.Errorf("could not read %s TLS cert %s: %v",
36+
name, tlsCertPath, err)
37+
}
38+
39+
opts := []grpc.DialOption{
40+
// From the grpcProxy doc: This codec is *crucial* to the
41+
// functioning of the proxy.
42+
grpc.WithCodec(grpcProxy.Codec()), // nolint
43+
grpc.WithTransportCredentials(tlsConfig),
44+
grpc.WithDefaultCallOptions(maxMsgRecvSize),
45+
grpc.WithConnectParams(grpc.ConnectParams{
46+
Backoff: backoff.DefaultConfig,
47+
MinConnectTimeout: defaultConnectTimeout,
48+
}),
49+
}
50+
51+
log.Infof("Dialing %s gRPC server at %s", name, dialAddr)
52+
cc, err := grpc.Dial(dialAddr, opts...)
53+
if err != nil {
54+
return nil, fmt.Errorf("failed dialing %s backend: %v", name,
55+
err)
56+
}
57+
return cc, nil
58+
}
59+
60+
// dialBufConnBackend dials an in-memory connection to an RPC listener and
61+
// ignores any TLS certificate mismatches.
62+
func dialBufConnBackend(listener *bufconn.Listener) (*grpc.ClientConn, error) {
63+
tlsConfig := credentials.NewTLS(&tls.Config{
64+
InsecureSkipVerify: true,
65+
})
66+
67+
opts := []grpc.DialOption{
68+
grpc.WithContextDialer(
69+
func(context.Context, string) (net.Conn, error) {
70+
return listener.Dial()
71+
},
72+
),
73+
// From the grpcProxy doc: This codec is *crucial* to the
74+
// functioning of the proxy.
75+
grpc.WithCodec(grpcProxy.Codec()), // nolint
76+
grpc.WithTransportCredentials(tlsConfig),
77+
grpc.WithDefaultCallOptions(maxMsgRecvSize),
78+
grpc.WithConnectParams(grpc.ConnectParams{
79+
Backoff: backoff.DefaultConfig,
80+
MinConnectTimeout: defaultConnectTimeout,
81+
}),
82+
}
83+
84+
return grpc.Dial("", opts...)
85+
}

log.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
mid "github.com/lightninglabs/lightning-terminal/rpcmiddleware"
1111
"github.com/lightninglabs/lightning-terminal/rules"
1212
"github.com/lightninglabs/lightning-terminal/session"
13+
"github.com/lightninglabs/lightning-terminal/subservers"
1314
"github.com/lightninglabs/loop/loopd"
1415
"github.com/lightninglabs/pool"
1516
"github.com/lightningnetwork/lnd"
@@ -77,6 +78,10 @@ func SetupLoggers(root *build.RotatingLogWriter, intercept signal.Interceptor) {
7778
autopilotserver.UseLogger,
7879
)
7980

81+
lnd.AddSubLogger(
82+
root, subservers.Subsystem, intercept, subservers.UseLogger,
83+
)
84+
8085
// Add daemon loggers to lnd's root logger.
8186
faraday.SetupLoggers(root, intercept)
8287
loopd.SetupLoggers(root, intercept)

0 commit comments

Comments
 (0)