-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
307 lines (261 loc) · 10.8 KB
/
config.go
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package main
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"log"
"github.com/btcsuite/btcutil"
"github.com/jessevdk/go-flags"
)
const (
defaultHubPort = "8686"
defaultHubHost = "localhost"
defaultGraphQLHost = "0.0.0.0"
defaultGraphQLPort = "3000"
defaultGraphQLSecurePort = "3443"
defaultLogDirname = "logs"
defaultLogLevel = "info"
defaultConfigFilename = "hub.conf"
defaultLogFilename = "hub.log"
defaultPrometheusHost = "0.0.0.0"
defaultPrometheusPort = "19999"
defaultUpdatesLogFileName = "log.protobuf"
defaultDbPath = "/tmp"
defaultNet = "simnet"
)
type graphqlConfig struct {
ListenHost string `long:"listenhost" description:"The host on which GraphQL will listen for incoming requests"`
ListenPort string `long:"listenport" description:"The port on which GraphQL will listen for incoming requests"`
SecureListenPort string `long:"securelistenport" description:"The secure port on which GraphQL will listen for incoming requests"`
}
var (
homeDir = btcutil.AppDataDir("hubmanager", false)
defaultConfigFile = filepath.Join(homeDir, defaultConfigFilename)
defaultLogDir = filepath.Join(homeDir, defaultLogDirname)
)
// config defines the configuration options for lnd.
//
// See loadConfig for further details regarding the configuration
// loading+parsing process.
type config struct {
ShowVersion bool `long:"version" description:"Display version information and exit"`
UpdateLogFile string `long:"updateslog" description:"Path to log file in which manager will direct lightning node network updates output"`
LND *lndClientConfig `group:"Lnd" namespace:"lnd"`
Bitcoind *bitcoindConfig `group:"Bitcoind" namespace:"bitcoind"`
Prometheus *prometheusConfig `group:"Prometheus" namespace:"prometheus"`
Hub *hubConfig `group:"Hub" namespace:"hub"`
GraphQL *graphqlConfig `group:"GraphQL" namespace:"graphql"`
ConfigFile string `long:"config" description:"Path to configuration file"`
LogDir string `long:"logdir" description:"Directory to log output."`
DebugLevel string `long:"debuglevel" description:"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 -- Use show to list available subsystems"`
}
// hubConfig defines the parameters for gRPC endpoint of hub management,
// with this third-party optimisation programs could send lightning
// node channels equilibrium state.
type hubConfig struct {
Port string `long:"port" description:"Port on which GRPC hub manager is working"`
Host string `long:"host" description:"Host on which GRPC hub manager is working"`
}
type lndClientConfig struct {
Network string `long:"network" description:"Blockchain network which should be used" choice:"simnet" choice:"testnet" choice:"mainnet"`
DataDir string `long:"dbpath" description:"Path to dir where BoltDB will be stored"`
TlsCertPath string `long:"tlscertpath" description:"Path to the LND certificate"`
MacaroonPath string `long:"macaroonpath" description:"Path to the LND macaroon"`
GRPCHost string `long:"grpchost" description:"Host on which we expect to find LND gRPC endpoint"`
GRPCPort string `long:"grpcport" description:"Port on which we expect to find LND gRPC endpoint"`
NeutrinoHost string `long:"neutrinohost" description:"Public host where neutrino Bitcoin node resides. Needed only to inform users over API"`
NeutrinoPort string `long:"neutrinoport" description:"Public port where neutrino Bitcoin node resides. Needed only to inform users over API"`
PeerHost string `long:"peerhost" description:"Public host where LND node resides. Needed only to inform users over API"`
PeerPort string `long:"peerport" description:"Public port where LND node resides. Needed only to inform users over API"`
KnownPeers map[string]string `long:"knownpeer" description:"A map from peer alias to its public key"`
}
type bitcoindConfig struct {
User string `long:"user" description:"User for accessing rpc endpoint"`
Pass string `long:"pass" description:"Password for accessing rpc endpoint"`
Host string `long:"host" description:"Host on which we expect to bitcoind"`
Port string `long:"port" description:"Port on which we expect to bitcoind"`
}
type prometheusConfig struct {
ListenHost string `long:"listenhost" description:"The host of the prometheus metrics endpoint, from which metric server is trying to fetch metrics"`
ListenPort string `long:"listenport" description:"The port of the prometheus metrics endpoint, from which metric server is trying to fetch metrics"`
}
// getDefaultConfig return default version of service config.
func getDefaultConfig() config {
return config{
ConfigFile: defaultConfigFile,
LogDir: defaultLogDir,
DebugLevel: defaultLogLevel,
UpdateLogFile: defaultUpdatesLogFileName,
Hub: &hubConfig{
Port: defaultHubPort,
Host: defaultHubHost,
},
Prometheus: &prometheusConfig{
ListenHost: defaultPrometheusHost,
ListenPort: defaultPrometheusPort,
},
LND: &lndClientConfig{
Network: defaultNet,
DataDir: defaultDbPath,
},
GraphQL: &graphqlConfig{
ListenHost: defaultGraphQLHost,
ListenPort: defaultGraphQLPort,
SecureListenPort: defaultGraphQLSecurePort,
},
}
}
// loadConfig initializes and parses the config using a config file and command
// line options.
//
// The configuration proceeds as follows:
// 1) Start with a default config with sane settings
// 2) Pre-parse the command line to check for an alternative config file
// 3) Load configuration file overwriting defaults with any specified options
// 4) Parse CLI options and overwrite/add any specified options
func (c *config) loadConfig() error {
// Pre-parse the command line options to pick up an alternative config
// file.
preCfg := c
if _, err := flags.Parse(preCfg); err != nil {
return err
}
// Show the version and exit if the version flag was specified.
appName := filepath.Base(os.Args[0])
appName = strings.TrimSuffix(appName, filepath.Ext(appName))
usageMessage := fmt.Sprintf("Use %s -h to show usage", appName)
if preCfg.ShowVersion {
fmt.Println(appName, "version", version())
os.Exit(0)
}
// Create the home directory if it doesn't already exist.
funcName := "loadConfig"
if err := os.MkdirAll(homeDir, 0700); err != nil {
// Show a nicer error message if it's because a symlink is
// linked to a directory that does not exist (probably because
// it's not mounted).
if e, ok := err.(*os.PathError); ok && os.IsExist(err) {
if link, lerr := os.Readlink(e.Path); lerr == nil {
str := "is symlink %s -> %s mounted?"
err = fmt.Errorf(str, e.Path, link)
}
}
str := "%s: Failed to create home directory: %v"
err := fmt.Errorf(str, funcName, err)
fmt.Fprintln(os.Stderr, err)
return err
}
// Next, load any additional configuration options from the file.
var configFileError error
if err := flags.IniParse(preCfg.ConfigFile, c); err != nil {
configFileError = err
}
// Finally, parse the remaining command line options again to ensure
// they take precedence.
if _, err := flags.Parse(c); err != nil {
return err
}
// Ensure that the paths are expanded and cleaned.
c.LogDir = cleanAndExpandPath(c.LogDir)
// Parse, validate, and set debug log level(s).
if err := parseAndSetDebugLevels(c.DebugLevel); err != nil {
err := fmt.Errorf("%s: %v", funcName, err.Error())
fmt.Fprintln(os.Stderr, err)
fmt.Fprintln(os.Stderr, usageMessage)
return err
}
// Warn about missing config file only after all other configuration is
// done. This prevents the warning on help messages and invalid
// options. Note this should go directly before the return.
if configFileError != nil {
log.Printf("%v", configFileError)
}
return nil
}
// 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 {
// Expand initial ~ to OS specific home directory.
if strings.HasPrefix(path, "~") {
homeDir := filepath.Dir(homeDir)
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))
}
// parseAndSetDebugLevels attempts to parse the specified debug level and set
// the levels accordingly. An appropriate error is returned if anything is
// invalid.
func parseAndSetDebugLevels(debugLevel string) error {
// When the specified string doesn't have any delimters, treat it as
// the log level for all subsystems.
if !strings.Contains(debugLevel, ",") && !strings.Contains(debugLevel, "=") {
// Validate debug log level.
if !validLogLevel(debugLevel) {
str := "The specified debug level [%v] is invalid"
return fmt.Errorf(str, debugLevel)
}
// Change the logging level for all subsystems.
setLogLevels(debugLevel)
return nil
}
// Split the specified string into subsystem/level pairs while detecting
// issues and update the log levels accordingly.
for _, logLevelPair := range strings.Split(debugLevel, ",") {
if !strings.Contains(logLevelPair, "=") {
str := "The specified debug level contains an invalid " +
"subsystem/level pair [%v]"
return fmt.Errorf(str, logLevelPair)
}
// Extract the specified subsystem and log level.
fields := strings.Split(logLevelPair, "=")
subsysID, logLevel := fields[0], fields[1]
// Validate subsystem.
if _, exists := subsystemLoggers[subsysID]; !exists {
str := "The specified subsystem [%v] is invalid -- " +
"supported subsytems %v"
return fmt.Errorf(str, subsysID, supportedSubsystems())
}
// Validate log level.
if !validLogLevel(logLevel) {
str := "The specified debug level [%v] is invalid"
return fmt.Errorf(str, logLevel)
}
setLogLevel(subsysID, logLevel)
}
return nil
}
// validLogLevel returns whether or not logLevel is a valid debug log level.
func validLogLevel(logLevel string) bool {
switch logLevel {
case "trace":
fallthrough
case "debug":
fallthrough
case "info":
fallthrough
case "warn":
fallthrough
case "error":
fallthrough
case "critical":
return true
}
return false
}
// supportedSubsystems returns a sorted slice of the supported subsystems for
// logging purposes.
func supportedSubsystems() []string {
// Convert the subsystemLoggers map keys to a slice.
subsystems := make([]string, 0, len(subsystemLoggers))
for subsysID := range subsystemLoggers {
subsystems = append(subsystems, subsysID)
}
// Sort the subsystems for stable display.
sort.Strings(subsystems)
return subsystems
}