-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
host.go
108 lines (96 loc) · 3.04 KB
/
host.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"context"
"log"
"time"
"github.com/libp2p/go-libp2p/p2p/net/connmgr"
"github.com/libp2p/go-libp2p"
dht "github.com/libp2p/go-libp2p-kad-dht"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/routing"
"github.com/libp2p/go-libp2p/p2p/security/noise"
libp2ptls "github.com/libp2p/go-libp2p/p2p/security/tls"
)
func main() {
run()
}
func run() {
// To construct a simple host with all the default settings, just use `New`
h, err := libp2p.New()
if err != nil {
panic(err)
}
defer h.Close()
log.Printf("Hello World, my hosts ID is %s\n", h.ID())
// Now, normally you do not just want a simple host, you want
// that is fully configured to best support your p2p application.
// Let's create a second host setting some more options.
// Set your own keypair
priv, _, err := crypto.GenerateKeyPair(
crypto.Ed25519, // Select your key type. Ed25519 are nice short
-1, // Select key length when possible (i.e. RSA).
)
if err != nil {
panic(err)
}
var idht *dht.IpfsDHT
connmgr, err := connmgr.NewConnManager(
100, // Lowwater
400, // HighWater,
connmgr.WithGracePeriod(time.Minute),
)
if err != nil {
panic(err)
}
h2, err := libp2p.New(
// Use the keypair we generated
libp2p.Identity(priv),
// Multiple listen addresses
libp2p.ListenAddrStrings(
"/ip4/0.0.0.0/tcp/9000", // regular tcp connections
"/ip4/0.0.0.0/udp/9000/quic-v1", // a UDP endpoint for the QUIC transport
),
// support TLS connections
libp2p.Security(libp2ptls.ID, libp2ptls.New),
// support noise connections
libp2p.Security(noise.ID, noise.New),
// support any other default transports (TCP)
libp2p.DefaultTransports,
// Let's prevent our peer from having too many
// connections by attaching a connection manager.
libp2p.ConnectionManager(connmgr),
// Attempt to open ports using uPNP for NATed hosts.
libp2p.NATPortMap(),
// Let this host use the DHT to find other hosts
libp2p.Routing(func(h host.Host) (routing.PeerRouting, error) {
idht, err = dht.New(context.Background(), h)
return idht, err
}),
// If you want to help other peers to figure out if they are behind
// NATs, you can launch the server-side of AutoNAT too (AutoRelay
// already runs the client)
//
// This service is highly rate-limited and should not cause any
// performance issues.
libp2p.EnableNATService(),
)
if err != nil {
panic(err)
}
defer h2.Close()
// The last step to get fully up and running would be to connect to
// bootstrap peers (or any other peers). We leave this commented as
// this is an example and the peer will die as soon as it finishes, so
// it is unnecessary to put strain on the network.
/*
// This connects to public bootstrappers
for _, addr := range dht.DefaultBootstrapPeers {
pi, _ := peer.AddrInfoFromP2pAddr(addr)
// We ignore errors as some bootstrap peers may be down
// and that is fine.
h2.Connect(ctx, *pi)
}
*/
log.Printf("Hello World, my second hosts ID is %s\n", h2.ID())
}