-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
160 lines (144 loc) · 4.3 KB
/
main.go
File metadata and controls
160 lines (144 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"context"
"fmt"
"os"
"os/user"
"path/filepath"
"strings"
"github.com/lightninglabs/lndclient"
"github.com/lightningnetwork/lnd/lnrpc/verrpc"
"github.com/urfave/cli/v2"
)
const (
defaultLndDir = "~/.lnd"
defaultDataDir = "data"
defaultChainSubDir = "chain"
defaultTLSCertFilename = "tls.cert"
defaultMacaroonFilename = "admin.macaroon"
defaultRPCPort = "10009"
defaultRPCHostPort = "localhost:" + defaultRPCPort
)
var (
// minRequiredLndVersion is the minimum required version of lnd that
// is compatible with the current version of the lnb.
minRequiredLndVersion = &verrpc.Version{
AppMajor: 0,
AppMinor: 17,
AppPatch: 0,
}
)
// cleanAndExpandPath expands environment variables and leading ~ in the
// passed path, cleans the result, and returns it.
// This function is taken from https://github.com/btcsuite/btcd
func cleanAndExpandPath(path string) string {
if path == "" {
return ""
}
// Expand initial ~ to OS specific home directory.
if strings.HasPrefix(path, "~") {
var homeDir string
user, err := user.Current()
if err == nil {
homeDir = user.HomeDir
} else {
homeDir = os.Getenv("HOME")
}
path = strings.Replace(path, "~", homeDir, 1)
}
// NOTE: The os.ExpandEnv doesn't work with Windows-style %VARIABLE%,
// but the variables can still be expanded via POSIX-style $VARIABLE.
return filepath.Clean(os.ExpandEnv(path))
}
func getClient(callerCtx context.Context, ctx *cli.Context) (*lndclient.GrpcLndServices, error) {
// We'll now fetch the lnddir so we can make a decision on how to
// properly read the macaroons (if needed) and also the cert. This will
// either be the default, or will have been overwritten by the end
// user.
lndDir := cleanAndExpandPath(ctx.String("lnddir"))
network := strings.ToLower(ctx.String("network"))
// If the macaroon path as been manually provided, then we'll only
// target the specified file.
var macPath string
if ctx.String("macaroonpath") != "" {
macPath = cleanAndExpandPath(ctx.String("macaroonpath"))
} else {
// Otherwise, we'll go into the path:
// lnddir/data/chain/<chain>/<network> in order to fetch the
// macaroon that we need.
macPath = filepath.Join(
lndDir, defaultDataDir, defaultChainSubDir, "bitcoin",
network, defaultMacaroonFilename,
)
}
tlsCertPath := cleanAndExpandPath(ctx.String("tlscertpath"))
// If a custom lnd directory was set, we'll also check if custom paths
// for the TLS cert and macaroon file were set as well. If not, we'll
// override their paths so they can be found within the custom lnd
// directory set. This allows us to set a custom lnd directory, along
// with custom paths to the TLS cert and macaroon file.
if lndDir != cleanAndExpandPath(defaultLndDir) {
tlsCertPath = filepath.Join(lndDir, defaultTLSCertFilename)
}
return lndclient.NewLndServices(&lndclient.LndServicesConfig{
LndAddress: ctx.String("rpcserver"),
Network: lndclient.Network(ctx.String("network")),
CustomMacaroonPath: macPath,
TLSPath: tlsCertPath,
CheckVersion: minRequiredLndVersion,
CallerCtx: callerCtx,
})
}
func main() {
app := cli.NewApp()
app.EnableBashCompletion = true
app.Name = "lnb"
app.Usage = "lighting channel balancer for lnd"
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "rpcserver",
Value: defaultRPCHostPort,
Usage: "host:port of ln daemon",
},
&cli.StringFlag{
Name: "lnddir",
Value: defaultLndDir,
Usage: "path to lnd's base directory",
},
&cli.StringFlag{
Name: "tlscertpath",
Usage: "path to lnd's tls.cert",
},
&cli.StringFlag{
Name: "network, n",
Usage: "the network lnd is running on e.g. mainnet, " +
"testnet, etc.",
Value: "mainnet",
},
&cli.BoolFlag{
Name: "no-macaroons",
Usage: "disable macaroon authentication",
},
&cli.StringFlag{
Name: "macaroonpath",
Usage: "path to macaroon file",
},
&cli.Int64Flag{
Name: "macaroontimeout",
Value: 60,
Usage: "anti-replay macaroon validity time in seconds",
},
&cli.StringFlag{
Name: "macaroonip",
Usage: "if set, lock macaroon to specific IP address",
},
}
app.Commands = []*cli.Command{
&getCommand,
&listCommand,
}
if err := app.Run(os.Args); err != nil {
fmt.Fprintf(os.Stderr, "[lnb] %v\n", err)
os.Exit(1)
}
}