This repository has been archived by the owner on May 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from libp2p/feat/opt
change autonat interface to use functional options
- Loading branch information
Showing
6 changed files
with
120 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package autonat | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/libp2p/go-libp2p-core/network" | ||
"github.com/libp2p/go-libp2p-core/peer" | ||
|
||
ma "github.com/multiformats/go-multiaddr" | ||
) | ||
|
||
// AutoNAT is the interface for NAT autodiscovery | ||
type AutoNAT interface { | ||
// Status returns the current NAT status | ||
Status() network.Reachability | ||
// PublicAddr returns the public dial address when NAT status is public and an | ||
// error otherwise | ||
PublicAddr() (ma.Multiaddr, error) | ||
} | ||
|
||
// Client is a stateless client interface to AutoNAT peers | ||
type Client interface { | ||
// DialBack requests from a peer providing AutoNAT services to test dial back | ||
// and report the address on a successful connection. | ||
DialBack(ctx context.Context, p peer.ID) (ma.Multiaddr, error) | ||
} | ||
|
||
// GetAddrs is a function returning the candidate addresses for the local host. | ||
type GetAddrs func() []ma.Multiaddr | ||
|
||
// Option is an Autonat option for configuration | ||
type Option func(*config) error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package autonat | ||
|
||
import ( | ||
"errors" | ||
"time" | ||
) | ||
|
||
// config holds configurable options for the autonat subsystem. | ||
type config struct { | ||
getAddressFunc GetAddrs | ||
bootDelay time.Duration | ||
retryInterval time.Duration | ||
refreshInterval time.Duration | ||
requestTimeout time.Duration | ||
} | ||
|
||
var defaults = func(c *config) error { | ||
c.bootDelay = 15 * time.Second | ||
c.retryInterval = 90 * time.Second | ||
c.refreshInterval = 15 * time.Minute | ||
c.requestTimeout = 30 * time.Second | ||
|
||
return nil | ||
} | ||
|
||
// WithAddresses allows overriding which Addresses the AutoNAT client beliieves | ||
// are "its own". Useful for testing, or for more exotic port-forwarding | ||
// scenarios where the host may be listening on different ports than it wants | ||
// to externally advertise or verify connectability on. | ||
func WithAddresses(addrFunc GetAddrs) Option { | ||
return func(c *config) error { | ||
if addrFunc == nil { | ||
return errors.New("invalid address function supplied") | ||
} | ||
c.getAddressFunc = addrFunc | ||
return nil | ||
} | ||
} | ||
|
||
// WithSchedule configures how agressively probes will be made to verify the | ||
// address of the host. retryInterval indicates how often probes should be made | ||
// when the host lacks confident about its address, while refresh interval | ||
// is the schedule of periodic probes when the host believes it knows its | ||
// steady-state reachability. | ||
func WithSchedule(retryInterval, refreshInterval time.Duration) Option { | ||
return func(c *config) error { | ||
c.retryInterval = retryInterval | ||
c.refreshInterval = refreshInterval | ||
return nil | ||
} | ||
} | ||
|
||
// WithoutStartupDelay removes the initial delay the NAT subsystem typically | ||
// uses as a buffer for ensuring that connectivity and guesses as to the hosts | ||
// local interfaces have settled down during startup. | ||
func WithoutStartupDelay() Option { | ||
return func(c *config) error { | ||
c.bootDelay = 1 | ||
return nil | ||
} | ||
} |