-
Notifications
You must be signed in to change notification settings - Fork 1k
/
notify.go
54 lines (42 loc) · 1.29 KB
/
notify.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
package relay
import (
"context"
"time"
inet "github.com/libp2p/go-libp2p-core/network"
peer "github.com/libp2p/go-libp2p-core/peer"
ma "github.com/multiformats/go-multiaddr"
)
var _ inet.Notifiee = (*RelayNotifiee)(nil)
type RelayNotifiee Relay
func (r *Relay) notifiee() inet.Notifiee {
return (*RelayNotifiee)(r)
}
func (n *RelayNotifiee) Relay() *Relay {
return (*Relay)(n)
}
func (n *RelayNotifiee) Listen(net inet.Network, a ma.Multiaddr) {}
func (n *RelayNotifiee) ListenClose(net inet.Network, a ma.Multiaddr) {}
func (n *RelayNotifiee) OpenedStream(net inet.Network, s inet.Stream) {}
func (n *RelayNotifiee) ClosedStream(net inet.Network, s inet.Stream) {}
func (n *RelayNotifiee) Connected(s inet.Network, c inet.Conn) {
if n.Relay().Matches(c.RemoteMultiaddr()) {
return
}
go func(id peer.ID) {
ctx, cancel := context.WithTimeout(n.ctx, time.Second)
defer cancel()
canhop, err := n.Relay().CanHop(ctx, id)
if err != nil {
log.Debugf("Error testing relay hop: %s", err.Error())
return
}
if canhop {
log.Debugf("Discovered hop relay %s", id.Pretty())
n.mx.Lock()
n.relays[id] = struct{}{}
n.mx.Unlock()
n.host.ConnManager().TagPeer(id, "relay-hop", 2)
}
}(c.RemotePeer())
}
func (n *RelayNotifiee) Disconnected(s inet.Network, c inet.Conn) {}