main.go 4.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2015 The go-ethereum Authors
// This file is part of go-ethereum.
//
// go-ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// go-ethereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 13 14
// GNU General Public License for more details.
//
// 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

17
// bootnode runs a bootstrap node for the Ethereum Discovery Protocol.
18 19 20 21 22
package main

import (
	"crypto/ecdsa"
	"flag"
23
	"fmt"
24
	"net"
25 26
	"os"

27
	"github.com/ethereum/go-ethereum/cmd/utils"
28
	"github.com/ethereum/go-ethereum/crypto"
29
	"github.com/ethereum/go-ethereum/log"
30
	"github.com/ethereum/go-ethereum/p2p/discover"
31
	"github.com/ethereum/go-ethereum/p2p/enode"
32
	"github.com/ethereum/go-ethereum/p2p/nat"
33
	"github.com/ethereum/go-ethereum/p2p/netutil"
34 35 36 37 38
)

func main() {
	var (
		listenAddr  = flag.String("addr", ":30301", "listen address")
39
		genKey      = flag.String("genkey", "", "generate a node key")
40
		writeAddr   = flag.Bool("writeaddress", false, "write out the node's public key and quit")
41 42
		nodeKeyFile = flag.String("nodekey", "", "private key filename")
		nodeKeyHex  = flag.String("nodekeyhex", "", "private key as hex (for testing)")
43
		natdesc     = flag.String("nat", "none", "port mapping mechanism (any|none|upnp|pmp|extip:<IP>)")
44
		netrestrict = flag.String("netrestrict", "", "restrict network communication to the given IP networks (CIDR masks)")
45
		runv5       = flag.Bool("v5", false, "run a v5 topic discovery bootnode")
46
		verbosity   = flag.Int("verbosity", int(log.LvlInfo), "log verbosity (0-5)")
47
		vmodule     = flag.String("vmodule", "", "log verbosity pattern")
48 49 50

		nodeKey *ecdsa.PrivateKey
		err     error
51 52 53
	)
	flag.Parse()

54
	glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false)))
55 56 57 58
	glogger.Verbosity(log.Lvl(*verbosity))
	glogger.Vmodule(*vmodule)
	log.Root().SetHandler(glogger)

59 60
	natm, err := nat.Parse(*natdesc)
	if err != nil {
61
		utils.Fatalf("-nat: %v", err)
62
	}
63
	switch {
64 65 66
	case *genKey != "":
		nodeKey, err = crypto.GenerateKey()
		if err != nil {
67
			utils.Fatalf("could not generate key: %v", err)
68 69
		}
		if err = crypto.SaveECDSA(*genKey, nodeKey); err != nil {
70
			utils.Fatalf("%v", err)
71
		}
72 73 74
		if !*writeAddr {
			return
		}
75
	case *nodeKeyFile == "" && *nodeKeyHex == "":
76
		utils.Fatalf("Use -nodekey or -nodekeyhex to specify a private key")
77
	case *nodeKeyFile != "" && *nodeKeyHex != "":
78
		utils.Fatalf("Options -nodekey and -nodekeyhex are mutually exclusive")
79 80
	case *nodeKeyFile != "":
		if nodeKey, err = crypto.LoadECDSA(*nodeKeyFile); err != nil {
81
			utils.Fatalf("-nodekey: %v", err)
82 83 84
		}
	case *nodeKeyHex != "":
		if nodeKey, err = crypto.HexToECDSA(*nodeKeyHex); err != nil {
85
			utils.Fatalf("-nodekeyhex: %v", err)
86 87 88
		}
	}

89
	if *writeAddr {
90
		fmt.Printf("%x\n", crypto.FromECDSAPub(&nodeKey.PublicKey)[1:])
91 92 93
		os.Exit(0)
	}

94 95 96 97
	var restrictList *netutil.Netlist
	if *netrestrict != "" {
		restrictList, err = netutil.ParseNetlist(*netrestrict)
		if err != nil {
98
			utils.Fatalf("-netrestrict: %v", err)
99 100 101
		}
	}

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
	addr, err := net.ResolveUDPAddr("udp", *listenAddr)
	if err != nil {
		utils.Fatalf("-ResolveUDPAddr: %v", err)
	}
	conn, err := net.ListenUDP("udp", addr)
	if err != nil {
		utils.Fatalf("-ListenUDP: %v", err)
	}

	realaddr := conn.LocalAddr().(*net.UDPAddr)
	if natm != nil {
		if !realaddr.IP.IsLoopback() {
			go nat.Map(natm, nil, "udp", realaddr.Port, realaddr.Port, "ethereum discovery")
		}
		if ext, err := natm.ExternalIP(); err == nil {
			realaddr = &net.UDPAddr{IP: ext, Port: realaddr.Port}
		}
	}

121 122
	printNotice(&nodeKey.PublicKey, *realaddr)

123 124 125 126 127 128
	db, _ := enode.OpenDB("")
	ln := enode.NewLocalNode(db, nodeKey)
	cfg := discover.Config{
		PrivateKey:  nodeKey,
		NetRestrict: restrictList,
	}
129
	if *runv5 {
130
		if _, err := discover.ListenV5(conn, ln, cfg); err != nil {
131
			utils.Fatalf("%v", err)
132 133
		}
	} else {
134
		if _, err := discover.ListenUDP(conn, ln, cfg); err != nil {
135
			utils.Fatalf("%v", err)
136
		}
137
	}
138

139 140
	select {}
}
141 142 143 144 145 146

func printNotice(nodeKey *ecdsa.PublicKey, addr net.UDPAddr) {
	if addr.IP.IsUnspecified() {
		addr.IP = net.IP{127, 0, 0, 1}
	}
	n := enode.NewV4(nodeKey, addr.IP, 0, addr.Port)
147
	fmt.Println(n.URLv4())
148 149 150
	fmt.Println("Note: you're using cmd/bootnode, a developer tool.")
	fmt.Println("We recommend using a regular node as bootstrap node for production deployments.")
}