main.go 4.2 KB

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