Skip to content

Commit

Permalink
pass static relays to EnableAutoRelay
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Nov 14, 2021
1 parent 9734b8d commit 4141cac
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 30 deletions.
26 changes: 9 additions & 17 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ type Config struct {

EnableAutoRelay bool
AutoNATConfig
StaticRelays []peer.AddrInfo
StaticRelayOpt autorelay.StaticRelayOption

EnableHolePunching bool
HolePunchingOptions []holepunch.Option
Expand Down Expand Up @@ -251,12 +251,9 @@ func (cfg *Config) NewNode() (host.Host, error) {
return nil, fmt.Errorf("cannot enable autorelay; relay is not enabled")
}

if len(cfg.StaticRelays) > 0 {
var err error
ar, err = autorelay.NewAutoRelay(h, router, autorelay.WithStaticRelays(cfg.StaticRelays))
if err != nil {
return nil, err
}
var opts []autorelay.Option
if cfg.StaticRelayOpt != nil {
opts = append(opts, autorelay.Option(cfg.StaticRelayOpt))
} else {
if router == nil {
h.Close()
Expand All @@ -267,16 +264,11 @@ func (cfg *Config) NewNode() (host.Host, error) {
h.Close()
return nil, fmt.Errorf("cannot enable autorelay; no suitable routing for discovery")
}
var err error
ar, err = autorelay.NewAutoRelay(
h,
router,
autorelay.WithDiscoverer(discovery.NewRoutingDiscovery(crouter)),
autorelay.WithStaticRelays(cfg.StaticRelays),
)
if err != nil {
return nil, err
}
opts = append(opts, autorelay.WithDiscoverer(discovery.NewRoutingDiscovery(crouter)))
}
ar, err = autorelay.NewAutoRelay(h, router, opts...)
if err != nil {
return nil, err
}
}

Expand Down
28 changes: 17 additions & 11 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,14 @@ func EnableRelayService(opts ...relayv2.Option) Option {
//
// This subsystem performs automatic address rewriting to advertise relay addresses when it
// detects that the node is publicly unreachable (e.g. behind a NAT).
func EnableAutoRelay() Option {
func EnableAutoRelay(opts ...autorelay.StaticRelayOption) Option {
return func(cfg *Config) error {
if len(opts) > 0 {
if len(opts) > 1 {
return errors.New("only expected a single static relay configuration option")
}
cfg.StaticRelayOpt = opts[0]
}
cfg.EnableAutoRelay = true
return nil
}
Expand All @@ -260,26 +266,26 @@ func EnableAutoRelay() Option {
// StaticRelays configures known relays for autorelay; when this option is enabled
// then the system will use the configured relays instead of querying the DHT to
// discover relays.
// Deprecated: pass an autorelay.WithStaticRelays option to EnableAutoRelay.
func StaticRelays(relays []peer.AddrInfo) Option {
return func(cfg *Config) error {
cfg.StaticRelays = append(cfg.StaticRelays, relays...)
cfg.StaticRelayOpt = autorelay.WithStaticRelays(relays)
return nil
}
}

// DefaultStaticRelays configures the static relays to use the known PL-operated relays.
// Deprecated: pass autorelay.WithDefaultStaticRelays to EnableAutoRelay.
func DefaultStaticRelays() Option {
return func(cfg *Config) error {
for _, addr := range autorelay.DefaultRelays {
pi, err := peer.AddrInfoFromString(addr)
if err != nil {
return err
}
cfg.StaticRelays = append(cfg.StaticRelays, *pi)
relays := make([]peer.AddrInfo, 0, len(autorelay.DefaultRelays))
for _, addr := range autorelay.DefaultRelays {
pi, err := peer.AddrInfoFromString(addr)
if err != nil {
panic(fmt.Sprintf("failed to initialize default static relays: %s", err))
}

return nil
relays = append(relays, *pi)
}
return StaticRelays(relays)
}

// ForceReachabilityPublic overrides automatic reachability detection in the AutoNAT subsystem,
Expand Down
5 changes: 3 additions & 2 deletions p2p/host/autorelay/autorelay.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ func init() {
}

type Option func(*AutoRelay) error
type StaticRelayOption Option

func WithStaticRelays(static []peer.AddrInfo) Option {
func WithStaticRelays(static []peer.AddrInfo) StaticRelayOption {
return func(r *AutoRelay) error {
if len(r.static) > 0 {
return errors.New("can't set static relays, static relays already configured")
Expand All @@ -77,7 +78,7 @@ func WithStaticRelays(static []peer.AddrInfo) Option {
}
}

func WithDefaultStaticRelays() Option {
func WithDefaultStaticRelays() StaticRelayOption {
return WithStaticRelays(defaultStaticRelays)
}

Expand Down

0 comments on commit 4141cac

Please sign in to comment.