sanitizers.go raw
1 package podconfig
2
3 const (
4 appName = "pod"
5 confExt = ".json"
6 appLanguage = "en"
7 podConfigFilename = appName + confExt
8 PARSER = "json"
9 )
10
11 var funcName = "loadConfig"
12
13 // func initDictionary(cfg *opts.Config) {
14 // if cfg.Locale == nil || cfg.Locale.V() == "" {
15 // cfg.Locale.Set(Lang("en"))
16 // }
17 // T.Ln("lang set to", *cfg.Locale)
18 // }
19
20 // func validatePort(port string) bool {
21 // var e error
22 // var p int64
23 // if p, e = strconv.ParseInt(port, 10, 32); E.Chk(e) {
24 // return false
25 // }
26 // if p < 1024 || p > 65535 {
27 // return false
28 // }
29 // return true
30 // }
31
32 // func initListeners(cx *pod.State, commandName string, initial bool) {
33 // cfg := cx.Config
34 // var e error
35 // var fP int
36 // if fP, e = GetFreePort(); E.Chk(e) {
37 // }
38 // // if cfg.AutoListen.True() {
39 // // _, allAddresses := routeable.GetAddressesAndInterfaces()
40 // // p2pAddresses := cli.StringSlice{}
41 // // for addr := range allAddresses {
42 // // p2pAddresses = append(p2pAddresses, net.JoinHostPort(addr, cx.ActiveNet.DefaultPort))
43 // // }
44 // // cfg.P2PConnect.Set(p2pAddresses)
45 // // }
46 // // if cfg.P2PListeners.Len() < 1 && !cfg.DisableListen.True() && cfg.ConnectPeers.Len() < 1 {
47 // // cfg.P2PListeners.Set([]string{fmt.Sprintf("0.0.0.0:" + cx.ActiveNet.DefaultPort)})
48 // // cx.StateCfg.Save = true
49 // // D.Ln("P2PListeners")
50 // // }
51 // // if cfg.RPCListeners.Len() < 1 {
52 // // address := fmt.Sprintf("127.0.0.1:%s", cx.ActiveNet.RPCClientPort)
53 // // cfg.RPCListeners.Set([]string{address})
54 // // cfg.RPCConnect.Set(fmt.Sprintf("127.0.0.1:%s", cx.ActiveNet.RPCClientPort))
55 // // D.Ln("setting save flag because rpc listeners is empty and rpc is not disabled")
56 // // cx.StateCfg.Save = true
57 // // D.Ln("RPCListeners")
58 // // }
59 // // if cfg.WalletRPCListeners.Len() < 1 && !cfg.DisableRPC.True() {
60 // // address := fmt.Sprintf("127.0.0.1:" + cx.ActiveNet.WalletRPCServerPort)
61 // // cfg.WalletRPCListeners.Set([]string{address})
62 // // cfg.WalletServer.Set(address)
63 // // D.Ln(
64 // // "setting save flag because wallet rpc listeners is empty and" +
65 // // " rpc is not disabled",
66 // // )
67 // // cx.StateCfg.Save = true
68 // // D.Ln("WalletRPCListeners")
69 // // }
70 // // if cx.Config.AutoPorts.True() || !initial {
71 // // if fP, e = GetFreePort(); E.Chk(e) {
72 // // }
73 // // cfg.P2PListeners.Set([]string{"0.0.0.0:" + fmt.Sprint(fP)})
74 // // if fP, e = GetFreePort(); E.Chk(e) {
75 // // }
76 // // cfg.RPCListeners.Set([]string{"127.0.0.1:" + fmt.Sprint(fP)})
77 // // if fP, e = GetFreePort(); E.Chk(e) {
78 // // }
79 // // cfg.WalletRPCListeners.Set([]string{"127.0.0.1:" + fmt.Sprint(fP)})
80 // // cx.StateCfg.Save = true
81 // // D.Ln("autoports")
82 // // } else {
83 // // sanitize user input and set auto on any that fail
84 // // l := cfg.P2PListeners.S()
85 // // r := cfg.RPCListeners.S()
86 // // w := cfg.WalletRPCListeners.S()
87 // // for i := range l {
88 // // if _, p, e := net.SplitHostPort((l)[i]); !E.Chk(e) {
89 // // if !validatePort(p) {
90 // // if fP, e = GetFreePort(); E.Chk(e) {
91 // // }
92 // // l[i] = "0.0.0.0:" + fmt.Sprint(fP)
93 // // cx.StateCfg.Save = true
94 // // D.Ln("port not validate P2PListeners")
95 // // }
96 // // }
97 // // }
98 // // for i := range r {
99 // // if _, p, e := net.SplitHostPort((r)[i]); !E.Chk(e) {
100 // // if !validatePort(p) {
101 // // if fP, e = GetFreePort(); E.Chk(e) {
102 // // }
103 // // r[i] = "127.0.0.1:" + fmt.Sprint(fP)
104 // // cx.StateCfg.Save = true
105 // // D.Ln("port not validate RPCListeners")
106 // // }
107 // // }
108 // // }
109 // // for i := range w {
110 // // if _, p, e := net.SplitHostPort((w)[i]); !E.Chk(e) {
111 // // if !validatePort(p) {
112 // // if fP, e = GetFreePort(); E.Chk(e) {
113 // // }
114 // // w[i] = "127.0.0.1:" + fmt.Sprint(fP)
115 // // cx.StateCfg.Save = true
116 // // D.Ln("port not validate WalletRPCListeners")
117 // // }
118 // // }
119 // // }
120 // }
121 // // if cfg.LAN.True() && cx.ActiveNet.Name != "mainnet" {
122 // // cfg.DisableDNSSeed.T()
123 // // }
124 // // if cfg.WalletRPCListeners.Len() > 0 {
125 // // cfg.WalletServer.Set(cfg.WalletRPCListeners.S()[0])
126 // // }
127 // }
128
129 // // GetFreePort asks the kernel for free open ports that are ready to use.
130 // func GetFreePort() (int, error) {
131 // var port int
132 // addr, e := net.ResolveTCPAddr("tcp", "localhost:0")
133 // if e != nil {
134 // return 0, e
135 // }
136 // var l *net.TCPListener
137 // l, e = net.ListenTCP("tcp", addr)
138 // if e != nil {
139 // return 0, e
140 // }
141 // defer func() {
142 // if e := l.Close(); E.Chk(e) {
143 // }
144 // }()
145 // port = l.Addr().(*net.TCPAddr).Port
146 // return port, nil
147 // }
148
149 // func normalizeAddresses(cfg *opts.Config) {
150 // T.Ln("normalising addresses")
151 // port := fmt.Sprint(constant.DefaultP2PPort)
152 // nrm := normalize.StringSliceAddresses
153 // nrm(cfg.AddPeers.V(), port)
154 // nrm(cfg.ConnectPeers.V(), port)
155 // nrm(cfg.P2PListeners.V(), port)
156 // nrm(cfg.Whitelists.V(), port)
157 // // nrm(cfg.RPCListeners, port)
158 // }
159
160 // func configListener(cfg *opts.Config, params *chaincfg.Params) {
161 // // --proxy or --connect without --listen disables listening.
162 // T.Ln("checking proxy/connect for disabling listening")
163 // if (cfg.ProxyAddress.V() != "" ||
164 // cfg.ConnectPeers.Len() > 0) &&
165 // cfg.P2PListeners.Len() == 0 {
166 // cfg.DisableListen.T()
167 // }
168 // // Add the default listener if none were specified. The default listener is all
169 // // addresses on the listen port for the network we are to connect to.
170 // T.Ln("checking if listener was set")
171 // if cfg.P2PListeners.Len() == 0 {
172 // cfg.P2PListeners.Set([]string{"0.0.0.0:" + params.DefaultPort})
173 // }
174 // }
175
176 // func configRPC(cfg *opts.Config, params *chaincfg.Params) {
177 // // // The RPC server is disabled if no username or password is provided.
178 // // // T.Ln("checking rpc server has a login enabled")
179 // // // if (cfg.Username.Empty() || cfg.Password.Empty()) &&
180 // // // (cfg.LimitUser.Empty() || cfg.LimitPass.Empty()) {
181 // // // cfg.DisableRPC.T()
182 // // // }
183 // // // if cfg.DisableRPC.True() {
184 // // // T.Ln("RPC service is disabled")
185 // // // }
186 // // T.Ln("checking rpc server has listeners set")
187 // // if cfg.DisableRPC.False() && cfg.RPCListeners.Len() == 0 {
188 // // D.Ln("looking up default listener")
189 // // addrs, e := net.LookupHost(constant.DefaultRPCListener)
190 // // if e != nil {
191 // // E.Ln(e)
192 // // // os.Exit(1)
193 // // }
194 // // tmp := make([]string, 0, len(addrs))
195 // // D.Ln("setting listeners")
196 // // for _, addr := range addrs {
197 // // tmp = append(tmp, addr)
198 // // addr = net.JoinHostPort(addr, params.RPCClientPort)
199 // // }
200 // // cfg.RPCListeners.Set(tmp)
201 // // }
202 // // // T.Ln("checking rpc max concurrent requests")
203 // // // if cfg.RPCMaxConcurrentReqs.V() < 0 {
204 // // // str := "%s: The rpcmaxwebsocketconcurrentrequests opt may not be" +
205 // // // " less than 0 -- parsed [%d]"
206 // // // e := fmt.Errorf(str, funcName, cfg.RPCMaxConcurrentReqs.V())
207 // // // _, _ = fmt.Fprintln(os.Stderr, e)
208 // // // // os.Exit(1)
209 // // // }
210 // // T.Ln("checking rpc listener addresses")
211 // // nrms := normalize.Addresses
212 // // // Add default port to all added peer addresses if needed and remove duplicate addresses.
213 // // cfg.AddPeers.Set(nrms(cfg.AddPeers.S(), params.DefaultPort))
214 // // cfg.ConnectPeers.Set(nrms(cfg.ConnectPeers.S(), params.DefaultPort))
215 // }
216
217 // func validatePolicies(cfg *opts.Config, stateConfig *state.Config) {
218 // // var e error
219 // // // Validate the the minrelaytxfee.
220 // // T.Ln("checking min relay tx fee")
221 // // stateConfig.ActiveMinRelayTxFee, e = amt.NewAmount(cfg.MinRelayTxFee.V())
222 // // if e != nil {
223 // // E.Ln(e)
224 // // str := "%s: invalid minrelaytxfee: %v"
225 // // e = fmt.Errorf(str, funcName, e)
226 // // _, _ = fmt.Fprintln(os.Stderr, e)
227 // // }
228 // // // Limit the max block size to a sane value.
229 // // T.Ln("checking max block size")
230 // // if cfg.BlockMaxSize.V() < constant.BlockMaxSizeMin ||
231 // // cfg.BlockMaxSize.V() > constant.BlockMaxSizeMax {
232 // // str := "%s: The blockmaxsize opt must be in between %d and %d -- parsed [%d]"
233 // // e = fmt.Errorf(
234 // // str, funcName, constant.BlockMaxSizeMin,
235 // // constant.BlockMaxSizeMax, cfg.BlockMaxSize.V(),
236 // // )
237 // // _, _ = fmt.Fprintln(os.Stderr, e)
238 // // }
239 // // Limit the max block weight to a sane value.
240 // // T.Ln("checking max block weight")
241 // // if cfg.BlockMaxWeight.V() < constant.BlockMaxWeightMin ||
242 // // cfg.BlockMaxWeight.V() > constant.BlockMaxWeightMax {
243 // // str := "%s: The blockmaxweight opt must be in between %d and %d -- parsed [%d]"
244 // // e = fmt.Errorf(
245 // // str, funcName, constant.BlockMaxWeightMin,
246 // // constant.BlockMaxWeightMax, cfg.BlockMaxWeight.V(),
247 // // )
248 // // _, _ = fmt.Fprintln(os.Stderr, e)
249 // // }
250 // // Limit the max orphan count to a sane vlue.
251 // // T.Ln("checking max orphan limit")
252 // // if cfg.MaxOrphanTxs.V() < 0 {
253 // // str := "%s: The maxorphantx opt may not be less than 0 -- parsed [%d]"
254 // // e = fmt.Errorf(str, funcName, cfg.MaxOrphanTxs.V())
255 // // _, _ = fmt.Fprintln(os.Stderr, e)
256 // // }
257 // // // Limit the block priority and minimum block txsizes to max block size.
258 // // T.Ln("validating block priority and minimum size/weight")
259 // // cfg.BlockPrioritySize.Set(
260 // // int(
261 // // apputil.MinUint32(
262 // // uint32(cfg.BlockPrioritySize.V()),
263 // // uint32(cfg.BlockMaxSize.V()),
264 // // ),
265 // // ),
266 // // )
267 // // cfg.BlockMinSize.Set(
268 // // int(
269 // // apputil.MinUint32(
270 // // uint32(cfg.BlockMinSize.V()),
271 // // uint32(cfg.BlockMaxSize.V()),
272 // // ),
273 // // ),
274 // // )
275 // // cfg.BlockMinWeight.Set(
276 // // int(
277 // // apputil.MinUint32(
278 // // uint32(cfg.BlockMinWeight.V()),
279 // // uint32(cfg.BlockMaxWeight.V()),
280 // // ),
281 // // ),
282 // // )
283 // // switch {
284 // // // If the max block size isn't set, but the max weight is, then we'll set the
285 // // // limit for the max block size to a safe limit so weight takes precedence.
286 // // case cfg.BlockMaxSize.V() == constant.DefaultBlockMaxSize &&
287 // // cfg.BlockMaxWeight.V() != constant.DefaultBlockMaxWeight:
288 // // cfg.BlockMaxSize.Set(blockchain.MaxBlockBaseSize - 1000)
289 // // // If the max block weight isn't set, but the block size is, then we'll scale
290 // // // the set weight accordingly based on the max block size value.
291 // // case cfg.BlockMaxSize.V() != constant.DefaultBlockMaxSize &&
292 // // cfg.BlockMaxWeight.V() == constant.DefaultBlockMaxWeight:
293 // // cfg.BlockMaxWeight.Set(cfg.BlockMaxSize.V() * blockchain.WitnessScaleFactor)
294 // // }
295 // // Look for illegal characters in the user agent comments.
296 // // T.Ln("checking user agent comments", cfg.UserAgentComments)
297 // // for _, uaComment := range cfg.UserAgentComments.S() {
298 // // if strings.ContainsAny(uaComment, "/:()") {
299 // // e = fmt.Errorf(
300 // // "%s: The following characters must not "+
301 // // "appear in user agent comments: '/', ':', '(', ')'",
302 // // funcName,
303 // // )
304 // // _, _ = fmt.Fprintln(os.Stderr, e)
305 // // }
306 // // }
307 // // // Chk the checkpoints for syntax errors.
308 // // T.Ln("checking the checkpoints")
309 // // stateConfig.AddedCheckpoints, e = ParseCheckpoints(
310 // // cfg.AddCheckpoints.S(),
311 // // )
312 // // if e != nil {
313 // // E.Ln(e)
314 // // str := "%s: err parsing checkpoints: %v"
315 // // e = fmt.Errorf(str, funcName, e)
316 // // _, _ = fmt.Fprintln(os.Stderr, e)
317 // // }
318 // }
319
320 // func validateOnions(cfg *opts.Config) {
321 // // --onionproxy and not --onion are contradictory
322 // // TODO: this is kinda stupid hm? switch *and* toggle by presence of flag value, one should be enough
323 // if cfg.OnionEnabled.True() && !cfg.OnionProxyAddress.Empty() {
324 // E.Ln("onion enabled but no onionproxy has been configured")
325 // T.Ln("halting to avoid exposing IP address")
326 // }
327 // // Tor stream isolation requires either proxy or onion proxy to be set.
328 // if cfg.TorIsolation.True() &&
329 // cfg.ProxyAddress.Empty() &&
330 // cfg.OnionProxyAddress.Empty() {
331 // str := "%s: Tor stream isolation requires either proxy or onionproxy to be set"
332 // e := fmt.Errorf(str, funcName)
333 // _, _ = fmt.Fprintln(os.Stderr, e)
334 // // os.Exit(1)
335 // }
336 // if cfg.OnionEnabled.False() {
337 // cfg.OnionProxyAddress.Set("")
338 // }
339 //
340 // }
341
342 // func setDiallers(cfg *opts.Config, stateConfig *state.Config) {
343 // // Setup dial and DNS resolution (lookup) functions depending on the specified
344 // // options. The default is to use the standard net.DialTimeout function as well
345 // // as the system DNS resolver. When a proxy is specified, the dial function is
346 // // set to the proxy specific dial function and the lookup is set to use tor
347 // // (unless --noonion is specified in which case the system DNS resolver is
348 // // used).
349 // T.Ln("setting network dialer and lookup")
350 // stateConfig.Dial = net.DialTimeout
351 // stateConfig.Lookup = net.LookupIP
352 // var e error
353 // if !cfg.ProxyAddress.Empty() {
354 // T.Ln("we are loading a proxy!")
355 // _, _, e = net.SplitHostPort(cfg.ProxyAddress.V())
356 // if e != nil {
357 // E.Ln(e)
358 // str := "%s: Proxy address '%s' is invalid: %v"
359 // e = fmt.Errorf(str, funcName, cfg.ProxyAddress.V(), e)
360 // fmt.Fprintln(os.Stderr, e)
361 // // os.Exit(1)
362 // }
363 // // Tor isolation flag means proxy credentials will be overridden unless there is
364 // // also an onion proxy configured in which case that one will be overridden.
365 // torIsolation := false
366 // if cfg.TorIsolation.True() && cfg.OnionProxyAddress.Empty() &&
367 // (!cfg.ProxyUser.Empty() || !cfg.ProxyPass.Empty()) {
368 // torIsolation = true
369 // W.Ln(
370 // "Tor isolation set -- overriding specified" +
371 // " proxy user credentials",
372 // )
373 // }
374 // proxy := &socks.Proxy{
375 // Addr: cfg.ProxyAddress.V(),
376 // Username: cfg.ProxyUser.V(),
377 // Password: cfg.ProxyPass.V(),
378 // TorIsolation: torIsolation,
379 // }
380 // stateConfig.Dial = proxy.DialTimeout
381 // // Treat the proxy as tor and perform DNS resolution through it unless the
382 // // --noonion flag is set or there is an onion-specific proxy configured.
383 // if cfg.OnionEnabled.True() &&
384 // cfg.OnionProxyAddress.Empty() {
385 // stateConfig.Lookup = func(host string) ([]net.IP, error) {
386 // return connmgr.TorLookupIP(host, cfg.ProxyAddress.V())
387 // }
388 // }
389 // }
390 // // Setup onion address dial function depending on the specified options. The
391 // // default is to use the same dial function selected above. However, when an
392 // // onion-specific proxy is specified, the onion address dial function is set to
393 // // use the onion-specific proxy while leaving the normal dial function as
394 // // selected above. This allows .onion address traffic to be routed through a
395 // // different proxy than normal traffic.
396 // T.Ln("setting up tor proxy if enabled")
397 // if !cfg.OnionProxyAddress.Empty() {
398 // if _, _, e = net.SplitHostPort(cfg.OnionProxyAddress.V()); E.Chk(e) {
399 // e = fmt.Errorf("%s: Onion proxy address '%s' is invalid: %v",
400 // funcName, cfg.OnionProxyAddress.V(), e,
401 // )
402 // // _, _ = fmt.Fprintln(os.Stderr, e)
403 // }
404 // // Tor isolation flag means onion proxy credentials will be overridden.
405 // if cfg.TorIsolation.True() &&
406 // (!cfg.OnionProxyUser.Empty() || !cfg.OnionProxyPass.Empty()) {
407 // W.Ln(
408 // "Tor isolation set - overriding specified onionproxy user" +
409 // " credentials",
410 // )
411 // }
412 // }
413 // T.Ln("setting onion dialer")
414 // stateConfig.Oniondial =
415 // func(network, addr string, timeout time.Duration) (net.Conn, error) {
416 // proxy := &socks.Proxy{
417 // Addr: cfg.OnionProxyAddress.V(),
418 // Username: cfg.OnionProxyUser.V(),
419 // Password: cfg.OnionProxyPass.V(),
420 // TorIsolation: cfg.TorIsolation.True(),
421 // }
422 // return proxy.DialTimeout(network, addr, timeout)
423 // }
424 //
425 // // When configured in bridge mode (both --onion and --proxy are configured), it
426 // // means that the proxy configured by --proxy is not a tor proxy, so override
427 // // the DNS resolution to use the onion-specific proxy.
428 // T.Ln("setting proxy lookup")
429 // if !cfg.ProxyAddress.Empty() {
430 // stateConfig.Lookup = func(host string) ([]net.IP, error) {
431 // return connmgr.TorLookupIP(host, cfg.OnionProxyAddress.V())
432 // }
433 // } else {
434 // stateConfig.Oniondial = stateConfig.Dial
435 // }
436 // // Specifying --noonion means the onion address dial function results in an error.
437 // if cfg.OnionEnabled.False() {
438 // stateConfig.Oniondial = func(a, b string, t time.Duration) (net.Conn, error) {
439 // return nil, errors.New("tor has been disabled")
440 // }
441 // }
442 // }
443
444 // func validateMiningStuff(
445 // cfg *opts.Config, state *state.Config,
446 // params *chaincfg.Params,
447 // ) {
448 // if state == nil {
449 // panic("state is nil")
450 // }
451 // // // Chk mining addresses are valid and saved parsed versions.
452 // // T.Ln("checking mining addresses")
453 // // aml := 99
454 // // if cfg.MiningAddrs != nil {
455 // // aml = cfg.MiningAddrs.Len()
456 // // } else {
457 // // D.Ln("MiningAddrs is nil")
458 // // return
459 // // }
460 // // state.ActiveMiningAddrs = make([]btcaddr.Address, 0, aml)
461 // // for _, strAddr := range cfg.MiningAddrs.S() {
462 // // addr, e := btcaddr.Decode(strAddr, params)
463 // // if e != nil {
464 // // E.Ln(e)
465 // // str := "%s: mining address '%s' failed to decode: %v"
466 // // e = fmt.Errorf(str, funcName, strAddr, e)
467 // // _, _ = fmt.Fprintln(os.Stderr, e)
468 // // // os.Exit(1)
469 // // continue
470 // // }
471 // // if !addr.IsForNet(params) {
472 // // str := "%s: mining address '%s' is on the wrong network"
473 // // e := fmt.Errorf(str, funcName, strAddr)
474 // // _, _ = fmt.Fprintln(os.Stderr, e)
475 // // // os.Exit(1)
476 // // continue
477 // // }
478 // // state.ActiveMiningAddrs = append(state.ActiveMiningAddrs, addr)
479 // // }
480 // if cfg.MulticastPass.Empty() {
481 // D.Ln("--------------- generating new miner key")
482 // cfg.MulticastPass.Set(hex.EncodeToString(forkhash.Argon2i(cfg.MulticastPass.Bytes())))
483 // state.ActiveMinerKey = cfg.MulticastPass.Bytes()
484 // }
485 // }
486
487 // func validateUsers(cfg *opts.Config) {
488 // // Chk to make sure limited and admin users don't have the same username
489 // T.Ln("checking admin and limited username is different")
490 // if !cfg.Username.Empty() &&
491 // cfg.Username.V() == cfg.LimitUser.V() {
492 // str := "%s: --username and --limituser must not specify the same username"
493 // e := fmt.Errorf(str, funcName)
494 // _, _ = fmt.Fprintln(os.Stderr, e)
495 // }
496 // // Chk to make sure limited and admin users don't have the same password
497 // T.Ln("checking limited and admin passwords are not the same")
498 // if !cfg.Password.Empty() &&
499 // cfg.Password.V() == cfg.LimitPass.V() {
500 // str := "%s: --password and --limitpass must not specify the same password"
501 // e := fmt.Errorf(str, funcName)
502 // _, _ = fmt.Fprintln(os.Stderr, e)
503 // // os.Exit(1)
504 // }
505 // }
506
507 // func initDataDir(cfg *opts.Config) {
508 // if cfg.DataDir == nil || cfg.DataDir.V() == "" {
509 // D.Ln("setting default data dir")
510 // cfg.DataDir.Set(appdata.Dir("pod", false))
511 // }
512 // T.Ln("datadir set to", *cfg.DataDir)
513 // }
514
515 // func initWalletFile(cx *pod.State) {
516 // if cx.Config.WalletFile == nil || cx.Config.WalletFile.V() == "" {
517 // cx.Config.WalletFile.Set(filepath.Join(cx.Config.DataDir.V(), cx.ActiveNet.Name, constant.DbName))
518 // }
519 // T.Ln("wallet file set to", *cx.Config.WalletFile, *cx.Config.Network)
520 // }
521
522 // func initConfigFile(cfg *opts.Config) {
523 // if cfg.ConfigFile.V() == "" {
524 // cfg.ConfigFile.Set(filepath.Join(cfg.DataDir.V(), podConfigFilename))
525 // }
526 // T.Ln("using config file:", *cfg.ConfigFile)
527 // }
528
529 // func initLogDir(cfg *opts.Config) {
530 // if cfg.LogDir.V() != "" {
531 // // logi.L.SetLogPaths(*cfg.LogDir, "pod")
532 // interrupt.AddHandler(
533 // func() {
534 // D.Ln("initLogDir interrupt")
535 // // _ = logi.L.LogFileHandle.Close()
536 // },
537 // )
538 // }
539 // }
540
541 // func initParams(cx *pod.State) {
542 // network := "mainnet"
543 // if cx.Config.Network != nil {
544 // network = cx.Config.Network.V()
545 // }
546 // switch network {
547 // case "testnet", "testnet3", "t":
548 // T.Ln("on testnet")
549 // cx.ActiveNet = &chaincfg.TestNet3Params
550 // fork.IsTestnet = true
551 // case "regtestnet", "regressiontest", "r":
552 // T.Ln("on regression testnet")
553 // cx.ActiveNet = &chaincfg.RegressionTestParams
554 // case "simnet", "s":
555 // T.Ln("on simnet")
556 // cx.ActiveNet = &chaincfg.SimNetParams
557 // default:
558 // if network != "mainnet" && network != "m" {
559 // D.Ln("using mainnet for node")
560 // }
561 // T.Ln("on mainnet")
562 // cx.ActiveNet = &chaincfg.MainNetParams
563 // }
564 // }
565
566 // func initTLSStuffs(cfg *opts.Config, st *state.Config) {
567 // isNew := false
568 // if cfg.RPCCert.V() == "" {
569 // cfg.RPCCert.Set(filepath.Join(cfg.DataDir.V(), "rpc.cert"))
570 // D.Ln("setting save flag because rpc cert path was not set")
571 // st.Save = true
572 // isNew = true
573 // }
574 // if cfg.RPCKey.V() == "" {
575 // cfg.RPCKey.Set(filepath.Join(cfg.DataDir.V(), "rpc.key"))
576 // D.Ln("setting save flag because rpc key path was not set")
577 // st.Save = true
578 // isNew = true
579 // }
580 // if cfg.CAFile.V() == "" {
581 // cfg.CAFile.Set(filepath.Join(cfg.DataDir.V(), "ca.cert"))
582 // D.Ln("setting save flag because CA cert path was not set")
583 // st.Save = true
584 // isNew = true
585 // }
586 // if isNew {
587 // // Now is the best time to make the certs
588 // I.Ln("generating TLS certificates")
589 // // Create directories for cert and key files if they do not yet exist.
590 // D.Ln("rpc tls ", *cfg.RPCCert, " ", *cfg.RPCKey)
591 // certDir, _ := filepath.Split(cfg.RPCCert.V())
592 // keyDir, _ := filepath.Split(cfg.RPCKey.V())
593 // var e error
594 // e = os.MkdirAll(certDir, 0700)
595 // if e != nil {
596 // E.Ln(e)
597 // return
598 // }
599 // e = os.MkdirAll(keyDir, 0700)
600 // if e != nil {
601 // E.Ln(e)
602 // return
603 // }
604 // // Generate cert pair.
605 // org := "pod/wallet autogenerated cert"
606 // validUntil := time.Now().Add(time.Hour * 24 * 365 * 10)
607 // cert, key, e := util.NewTLSCertPair(org, validUntil, nil)
608 // if e != nil {
609 // E.Ln(e)
610 // return
611 // }
612 // _, e = tls.X509KeyPair(cert, key)
613 // if e != nil {
614 // E.Ln(e)
615 // return
616 // }
617 // // Write cert and (potentially) the key files.
618 // e = ioutil.WriteFile(cfg.RPCCert.V(), cert, 0600)
619 // if e != nil {
620 // rmErr := os.Remove(cfg.RPCCert.V())
621 // if rmErr != nil {
622 // E.Ln("cannot remove written certificates:", rmErr)
623 // }
624 // return
625 // }
626 // e = ioutil.WriteFile(cfg.CAFile.V(), cert, 0600)
627 // if e != nil {
628 // rmErr := os.Remove(cfg.RPCCert.V())
629 // if rmErr != nil {
630 // E.Ln("cannot remove written certificates:", rmErr)
631 // }
632 // return
633 // }
634 // e = ioutil.WriteFile(cfg.RPCKey.V(), key, 0600)
635 // if e != nil {
636 // E.Ln(e)
637 // rmErr := os.Remove(cfg.RPCCert.V())
638 // if rmErr != nil {
639 // E.Ln("cannot remove written certificates:", rmErr)
640 // }
641 // rmErr = os.Remove(cfg.CAFile.V())
642 // if rmErr != nil {
643 // E.Ln("cannot remove written certificates:", rmErr)
644 // }
645 // return
646 // }
647 // I.Ln("done generating TLS certificates")
648 // return
649 // }
650 // }
651
652 // func initLogLevel(cfg *opts.Config) {
653 // loglevel := cfg.LogLevel.V()
654 // switch loglevel {
655 // case "trace", "debug", "info", "warn", "error", "fatal", "off":
656 // D.Ln("log level", loglevel)
657 // default:
658 // E.Ln("unrecognised loglevel", loglevel, "setting default info")
659 // cfg.LogLevel.Set("info")
660 // }
661 // log.SetLogLevel(cfg.LogLevel.V())
662 // }
663
664 // func setRelayReject(cfg *opts.Config) {
665 // relayNonStd := *cfg.RelayNonStd
666 // switch {
667 // case cfg.RelayNonStd.True() && cfg.RejectNonStd.True():
668 // errf := "%s: rejectnonstd and relaynonstd cannot be used together" +
669 // " -- choose only one, leaving neither activated"
670 // E.Ln(errf, funcName)
671 // // just leave both false
672 // cfg.RelayNonStd.F()
673 // cfg.RejectNonStd.F()
674 // case cfg.RejectNonStd.True():
675 // relayNonStd.F()
676 // case cfg.RelayNonStd.True():
677 // relayNonStd.F()
678 // }
679 // *cfg.RelayNonStd = relayNonStd
680 // }
681 //
682 // func validateDBtype(cfg *opts.Config) {
683 // // Validate database type.
684 // T.Ln("validating database type")
685 // if !ValidDbType(cfg.DbType.V()) {
686 // str := "%s: The specified database type [%v] is invalid -- " +
687 // "supported types %v"
688 // e := fmt.Errorf(str, funcName, *cfg.DbType, KnownDbTypes)
689 // E.Ln(funcName, e)
690 // // set to default
691 // cfg.DbType.Set(KnownDbTypes[0])
692 // }
693 // }
694
695 // func validateProfilePort(cfg *opts.Config) {
696 // // Validate profile port number
697 // T.Ln("validating profile port number")
698 // if cfg.Profile.V() != "" {
699 // profilePort, e := strconv.Atoi(cfg.Profile.V())
700 // if e != nil || profilePort < 1024 || profilePort > 65535 {
701 // str := "%s: The profile port must be between 1024 and 65535"
702 // e = fmt.Errorf(str, funcName)
703 // E.Ln(funcName, e)
704 // cfg.Profile.Set("")
705 // }
706 // }
707 // }
708
709 // func validateBanDuration(cfg *opts.Config) {
710 // // Don't allow ban durations that are too short.
711 // T.Ln("validating ban duration")
712 // if cfg.BanDuration.V() < time.Second {
713 // e := fmt.Errorf(
714 // "%s: The banduration opt may not be less than 1s -- parsed [%v]",
715 // funcName, *cfg.BanDuration,
716 // )
717 // I.Ln(funcName, e)
718 // cfg.BanDuration.Set(constant.DefaultBanDuration)
719 // }
720 // }
721 //
722 // func validateWhitelists(cfg *opts.Config, st *state.Config) {
723 // // Validate any given whitelisted IP addresses and networks.
724 // T.Ln("validating whitelists")
725 // if cfg.Whitelists.Len() > 0 {
726 // var ip net.IP
727 // st.ActiveWhitelists = make([]*net.IPNet, 0, cfg.Whitelists.Len())
728 // for _, addr := range cfg.Whitelists.S() {
729 // _, ipnet, e := net.ParseCIDR(addr)
730 // if e != nil {
731 // E.Ln(e)
732 // ip = net.ParseIP(addr)
733 // if ip == nil {
734 // str := e.Error() + " %s: The whitelist value of '%s' is invalid"
735 // e = fmt.Errorf(str, funcName, addr)
736 // E.Ln(e)
737 // _, _ = fmt.Fprintln(os.Stderr, e)
738 // interrupt.Request()
739 // // os.Exit(1)
740 // } else {
741 // var bits int
742 // if ip.To4() == nil {
743 // // IPv6
744 // bits = 128
745 // } else {
746 // bits = 32
747 // }
748 // ipnet = &net.IPNet{
749 // IP: ip,
750 // Mask: net.CIDRMask(bits, bits),
751 // }
752 // }
753 // }
754 // st.ActiveWhitelists = append(st.ActiveWhitelists, ipnet)
755 // }
756 // }
757 // }
758
759 // func validatePeerLists(cfg *opts.Config) {
760 // T.Ln("checking addpeer and connectpeer lists")
761 // if cfg.AddPeers.Len() > 0 && cfg.ConnectPeers.Len() > 0 {
762 // e := fmt.Errorf(
763 // "%s: the --addpeer and --connect options can not be mixed",
764 // funcName,
765 // )
766 // _, _ = fmt.Fprintln(os.Stderr, e)
767 // // os.Exit(1)
768 // }
769 // }
770