aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/bootnode/main.go
blob: 7d3f9fdb3f237253f95c9a7dfbb1e07d4cd95f5e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// 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
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.

// bootnode runs a bootstrap node for the Ethereum Discovery Protocol.
package main

import (
    "crypto/ecdsa"
    "flag"
    "os"

    "github.com/ethereum/go-ethereum/cmd/utils"
    "github.com/ethereum/go-ethereum/crypto"
    "github.com/ethereum/go-ethereum/logger/glog"
    "github.com/ethereum/go-ethereum/p2p/discover"
    "github.com/ethereum/go-ethereum/p2p/nat"
)

func main() {
    var (
        listenAddr  = flag.String("addr", ":30301", "listen address")
        genKey      = flag.String("genkey", "", "generate a node key and quit")
        nodeKeyFile = flag.String("nodekey", "", "private key filename")
        nodeKeyHex  = flag.String("nodekeyhex", "", "private key as hex (for testing)")
        natdesc     = flag.String("nat", "none", "port mapping mechanism (any|none|upnp|pmp|extip:<IP>)")

        nodeKey *ecdsa.PrivateKey
        err     error
    )
    flag.Var(glog.GetVerbosity(), "verbosity", "log verbosity (0-9)")
    flag.Var(glog.GetVModule(), "vmodule", "log verbosity pattern")
    glog.SetToStderr(true)
    flag.Parse()

    if *genKey != "" {
        key, err := crypto.GenerateKey()
        if err != nil {
            utils.Fatalf("could not generate key: %v", err)
        }
        if err := crypto.SaveECDSA(*genKey, key); err != nil {
            utils.Fatalf("%v", err)
        }
        os.Exit(0)
    }

    natm, err := nat.Parse(*natdesc)
    if err != nil {
        utils.Fatalf("-nat: %v", err)
    }
    switch {
    case *nodeKeyFile == "" && *nodeKeyHex == "":
        utils.Fatalf("Use -nodekey or -nodekeyhex to specify a private key")
    case *nodeKeyFile != "" && *nodeKeyHex != "":
        utils.Fatalf("Options -nodekey and -nodekeyhex are mutually exclusive")
    case *nodeKeyFile != "":
        if nodeKey, err = crypto.LoadECDSA(*nodeKeyFile); err != nil {
            utils.Fatalf("-nodekey: %v", err)
        }
    case *nodeKeyHex != "":
        if nodeKey, err = crypto.HexToECDSA(*nodeKeyHex); err != nil {
            utils.Fatalf("-nodekeyhex: %v", err)
        }
    }

    if _, err := discover.ListenUDP(nodeKey, *listenAddr, natm, ""); err != nil {
        utils.Fatalf("%v", err)
    }
    select {}
}