forked from p9c/pod-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sanitizers.go
837 lines (805 loc) · 26 KB
/
sanitizers.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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
package config
import (
"crypto/tls"
"errors"
"fmt"
"io/ioutil"
"net"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
"github.com/l0k18/pod/app/apputil"
"github.com/l0k18/pod/cmd/node"
blockchain "github.com/l0k18/pod/pkg/chain"
"github.com/l0k18/pod/pkg/chain/forkhash"
"github.com/l0k18/pod/pkg/comm/peer/connmgr"
"github.com/l0k18/pod/pkg/util"
"github.com/l0k18/pod/pkg/util/interrupt"
"github.com/l0k18/pod/pkg/util/normalize"
"github.com/l0k18/pod/pkg/util/routeable"
"github.com/l0k18/pod/pkg/wallet"
"github.com/btcsuite/go-socks/socks"
"github.com/urfave/cli"
"github.com/l0k18/pod/app/appdata"
"github.com/l0k18/pod/app/conte"
"github.com/l0k18/pod/cmd/node/state"
"github.com/l0k18/pod/pkg/chain/config/netparams"
"github.com/l0k18/pod/pkg/chain/fork"
"github.com/l0k18/pod/pkg/pod"
"github.com/l0k18/pod/pkg/util/logi"
)
const (
appName = "pod"
confExt = ".json"
appLanguage = "en"
podConfigFilename = appName + confExt
PARSER = "json"
)
var funcName = "loadConfig"
func initDictionary(cfg *pod.Config) {
if cfg.Language == nil || *cfg.Language == "" {
*cfg.Language = Lang("en")
}
Trace("lang set to", *cfg.Language)
}
func initDataDir(cfg *pod.Config) {
if cfg.DataDir == nil || *cfg.DataDir == "" {
Debug("setting default data dir")
*cfg.DataDir = appdata.Dir("pod", false)
}
Trace("datadir set to", *cfg.DataDir)
}
func initWalletFile(cx *conte.Xt) {
if cx.Config.WalletFile == nil || *cx.Config.WalletFile == "" {
*cx.Config.WalletFile = *cx.Config.DataDir + string(os.PathSeparator) +
cx.ActiveNet.Name + string(os.PathSeparator) + wallet.DbName
}
Trace("wallet file set to", *cx.Config.WalletFile, *cx.Config.Network)
}
func initConfigFile(cfg *pod.Config) {
if *cfg.ConfigFile == "" {
*cfg.ConfigFile =
*cfg.DataDir + string(os.PathSeparator) + podConfigFilename
}
Trace("using config file:", *cfg.ConfigFile)
}
func initLogDir(cfg *pod.Config) {
if *cfg.LogDir != "" {
logi.L.SetLogPaths(*cfg.LogDir, "pod")
interrupt.AddHandler(func() {
Debug("initLogDir interrupt")
_ = logi.L.LogFileHandle.Close()
})
}
}
func initParams(cx *conte.Xt) {
network := "mainnet"
if cx.Config.Network != nil {
network = *cx.Config.Network
}
switch network {
case "testnet", "testnet3", "t":
Trace("on testnet")
cx.ActiveNet = &netparams.TestNet3Params
fork.IsTestnet = true
case "regtestnet", "regressiontest", "r":
Trace("on regression testnet")
cx.ActiveNet = &netparams.RegressionTestParams
case "simnet", "s":
Trace("on simnet")
cx.ActiveNet = &netparams.SimNetParams
default:
if network != "mainnet" && network != "m" {
Warn("using mainnet for node")
}
Trace("on mainnet")
cx.ActiveNet = &netparams.MainNetParams
}
}
func validatePort(port string) bool {
var err error
var p int64
if p, err = strconv.ParseInt(port, 10, 32); Check(err) {
return false
}
if p < 1024 || p > 65535 {
return false
}
return true
}
func initListeners(cx *conte.Xt, commandName string, initial bool) {
cfg := cx.Config
var e error
var fP int
if fP, e = GetFreePort(); Check(e) {
}
// var routeableAddresses []net.Addr
_, addresses := routeable.GetInterface()
// routeableAddresses, e = routeableInterface[0].Addrs()
// Debug(routeableAddresses)
routeableAddress := addresses[0]
// Debug("********************", routeableAddress)
*cfg.Controller = net.JoinHostPort(routeableAddress, fmt.Sprint(fP))
if len(*cfg.Listeners) < 1 && !*cfg.DisableListen && len(*cfg.ConnectPeers) < 1 {
cfg.Listeners = &cli.StringSlice{fmt.Sprintf(routeableAddress + ":" + cx.ActiveNet.DefaultPort)}
cx.StateCfg.Save = true
Debug("Listeners")
}
if len(*cfg.RPCListeners) < 1 {
address := fmt.Sprintf("%s:%s", routeableAddress, cx.ActiveNet.RPCClientPort)
*cfg.RPCListeners = cli.StringSlice{address}
*cfg.RPCConnect = address
Debug("setting save flag because rpc listeners is empty and rpc is not disabled")
cx.StateCfg.Save = true
Debug("RPCListeners")
}
if len(*cfg.WalletRPCListeners) < 1 && !*cfg.DisableRPC {
address := fmt.Sprintf(routeableAddress + ":" + cx.ActiveNet.WalletRPCServerPort)
*cfg.WalletRPCListeners = cli.StringSlice{address}
*cfg.WalletServer = address
Debug("setting save flag because wallet rpc listeners is empty and" +
" rpc is not disabled")
cx.StateCfg.Save = true
Debug("WalletRPCListeners")
}
// msgBase := pause.GetPauseContainer(cx)
// mC := job.Get(cx, util.NewBlock(tpl.Block), msgBase)
// listenHost := msgBase.GetIPs()[0].String() + ":0"
// switch commandName {
// // only the wallet listener is important with shell as it proxies for
// // node, the rest better they are automatic
// case "shell":
// *cfg.Listeners = cli.StringSlice{listenHost}
// *cfg.RPCListeners = cli.StringSlice{listenHost}
// }
if *cx.Config.AutoPorts || !initial {
if fP, e = GetFreePort(); Check(e) {
}
*cfg.Listeners = cli.StringSlice{routeableAddress + ":" + fmt.Sprint(fP)}
if fP, e = GetFreePort(); Check(e) {
}
*cfg.RPCListeners = cli.StringSlice{routeableAddress + ":" + fmt.Sprint(fP)}
if fP, e = GetFreePort(); Check(e) {
}
*cfg.WalletRPCListeners = cli.StringSlice{routeableAddress + ":" + fmt.Sprint(fP)}
cx.StateCfg.Save = true
Debug("autoports")
} else {
// sanitize user input and set auto on any that fail
l := cfg.Listeners
r := cfg.RPCListeners
w := cfg.WalletRPCListeners
for i := range *l {
if _, p, e := net.SplitHostPort((*l)[i]); !Check(e) {
if !validatePort(p) {
if fP, e = GetFreePort(); Check(e) {
}
(*l)[i] = routeableAddress + ":" + fmt.Sprint(fP)
cx.StateCfg.Save = true
Debug("port not validate Listeners")
}
}
}
for i := range *r {
if _, p, e := net.SplitHostPort((*r)[i]); !Check(e) {
if !validatePort(p) {
if fP, e = GetFreePort(); Check(e) {
}
(*r)[i] = routeableAddress + ":" + fmt.Sprint(fP)
cx.StateCfg.Save = true
Debug("port not validate RPCListeners")
}
}
}
// if *cfg.RPCConnect == "" {
// *cfg.RPCConnect = routeableAddress + ":" + fmt.Sprint(fP)
// Debug("setting save flag because rpcconnect was not configured")
// cx.StateCfg.Save = true
// }
for i := range *w {
if _, p, e := net.SplitHostPort((*w)[i]); !Check(e) {
if !validatePort(p) {
if fP, e = GetFreePort(); Check(e) {
}
(*w)[i] = routeableAddress + ":" + fmt.Sprint(fP)
cx.StateCfg.Save = true
Debug("port not validate WalletRPCListeners")
}
}
}
}
// all of these can be autodiscovered/set but to do that and know what they are we have to reserve them
//
// listeners := []*cli.StringSlice{
// cfg.WalletRPCListeners,
// cfg.Listeners,
// cfg.RPCListeners,
// }
// for i := range listeners {
// if h, p, err := net.SplitHostPort((*listeners[i])[0]); p == "0" {
// if err != nil {
// Error(err)
// } else {
// fP, err := GetFreePort()
// if err != nil {
// Error(err)
// }
// *listeners[i] = cli.
// StringSlice{net.JoinHostPort(h, fmt.Sprint(fP))}
// }
// }
// }
// (*cfg.WalletRPCListeners)[0] = (*listeners[0])[0]
// (*cfg.Listeners)[0] = (*listeners[1])[0]
// (*cfg.RPCListeners)[0] = (*listeners[2])[0]
//
// if lan mode is set, remove the peers.json so no unwanted nodes are connected to
if *cfg.LAN && cx.ActiveNet.Name != "mainnet" {
peersFile := filepath.Join(filepath.Join(*cfg.DataDir, cx.ActiveNet.Name), "peers.json")
var err error
if err = os.Remove(peersFile); err != nil {
Trace("nothing to remove?", err)
}
Trace("removed", peersFile)
}
// *cfg.RPCConnect = (*cfg.RPCListeners)[0]
// h, p, _ := net.SplitHostPort(*cfg.RPCConnect)
// if h == "" {
// *cfg.RPCConnect = net.JoinHostPort(routeableAddress, p)
// }
if len(*cfg.WalletRPCListeners) > 0 {
splitted := strings.Split((*cfg.WalletRPCListeners)[0], ":")
*cfg.WalletServer = routeableAddress + ":" + splitted[1]
}
// save.Pod(cfg)
}
// GetFreePort asks the kernel for free open ports that are ready to use.
func GetFreePort() (int, error) {
var port int
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
return 0, err
}
l, err := net.ListenTCP("tcp", addr)
if err != nil {
return 0, err
}
defer func() {
if err := l.Close(); Check(err) {
}
}()
port = l.Addr().(*net.TCPAddr).Port
return port, nil
}
func initTLSStuffs(cfg *pod.Config, st *state.Config) {
isNew := false
if *cfg.RPCCert == "" {
*cfg.RPCCert =
*cfg.DataDir + string(os.PathSeparator) + "rpc.cert"
Debug("setting save flag because rpc cert path was not set")
st.Save = true
isNew = true
}
if *cfg.RPCKey == "" {
*cfg.RPCKey =
*cfg.DataDir + string(os.PathSeparator) + "rpc.key"
Debug("setting save flag because rpc key path was not set")
st.Save = true
isNew = true
}
if *cfg.CAFile == "" {
*cfg.CAFile =
*cfg.DataDir + string(os.PathSeparator) + "ca.cert"
Debug("setting save flag because CA cert path was not set")
st.Save = true
isNew = true
}
if isNew {
// Now is the best time to make the certs
Info("generating TLS certificates")
// Create directories for cert and key files if they do not yet exist.
Warn("rpc tls ", *cfg.RPCCert, " ", *cfg.RPCKey)
certDir, _ := filepath.Split(*cfg.RPCCert)
keyDir, _ := filepath.Split(*cfg.RPCKey)
err := os.MkdirAll(certDir, 0700)
if err != nil {
Error(err)
return
}
err = os.MkdirAll(keyDir, 0700)
if err != nil {
Error(err)
return
}
// Generate cert pair.
org := "pod/wallet autogenerated cert"
validUntil := time.Now().Add(time.Hour * 24 * 365 * 10)
cert, key, err := util.NewTLSCertPair(org, validUntil, nil)
if err != nil {
Error(err)
return
}
_, err = tls.X509KeyPair(cert, key)
if err != nil {
Error(err)
return
}
// Write cert and (potentially) the key files.
err = ioutil.WriteFile(*cfg.RPCCert, cert, 0600)
if err != nil {
rmErr := os.Remove(*cfg.RPCCert)
if rmErr != nil {
Warn("cannot remove written certificates:", rmErr)
}
return
}
err = ioutil.WriteFile(*cfg.CAFile, cert, 0600)
if err != nil {
rmErr := os.Remove(*cfg.RPCCert)
if rmErr != nil {
Warn("cannot remove written certificates:", rmErr)
}
return
}
err = ioutil.WriteFile(*cfg.RPCKey, key, 0600)
if err != nil {
Error(err)
rmErr := os.Remove(*cfg.RPCCert)
if rmErr != nil {
Warn("cannot remove written certificates:", rmErr)
}
rmErr = os.Remove(*cfg.CAFile)
if rmErr != nil {
Warn("cannot remove written certificates:", rmErr)
}
return
}
Info("done generating TLS certificates")
return
}
}
func initLogLevel(cfg *pod.Config) {
loglevel := *cfg.LogLevel
switch loglevel {
case "trace", "debug", "info", "warn", "error", "fatal", "off":
Debug("log level", loglevel)
default:
Error("unrecognised loglevel", loglevel, "setting default info")
*cfg.LogLevel = "info"
}
color := true
if runtime.GOOS == "windows" {
color = false
}
logi.L.SetLevel(*cfg.LogLevel, color, "pod")
}
func normalizeAddresses(cfg *pod.Config) {
Trace("normalising addresses")
port := node.DefaultPort
nrm := normalize.StringSliceAddresses
nrm(cfg.AddPeers, port)
nrm(cfg.ConnectPeers, port)
// nrm(cfg.Listeners, port)
nrm(cfg.Whitelists, port)
// nrm(cfg.RPCListeners, port)
}
func setRelayReject(cfg *pod.Config) {
relayNonStd := *cfg.RelayNonStd
switch {
case *cfg.RelayNonStd && *cfg.RejectNonStd:
errf := "%s: rejectnonstd and relaynonstd cannot be used together" +
" -- choose only one, leaving neither activated"
Error(errf, funcName)
// just leave both false
*cfg.RelayNonStd = false
*cfg.RejectNonStd = false
case *cfg.RejectNonStd:
relayNonStd = false
case *cfg.RelayNonStd:
relayNonStd = true
}
*cfg.RelayNonStd = relayNonStd
}
func validateDBtype(cfg *pod.Config) {
// Validate database type.
Trace("validating database type")
if !node.ValidDbType(*cfg.DbType) {
str := "%s: The specified database type [%v] is invalid -- " +
"supported types %v"
err := fmt.Errorf(str, funcName, *cfg.DbType, node.KnownDbTypes)
Error(funcName, err)
// set to default
*cfg.DbType = node.KnownDbTypes[0]
}
}
func validateProfilePort(cfg *pod.Config) {
// Validate profile port number
Trace("validating profile port number")
if *cfg.Profile != "" {
profilePort, err := strconv.Atoi(*cfg.Profile)
if err != nil || profilePort < 1024 || profilePort > 65535 {
str := "%s: The profile port must be between 1024 and 65535"
err := fmt.Errorf(str, funcName)
Error(funcName, err)
*cfg.Profile = ""
}
}
}
func validateBanDuration(cfg *pod.Config) {
// Don't allow ban durations that are too short.
Trace("validating ban duration")
if *cfg.BanDuration < time.Second {
err := fmt.Errorf("%s: The banduration option may not be less than 1s -- parsed [%v]",
funcName, *cfg.BanDuration)
Info(funcName, err)
*cfg.BanDuration = node.DefaultBanDuration
}
}
func validateWhitelists(cfg *pod.Config, st *state.Config) {
// Validate any given whitelisted IP addresses and networks.
Trace("validating whitelists")
if len(*cfg.Whitelists) > 0 {
var ip net.IP
st.ActiveWhitelists = make([]*net.IPNet, 0, len(*cfg.Whitelists))
for _, addr := range *cfg.Whitelists {
_, ipnet, err := net.ParseCIDR(addr)
if err != nil {
Error(err)
ip = net.ParseIP(addr)
if ip == nil {
str := err.Error() + " %s: The whitelist value of '%s' is invalid"
err = fmt.Errorf(str, funcName, addr)
Error(err)
_, _ = fmt.Fprintln(os.Stderr, err)
interrupt.Request()
// os.Exit(1)
} else {
var bits int
if ip.To4() == nil {
// IPv6
bits = 128
} else {
bits = 32
}
ipnet = &net.IPNet{
IP: ip,
Mask: net.CIDRMask(bits, bits),
}
}
}
st.ActiveWhitelists = append(st.ActiveWhitelists, ipnet)
}
}
}
func validatePeerLists(cfg *pod.Config) {
Trace("checking addpeer and connectpeer lists")
if len(*cfg.AddPeers) > 0 && len(*cfg.ConnectPeers) > 0 {
err := fmt.Errorf(
"%s: the --addpeer and --connect options can not be mixed",
funcName)
_, _ = fmt.Fprintln(os.Stderr, err)
// os.Exit(1)
}
}
func configListener(cfg *pod.Config, params *netparams.Params) {
// --proxy or --connect without --listen disables listening.
Trace("checking proxy/connect for disabling listening")
if (*cfg.Proxy != "" ||
len(*cfg.ConnectPeers) > 0) &&
len(*cfg.Listeners) == 0 {
*cfg.DisableListen = true
}
// Add the default listener if none were specified. The default listener is all addresses on the listen port for the
// network we are to connect to.
Trace("checking if listener was set")
if len(*cfg.Listeners) == 0 {
*cfg.Listeners = []string{":" + params.DefaultPort}
}
}
func validateUsers(cfg *pod.Config) {
// Check to make sure limited and admin users don't have the same username
Trace("checking admin and limited username is different")
if *cfg.Username != "" &&
*cfg.Username == *cfg.LimitUser {
str := "%s: --username and --limituser must not specify the same username"
err := fmt.Errorf(str, funcName)
_, _ = fmt.Fprintln(os.Stderr, err)
// os.Exit(1)
}
// Check to make sure limited and admin users don't have the same password
Trace("checking limited and admin passwords are not the same")
if *cfg.Password != "" &&
*cfg.Password == *cfg.LimitPass {
str := "%s: --password and --limitpass must not specify the same password"
err := fmt.Errorf(str, funcName)
_, _ = fmt.Fprintln(os.Stderr, err)
// os.Exit(1)
}
}
func configRPC(cfg *pod.Config, params *netparams.Params) {
// The RPC server is disabled if no username or password is provided.
Trace("checking rpc server has a login enabled")
if (*cfg.Username == "" || *cfg.Password == "") &&
(*cfg.LimitUser == "" || *cfg.LimitPass == "") {
*cfg.DisableRPC = true
}
if *cfg.DisableRPC {
Trace("RPC service is disabled")
}
Trace("checking rpc server has listeners set")
if !*cfg.DisableRPC && len(*cfg.RPCListeners) == 0 {
Debug("looking up default listener")
addrs, err := net.LookupHost(node.DefaultRPCListener)
if err != nil {
Error(err)
// os.Exit(1)
}
*cfg.RPCListeners = make([]string, 0, len(addrs))
Debug("setting listeners")
for _, addr := range addrs {
*cfg.RPCListeners = append(*cfg.RPCListeners, addr)
addr = net.JoinHostPort(addr, params.RPCClientPort)
}
}
Trace("checking rpc max concurrent requests")
if *cfg.RPCMaxConcurrentReqs < 0 {
str := "%s: The rpcmaxwebsocketconcurrentrequests option may not be" +
" less than 0 -- parsed [%d]"
err := fmt.Errorf(str, funcName, *cfg.RPCMaxConcurrentReqs)
_, _ = fmt.Fprintln(os.Stderr, err)
// os.Exit(1)
}
Trace("checking rpc listener addresses")
nrms := normalize.Addresses
// Add default port to all rpc listener addresses if needed and remove duplicate addresses.
//
// *cfg.RPCListeners = nrms(*cfg.RPCListeners, cx.ActiveNet.RPCClientPort)
//
// Add default port to all listener addresses if needed and remove duplicate addresses.
//
// *cfg.Listeners = nrms(*cfg.Listeners, cx.ActiveNet.DefaultPort)
//
// Add default port to all added peer addresses if needed and remove duplicate addresses.
*cfg.AddPeers = nrms(*cfg.AddPeers, params.DefaultPort)
*cfg.ConnectPeers = nrms(*cfg.ConnectPeers, params.DefaultPort)
}
func validatePolicies(cfg *pod.Config, stateConfig *state.Config) {
var err error
// Validate the the minrelaytxfee.
Trace("checking min relay tx fee")
stateConfig.ActiveMinRelayTxFee, err = util.NewAmount(*cfg.MinRelayTxFee)
if err != nil {
Error(err)
str := "%s: invalid minrelaytxfee: %v"
err := fmt.Errorf(str, funcName, err)
_, _ = fmt.Fprintln(os.Stderr, err)
// os.Exit(1)
}
// Limit the max block size to a sane value.
Trace("checking max block size")
if *cfg.BlockMaxSize < node.BlockMaxSizeMin ||
*cfg.BlockMaxSize > node.BlockMaxSizeMax {
str := "%s: The blockmaxsize option must be in between %d and %d -- parsed [%d]"
err := fmt.Errorf(str, funcName, node.BlockMaxSizeMin,
node.BlockMaxSizeMax, *cfg.BlockMaxSize)
_, _ = fmt.Fprintln(os.Stderr, err)
// os.Exit(1)
}
// Limit the max block weight to a sane value.
Trace("checking max block weight")
if *cfg.BlockMaxWeight < node.BlockMaxWeightMin ||
*cfg.BlockMaxWeight > node.BlockMaxWeightMax {
str := "%s: The blockmaxweight option must be in between %d and %d -- parsed [%d]"
err := fmt.Errorf(str, funcName, node.BlockMaxWeightMin,
node.BlockMaxWeightMax, *cfg.BlockMaxWeight)
_, _ = fmt.Fprintln(os.Stderr, err)
// os.Exit(1)
}
// Limit the max orphan count to a sane vlue.
Trace("checking max orphan limit")
if *cfg.MaxOrphanTxs < 0 {
str := "%s: The maxorphantx option may not be less than 0 -- parsed [%d]"
err := fmt.Errorf(str, funcName, *cfg.MaxOrphanTxs)
_, _ = fmt.Fprintln(os.Stderr, err)
// os.Exit(1)
}
// Limit the block priority and minimum block sizes to max block size.
Trace("validating block priority and minimum size/weight")
*cfg.BlockPrioritySize = int(apputil.MinUint32(
uint32(*cfg.BlockPrioritySize),
uint32(*cfg.BlockMaxSize)))
*cfg.BlockMinSize = int(apputil.MinUint32(
uint32(*cfg.BlockMinSize),
uint32(*cfg.BlockMaxSize)))
*cfg.BlockMinWeight = int(apputil.MinUint32(
uint32(*cfg.BlockMinWeight),
uint32(*cfg.BlockMaxWeight)))
switch {
// If the max block size isn't set, but the max weight is, then we'll set the limit for the max block size to a safe
// limit so weight takes precedence.
case *cfg.BlockMaxSize == node.DefaultBlockMaxSize &&
*cfg.BlockMaxWeight != node.DefaultBlockMaxWeight:
*cfg.BlockMaxSize = blockchain.MaxBlockBaseSize - 1000
// If the max block weight isn't set, but the block size is, then we'll scale the set weight accordingly based
// on the max block size value.
case *cfg.BlockMaxSize != node.DefaultBlockMaxSize &&
*cfg.BlockMaxWeight == node.DefaultBlockMaxWeight:
*cfg.BlockMaxWeight = *cfg.BlockMaxSize * blockchain.WitnessScaleFactor
}
// Look for illegal characters in the user agent comments.
Trace("checking user agent comments", cfg.UserAgentComments)
for _, uaComment := range *cfg.UserAgentComments {
if strings.ContainsAny(uaComment, "/:()") {
err := fmt.Errorf("%s: The following characters must not "+
"appear in user agent comments: '/', ':', '(', ')'",
funcName)
_, _ = fmt.Fprintln(os.Stderr, err)
// os.Exit(1)
}
}
// Check the checkpoints for syntax errors.
Trace("checking the checkpoints")
stateConfig.AddedCheckpoints, err = node.ParseCheckpoints(*cfg.
AddCheckpoints)
if err != nil {
Error(err)
str := "%s: Error parsing checkpoints: %v"
err := fmt.Errorf(str, funcName, err)
_, _ = fmt.Fprintln(os.Stderr, err)
// os.Exit(1)
}
}
func validateOnions(cfg *pod.Config) {
// --onionproxy and not --onion are contradictory
// TODO: this is kinda stupid hm? switch *and* toggle by presence of flag value, one should be enough
if *cfg.Onion && *cfg.OnionProxy != "" {
Error("onion enabled but no onionproxy has been configured")
Fatal("halting to avoid exposing IP address")
// os.Exit(1)
}
// Tor stream isolation requires either proxy or onion proxy to be set.
if *cfg.TorIsolation &&
*cfg.Proxy == "" &&
*cfg.OnionProxy == "" {
str := "%s: Tor stream isolation requires either proxy or onionproxy to be set"
err := fmt.Errorf(str, funcName)
_, _ = fmt.Fprintln(os.Stderr, err)
// os.Exit(1)
}
if !*cfg.Onion {
*cfg.OnionProxy = ""
}
}
func validateMiningStuff(cfg *pod.Config, state *state.Config,
params *netparams.Params) {
// Check mining addresses are valid and saved parsed versions.
Trace("checking mining addresses")
state.ActiveMiningAddrs = make([]util.Address, 0, len(*cfg.MiningAddrs))
for _, strAddr := range *cfg.MiningAddrs {
addr, err := util.DecodeAddress(strAddr, params)
if err != nil {
Error(err)
str := "%s: mining address '%s' failed to decode: %v"
err := fmt.Errorf(str, funcName, strAddr, err)
_, _ = fmt.Fprintln(os.Stderr, err)
// os.Exit(1)
continue
}
if !addr.IsForNet(params) {
str := "%s: mining address '%s' is on the wrong network"
err := fmt.Errorf(str, funcName, strAddr)
_, _ = fmt.Fprintln(os.Stderr, err)
// os.Exit(1)
continue
}
state.ActiveMiningAddrs = append(state.ActiveMiningAddrs, addr)
}
// Ensure there is at least one mining address when the generate flag is set.
if (*cfg.Generate) && len(state.ActiveMiningAddrs) == 0 {
Error("the generate flag is set, " +
"but there are no mining addresses specified ")
// Traces(cfg)
*cfg.Generate = false
// os.Exit(1)
}
if *cfg.MinerPass != "" {
state.ActiveMinerKey = forkhash.Argon2i([]byte(*cfg.MinerPass))
}
}
func setDiallers(cfg *pod.Config, stateConfig *state.Config) {
// Setup dial and DNS resolution (lookup) functions depending on the specified options. The default is to use the
// standard net.DialTimeout function as well as the system DNS resolver. When a proxy is specified, the dial
// function is set to the proxy specific dial function and the lookup is set to use tor (unless --noonion is
// specified in which case the system DNS resolver is used).
Trace("setting network dialer and lookup")
stateConfig.Dial = net.DialTimeout
stateConfig.Lookup = net.LookupIP
if *cfg.Proxy != "" {
Trace("we are loading a proxy!")
_, _, err := net.SplitHostPort(*cfg.Proxy)
if err != nil {
Error(err)
str := "%s: Proxy address '%s' is invalid: %v"
err := fmt.Errorf(str, funcName, *cfg.Proxy, err)
_, _ = fmt.Fprintln(os.Stderr, err)
// os.Exit(1)
}
// Tor isolation flag means proxy credentials will be overridden unless there is also an onion proxy configured
// in which case that one will be overridden.
torIsolation := false
if *cfg.TorIsolation &&
*cfg.OnionProxy == "" &&
(*cfg.ProxyUser != "" ||
*cfg.ProxyPass != "") {
torIsolation = true
Warn("Tor isolation set -- overriding specified" +
" proxy user credentials")
}
proxy := &socks.Proxy{
Addr: *cfg.Proxy,
Username: *cfg.ProxyUser,
Password: *cfg.ProxyPass,
TorIsolation: torIsolation,
}
stateConfig.Dial = proxy.DialTimeout
// Treat the proxy as tor and perform DNS resolution through it unless the --noonion flag is set or there is an
// onion-specific proxy configured.
if *cfg.Onion &&
*cfg.OnionProxy == "" {
stateConfig.Lookup = func(host string) ([]net.IP, error) {
return connmgr.TorLookupIP(host, *cfg.Proxy)
}
}
}
// Setup onion address dial function depending on the specified options. The default is to use the same dial
// function selected above. However, when an onion-specific proxy is specified, the onion address dial function is
// set to use the onion-specific proxy while leaving the normal dial function as selected above. This allows .onion
// address traffic to be routed through a different proxy than normal traffic.
Trace("setting up tor proxy if enabled")
if *cfg.OnionProxy != "" {
_, _, err := net.SplitHostPort(*cfg.OnionProxy)
if err != nil {
Error(err)
str := "%s: Onion proxy address '%s' is invalid: %v"
err := fmt.Errorf(str, funcName, *cfg.OnionProxy, err)
_, _ = fmt.Fprintln(os.Stderr, err)
// os.Exit(1)
}
// Tor isolation flag means onion proxy credentials will be overridden.
if *cfg.TorIsolation &&
(*cfg.OnionProxyUser != "" || *cfg.OnionProxyPass != "") {
Warn("Tor isolation set - overriding specified onionproxy user" +
" credentials")
}
}
Trace("setting onion dialer")
stateConfig.Oniondial =
func(network, addr string, timeout time.Duration) (net.Conn, error) {
proxy := &socks.Proxy{
Addr: *cfg.OnionProxy,
Username: *cfg.OnionProxyUser,
Password: *cfg.OnionProxyPass,
TorIsolation: *cfg.TorIsolation,
}
return proxy.DialTimeout(network, addr, timeout)
}
// When configured in bridge mode (both --onion and --proxy are configured), it means that the proxy configured by
// --proxy is not a tor proxy, so override the DNS resolution to use the onion-specific proxy.
Trace("setting proxy lookup")
if *cfg.Proxy != "" {
stateConfig.Lookup = func(host string) ([]net.IP, error) {
return connmgr.TorLookupIP(host, *cfg.OnionProxy)
}
} else {
stateConfig.Oniondial = stateConfig.Dial
}
// Specifying --noonion means the onion address dial function results in an error.
if !*cfg.Onion {
stateConfig.Oniondial = func(a, b string, t time.Duration) (net.Conn, error) {
return nil, errors.New("tor has been disabled")
}
}
}