status-go/vendor/github.com/libp2p/go-libp2p/p2p/host/autonat/autonat.go

451 lines
12 KiB
Go
Raw Normal View History

2019-06-09 07:24:20 +00:00
package autonat
import (
"context"
"math/rand"
"sync/atomic"
2019-06-09 07:24:20 +00:00
"time"
2022-11-04 13:57:20 +00:00
"github.com/libp2p/go-libp2p/core/event"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/p2p/host/eventbus"
2019-06-09 07:24:20 +00:00
2022-04-01 16:16:46 +00:00
logging "github.com/ipfs/go-log/v2"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
2019-06-09 07:24:20 +00:00
)
var log = logging.Logger("autonat")
2019-06-09 07:24:20 +00:00
2023-05-19 20:23:55 +00:00
const maxConfidence = 3
2019-06-09 07:24:20 +00:00
// AmbientAutoNAT is the implementation of ambient NAT autodiscovery
type AmbientAutoNAT struct {
host host.Host
*config
2019-06-09 07:24:20 +00:00
2022-04-01 16:16:46 +00:00
ctx context.Context
ctxCancel context.CancelFunc // is closed when Close is called
backgroundRunning chan struct{} // is closed when the background go routine exits
2023-05-19 20:23:55 +00:00
inboundConn chan network.Conn
dialResponses chan error
// status is an autoNATResult reflecting current status.
2023-05-19 20:23:55 +00:00
status atomic.Pointer[network.Reachability]
2019-06-09 07:24:20 +00:00
// Reflects the confidence on of the NATStatus being private, as a single
// dialback may fail for reasons unrelated to NAT.
// If it is <3, then multiple autoNAT peers may be contacted for dialback
// If only a single autoNAT peer is known, then the confidence increases
// for each failure until it reaches 3.
confidence int
lastInbound time.Time
lastProbeTry time.Time
lastProbe time.Time
recentProbes map[peer.ID]time.Time
service *autoNATService
emitReachabilityChanged event.Emitter
subscriber event.Subscription
}
// StaticAutoNAT is a simple AutoNAT implementation when a single NAT status is desired.
type StaticAutoNAT struct {
host host.Host
reachability network.Reachability
service *autoNATService
2019-06-09 07:24:20 +00:00
}
// New creates a new NAT autodiscovery system attached to a host
2022-04-01 16:16:46 +00:00
func New(h host.Host, options ...Option) (AutoNAT, error) {
var err error
conf := new(config)
conf.host = h
conf.dialPolicy.host = h
if err = defaults(conf); err != nil {
return nil, err
}
if conf.addressFunc == nil {
conf.addressFunc = h.Addrs
}
for _, o := range options {
if err = o(conf); err != nil {
return nil, err
}
}
emitReachabilityChanged, _ := h.EventBus().Emitter(new(event.EvtLocalReachabilityChanged), eventbus.Stateful)
var service *autoNATService
if (!conf.forceReachability || conf.reachability == network.ReachabilityPublic) && conf.dialer != nil {
2022-04-01 16:16:46 +00:00
service, err = newAutoNATService(conf)
if err != nil {
return nil, err
}
service.Enable()
}
if conf.forceReachability {
emitReachabilityChanged.Emit(event.EvtLocalReachabilityChanged{Reachability: conf.reachability})
return &StaticAutoNAT{
host: h,
reachability: conf.reachability,
service: service,
}, nil
2019-06-09 07:24:20 +00:00
}
2022-04-01 16:16:46 +00:00
ctx, cancel := context.WithCancel(context.Background())
2019-06-09 07:24:20 +00:00
as := &AmbientAutoNAT{
2022-04-01 16:16:46 +00:00
ctx: ctx,
ctxCancel: cancel,
backgroundRunning: make(chan struct{}),
host: h,
config: conf,
inboundConn: make(chan network.Conn, 5),
2023-05-19 20:23:55 +00:00
dialResponses: make(chan error, 1),
emitReachabilityChanged: emitReachabilityChanged,
service: service,
recentProbes: make(map[peer.ID]time.Time),
}
2023-05-19 20:23:55 +00:00
reachability := network.ReachabilityUnknown
as.status.Store(&reachability)
subscriber, err := as.host.EventBus().Subscribe(
[]any{new(event.EvtLocalAddressesUpdated), new(event.EvtPeerIdentificationCompleted)},
eventbus.Name("autonat"),
)
if err != nil {
return nil, err
2019-06-09 07:24:20 +00:00
}
as.subscriber = subscriber
2019-06-09 07:24:20 +00:00
h.Network().Notify(as)
go as.background()
return as, nil
2019-06-09 07:24:20 +00:00
}
// Status returns the AutoNAT observed reachability status.
func (as *AmbientAutoNAT) Status() network.Reachability {
s := as.status.Load()
2023-05-19 20:23:55 +00:00
return *s
2019-06-09 07:24:20 +00:00
}
func (as *AmbientAutoNAT) emitStatus() {
2023-05-19 20:23:55 +00:00
status := *as.status.Load()
as.emitReachabilityChanged.Emit(event.EvtLocalReachabilityChanged{Reachability: status})
if as.metricsTracer != nil {
2023-05-19 20:23:55 +00:00
as.metricsTracer.ReachabilityStatus(status)
2019-06-09 07:24:20 +00:00
}
}
func ipInList(candidate ma.Multiaddr, list []ma.Multiaddr) bool {
candidateIP, _ := manet.ToIP(candidate)
for _, i := range list {
if ip, err := manet.ToIP(i); err == nil && ip.Equal(candidateIP) {
return true
}
}
return false
2019-06-09 07:24:20 +00:00
}
func (as *AmbientAutoNAT) background() {
2022-04-01 16:16:46 +00:00
defer close(as.backgroundRunning)
2019-06-09 07:24:20 +00:00
// wait a bit for the node to come online and establish some connections
// before starting autodetection
delay := as.config.bootDelay
subChan := as.subscriber.Out()
defer as.subscriber.Close()
defer as.emitReachabilityChanged.Close()
2019-06-09 07:24:20 +00:00
timer := time.NewTimer(delay)
defer timer.Stop()
timerRunning := true
2023-05-19 20:23:55 +00:00
retryProbe := false
2019-06-09 07:24:20 +00:00
for {
select {
// new inbound connection.
case conn := <-as.inboundConn:
localAddrs := as.host.Addrs()
if manet.IsPublicAddr(conn.RemoteMultiaddr()) &&
!ipInList(conn.RemoteMultiaddr(), localAddrs) {
as.lastInbound = time.Now()
}
2019-06-09 07:24:20 +00:00
case e := <-subChan:
switch e := e.(type) {
case event.EvtLocalAddressesUpdated:
2023-05-19 20:23:55 +00:00
// On local address update, reduce confidence from maximum so that we schedule
// the next probe sooner
if as.confidence == maxConfidence {
as.confidence--
}
case event.EvtPeerIdentificationCompleted:
if s, err := as.host.Peerstore().SupportsProtocols(e.Peer, AutoNATProto); err == nil && len(s) > 0 {
2023-05-19 20:23:55 +00:00
currentStatus := *as.status.Load()
if currentStatus == network.ReachabilityUnknown {
as.tryProbe(e.Peer)
}
}
default:
log.Errorf("unknown event type: %T", e)
}
2019-06-09 07:24:20 +00:00
// probe finished.
2023-05-19 20:23:55 +00:00
case err, ok := <-as.dialResponses:
if !ok {
return
}
2023-05-19 20:23:55 +00:00
if IsDialRefused(err) {
retryProbe = true
} else {
as.handleDialResponse(err)
}
case <-timer.C:
peer := as.getPeerToProbe()
as.tryProbe(peer)
timerRunning = false
2023-05-19 20:23:55 +00:00
retryProbe = false
2019-06-09 07:24:20 +00:00
case <-as.ctx.Done():
return
}
// Drain the timer channel if it hasn't fired in preparation for Resetting it.
if timerRunning && !timer.Stop() {
<-timer.C
}
2023-05-19 20:23:55 +00:00
timer.Reset(as.scheduleProbe(retryProbe))
timerRunning = true
2019-06-09 07:24:20 +00:00
}
}
2019-06-09 07:24:20 +00:00
func (as *AmbientAutoNAT) cleanupRecentProbes() {
fixedNow := time.Now()
for k, v := range as.recentProbes {
if fixedNow.Sub(v) > as.throttlePeerPeriod {
delete(as.recentProbes, k)
}
2019-06-09 07:24:20 +00:00
}
}
2019-06-09 07:24:20 +00:00
// scheduleProbe calculates when the next probe should be scheduled for.
2023-05-19 20:23:55 +00:00
func (as *AmbientAutoNAT) scheduleProbe(retryProbe bool) time.Duration {
// Our baseline is a probe every 'AutoNATRefreshInterval'
// This is modulated by:
2023-05-19 20:23:55 +00:00
// * if we are in an unknown state, have low confidence, or we want to retry because a probe was refused that
// should drop to 'AutoNATRetryInterval'
// * recent inbound connections (implying continued connectivity) should decrease the retry when public
// * recent inbound connections when not public mean we should try more actively to see if we're public.
fixedNow := time.Now()
2023-05-19 20:23:55 +00:00
currentStatus := *as.status.Load()
nextProbe := fixedNow
// Don't look for peers in the peer store more than once per second.
if !as.lastProbeTry.IsZero() {
backoff := as.lastProbeTry.Add(time.Second)
if backoff.After(nextProbe) {
nextProbe = backoff
}
2019-06-09 07:24:20 +00:00
}
if !as.lastProbe.IsZero() {
untilNext := as.config.refreshInterval
2023-05-19 20:23:55 +00:00
if retryProbe {
untilNext = as.config.retryInterval
2023-05-19 20:23:55 +00:00
} else if currentStatus == network.ReachabilityUnknown {
untilNext = as.config.retryInterval
2023-05-19 20:23:55 +00:00
} else if as.confidence < maxConfidence {
untilNext = as.config.retryInterval
} else if currentStatus == network.ReachabilityPublic && as.lastInbound.After(as.lastProbe) {
untilNext *= 2
2023-05-19 20:23:55 +00:00
} else if currentStatus != network.ReachabilityPublic && as.lastInbound.After(as.lastProbe) {
untilNext /= 5
}
2019-06-09 07:24:20 +00:00
if as.lastProbe.Add(untilNext).After(nextProbe) {
nextProbe = as.lastProbe.Add(untilNext)
}
2019-06-09 07:24:20 +00:00
}
if as.metricsTracer != nil {
as.metricsTracer.NextProbeTime(nextProbe)
}
return nextProbe.Sub(fixedNow)
}
2019-06-09 07:24:20 +00:00
2023-05-19 20:23:55 +00:00
// handleDialResponse updates the current status based on dial response.
func (as *AmbientAutoNAT) handleDialResponse(dialErr error) {
var observation network.Reachability
switch {
case dialErr == nil:
observation = network.ReachabilityPublic
case IsDialError(dialErr):
observation = network.ReachabilityPrivate
default:
observation = network.ReachabilityUnknown
}
as.recordObservation(observation)
}
// recordObservation updates NAT status and confidence
func (as *AmbientAutoNAT) recordObservation(observation network.Reachability) {
currentStatus := *as.status.Load()
2023-05-19 20:23:55 +00:00
if observation == network.ReachabilityPublic {
changed := false
2023-05-19 20:23:55 +00:00
if currentStatus != network.ReachabilityPublic {
// Aggressively switch to public from other states ignoring confidence
2023-05-19 20:23:55 +00:00
log.Debugf("NAT status is public")
2019-06-09 07:24:20 +00:00
// we are flipping our NATStatus, so confidence drops to 0
as.confidence = 0
if as.service != nil {
as.service.Enable()
}
changed = true
2023-05-19 20:23:55 +00:00
} else if as.confidence < maxConfidence {
2019-06-09 07:24:20 +00:00
as.confidence++
}
as.status.Store(&observation)
if changed {
as.emitStatus()
}
2023-05-19 20:23:55 +00:00
} else if observation == network.ReachabilityPrivate {
if currentStatus != network.ReachabilityPrivate {
if as.confidence > 0 {
as.confidence--
} else {
2023-05-19 20:23:55 +00:00
log.Debugf("NAT status is private")
// we are flipping our NATStatus, so confidence drops to 0
as.confidence = 0
as.status.Store(&observation)
if as.service != nil {
as.service.Disable()
}
as.emitStatus()
}
2023-05-19 20:23:55 +00:00
} else if as.confidence < maxConfidence {
2019-06-09 07:24:20 +00:00
as.confidence++
as.status.Store(&observation)
2019-06-09 07:24:20 +00:00
}
} else if as.confidence > 0 {
// don't just flip to unknown, reduce confidence first
as.confidence--
} else {
log.Debugf("NAT status is unknown")
2023-05-19 20:23:55 +00:00
as.status.Store(&observation)
if currentStatus != network.ReachabilityUnknown {
if as.service != nil {
as.service.Enable()
}
as.emitStatus()
}
2019-06-09 07:24:20 +00:00
}
if as.metricsTracer != nil {
as.metricsTracer.ReachabilityStatusConfidence(as.confidence)
}
2019-06-09 07:24:20 +00:00
}
func (as *AmbientAutoNAT) tryProbe(p peer.ID) bool {
as.lastProbeTry = time.Now()
if p.Validate() != nil {
return false
}
2019-06-09 07:24:20 +00:00
if lastTime, ok := as.recentProbes[p]; ok {
if time.Since(lastTime) < as.throttlePeerPeriod {
return false
}
2019-06-09 07:24:20 +00:00
}
as.cleanupRecentProbes()
2019-06-09 07:24:20 +00:00
info := as.host.Peerstore().PeerInfo(p)
2019-06-09 07:24:20 +00:00
if !as.config.dialPolicy.skipPeer(info.Addrs) {
as.recentProbes[p] = time.Now()
as.lastProbe = time.Now()
go as.probe(&info)
return true
}
return false
}
func (as *AmbientAutoNAT) probe(pi *peer.AddrInfo) {
cli := NewAutoNATClient(as.host, as.config.addressFunc, as.metricsTracer)
ctx, cancel := context.WithTimeout(as.ctx, as.config.requestTimeout)
defer cancel()
2023-05-19 20:23:55 +00:00
err := cli.DialBack(ctx, pi.ID)
log.Debugf("Dialback through peer %s completed: err: %s", pi.ID, err)
select {
2023-05-19 20:23:55 +00:00
case as.dialResponses <- err:
case <-as.ctx.Done():
return
2019-06-09 07:24:20 +00:00
}
}
2019-06-09 07:24:20 +00:00
func (as *AmbientAutoNAT) getPeerToProbe() peer.ID {
peers := as.host.Network().Peers()
if len(peers) == 0 {
return ""
}
2019-06-09 07:24:20 +00:00
candidates := make([]peer.ID, 0, len(peers))
for _, p := range peers {
info := as.host.Peerstore().PeerInfo(p)
// Exclude peers which don't support the autonat protocol.
if proto, err := as.host.Peerstore().SupportsProtocols(p, AutoNATProto); len(proto) == 0 || err != nil {
continue
}
// Exclude peers in backoff.
if lastTime, ok := as.recentProbes[p]; ok {
if time.Since(lastTime) < as.throttlePeerPeriod {
continue
}
}
if as.config.dialPolicy.skipPeer(info.Addrs) {
continue
}
candidates = append(candidates, p)
}
if len(candidates) == 0 {
return ""
2019-06-09 07:24:20 +00:00
}
2023-05-19 20:23:55 +00:00
return candidates[rand.Intn(len(candidates))]
2019-06-09 07:24:20 +00:00
}
2022-04-01 16:16:46 +00:00
func (as *AmbientAutoNAT) Close() error {
as.ctxCancel()
if as.service != nil {
as.service.Disable()
}
<-as.backgroundRunning
return nil
}
// Status returns the AutoNAT observed reachability status.
func (s *StaticAutoNAT) Status() network.Reachability {
return s.reachability
}
2022-04-01 16:16:46 +00:00
func (s *StaticAutoNAT) Close() error {
if s.service != nil {
s.service.Disable()
}
return nil
}