-
Notifications
You must be signed in to change notification settings - Fork 15
/
host.go
62 lines (56 loc) · 1.94 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
package util
import (
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/p2p/muxer/yamux"
"github.com/libp2p/go-libp2p/p2p/security/noise"
tls "github.com/libp2p/go-libp2p/p2p/security/tls"
quic "github.com/libp2p/go-libp2p/p2p/transport/quic"
"github.com/libp2p/go-libp2p/p2p/transport/tcp"
"github.com/libp2p/go-libp2p/p2p/transport/websocket"
webtransport "github.com/libp2p/go-libp2p/p2p/transport/webtransport"
"github.com/multiformats/go-multiaddr"
)
const yamuxID = "/yamux/1.0.0"
// InitHost initializes a new libp2p host with the provided options and listen addresses.
//
// Parameters:
// - opts: A slice of libp2p options to configure the host.
// - listenAddrs: A variadic list of multiaddresses for the host to listen on.
//
// Returns:
// 1. A libp2p host instance that represents the initialized host.
// 2. An error, which will be non-nil if any error occurred during host initialization.
func InitHost(opts []libp2p.Option, listenAddrs ...multiaddr.Multiaddr) (host.Host, error) {
opts = append([]libp2p.Option{
libp2p.Identity(nil),
libp2p.ResourceManager(&network.NullResourceManager{})},
opts...)
if len(listenAddrs) > 0 {
opts = append([]libp2p.Option{libp2p.ListenAddrs(listenAddrs...)}, opts...)
}
// add transports
opts = append([]libp2p.Option{
libp2p.Transport(tcp.NewTCPTransport, tcp.WithMetrics()),
libp2p.Transport(websocket.New),
libp2p.Transport(quic.NewTransport),
libp2p.Transport(webtransport.New)},
opts...)
// add security
opts = append([]libp2p.Option{
libp2p.Security(tls.ID, tls.New),
libp2p.Security(noise.ID, noise.New)},
opts...)
// add muxers
opts = append([]libp2p.Option{
libp2p.Muxer(yamuxID, yamuxTransport())},
opts...)
//nolint:wrapcheck
return libp2p.New(opts...)
}
func yamuxTransport() network.Multiplexer {
tpt := *yamux.DefaultTransport
tpt.AcceptBacklog = 512
return &tpt
}