forked from hsanjuan/ipfs-lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
128 lines (113 loc) · 3.99 KB
/
util.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package ipfslite
import (
"context"
"time"
datastore "github.com/ipfs/go-datastore"
dssync "github.com/ipfs/go-datastore/sync"
ipns "github.com/ipfs/go-ipns"
libp2p "github.com/libp2p/go-libp2p"
dht "github.com/libp2p/go-libp2p-kad-dht"
dualdht "github.com/libp2p/go-libp2p-kad-dht/dual"
record "github.com/libp2p/go-libp2p-record"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/pnet"
"github.com/libp2p/go-libp2p/core/routing"
"github.com/libp2p/go-libp2p/p2p/host/autorelay"
"github.com/libp2p/go-libp2p/p2p/net/connmgr"
"github.com/libp2p/go-libp2p/p2p/transport/tcp"
"github.com/libp2p/go-libp2p/p2p/transport/websocket"
"github.com/multiformats/go-multiaddr"
)
// DefaultBootstrapPeers returns the default bootstrap peers (for use
// with NewLibp2pHost.
func DefaultBootstrapPeers() []peer.AddrInfo {
peers, _ := peer.AddrInfosFromP2pAddrs(dht.DefaultBootstrapPeers...)
return peers
}
// NewInMemoryDatastore provides a sync datastore that lives in-memory only
// and is not persisted.
func NewInMemoryDatastore() datastore.Batching {
return dssync.MutexWrap(datastore.NewMapDatastore())
}
var connMgr, _ = connmgr.NewConnManager(100, 600, connmgr.WithGracePeriod(time.Minute))
// Libp2pOptionsExtra provides some useful libp2p options
// to create a fully featured libp2p host. It can be used with
// SetupLibp2p.
var Libp2pOptionsExtra = []libp2p.Option{
libp2p.NATPortMap(),
libp2p.ConnectionManager(connMgr),
libp2p.EnableAutoRelay(autorelay.WithPeerSource(func(_ context.Context, num int) <-chan peer.AddrInfo {
peerChan := make(chan peer.AddrInfo, num)
defer close(peerChan)
ipfspeers := DefaultBootstrapPeers()
for i := 0; i < num && i < len(ipfspeers); i++ {
peerChan <- ipfspeers[i]
}
return peerChan
}, time.Minute)),
libp2p.EnableNATService(),
}
// SetupLibp2p returns a routed host and DHT instances that can be used to
// easily create a ipfslite Peer. You may consider to use Peer.Bootstrap()
// after creating the IPFS-Lite Peer to connect to other peers. When the
// datastore parameter is nil, the DHT will use an in-memory datastore, so all
// provider records are lost on program shutdown.
//
// Additional libp2p options can be passed. Note that the Identity,
// ListenAddrs and PrivateNetwork options will be setup automatically.
// Interesting options to pass: NATPortMap() EnableAutoRelay(),
// libp2p.EnableNATService(), DisableRelay(), ConnectionManager(...)... see
// https://godoc.org/github.com/libp2p/go-libp2p#Option for more info.
//
// The secret should be a 32-byte pre-shared-key byte slice.
func SetupLibp2p(
ctx context.Context,
hostKey crypto.PrivKey,
secret pnet.PSK,
listenAddrs []multiaddr.Multiaddr,
ds datastore.Batching,
opts ...libp2p.Option,
) (host.Host, *dualdht.DHT, error) {
var ddht *dualdht.DHT
var err error
var transports = libp2p.DefaultTransports
if secret != nil {
transports = libp2p.ChainOptions(
libp2p.NoTransports,
libp2p.Transport(tcp.NewTCPTransport),
libp2p.Transport(websocket.New),
)
}
finalOpts := []libp2p.Option{
libp2p.Identity(hostKey),
libp2p.ListenAddrs(listenAddrs...),
libp2p.PrivateNetwork(secret),
transports,
libp2p.Routing(func(h host.Host) (routing.PeerRouting, error) {
ddht, err = newDHT(ctx, h, ds)
return ddht, err
}),
}
finalOpts = append(finalOpts, opts...)
h, err := libp2p.New(
finalOpts...,
)
if err != nil {
return nil, nil, err
}
return h, ddht, nil
}
func newDHT(ctx context.Context, h host.Host, ds datastore.Batching) (*dualdht.DHT, error) {
dhtOpts := []dualdht.Option{
dualdht.DHTOption(dht.NamespacedValidator("pk", record.PublicKeyValidator{})),
dualdht.DHTOption(dht.NamespacedValidator("ipns", ipns.Validator{KeyBook: h.Peerstore()})),
dualdht.DHTOption(dht.Concurrency(10)),
dualdht.DHTOption(dht.Mode(dht.ModeAuto)),
}
if ds != nil {
dhtOpts = append(dhtOpts, dualdht.DHTOption(dht.Datastore(ds)))
}
return dualdht.New(ctx, h, dhtOpts...)
}