main.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/logger/glog"
  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. nodeKey *ecdsa.PrivateKey
  42. err error
  43. )
  44. flag.Var(glog.GetVerbosity(), "verbosity", "log verbosity (0-9)")
  45. flag.Var(glog.GetVModule(), "vmodule", "log verbosity pattern")
  46. glog.SetToStderr(true)
  47. flag.Parse()
  48. natm, err := nat.Parse(*natdesc)
  49. if err != nil {
  50. utils.Fatalf("-nat: %v", err)
  51. }
  52. switch {
  53. case *genKey != "":
  54. nodeKey, err = crypto.GenerateKey()
  55. if err != nil {
  56. utils.Fatalf("could not generate key: %v", err)
  57. }
  58. if err = crypto.SaveECDSA(*genKey, nodeKey); err != nil {
  59. utils.Fatalf("%v", err)
  60. }
  61. case *nodeKeyFile == "" && *nodeKeyHex == "":
  62. utils.Fatalf("Use -nodekey or -nodekeyhex to specify a private key")
  63. case *nodeKeyFile != "" && *nodeKeyHex != "":
  64. utils.Fatalf("Options -nodekey and -nodekeyhex are mutually exclusive")
  65. case *nodeKeyFile != "":
  66. if nodeKey, err = crypto.LoadECDSA(*nodeKeyFile); err != nil {
  67. utils.Fatalf("-nodekey: %v", err)
  68. }
  69. case *nodeKeyHex != "":
  70. if nodeKey, err = crypto.HexToECDSA(*nodeKeyHex); err != nil {
  71. utils.Fatalf("-nodekeyhex: %v", err)
  72. }
  73. }
  74. if *writeAddr {
  75. fmt.Printf("%v\n", discover.PubkeyID(&nodeKey.PublicKey))
  76. os.Exit(0)
  77. }
  78. var restrictList *netutil.Netlist
  79. if *netrestrict != "" {
  80. restrictList, err = netutil.ParseNetlist(*netrestrict)
  81. if err != nil {
  82. utils.Fatalf("-netrestrict: %v", err)
  83. }
  84. }
  85. if *runv5 {
  86. if _, err := discv5.ListenUDP(nodeKey, *listenAddr, natm, "", restrictList); err != nil {
  87. utils.Fatalf("%v", err)
  88. }
  89. } else {
  90. if _, err := discover.ListenUDP(nodeKey, *listenAddr, natm, "", restrictList); err != nil {
  91. utils.Fatalf("%v", err)
  92. }
  93. }
  94. select {}
  95. }