main.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. // Command bootnode runs a bootstrap node for the Discovery Protocol.
  17. package main
  18. import (
  19. "crypto/ecdsa"
  20. "encoding/hex"
  21. "flag"
  22. "fmt"
  23. "io/ioutil"
  24. "log"
  25. "os"
  26. "github.com/ethereum/go-ethereum/crypto"
  27. "github.com/ethereum/go-ethereum/logger"
  28. "github.com/ethereum/go-ethereum/p2p/discover"
  29. "github.com/ethereum/go-ethereum/p2p/nat"
  30. )
  31. func main() {
  32. var (
  33. listenAddr = flag.String("addr", ":30301", "listen address")
  34. genKey = flag.String("genkey", "", "generate a node key 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. nodeKey *ecdsa.PrivateKey
  39. err error
  40. )
  41. flag.Parse()
  42. logger.AddLogSystem(logger.NewStdLogSystem(os.Stdout, log.LstdFlags, logger.DebugLevel))
  43. if *genKey != "" {
  44. writeKey(*genKey)
  45. os.Exit(0)
  46. }
  47. natm, err := nat.Parse(*natdesc)
  48. if err != nil {
  49. log.Fatalf("-nat: %v", err)
  50. }
  51. switch {
  52. case *nodeKeyFile == "" && *nodeKeyHex == "":
  53. log.Fatal("Use -nodekey or -nodekeyhex to specify a private key")
  54. case *nodeKeyFile != "" && *nodeKeyHex != "":
  55. log.Fatal("Options -nodekey and -nodekeyhex are mutually exclusive")
  56. case *nodeKeyFile != "":
  57. if nodeKey, err = crypto.LoadECDSA(*nodeKeyFile); err != nil {
  58. log.Fatalf("-nodekey: %v", err)
  59. }
  60. case *nodeKeyHex != "":
  61. if nodeKey, err = crypto.HexToECDSA(*nodeKeyHex); err != nil {
  62. log.Fatalf("-nodekeyhex: %v", err)
  63. }
  64. }
  65. if _, err := discover.ListenUDP(nodeKey, *listenAddr, natm, ""); err != nil {
  66. log.Fatal(err)
  67. }
  68. select {}
  69. }
  70. func writeKey(target string) {
  71. key, err := crypto.GenerateKey()
  72. if err != nil {
  73. log.Fatal("could not generate key: %v", err)
  74. }
  75. b := crypto.FromECDSA(key)
  76. if target == "-" {
  77. fmt.Println(hex.EncodeToString(b))
  78. } else {
  79. if err := ioutil.WriteFile(target, b, 0600); err != nil {
  80. log.Fatal("write error: ", err)
  81. }
  82. }
  83. }