├── .gitignore ├── LICENSE ├── main.go └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | 8 | # Test binary, build with `go test -c` 9 | *.test 10 | 11 | # Output of the go coverage tool, specifically when used with LiteIDE 12 | *.out 13 | .idea/ 14 | .DS_Store 15 | .vs/ 16 | build/ 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 BenDerPan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "flag" 6 | "fmt" 7 | "github.com/libp2p/go-libp2p" 8 | "github.com/libp2p/go-libp2p-core/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 | libp2p.ListenAddrs(sourceMultiAddr), 46 | libp2p.Identity(prvKey), 47 | ) 48 | 49 | if err != nil { 50 | panic(err) 51 | } 52 | 53 | _, err = dht.New(ctx, host) 54 | if err != nil { 55 | panic(err) 56 | } 57 | fmt.Println("") 58 | fmt.Printf("[*] Your Bootstrap ID Is: /ip4/%s/tcp/%v/p2p/%s\n", *listenHost, *port, host.ID().Pretty()) 59 | fmt.Println("") 60 | select {} 61 | } 62 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | This project is a simple implementation of kad-dht bootstrap node based on go-libp2p. 2 | 3 | ### Usage: 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) 22 | --------------------------------------------------------------------------------