Skip to content

Commit 49cbe9a

Browse files
committed
loopd: add swap server TLS cert path
We need the ability to connect to a swap server that uses a self-signed certificate. The LSAT proxy cannot proxy insecure gRPC requests since they don't conform to the HTTP 1.1 standard. Therefore the LSAT proxy fill only serve TLS connections. This means, we need the TLS path option to specify the certificate the test environment LSAT proxy uses.
1 parent 8b8b878 commit 49cbe9a

File tree

6 files changed

+44
-22
lines changed

6 files changed

+44
-22
lines changed

client.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,17 @@ type Client struct {
7171

7272
// NewClient returns a new instance to initiate swaps with.
7373
func NewClient(dbDir string, serverAddress string, insecure bool,
74-
lnd *lndclient.LndServices) (*Client, func(), error) {
74+
tlsPathServer string, lnd *lndclient.LndServices) (*Client, func(),
75+
error) {
7576

7677
store, err := loopdb.NewBoltSwapStore(dbDir, lnd.ChainParams)
7778
if err != nil {
7879
return nil, nil, err
7980
}
8081

81-
swapServerClient, err := newSwapServerClient(serverAddress, insecure)
82+
swapServerClient, err := newSwapServerClient(
83+
serverAddress, insecure, tlsPathServer,
84+
)
8285
if err != nil {
8386
return nil, nil, err
8487
}

cmd/loopd/config.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ type lndConfig struct {
2727
type viewParameters struct{}
2828

2929
type config struct {
30-
ShowVersion bool `short:"V" long:"version" description:"Display version information and exit"`
31-
Insecure bool `long:"insecure" description:"disable tls"`
32-
Network string `long:"network" description:"network to run on" choice:"regtest" choice:"testnet" choice:"mainnet" choice:"simnet"`
33-
SwapServer string `long:"swapserver" description:"swap server address host:port"`
34-
RPCListen string `long:"rpclisten" description:"Address to listen on for gRPC clients"`
35-
RESTListen string `long:"restlisten" description:"Address to listen on for REST clients"`
30+
ShowVersion bool `short:"V" long:"version" description:"Display version information and exit"`
31+
Insecure bool `long:"insecure" description:"disable tls"`
32+
Network string `long:"network" description:"network to run on" choice:"regtest" choice:"testnet" choice:"mainnet" choice:"simnet"`
33+
SwapServer string `long:"swapserver" description:"swap server address host:port"`
34+
TLSPathSwapSrv string `long:"tlspathswapserver" description:"Path to swap server tls certificate. Only needed if the swap server uses a self-signed certificate."`
35+
RPCListen string `long:"rpclisten" description:"Address to listen on for gRPC clients"`
36+
RESTListen string `long:"restlisten" description:"Address to listen on for REST clients"`
3637

3738
LogDir string `long:"logdir" description:"Directory to log output."`
3839
MaxLogFiles int `long:"maxlogfiles" description:"Maximum logfiles to keep (0 for no rotation)"`

cmd/loopd/daemon.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func daemon(config *config) error {
4545
// Create an instance of the loop client library.
4646
swapClient, cleanup, err := getClient(
4747
config.Network, config.SwapServer, config.Insecure,
48-
&lnd.LndServices,
48+
config.TLSPathSwapSrv, &lnd.LndServices,
4949
)
5050
if err != nil {
5151
return err

cmd/loopd/utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func getLnd(network string, cfg *lndConfig) (*lndclient.GrpcLndServices, error)
1616
}
1717

1818
// getClient returns an instance of the swap client.
19-
func getClient(network, swapServer string, insecure bool,
19+
func getClient(network, swapServer string, insecure bool, tlsPathServer string,
2020
lnd *lndclient.LndServices) (*loop.Client, func(), error) {
2121

2222
storeDir, err := getStoreDir(network)
@@ -25,7 +25,7 @@ func getClient(network, swapServer string, insecure bool,
2525
}
2626

2727
swapClient, cleanUp, err := loop.NewClient(
28-
storeDir, swapServer, insecure, lnd,
28+
storeDir, swapServer, insecure, tlsPathServer, lnd,
2929
)
3030
if err != nil {
3131
return nil, nil, err

cmd/loopd/view.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ func view(config *config) error {
2424
defer lnd.Close()
2525

2626
swapClient, cleanup, err := getClient(
27-
config.Network, config.SwapServer, config.Insecure, &lnd.LndServices,
27+
config.Network, config.SwapServer, config.Insecure,
28+
config.TLSPathSwapSrv, &lnd.LndServices,
2829
)
2930
if err != nil {
3031
return err

swap_server_client.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ import (
88
"fmt"
99
"time"
1010

11-
"github.com/lightninglabs/loop/looprpc"
12-
"github.com/lightningnetwork/lnd/lntypes"
13-
1411
"github.com/btcsuite/btcd/btcec"
1512
"github.com/btcsuite/btcutil"
13+
"github.com/lightninglabs/loop/looprpc"
14+
"github.com/lightningnetwork/lnd/lntypes"
1615
"google.golang.org/grpc"
1716
"google.golang.org/grpc/credentials"
1817
)
@@ -49,10 +48,10 @@ type grpcSwapServerClient struct {
4948

5049
var _ swapServerClient = (*grpcSwapServerClient)(nil)
5150

52-
func newSwapServerClient(address string,
53-
insecure bool) (*grpcSwapServerClient, error) {
51+
func newSwapServerClient(address string, insecure bool, tlsPath string) (
52+
*grpcSwapServerClient, error) {
5453

55-
serverConn, err := getSwapServerConn(address, insecure)
54+
serverConn, err := getSwapServerConn(address, insecure, tlsPath)
5655
if err != nil {
5756
return nil, err
5857
}
@@ -227,19 +226,37 @@ func (s *grpcSwapServerClient) Close() {
227226
}
228227

229228
// getSwapServerConn returns a connection to the swap server.
230-
func getSwapServerConn(address string, insecure bool) (*grpc.ClientConn, error) {
229+
func getSwapServerConn(address string, insecure bool, tlsPath string) (
230+
*grpc.ClientConn, error) {
231+
231232
// Create a dial options array.
232233
opts := []grpc.DialOption{}
233-
if insecure {
234+
235+
// There are three options to connect to a swap server, either insecure,
236+
// using a self-signed certificate or with a certificate signed by a
237+
// public CA.
238+
switch {
239+
case insecure:
234240
opts = append(opts, grpc.WithInsecure())
235-
} else {
241+
242+
case tlsPath != "":
243+
// Load the specified TLS certificate and build
244+
// transport credentials
245+
creds, err := credentials.NewClientTLSFromFile(tlsPath, "")
246+
if err != nil {
247+
return nil, err
248+
}
249+
opts = append(opts, grpc.WithTransportCredentials(creds))
250+
251+
default:
236252
creds := credentials.NewTLS(&tls.Config{})
237253
opts = append(opts, grpc.WithTransportCredentials(creds))
238254
}
239255

240256
conn, err := grpc.Dial(address, opts...)
241257
if err != nil {
242-
return nil, fmt.Errorf("unable to connect to RPC server: %v", err)
258+
return nil, fmt.Errorf("unable to connect to RPC server: %v",
259+
err)
243260
}
244261

245262
return conn, nil

0 commit comments

Comments
 (0)