main.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. "os"
  22. "github.com/ethereum/go-ethereum/cmd/utils"
  23. "github.com/ethereum/go-ethereum/crypto"
  24. "github.com/ethereum/go-ethereum/logger/glog"
  25. "github.com/ethereum/go-ethereum/p2p/discover"
  26. "github.com/ethereum/go-ethereum/p2p/nat"
  27. )
  28. func main() {
  29. var (
  30. listenAddr = flag.String("addr", ":30301", "listen address")
  31. genKey = flag.String("genkey", "", "generate a node key and quit")
  32. nodeKeyFile = flag.String("nodekey", "", "private key filename")
  33. nodeKeyHex = flag.String("nodekeyhex", "", "private key as hex (for testing)")
  34. natdesc = flag.String("nat", "none", "port mapping mechanism (any|none|upnp|pmp|extip:<IP>)")
  35. nodeKey *ecdsa.PrivateKey
  36. err error
  37. )
  38. flag.Var(glog.GetVerbosity(), "verbosity", "log verbosity (0-9)")
  39. flag.Var(glog.GetVModule(), "vmodule", "log verbosity pattern")
  40. glog.SetToStderr(true)
  41. flag.Parse()
  42. if *genKey != "" {
  43. key, err := crypto.GenerateKey()
  44. if err != nil {
  45. utils.Fatalf("could not generate key: %v", err)
  46. }
  47. if err := crypto.SaveECDSA(*genKey, key); err != nil {
  48. utils.Fatalf("%v", err)
  49. }
  50. os.Exit(0)
  51. }
  52. natm, err := nat.Parse(*natdesc)
  53. if err != nil {
  54. utils.Fatalf("-nat: %v", err)
  55. }
  56. switch {
  57. case *nodeKeyFile == "" && *nodeKeyHex == "":
  58. utils.Fatalf("Use -nodekey or -nodekeyhex to specify a private key")
  59. case *nodeKeyFile != "" && *nodeKeyHex != "":
  60. utils.Fatalf("Options -nodekey and -nodekeyhex are mutually exclusive")
  61. case *nodeKeyFile != "":
  62. if nodeKey, err = crypto.LoadECDSA(*nodeKeyFile); err != nil {
  63. utils.Fatalf("-nodekey: %v", err)
  64. }
  65. case *nodeKeyHex != "":
  66. if nodeKey, err = crypto.HexToECDSA(*nodeKeyHex); err != nil {
  67. utils.Fatalf("-nodekeyhex: %v", err)
  68. }
  69. }
  70. if _, err := discover.ListenUDP(nodeKey, *listenAddr, natm, ""); err != nil {
  71. utils.Fatalf("%v", err)
  72. }
  73. select {}
  74. }