|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "github.com/libp2p/go-libp2p" |
| 8 | + "github.com/libp2p/go-libp2p-crypto" |
| 9 | + "github.com/libp2p/go-libp2p-kad-dht" |
| 10 | + "github.com/multiformats/go-multiaddr" |
| 11 | + mrand "math/rand" |
| 12 | + "os" |
| 13 | +) |
| 14 | + |
| 15 | +func main() { |
| 16 | + help := flag.Bool("help", false, "Display Help") |
| 17 | + listenHost := flag.String("host", "0.0.0.0", "The bootstrap node host listen address\n") |
| 18 | + port := flag.Int("port", 4001, "The bootstrap node listen port") |
| 19 | + flag.Parse() |
| 20 | + |
| 21 | + if *help { |
| 22 | + fmt.Printf("This is a simple bootstrap node for kad-dht application using libp2p\n\n") |
| 23 | + fmt.Printf("Usage: \n Run './bootnode'\nor Run './bootnode -host [host] -port [port]'\n") |
| 24 | + |
| 25 | + os.Exit(0) |
| 26 | + } |
| 27 | + |
| 28 | + fmt.Printf("[*] Listening on: %s with port: %d\n", *listenHost, *port) |
| 29 | + |
| 30 | + ctx := context.Background() |
| 31 | + r := mrand.New(mrand.NewSource(int64(*port))) |
| 32 | + |
| 33 | + // Creates a new RSA key pair for this host. |
| 34 | + prvKey, _, err := crypto.GenerateKeyPairWithReader(crypto.RSA, 2048, r) |
| 35 | + if err != nil { |
| 36 | + panic(err) |
| 37 | + } |
| 38 | + |
| 39 | + // 0.0.0.0 will listen on any interface device. |
| 40 | + sourceMultiAddr, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", *listenHost, *port)) |
| 41 | + |
| 42 | + // libp2p.New constructs a new libp2p Host. |
| 43 | + // Other options can be added here. |
| 44 | + host, err := libp2p.New( |
| 45 | + ctx, |
| 46 | + libp2p.ListenAddrs(sourceMultiAddr), |
| 47 | + libp2p.Identity(prvKey), |
| 48 | + ) |
| 49 | + |
| 50 | + if err != nil { |
| 51 | + panic(err) |
| 52 | + } |
| 53 | + |
| 54 | + _, err = dht.New(ctx, host) |
| 55 | + if err != nil { |
| 56 | + panic(err) |
| 57 | + } |
| 58 | + fmt.Println("") |
| 59 | + fmt.Printf("[*] Your Bootstrap ID Is: /ip4/%s/tcp/%v/p2p/%s\n", *listenHost, *port, host.ID().Pretty()) |
| 60 | + fmt.Println("") |
| 61 | + select {} |
| 62 | +} |
0 commit comments