-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
646 lines (575 loc) · 23.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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
// Copyright (c) 2019 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 (
"crypto/elliptic"
"fmt"
"io/ioutil"
"net"
"os"
"os/user"
"path/filepath"
"runtime"
"sort"
"strconv"
"strings"
"time"
flags "github.com/jessevdk/go-flags"
"github.com/decred/dcrd/certgen"
"github.com/HcashOrg/hcd/chaincfg"
"github.com/HcashOrg/hcd/hcutil"
"github.com/HcashOrg/slog"
"github.com/btclinux/hcpool/pool"
)
const (
defaultConfigFilename = "dcrpool.conf"
defaultDataDirname = "data"
defaultLogLevel = "debug"
defaultLogDirname = "log"
defaultLogFilename = "dcrpool.log"
defaultDBFilename = "dcrpool.kv"
defaultTLSCertFilename = "dcrpool.cert"
defaultTLSKeyFilename = "dcrpool.key"
defaultRPCCertFilename = "rpc.cert"
defaultRPCUser = "dcrp"
defaultRPCPass = "dcrppass"
defaultDcrdRPCHost = "127.0.0.1:19109"
defaultWalletGRPCHost = "127.0.0.1:51028"
defaultPoolFeeAddr = ""
defaultMaxGenTime = 15
defaultPoolFee = 0.01
defaultLastNPeriod = 86400 // 1 day
defaultWalletPass = ""
defaultMaxTxFeeReserve = 0.1
defaultSoloPool = false
defaultGUIPort = 8080
defaultGUIDir = "gui"
defaultUseLEHTTPS = false
defaultCPUPort = 5550
defaultD9Port = 5552
defaultDR3Port = 5553
defaultDR5Port = 5554
defaultD1Port = 5555
defaultDesignation = "YourPoolNameHere"
defaultMaxConnectionsPerHost = 100 // 100 connected clients per host
)
var (
defaultActiveNet = chaincfg.SimNetParams.Name
defaultPaymentMethod = pool.PPLNS
defaultMinPayment = 0.2
dcrpoolHomeDir = hcutil.AppDataDir("dcrpool", false)
defaultConfigFile = filepath.Join(dcrpoolHomeDir, defaultConfigFilename)
defaultDataDir = filepath.Join(dcrpoolHomeDir, defaultDataDirname)
defaultDBFile = filepath.Join(defaultDataDir, defaultDBFilename)
defaultLogDir = filepath.Join(dcrpoolHomeDir, defaultLogDirname)
defaultTLSCertFile = filepath.Join(dcrpoolHomeDir, defaultTLSCertFilename)
defaultTLSKeyFile = filepath.Join(dcrpoolHomeDir, defaultTLSKeyFilename)
)
// runServiceCommand is only set to a real function on Windows. It is used
// to parse and execute service commands specified via the -s flag.
var runServiceCommand func(string) error
// config defines the configuration options for the pool.
type config struct {
HomeDir string `long:"homedir" ini-name:"homedir" description:"Path to application home directory."`
ConfigFile string `long:"configfile" ini-name:"configfile" description:"Path to configuration file."`
DataDir string `long:"datadir" ini-name:"datadir" description:"The data directory."`
ActiveNet string `long:"activenet" ini-name:"activenet" description:"The active network being mined on. {testnet3, mainnet, simnet}"`
GUIPort uint32 `long:"guiport" ini-name:"guiport" description:"The pool GUI port."`
DebugLevel string `long:"debuglevel" ini-name:"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"`
LogDir string `long:"logdir" ini-name:"logdir" description:"Directory to log output."`
DBFile string `long:"dbfile" ini-name:"dbfile" description:"Path to the database file."`
DcrdRPCHost string `long:"dcrdrpchost" ini-name:"dcrdrpchost" description:"The ip:port to establish an RPC connection for dcrd."`
DcrdRPCCert string `long:"dcrdrpccert" ini-name:"dcrdrpccert" description:"The dcrd RPC certificate."`
WalletGRPCHost string `long:"walletgrpchost" ini-name:"walletgrpchost" description:"The ip:port to establish a GRPC connection for the wallet."`
WalletRPCCert string `long:"walletrpccert" ini-name:"walletrpccert" description:"The wallet RPC certificate."`
RPCUser string `long:"rpcuser" ini-name:"rpcuser" description:"Username for RPC connections."`
RPCPass string `long:"rpcpass" ini-name:"rpcpass" default-mask:"-" description:"Password for RPC connections."`
PoolFeeAddrs []string `long:"poolfeeaddrs" ini-name:"poolfeeaddrs" description:"Payment addresses to use for pool fee transactions. These addresses should be generated from a dedicated wallet account for pool fees."`
PoolFee float64 `long:"poolfee" ini-name:"poolfee" description:"The fee charged for pool participation. eg. 0.01 (1%), 0.05 (5%)."`
MaxTxFeeReserve float64 `long:"maxtxfeereserve" ini-name:"maxtxfeereserve" description:"The maximum amount reserved for transaction fees, in DCR."`
MaxGenTime uint64 `long:"maxgentime" ini-name:"maxgentime" description:"The share creation target time for the pool in seconds. This currently should be below 30 seconds to increase the likelihood a work submission for clients between new work distributions by the pool."`
PaymentMethod string `long:"paymentmethod" ini-name:"paymentmethod" description:"The payment method of the pool. {pps, pplns}"`
LastNPeriod uint32 `long:"lastnperiod" ini-name:"lastnperiod" description:"The period of interest when using the PPLNS payment scheme."`
WalletPass string `long:"walletpass" ini-name:"walletpass" description:"The wallet passphrase."`
MinPayment float64 `long:"minpayment" ini-name:"minpayment" description:"The minimum payment to process for an account."`
SoloPool bool `long:"solopool" ini-name:"solopool" description:"Solo pool mode. This disables payment processing when enabled."`
BackupPass string `long:"backuppass" ini-name:"backuppass" description:"The admin password, required for database backup."`
GUIDir string `long:"guidir" ini-name:"guidir" description:"The path to the directory containing the pool's user interface assets (templates, css etc.)"`
Domain string `long:"domain" ini-name:"domain" description:"The domain of the mining pool, required for TLS."`
UseLEHTTPS bool `long:"uselehttps" ini-name:"uselehttps" description:"This enables HTTPS using a Letsencrypt certificate. By default the pool uses a self-signed certificate for HTTPS."`
TLSCert string `long:"tlscert" ini-name:"tlscert" description:"Path to the TLS cert file."`
TLSKey string `long:"tlskey" ini-name:"tlskey" description:"Path to the TLS key file."`
Designation string `long:"designation" ini-name:"designation" description:"The designated codename for this pool. Customises the logo in the top toolbar."`
MaxConnectionsPerHost uint32 `long:"maxconnperhost" init-name:"maxconnperhost" description:"The maximum number of connections allowed per host."`
Profile string `long:"profile" init-name:"Enable HTTP profiling on given [addr:]port -- NOTE port must be between 1024 and 65536"`
CPUPort uint32 `long:"cpuport" ini-name:"cpuport" description:"CPU miner connection port."`
D9Port uint32 `long:"d9port" ini-name:"d9port" description:"Innosilicon D9 connection port."`
DR3Port uint32 `long:"dr3port" ini-name:"dr3port" description:"Antminer DR3 connection port."`
DR5Port uint32 `long:"dr5port" ini-name:"dr5port" description:"Antminer DR5 connection port."`
D1Port uint32 `long:"d1port" ini-name:"d1port" description:"Whatsminer D1 connection port."`
poolFeeAddrs []hcutil.Address
dcrdRPCCerts []byte
net *chaincfg.Params
}
// serviceOptions defines the configuration options for the daemon as a service on
// Windows.
type serviceOptions struct {
ServiceCommand string `short:"s" long:"service" description:"Service command {install, remove, start, stop}"`
}
// validLogLevel returns whether or not logLevel is a valid debug log level.
func validLogLevel(logLevel string) bool {
_, ok := slog.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 subsystems 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 delimiters, 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
}
// fileExists reports whether the named file or directory exists.
func fileExists(name string) bool {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
// genCertPair generates a key/cert pair to the paths provided.
func genCertPair(certFile, keyFile string) error {
org := "dcrpool autogenerated cert"
validUntil := time.Now().Add(10 * 365 * 24 * time.Hour)
cert, key, err := certgen.NewTLSCertPair(elliptic.P256(), org,
validUntil, nil)
if err != nil {
return err
}
// Write cert and key files.
if err = ioutil.WriteFile(certFile, cert, 0644); err != nil {
return err
}
if err = ioutil.WriteFile(keyFile, key, 0600); err != nil {
os.Remove(certFile)
return err
}
return nil
}
// newConfigParser returns a new command line flags parser.
func newConfigParser(cfg *config, so *serviceOptions, options flags.Options) (*flags.Parser, error) {
parser := flags.NewParser(cfg, options)
if runtime.GOOS == "windows" {
_, err := parser.AddGroup("Service Options", "Service Options", so)
if err != nil {
return nil, err
}
}
return parser, nil
}
// cleanAndExpandPath expands environment variables and leading ~ in the
// passed path, cleans the result, and returns it.
func cleanAndExpandPath(path string) string {
// Nothing to do when no path is given.
if path == "" {
return path
}
// NOTE: The os.ExpandEnv doesn't work with Windows cmd.exe-style
// %VARIABLE%, but the 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)
}
// 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 dcrpool 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() (*config, []string, error) {
// Default config.
cfg := config{
HomeDir: dcrpoolHomeDir,
ConfigFile: defaultConfigFile,
DataDir: defaultDataDir,
DBFile: defaultDBFile,
DebugLevel: defaultLogLevel,
LogDir: defaultLogDir,
RPCUser: defaultRPCUser,
RPCPass: defaultRPCPass,
DcrdRPCHost: defaultDcrdRPCHost,
WalletGRPCHost: defaultWalletGRPCHost,
PoolFeeAddrs: []string{defaultPoolFeeAddr},
PoolFee: defaultPoolFee,
MaxTxFeeReserve: defaultMaxTxFeeReserve,
MaxGenTime: defaultMaxGenTime,
ActiveNet: defaultActiveNet,
PaymentMethod: defaultPaymentMethod,
LastNPeriod: defaultLastNPeriod,
WalletPass: defaultWalletPass,
MinPayment: defaultMinPayment,
SoloPool: defaultSoloPool,
GUIPort: defaultGUIPort,
GUIDir: defaultGUIDir,
UseLEHTTPS: defaultUseLEHTTPS,
TLSCert: defaultTLSCertFile,
TLSKey: defaultTLSKeyFile,
Designation: defaultDesignation,
MaxConnectionsPerHost: defaultMaxConnectionsPerHost,
CPUPort: defaultCPUPort,
D9Port: defaultD9Port,
DR3Port: defaultDR3Port,
DR5Port: defaultDR5Port,
D1Port: defaultD1Port,
}
// Service options which are only added on Windows.
serviceOpts := serviceOptions{}
// Pre-parse the command line options to see if an alternative config
// file or the version flag was specified. Any errors aside from the
// help message error can be ignored here since they will be caught by
// the final parse below.
preCfg := cfg
preParser, err := newConfigParser(&preCfg, &serviceOpts, flags.HelpFlag)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
_, err = preParser.Parse()
if err != nil {
if e, ok := err.(*flags.Error); ok && e.Type != flags.ErrHelp {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
} else if ok && e.Type == flags.ErrHelp {
fmt.Fprintln(os.Stdout, err)
os.Exit(0)
}
}
appName := filepath.Base(os.Args[0])
appName = strings.TrimSuffix(appName, filepath.Ext(appName))
usageMessage := fmt.Sprintf("Use %s -h to show usage", appName)
// Perform service command and exit if specified. Invalid service
// commands show an appropriate error. Only runs on Windows since
// the runServiceCommand function will be nil when not on Windows.
if serviceOpts.ServiceCommand != "" && runServiceCommand != nil {
err := runServiceCommand(serviceOpts.ServiceCommand)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
os.Exit(0)
}
// Update the home directory for dcrpool if specified. Since the home
// directory is updated, other variables need to be updated to
// reflect the new changes.
if preCfg.HomeDir != "" {
cfg.HomeDir, _ = filepath.Abs(preCfg.HomeDir)
if preCfg.ConfigFile == defaultConfigFile {
defaultConfigFile = filepath.Join(cfg.HomeDir,
defaultConfigFilename)
preCfg.ConfigFile = defaultConfigFile
cfg.ConfigFile = defaultConfigFile
} else {
cfg.ConfigFile = preCfg.ConfigFile
}
if preCfg.DataDir == defaultDataDir {
cfg.DataDir = filepath.Join(cfg.HomeDir, defaultDataDirname)
} else {
cfg.DataDir = preCfg.DataDir
}
if preCfg.LogDir == defaultLogDir {
cfg.LogDir = filepath.Join(cfg.HomeDir, defaultLogDirname)
} else {
cfg.LogDir = preCfg.LogDir
}
if preCfg.DBFile == defaultDBFile {
cfg.DBFile = filepath.Join(cfg.DataDir, defaultDBFilename)
} else {
cfg.DBFile = preCfg.DBFile
}
if preCfg.TLSCert == defaultTLSCertFile {
cfg.TLSCert = filepath.Join(cfg.HomeDir, defaultTLSCertFilename)
} else {
cfg.TLSCert = preCfg.TLSCert
}
if preCfg.TLSKey == defaultTLSKeyFile {
cfg.TLSKey = filepath.Join(cfg.HomeDir, defaultTLSKeyFilename)
} else {
cfg.TLSKey = preCfg.TLSKey
}
}
// Create the home directory if it doesn't already exist.
funcName := "loadConfig"
err = os.MkdirAll(cfg.HomeDir, 0700)
if 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 nil, nil, err
}
// Create a default config file when one does not exist and the user did
// not specify an override.
if !fileExists(preCfg.ConfigFile) {
preIni := flags.NewIniParser(preParser)
err = preIni.WriteFile(preCfg.ConfigFile,
flags.IniIncludeComments|flags.IniIncludeDefaults)
if err != nil {
return nil, nil, fmt.Errorf("error creating a default "+
"config file: %v", err)
}
}
// Load additional config from file.
var configFileError error
parser, err := newConfigParser(&cfg, &serviceOpts, flags.Default)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return nil, nil, err
}
if preCfg.ConfigFile != defaultConfigFile {
err := flags.NewIniParser(parser).ParseFile(preCfg.ConfigFile)
if err != nil {
if _, ok := err.(*os.PathError); !ok {
fmt.Fprintf(os.Stderr, "error parsing config file: %v\n", err)
fmt.Fprintln(os.Stderr, usageMessage)
return nil, nil, 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 {
fmt.Fprintln(os.Stderr, usageMessage)
}
return nil, nil, err
}
cfg.DataDir = cleanAndExpandPath(cfg.DataDir)
cfg.LogDir = cleanAndExpandPath(cfg.LogDir)
logRotator = nil
// Initialize log rotation. After log rotation has been initialized, the
// logger variables may be used.
initLogRotator(filepath.Join(cfg.LogDir, defaultLogFilename))
// Ensure the backup password is set.
if cfg.BackupPass == "" {
str := "%s: pool backup password is not set"
err := fmt.Errorf(str, funcName)
return nil, nil, err
}
// Create the data directory.
err = os.MkdirAll(cfg.DataDir, 0700)
if err != nil {
str := "%s: failed to create data directory: %v"
err := fmt.Errorf(str, funcName, err)
return nil, nil, err
}
// Special show command to list supported subsystems and exit.
if cfg.DebugLevel == "show" {
fmt.Println("Supported subsystems", supportedSubsystems())
os.Exit(0)
}
// Parse, validate, and set debug log level(s).
if err := parseAndSetDebugLevels(cfg.DebugLevel); err != nil {
err := fmt.Errorf("%s: %v", funcName, err.Error())
fmt.Fprintln(os.Stderr, err)
fmt.Fprintln(os.Stderr, usageMessage)
return nil, nil, err
}
// Set the mining active network. Testnet proof of work
// parameters are modified here to mirror that of mainnet in order to
// generate reasonable difficulties for asics. Simnet is currently reserved
// for cpu miner tests only.
switch cfg.ActiveNet {
case chaincfg.TestNet3Params.Name:
cfg.net = &chaincfg.TestNet3Params
cfg.net.PowLimit = chaincfg.MainNetParams.PowLimit
case chaincfg.MainNetParams.Name:
cfg.net = &chaincfg.MainNetParams
case chaincfg.SimNetParams.Name:
cfg.net = &chaincfg.SimNetParams
default:
return nil, nil, fmt.Errorf("unknown network provided %v",
cfg.ActiveNet)
}
if !cfg.SoloPool {
// Ensure a valid payment method is set.
if cfg.PaymentMethod != pool.PPS && cfg.PaymentMethod != pool.PPLNS {
str := "%s: paymentmethod must be either %s or %s"
err := fmt.Errorf(str, funcName, pool.PPS, pool.PPLNS)
return nil, nil, err
}
for _, pAddr := range cfg.PoolFeeAddrs {
addr, err := hcutil.DecodeAddress(pAddr)
if err != nil {
str := "%s: pool fee address '%v' failed to decode: %v"
err := fmt.Errorf(str, funcName, pAddr, err)
fmt.Fprintln(os.Stderr, err)
fmt.Fprintln(os.Stderr, usageMessage)
return nil, nil, err
}
// Ensure pool fee address is valid and on the active pool.
if !addr.IsForNet(cfg.net) {
return nil, nil,
fmt.Errorf("pool fee address (%v) not on the active network "+
"(%s)", addr, cfg.ActiveNet)
}
cfg.poolFeeAddrs = append(cfg.poolFeeAddrs, addr)
}
}
// 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 {
mpLog.Warnf("%v", configFileError)
}
// Ensure a domain is set if HTTPS via letsencrypt is preferred.
if cfg.UseLEHTTPS && cfg.Domain == "" {
return nil, nil, fmt.Errorf("a valid domain is required for HTTPS " +
"via letsencrypt")
}
// Generate self-signed TLS cert and key if they do not already exist.
if !cfg.UseLEHTTPS && (!fileExists(cfg.TLSCert) || !fileExists(cfg.TLSKey)) {
err := genCertPair(cfg.TLSCert, cfg.TLSKey)
if err != nil {
return nil, nil,
fmt.Errorf("failed to generate dcrpool's TLS cert/key: %v", err)
}
}
// Load dcrd RPC certificate.
if !fileExists(cfg.DcrdRPCCert) {
return nil, nil,
fmt.Errorf("dcrd RPC certificate (%v) not found", cfg.DcrdRPCCert)
}
cfg.dcrdRPCCerts, err = ioutil.ReadFile(cfg.DcrdRPCCert)
if err != nil {
return nil, nil, err
}
// Validate format of profile, can be an address:port, or just a port.
if cfg.Profile != "" {
// If profile is just a number, then add a default host of "127.0.0.1"
// such that Profile is a valid tcp address.
if _, err := strconv.Atoi(cfg.Profile); err == nil {
cfg.Profile = net.JoinHostPort("127.0.0.1", cfg.Profile)
}
// Check the Profile is a valid address
_, portStr, err := net.SplitHostPort(cfg.Profile)
if err != nil {
str := "%s: profile: %s"
err := fmt.Errorf(str, funcName, err)
fmt.Fprintln(os.Stderr, err)
fmt.Fprintln(os.Stderr, usageMessage)
return nil, nil, err
}
// Finally, check the port is in range.
if port, _ := strconv.Atoi(portStr); port < 1024 || port > 65535 {
str := "%s: profile: address %s: port must be between 1024 and 65535"
err := fmt.Errorf(str, funcName, cfg.Profile)
fmt.Fprintln(os.Stderr, err)
fmt.Fprintln(os.Stderr, usageMessage)
return nil, nil, err
}
}
if !cfg.SoloPool {
// Load the wallet RPC certificate.
if !fileExists(cfg.WalletRPCCert) {
return nil, nil,
fmt.Errorf("wallet RPC certificate (%v) not found",
cfg.WalletRPCCert)
}
}
return &cfg, remainingArgs, nil
}