forked from libp2p/go-libp2p-daemon
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrelay.go
162 lines (146 loc) · 4.53 KB
/
relay.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package p2pd
// This file is based on https://github.com/ipfs/kubo/blob/master/core/node/libp2p/relay.go
import (
"context"
"runtime/debug"
"time"
dht "github.com/libp2p/go-libp2p-kad-dht"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
"github.com/cenkalti/backoff/v4"
)
func parseRelays(addrStrings []string) []peer.AddrInfo {
addrs := make([]peer.AddrInfo, 0, len(addrStrings))
for _, s := range addrStrings {
var addr *peer.AddrInfo
var err error
addr, err = peer.AddrInfoFromString(s)
if err != nil {
panic(err)
}
addrs = append(addrs, *addr)
}
return addrs
}
func MaybeConfigureAutoRelay(opts []libp2p.Option, relayDiscovery bool, trustedRelays []string) ([]libp2p.Option, chan peer.AddrInfo) {
var peerSourceChan chan peer.AddrInfo // default(nil) means no peerSource
if !relayDiscovery && len(trustedRelays) > 0 {
log.Debugf("Running with static relays only: %v\n", trustedRelays)
// static relays, no automatic discovery
opts = append(opts, libp2p.EnableAutoRelayWithStaticRelays(
parseRelays(trustedRelays),
))
} else if relayDiscovery {
log.Debug("Running with automatic relay discovery\n")
peerSourceChan = make(chan peer.AddrInfo)
// requires daemon to BeginRelayDiscovery once it is initialized
opts = append(opts, libp2p.EnableAutoRelayWithPeerSource(func(ctx context.Context, numPeers int) <-chan peer.AddrInfo {
r := make(chan peer.AddrInfo)
go func() {
defer close(r)
for ; numPeers != 0; numPeers-- {
select {
case v, ok := <-peerSourceChan:
if !ok {
return
}
select {
case r <- v:
case <-ctx.Done():
return
}
case <-ctx.Done():
return
}
}
}()
return r
}))
} else {
log.Debug("Running without autorelay\n")
}
return opts, peerSourceChan
}
func BeginRelayDiscovery(h host.Host, dht *dht.IpfsDHT, trustedRelays []string, peerSourceChan chan<- peer.AddrInfo) context.CancelFunc {
log.Debug("Began looking for potential relays in background\n")
var trustedRelayAddrs = parseRelays(trustedRelays)
ctx, cancel := context.WithCancel(context.Background())
done := make(chan struct{})
go func() {
defer close(done)
// Feed peers more often right after the bootstrap, then backoff
bo := backoff.NewExponentialBackOff()
bo.InitialInterval = 15 * time.Second
bo.Multiplier = 3
bo.MaxInterval = 1 * time.Hour
bo.MaxElapsedTime = 0 // never stop
t := backoff.NewTicker(bo)
defer t.Stop()
for {
func() { // gather peers once
defer func() { // recover from errors
if r := recover(); r != nil {
log.Warnw("Recovering from unexpected error in AutoRelayFeeder,", "caught", r)
debug.PrintStack()
}
}()
// Always feed trusted IDs (Peering.Peers in the config)
for _, trustedPeer := range trustedRelayAddrs {
if len(trustedPeer.Addrs) == 0 {
continue
}
select {
case peerSourceChan <- trustedPeer:
log.Debugf("Trying trusted peer as relay: %v\n", trustedPeer)
case <-ctx.Done():
return
}
}
// Additionally, feed closest peers discovered via DHT
if dht == nil {
panic("Daemon asked to perform relay discovery but has not DHT. Please set -dht=1")
}
closestPeers, err := dht.GetClosestPeers(ctx, h.ID().String())
if err != nil {
// no-op: usually 'failed to find any peer in table' during startup
return
}
for _, p := range closestPeers {
addrs := h.Peerstore().Addrs(p)
if len(addrs) == 0 {
continue
}
dhtPeer := peer.AddrInfo{ID: p, Addrs: addrs}
select {
case peerSourceChan <- dhtPeer:
log.Debugf("Trying dht peer as relay: %v\n", dhtPeer)
case <-ctx.Done():
return
}
}
}()
select {
case <-t.C:
case <-ctx.Done():
return
}
}
}()
return cancel
}
func ConfigureRelayService(opts []libp2p.Option, maxCircuits int, maxReservations int, relayBufferSize int, dataLimit int64, timeLimit time.Duration) []libp2p.Option {
resources := relay.DefaultResources()
resources.Limit = &relay.RelayLimit{Duration: timeLimit, Data: dataLimit}
resources.MaxCircuits = maxCircuits
resources.MaxReservations = maxReservations
resources.MaxReservationsPerPeer = maxReservations
resources.MaxReservationsPerIP = maxReservations
resources.MaxReservationsPerASN = maxReservations
resources.BufferSize = relayBufferSize
opts = append(opts, libp2p.EnableRelayService(
relay.WithResources(resources),
))
return opts
}