main.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. // bootnode runs a bootstrap node for the Ethereum Discovery Protocol.
  17. package main
  18. import (
  19. "crypto/ecdsa"
  20. "flag"
  21. "fmt"
  22. "os"
  23. "github.com/ethereum/go-ethereum/crypto"
  24. "github.com/ethereum/go-ethereum/log"
  25. "github.com/ethereum/go-ethereum/p2p/discover"
  26. "github.com/ethereum/go-ethereum/p2p/discv5"
  27. "github.com/ethereum/go-ethereum/p2p/nat"
  28. "github.com/ethereum/go-ethereum/p2p/netutil"
  29. )
  30. func main() {
  31. var (
  32. listenAddr = flag.String("addr", ":30301", "listen address")
  33. genKey = flag.String("genkey", "", "generate a node key")
  34. writeAddr = flag.Bool("writeaddress", false, "write out the node's pubkey hash and quit")
  35. nodeKeyFile = flag.String("nodekey", "", "private key filename")
  36. nodeKeyHex = flag.String("nodekeyhex", "", "private key as hex (for testing)")
  37. natdesc = flag.String("nat", "none", "port mapping mechanism (any|none|upnp|pmp|extip:<IP>)")
  38. netrestrict = flag.String("netrestrict", "", "restrict network communication to the given IP networks (CIDR masks)")
  39. runv5 = flag.Bool("v5", false, "run a v5 topic discovery bootnode")
  40. verbosity = flag.Int("verbosity", int(log.LvlInfo), "log verbosity (0-9)")
  41. vmodule = flag.String("vmodule", "", "log verbosity pattern")
  42. nodeKey *ecdsa.PrivateKey
  43. err error
  44. )
  45. flag.Parse()
  46. glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat()))
  47. glogger.Verbosity(log.Lvl(*verbosity))
  48. glogger.Vmodule(*vmodule)
  49. log.Root().SetHandler(glogger)
  50. natm, err := nat.Parse(*natdesc)
  51. if err != nil {
  52. log.Crit(fmt.Sprintf("-nat: %v", err))
  53. }
  54. switch {
  55. case *genKey != "":
  56. nodeKey, err = crypto.GenerateKey()
  57. if err != nil {
  58. log.Crit(fmt.Sprintf("could not generate key: %v", err))
  59. }
  60. if err = crypto.SaveECDSA(*genKey, nodeKey); err != nil {
  61. log.Crit(fmt.Sprintf("%v", err))
  62. }
  63. case *nodeKeyFile == "" && *nodeKeyHex == "":
  64. log.Crit(fmt.Sprintf("Use -nodekey or -nodekeyhex to specify a private key"))
  65. case *nodeKeyFile != "" && *nodeKeyHex != "":
  66. log.Crit(fmt.Sprintf("Options -nodekey and -nodekeyhex are mutually exclusive"))
  67. case *nodeKeyFile != "":
  68. if nodeKey, err = crypto.LoadECDSA(*nodeKeyFile); err != nil {
  69. log.Crit(fmt.Sprintf("-nodekey: %v", err))
  70. }
  71. case *nodeKeyHex != "":
  72. if nodeKey, err = crypto.HexToECDSA(*nodeKeyHex); err != nil {
  73. log.Crit(fmt.Sprintf("-nodekeyhex: %v", err))
  74. }
  75. }
  76. if *writeAddr {
  77. fmt.Printf("%v\n", discover.PubkeyID(&nodeKey.PublicKey))
  78. os.Exit(0)
  79. }
  80. var restrictList *netutil.Netlist
  81. if *netrestrict != "" {
  82. restrictList, err = netutil.ParseNetlist(*netrestrict)
  83. if err != nil {
  84. log.Crit(fmt.Sprintf("-netrestrict: %v", err))
  85. }
  86. }
  87. if *runv5 {
  88. if _, err := discv5.ListenUDP(nodeKey, *listenAddr, natm, "", restrictList); err != nil {
  89. log.Crit(fmt.Sprintf("%v", err))
  90. }
  91. } else {
  92. if _, err := discover.ListenUDP(nodeKey, *listenAddr, natm, "", restrictList); err != nil {
  93. log.Crit(fmt.Sprintf("%v", err))
  94. }
  95. }
  96. select {}
  97. }