pass static relays to EnableAutoRelay

This commit is contained in:
Marten Seemann 2021-11-14 15:33:59 +04:00
parent 9734b8d822
commit 4141cac6f9
3 changed files with 29 additions and 30 deletions

View File

@ -97,7 +97,7 @@ type Config struct {
EnableAutoRelay bool
AutoNATConfig
StaticRelays []peer.AddrInfo
StaticRelayOpt autorelay.StaticRelayOption
EnableHolePunching bool
HolePunchingOptions []holepunch.Option
@ -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()
@ -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
}
}

View File

@ -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
}
@ -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,

View File

@ -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")
@ -77,7 +78,7 @@ func WithStaticRelays(static []peer.AddrInfo) Option {
}
}
func WithDefaultStaticRelays() Option {
func WithDefaultStaticRelays() StaticRelayOption {
return WithStaticRelays(defaultStaticRelays)
}