Skip to content

Commit 99fab49

Browse files
committed
初步提交
1 parent b7e999a commit 99fab49

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@
1010

1111
# Output of the go coverage tool, specifically when used with LiteIDE
1212
*.out
13+
.idea/
14+
.DS_Store
15+
.vs/
16+
build/

main.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
}

readme.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
This project is a simple implementation of kad-dht bootstrap node based on go-libp2p.
2+
3+
### Useage:
4+
1. Run as default(bind on '0.0.0.0' port:4001):
5+
* for linux: `./bootnode.amdx64-linux`
6+
* for windows: `bootnode.amdx64-windows.exe`
7+
8+
2. or Run with custom:
9+
* for linux: `./bootnode.amdx64-linux -host [host] -port [port]`
10+
* for windows: `bootnode.amdx64-windows.exe -host [host] -port [port]`
11+
12+
example output:
13+
```
14+
[*] Listening on: 0.0.0.0 with port: 4001
15+
16+
[*] Your Bootstrap ID Is: /ip4/0.0.0.0/tcp/4001/p2p/QmP2C45o2vZfy1JXWFZDUEzrQCigMtd4r3nesvArV8dFKd
17+
```
18+
19+
3. Copy the bootstrap id to your bootstrap nodes list and enjoy.
20+
21+
### Notice: for a test example with kad-dht, visit [go-libp2p-examples](https://github.com/libp2p/go-libp2p-examples/blob/master/chat-with-rendezvous/chat.go)

0 commit comments

Comments
 (0)