-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
61 lines (50 loc) · 1.52 KB
/
main.go
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
package main
import (
"context"
"flag"
"fmt"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-kad-dht"
"github.com/multiformats/go-multiaddr"
mrand "math/rand"
"os"
)
func main() {
help := flag.Bool("help", false, "Display Help")
listenHost := flag.String("host", "0.0.0.0", "The bootstrap node host listen address\n")
port := flag.Int("port", 4001, "The bootstrap node listen port")
flag.Parse()
if *help {
fmt.Printf("This is a simple bootstrap node for kad-dht application using libp2p\n\n")
fmt.Printf("Usage: \n Run './bootnode'\nor Run './bootnode -host [host] -port [port]'\n")
os.Exit(0)
}
fmt.Printf("[*] Listening on: %s with port: %d\n", *listenHost, *port)
ctx := context.Background()
r := mrand.New(mrand.NewSource(int64(*port)))
// Creates a new RSA key pair for this host.
prvKey, _, err := crypto.GenerateKeyPairWithReader(crypto.RSA, 2048, r)
if err != nil {
panic(err)
}
// 0.0.0.0 will listen on any interface device.
sourceMultiAddr, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", *listenHost, *port))
// libp2p.New constructs a new libp2p Host.
// Other options can be added here.
host, err := libp2p.New(
libp2p.ListenAddrs(sourceMultiAddr),
libp2p.Identity(prvKey),
)
if err != nil {
panic(err)
}
_, err = dht.New(ctx, host)
if err != nil {
panic(err)
}
fmt.Println("")
fmt.Printf("[*] Your Bootstrap ID Is: /ip4/%s/tcp/%v/p2p/%s\n", *listenHost, *port, host.ID().Pretty())
fmt.Println("")
select {}
}