main.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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/cmd/utils"
  24. "github.com/ethereum/go-ethereum/crypto"
  25. "github.com/ethereum/go-ethereum/log"
  26. "github.com/ethereum/go-ethereum/p2p/discover"
  27. "github.com/ethereum/go-ethereum/p2p/discv5"
  28. "github.com/ethereum/go-ethereum/p2p/nat"
  29. "github.com/ethereum/go-ethereum/p2p/netutil"
  30. )
  31. func main() {
  32. var (
  33. listenAddr = flag.String("addr", ":30301", "listen address")
  34. genKey = flag.String("genkey", "", "generate a node key")
  35. writeAddr = flag.Bool("writeaddress", false, "write out the node's pubkey hash and quit")
  36. nodeKeyFile = flag.String("nodekey", "", "private key filename")
  37. nodeKeyHex = flag.String("nodekeyhex", "", "private key as hex (for testing)")
  38. natdesc = flag.String("nat", "none", "port mapping mechanism (any|none|upnp|pmp|extip:<IP>)")
  39. netrestrict = flag.String("netrestrict", "", "restrict network communication to the given IP networks (CIDR masks)")
  40. runv5 = flag.Bool("v5", false, "run a v5 topic discovery bootnode")
  41. verbosity = flag.Int("verbosity", int(log.LvlInfo), "log verbosity (0-9)")
  42. vmodule = flag.String("vmodule", "", "log verbosity pattern")
  43. nodeKey *ecdsa.PrivateKey
  44. err error
  45. )
  46. flag.Parse()
  47. glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false)))
  48. glogger.Verbosity(log.Lvl(*verbosity))
  49. glogger.Vmodule(*vmodule)
  50. log.Root().SetHandler(glogger)
  51. natm, err := nat.Parse(*natdesc)
  52. if err != nil {
  53. utils.Fatalf("-nat: %v", err)
  54. }
  55. switch {
  56. case *genKey != "":
  57. nodeKey, err = crypto.GenerateKey()
  58. if err != nil {
  59. utils.Fatalf("could not generate key: %v", err)
  60. }
  61. if err = crypto.SaveECDSA(*genKey, nodeKey); err != nil {
  62. utils.Fatalf("%v", err)
  63. }
  64. case *nodeKeyFile == "" && *nodeKeyHex == "":
  65. utils.Fatalf("Use -nodekey or -nodekeyhex to specify a private key")
  66. case *nodeKeyFile != "" && *nodeKeyHex != "":
  67. utils.Fatalf("Options -nodekey and -nodekeyhex are mutually exclusive")
  68. case *nodeKeyFile != "":
  69. if nodeKey, err = crypto.LoadECDSA(*nodeKeyFile); err != nil {
  70. utils.Fatalf("-nodekey: %v", err)
  71. }
  72. case *nodeKeyHex != "":
  73. if nodeKey, err = crypto.HexToECDSA(*nodeKeyHex); err != nil {
  74. utils.Fatalf("-nodekeyhex: %v", err)
  75. }
  76. }
  77. if *writeAddr {
  78. fmt.Printf("%v\n", discover.PubkeyID(&nodeKey.PublicKey))
  79. os.Exit(0)
  80. }
  81. var restrictList *netutil.Netlist
  82. if *netrestrict != "" {
  83. restrictList, err = netutil.ParseNetlist(*netrestrict)
  84. if err != nil {
  85. utils.Fatalf("-netrestrict: %v", err)
  86. }
  87. }
  88. if *runv5 {
  89. if _, err := discv5.ListenUDP(nodeKey, *listenAddr, natm, "", restrictList); err != nil {
  90. utils.Fatalf("%v", err)
  91. }
  92. } else {
  93. if _, err := discover.ListenUDP(nodeKey, *listenAddr, natm, "", restrictList); err != nil {
  94. utils.Fatalf("%v", err)
  95. }
  96. }
  97. select {}
  98. }