forked from decred/tumblebit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
484 lines (431 loc) · 16.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
// Copyright (c) 2013-2016 The btcsuite developers
// Copyright (c) 2015-2017 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"context"
"fmt"
"net"
"os"
"os/user"
"path/filepath"
"runtime"
"sort"
"strings"
"github.com/btcsuite/btclog"
"github.com/decred/dcrd/dcrutil"
"github.com/decred/tumblebit/internal/cfgutil"
"github.com/decred/tumblebit/netparams"
"github.com/decred/tumblebit/tumbler"
"github.com/decred/tumblebit/version"
flags "github.com/jessevdk/go-flags"
)
const (
defaultCAFilename = "dcrwallet.cert"
defaultConfigFilename = "tumblebit.conf"
defaultLogLevel = "info"
defaultLogDirname = "logs"
defaultLogFilename = "tumblebit.log"
)
var (
walletDefaultCAFile = filepath.Join(dcrutil.AppDataDir("dcrwallet", false), "rpc.cert")
defaultAppDataDir = dcrutil.AppDataDir("tumblebit", false)
defaultConfigFile = filepath.Join(defaultAppDataDir, defaultConfigFilename)
defaultRPCKeyFile = filepath.Join(defaultAppDataDir, "rpc.key")
defaultRPCCertFile = filepath.Join(defaultAppDataDir, "rpc.cert")
defaultLogDir = filepath.Join(defaultAppDataDir, defaultLogDirname)
)
type config struct {
// General application behavior
ConfigFile *cfgutil.ExplicitString `short:"C" long:"configfile" description:"Path to configuration file"`
ShowVersion bool `short:"V" long:"version" description:"Display version information and exit"`
AppDataDir *cfgutil.ExplicitString `short:"A" long:"appdata" description:"Application data directory for tumblebit config, databases and logs"`
TestNet bool `long:"testnet" description:"Use the test network"`
SimNet bool `long:"simnet" description:"Use the simulation test network"`
DebugLevel string `short:"d" long:"debuglevel" description:"Logging level {trace, debug, info, warn, error, critical}"`
LogDir *cfgutil.ExplicitString `long:"logdir" description:"Directory to log output."`
MemProfile string `long:"memprofile" description:"Write mem profile to the specified file"`
// RPC client options
RPCConnect string `short:"c" long:"rpcconnect" description:"Hostname/IP and port of dcrwallet RPC server to connect to"`
CAFile *cfgutil.ExplicitString `long:"cafile" description:"File containing root certificates to authenticate a TLS connections with dcrwallet"`
DisableClientTLS bool `long:"noclienttls" description:"Disable TLS for the RPC client -- NOTE: This is only allowed if the RPC client is connecting to localhost"`
WalletPassword string `long:"walletpassword" default-mask:"-" description:"The private passphrase to unlock the wallet"`
Account uint32 `long:"account" description:"BIP0044 account number to use for transactions"`
AccountName string `long:"accountname" description:"Name of the account to use for transactions -- NOTE: This takes precedence over the numeric specification"`
// RPC server options
RPCCert *cfgutil.ExplicitString `long:"rpccert" description:"File containing the certificate file"`
RPCKey *cfgutil.ExplicitString `long:"rpckey" description:"File containing the certificate key"`
TLSCurve *cfgutil.CurveFlag `long:"tlscurve" description:"Curve to use when generating TLS keypairs"`
OneTimeTLSKey bool `long:"onetimetlskey" description:"Generate a new TLS certpair at startup, but only write the certificate to disk"`
DisableServerTLS bool `long:"noservertls" description:"Disable TLS for the RPC servers -- NOTE: This is only allowed if the RPC server is bound to localhost"`
GRPCListeners []string `long:"grpclisten" description:"Listen for gRPC connections on this interface/port"`
// TumbleBit specific options
EpochDuration int32 `long:"epochduration" description:"Duration of a single epoch and a TumbleBit escrow"`
EpochRenewal int32 `long:"epochrenewal" description:"Interval between two consecutive epochs"`
PuzzleDifficulty int `long:"puzzledifficulty" description:"TumbleBit puzzle difficulty"`
}
// cleanAndExpandPath expands environement variables and leading ~ in the
// passed path, cleans the result, and returns it.
func cleanAndExpandPath(path string) string {
// NOTE: The os.ExpandEnv doesn't work with Windows cmd.exe-style
// %VARIABLE%, but they variables can still be expanded via POSIX-style
// $VARIABLE.
path = os.ExpandEnv(path)
if !strings.HasPrefix(path, "~") {
return filepath.Clean(path)
}
// Expand initial ~ to the current user's home directory, or ~otheruser
// to otheruser's home directory. On Windows, both forward and backward
// slashes can be used.
path = path[1:]
var pathSeparators string
if runtime.GOOS == "windows" {
pathSeparators = string(os.PathSeparator) + "/"
} else {
pathSeparators = string(os.PathSeparator)
}
userName := ""
if i := strings.IndexAny(path, pathSeparators); i != -1 {
userName = path[:i]
path = path[i:]
}
homeDir := ""
var u *user.User
var err error
if userName == "" {
u, err = user.Current()
} else {
u, err = user.Lookup(userName)
}
if err == nil {
homeDir = u.HomeDir
}
// Fallback to CWD if user lookup fails or user has no home directory.
if homeDir == "" {
homeDir = "."
}
return filepath.Join(homeDir, path)
}
// validLogLevel returns whether or not logLevel is a valid debug log level.
func validLogLevel(logLevel string) bool {
_, ok := btclog.LevelFromString(logLevel)
return ok
}
// 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 subsytems for stable display.
sort.Strings(subsystems)
return subsystems
}
// 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
}
// 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
//
// The above results in tumblebit functioning properly without any config
// settings while still allowing the user to override settings with config files
// and command line options. Command line options always take precedence.
func loadConfig(ctx context.Context) (*config, []string, error) {
loadConfigError := func(err error) (*config, []string, error) {
return nil, nil, err
}
// Default config.
cfg := config{
DebugLevel: defaultLogLevel,
ConfigFile: cfgutil.NewExplicitString(defaultConfigFile),
AppDataDir: cfgutil.NewExplicitString(defaultAppDataDir),
LogDir: cfgutil.NewExplicitString(defaultLogDir),
CAFile: cfgutil.NewExplicitString(""),
RPCKey: cfgutil.NewExplicitString(defaultRPCKeyFile),
RPCCert: cfgutil.NewExplicitString(defaultRPCCertFile),
TLSCurve: cfgutil.NewCurveFlag(cfgutil.CurveP521),
}
// Pre-parse the command line options to see if an alternative config
// file or the version flag was specified.
preCfg := cfg
preParser := flags.NewParser(&preCfg, flags.Default)
_, err := preParser.Parse()
if err != nil {
e, ok := err.(*flags.Error)
if ok && e.Type == flags.ErrHelp {
os.Exit(0)
}
preParser.WriteHelp(os.Stderr)
return loadConfigError(err)
}
// Show the version and exit if the version flag was specified.
funcName := "loadConfig"
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.Printf("%s version %s (Go version %s)\n", appName, version.String(), runtime.Version())
os.Exit(0)
}
// Load additional config from file.
var configFileError error
parser := flags.NewParser(&cfg, flags.Default)
configFilePath := preCfg.ConfigFile.Value
if preCfg.ConfigFile.ExplicitlySet() {
configFilePath = cleanAndExpandPath(configFilePath)
} else {
appDataDir := preCfg.AppDataDir.Value
if appDataDir != defaultAppDataDir {
configFilePath = filepath.Join(appDataDir, defaultConfigFilename)
}
}
err = flags.NewIniParser(parser).ParseFile(configFilePath)
if err != nil {
if _, ok := err.(*os.PathError); !ok {
fmt.Fprintln(os.Stderr, err)
parser.WriteHelp(os.Stderr)
return loadConfigError(err)
}
configFileError = err
}
// Parse command line options again to ensure they take precedence.
remainingArgs, err := parser.Parse()
if err != nil {
if e, ok := err.(*flags.Error); !ok || e.Type != flags.ErrHelp {
parser.WriteHelp(os.Stderr)
}
return loadConfigError(err)
}
// If an alternate data directory was specified, and paths with defaults
// relative to the data dir are unchanged, modify each path to be
// relative to the new data dir.
if cfg.AppDataDir.ExplicitlySet() {
cfg.AppDataDir.Value = cleanAndExpandPath(cfg.AppDataDir.Value)
if !cfg.RPCKey.ExplicitlySet() {
cfg.RPCKey.Value = filepath.Join(cfg.AppDataDir.Value, "rpc.key")
}
if !cfg.RPCCert.ExplicitlySet() {
cfg.RPCCert.Value = filepath.Join(cfg.AppDataDir.Value, "rpc.cert")
}
if !cfg.LogDir.ExplicitlySet() {
cfg.LogDir.Value = filepath.Join(cfg.AppDataDir.Value, defaultLogDirname)
}
}
// Choose the active network params based on the selected network.
// Multiple networks can't be selected simultaneously.
numNets := 0
if cfg.TestNet {
activeNet = &netparams.TestNet2Params
numNets++
}
if cfg.SimNet {
activeNet = &netparams.SimNetParams
numNets++
}
if numNets > 1 {
str := "%s: The testnet and simnet params can't be used " +
"together -- choose one"
err := fmt.Errorf(str, "loadConfig")
fmt.Fprintln(os.Stderr, err)
return loadConfigError(err)
}
// Append the network type to the log directory so it is "namespaced"
// per network.
cfg.LogDir.Value = cleanAndExpandPath(cfg.LogDir.Value)
cfg.LogDir.Value = filepath.Join(cfg.LogDir.Value, activeNet.Params.Name)
// Special show command to list supported subsystems and exit.
if cfg.DebugLevel == "show" {
fmt.Println("Supported subsystems", supportedSubsystems())
os.Exit(0)
}
// Initialize log rotation. After log rotation has been initialized, the
// logger variables may be used.
initLogRotator(filepath.Join(cfg.LogDir.Value, defaultLogFilename))
// Parse, validate, and set debug log level(s).
if err := parseAndSetDebugLevels(cfg.DebugLevel); err != nil {
err := fmt.Errorf("%s: %v", "loadConfig", err.Error())
fmt.Fprintln(os.Stderr, err)
parser.WriteHelp(os.Stderr)
return loadConfigError(err)
}
// Error and shutdown if config file is specified on the command line
// but cannot be found.
if configFileError != nil && cfg.ConfigFile.ExplicitlySet() {
if preCfg.ConfigFile.ExplicitlySet() || cfg.ConfigFile.ExplicitlySet() {
log.Errorf("%v", configFileError)
return loadConfigError(configFileError)
}
}
// Warn about missing config file after the final command line parse
// succeeds. This prevents the warning on help messages and invalid
// options.
if configFileError != nil {
log.Warnf("%v", configFileError)
}
if cfg.RPCConnect == "" {
cfg.RPCConnect = net.JoinHostPort("localhost", activeNet.WalletClientPort)
}
// Add default port to connect flag if missing.
cfg.RPCConnect, err = cfgutil.NormalizeAddress(cfg.RPCConnect,
activeNet.WalletClientPort)
if err != nil {
fmt.Fprintf(os.Stderr,
"Invalid rpcconnect network address: %v\n", err)
return loadConfigError(err)
}
localhostListeners := map[string]struct{}{
"localhost": {},
"127.0.0.1": {},
"::1": {},
}
RPCHost, _, err := net.SplitHostPort(cfg.RPCConnect)
if err != nil {
return loadConfigError(err)
}
if cfg.DisableClientTLS {
if _, ok := localhostListeners[RPCHost]; !ok {
str := "%s: the --noclienttls option may not be used " +
"when connecting RPC to non localhost " +
"addresses: %s"
err := fmt.Errorf(str, funcName, cfg.RPCConnect)
fmt.Fprintln(os.Stderr, err)
fmt.Fprintln(os.Stderr, usageMessage)
return loadConfigError(err)
}
} else {
// If CAFile is unset, choose either the copy or local dcrwallet cert.
if !cfg.CAFile.ExplicitlySet() {
cfg.CAFile.Value = filepath.Join(cfg.AppDataDir.Value, defaultCAFilename)
// If the CA copy does not exist, check if we're connecting to
// a local dcrwallet and switch to its RPC cert if it exists.
certExists, err := cfgutil.FileExists(cfg.CAFile.Value)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return loadConfigError(err)
}
if !certExists {
if _, ok := localhostListeners[RPCHost]; ok {
walletCertExists, err := cfgutil.FileExists(
walletDefaultCAFile)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return loadConfigError(err)
}
if walletCertExists {
cfg.CAFile.Value = walletDefaultCAFile
}
}
}
}
}
// Default to localhost listen addresses if no listeners were manually
// specified. When the RPC server is configured to be disabled, remove all
// listeners so it is not started.
localhostAddrs, err := net.LookupHost("localhost")
if err != nil {
return loadConfigError(err)
}
if len(cfg.GRPCListeners) == 0 {
cfg.GRPCListeners = make([]string, 0, len(localhostAddrs))
for _, addr := range localhostAddrs {
cfg.GRPCListeners = append(cfg.GRPCListeners,
net.JoinHostPort(addr, activeNet.TumblerServerPort))
}
}
// Add default port to all rpc listener addresses if needed and remove
// duplicate addresses.
cfg.GRPCListeners, err = cfgutil.NormalizeAddresses(
cfg.GRPCListeners, activeNet.TumblerServerPort)
if err != nil {
fmt.Fprintf(os.Stderr,
"Invalid network address in RPC listeners: %v\n", err)
return loadConfigError(err)
}
// Only allow server TLS to be disabled if the RPC server is bound to
// localhost addresses.
if cfg.DisableServerTLS {
for _, addr := range cfg.GRPCListeners {
host, _, err := net.SplitHostPort(addr)
if err != nil {
str := "%s: RPC listen interface '%s' is " +
"invalid: %v"
err := fmt.Errorf(str, funcName, addr, err)
fmt.Fprintln(os.Stderr, err)
fmt.Fprintln(os.Stderr, usageMessage)
return loadConfigError(err)
}
if _, ok := localhostListeners[host]; !ok {
str := "%s: the --noservertls option may not be used " +
"when binding RPC to non localhost " +
"addresses: %s"
err := fmt.Errorf(str, funcName, addr)
fmt.Fprintln(os.Stderr, err)
fmt.Fprintln(os.Stderr, usageMessage)
return loadConfigError(err)
}
}
}
// Expand environment variable and leading ~ for filepaths.
cfg.CAFile.Value = cleanAndExpandPath(cfg.CAFile.Value)
cfg.RPCCert.Value = cleanAndExpandPath(cfg.RPCCert.Value)
cfg.RPCKey.Value = cleanAndExpandPath(cfg.RPCKey.Value)
// TumbleBit defaults
if cfg.PuzzleDifficulty == 0 {
cfg.PuzzleDifficulty = tumbler.PuzzleDifficulty
}
if cfg.EpochDuration == 0 {
cfg.EpochDuration = tumbler.EpochDuration
}
if cfg.EpochRenewal == 0 {
cfg.EpochRenewal = tumbler.EpochRenewal
}
return &cfg, remainingArgs, nil
}