Skip to content

Commit 4242b05

Browse files
committed
cmd/bootnode: new command (replaces cmd/peerserver)
1 parent a21b30c commit 4242b05

1 file changed

Lines changed: 86 additions & 0 deletions

File tree

‎cmd/bootnode/main.go‎

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

0 commit comments

Comments
 (0)