cmd/loop: add TLS params to CLI

pull/286/head
Oliver Gugger 4 years ago
parent a8d93bec6a
commit 39d1121c4b
No known key found for this signature in database
GPG Key ID: 8E4256593F177720

@ -6,15 +6,20 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath"
"strconv" "strconv"
"strings"
"time" "time"
"github.com/lightninglabs/lndclient"
"github.com/lightninglabs/loop" "github.com/lightninglabs/loop"
"github.com/lightninglabs/loop/loopd"
"github.com/lightninglabs/loop/looprpc" "github.com/lightninglabs/loop/looprpc"
"github.com/lightninglabs/loop/swap" "github.com/lightninglabs/loop/swap"
"github.com/lightninglabs/protobuf-hex-display/json" "github.com/lightninglabs/protobuf-hex-display/json"
"github.com/lightninglabs/protobuf-hex-display/jsonpb" "github.com/lightninglabs/protobuf-hex-display/jsonpb"
"github.com/lightninglabs/protobuf-hex-display/proto" "github.com/lightninglabs/protobuf-hex-display/proto"
"github.com/lightningnetwork/lnd/lncfg"
"github.com/lightningnetwork/lnd/macaroons" "github.com/lightningnetwork/lnd/macaroons"
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
@ -43,10 +48,22 @@ var (
// that we set when sending it over the line. // that we set when sending it over the line.
defaultMacaroonTimeout int64 = 60 defaultMacaroonTimeout int64 = 60
loopDirFlag = cli.StringFlag{
Name: "loopdir",
Value: loopd.LoopDirBase,
Usage: "path to loop's base directory",
}
networkFlag = cli.StringFlag{
Name: "network, n",
Usage: "the network loop is running on e.g. mainnet, " +
"testnet, etc.",
Value: loopd.DefaultNetwork,
}
tlsCertFlag = cli.StringFlag{ tlsCertFlag = cli.StringFlag{
Name: "tlscertpath", Name: "tlscertpath",
Usage: "path to loop's TLS certificate, only needed if loop " + Usage: "path to loop's TLS certificate",
"runs in the same process as lnd", Value: loopd.DefaultTLSCertPath,
} }
macaroonPathFlag = cli.StringFlag{ macaroonPathFlag = cli.StringFlag{
Name: "macaroonpath", Name: "macaroonpath",
@ -103,6 +120,8 @@ func main() {
Value: "localhost:11010", Value: "localhost:11010",
Usage: "loopd daemon address host:port", Usage: "loopd daemon address host:port",
}, },
networkFlag,
loopDirFlag,
tlsCertFlag, tlsCertFlag,
macaroonPathFlag, macaroonPathFlag,
} }
@ -121,8 +140,10 @@ func main() {
func getClient(ctx *cli.Context) (looprpc.SwapClientClient, func(), error) { func getClient(ctx *cli.Context) (looprpc.SwapClientClient, func(), error) {
rpcServer := ctx.GlobalString("rpcserver") rpcServer := ctx.GlobalString("rpcserver")
tlsCertPath := ctx.GlobalString(tlsCertFlag.Name) tlsCertPath, macaroonPath, err := extractPathArgs(ctx)
macaroonPath := ctx.GlobalString(macaroonPathFlag.Name) if err != nil {
return nil, nil, err
}
conn, err := getClientConn(rpcServer, tlsCertPath, macaroonPath) conn, err := getClientConn(rpcServer, tlsCertPath, macaroonPath)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
@ -137,6 +158,40 @@ func getMaxRoutingFee(amt btcutil.Amount) btcutil.Amount {
return swap.CalcFee(amt, maxRoutingFeeBase, maxRoutingFeeRate) return swap.CalcFee(amt, maxRoutingFeeBase, maxRoutingFeeRate)
} }
// extractPathArgs parses the TLS certificate and macaroon paths from the
// command.
func extractPathArgs(ctx *cli.Context) (string, string, error) {
// We'll start off by parsing the network. This is needed to determine
// the correct path to the TLS certificate and macaroon when not
// specified.
networkStr := strings.ToLower(ctx.GlobalString("network"))
_, err := lndclient.Network(networkStr).ChainParams()
if err != nil {
return "", "", err
}
// We'll now fetch the loopdir so we can make a decision on how to
// properly read the cert. This will either be the default, or will have
// been overwritten by the end user.
loopDir := lncfg.CleanAndExpandPath(ctx.GlobalString(loopDirFlag.Name))
tlsCertPath := lncfg.CleanAndExpandPath(ctx.GlobalString(
tlsCertFlag.Name,
))
// If a custom lnd directory was set, we'll also check if custom paths
// for the TLS cert file were set as well. If not, we'll override their
// paths so they can be found within the custom loop directory set. This
// allows us to set a custom lnd directory, along with custom paths to
// the TLS cert file.
if loopDir != loopd.LoopDirBase || networkStr != loopd.DefaultNetwork {
tlsCertPath = filepath.Join(
loopDir, networkStr, loopd.DefaultTLSCertFilename,
)
}
return tlsCertPath, ctx.GlobalString(macaroonPathFlag.Name), nil
}
type inLimits struct { type inLimits struct {
maxMinerFee btcutil.Amount maxMinerFee btcutil.Amount
maxSwapFee btcutil.Amount maxSwapFee btcutil.Amount
@ -322,32 +377,23 @@ func getClientConn(address, tlsCertPath, macaroonPath string) (*grpc.ClientConn,
grpc.WithDefaultCallOptions(maxMsgRecvSize), grpc.WithDefaultCallOptions(maxMsgRecvSize),
} }
switch { // TLS cannot be disabled, we'll always have a cert file to read.
// If a TLS certificate file is specified, we need to load it and build creds, err := credentials.NewClientTLSFromFile(tlsCertPath, "")
// transport credentials with it. if err != nil {
case tlsCertPath != "": fatal(err)
creds, err := credentials.NewClientTLSFromFile(tlsCertPath, "") }
if err != nil {
fatal(err)
}
// Macaroons are only allowed to be transmitted over a TLS
// enabled connection.
if macaroonPath != "" {
opts = append(opts, readMacaroon(macaroonPath))
}
opts = append(opts, grpc.WithTransportCredentials(creds))
// By default, if no certificate is supplied, we assume the RPC server // Macaroons are not yet enabled by default.
// runs without TLS. if macaroonPath != "" {
default: opts = append(opts, readMacaroon(macaroonPath))
opts = append(opts, grpc.WithInsecure())
} }
opts = append(opts, grpc.WithTransportCredentials(creds))
conn, err := grpc.Dial(address, opts...) conn, err := grpc.Dial(address, opts...)
if err != nil { if err != nil {
return nil, fmt.Errorf("unable to connect to RPC server: %v", err) return nil, fmt.Errorf("unable to connect to RPC server: %v",
err)
} }
return conn, nil return conn, nil

Loading…
Cancel
Save