Skip to content

Commit

Permalink
use configuration option for static relays
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Nov 16, 2021
1 parent 93330d5 commit 0e7e5e1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
3 changes: 2 additions & 1 deletion core/node/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func LibP2P(bcfg *BuildCfg, cfg *config.Config) fx.Option {
fx.Invoke(libp2p.StartListening(cfg.Addresses.Swarm)),
fx.Invoke(libp2p.SetupDiscovery(cfg.Discovery.MDNS.Enabled, cfg.Discovery.MDNS.Interval)),
fx.Provide(libp2p.ForceReachability(cfg.Internal.Libp2pForceReachability)),
fx.Provide(libp2p.StaticRelays(cfg.Swarm.StaticRelays)),

fx.Provide(libp2p.Security(!bcfg.DisableEncryptedConnections, cfg.Swarm.Transports)),

Expand All @@ -145,7 +146,7 @@ func LibP2P(bcfg *BuildCfg, cfg *config.Config) fx.Option {

maybeProvide(libp2p.BandwidthCounter, !cfg.Swarm.DisableBandwidthMetrics),
maybeProvide(libp2p.NatPortMap, !cfg.Swarm.DisableNatPortMap),
maybeProvide(libp2p.AutoRelay, cfg.Swarm.EnableAutoRelay),
maybeProvide(libp2p.AutoRelay(len(cfg.Swarm.StaticRelays) == 0), cfg.Swarm.EnableAutoRelay),
autonat,
connmgr,
ps,
Expand Down
29 changes: 28 additions & 1 deletion core/node/libp2p/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package libp2p

import (
config "github.com/ipfs/go-ipfs-config"
"github.com/libp2p/go-libp2p-core/peer"

"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
Expand Down Expand Up @@ -42,4 +43,30 @@ func RelayService(enable bool, relayOpts config.RelayService) func() (opts Libp2
}
}

var AutoRelay = simpleOpt(libp2p.ChainOptions(libp2p.EnableAutoRelay(), libp2p.DefaultStaticRelays()))
func StaticRelays(relays []string) func() (opts Libp2pOpts, err error) {
return func() (opts Libp2pOpts, err error) {
staticRelays := make([]peer.AddrInfo, 0, len(relays))
for _, s := range relays {
var addr *peer.AddrInfo
addr, err = peer.AddrInfoFromString(s)
if err != nil {
return
}
staticRelays = append(staticRelays, *addr)
}
if len(staticRelays) > 0 {
opts.Opts = append(opts.Opts, libp2p.StaticRelays(staticRelays))
}
return
}
}

func AutoRelay(addDefaultRelays bool) func() (opts Libp2pOpts, err error) {
return func() (opts Libp2pOpts, err error) {
opts.Opts = append(opts.Opts, libp2p.EnableAutoRelay())
if addDefaultRelays {
opts.Opts = append(opts.Opts, libp2p.DefaultStaticRelays())
}
return
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ require (
github.com/ipfs/go-ipfs-blockstore v0.1.6
github.com/ipfs/go-ipfs-chunker v0.0.5
github.com/ipfs/go-ipfs-cmds v0.6.0
github.com/ipfs/go-ipfs-config v0.16.1-0.20211028222721-a25573d5289a
github.com/ipfs/go-ipfs-config v0.16.1-0.20211113111219-7bbc5ab0aa46
github.com/ipfs/go-ipfs-exchange-interface v0.0.1
github.com/ipfs/go-ipfs-exchange-offline v0.0.1
github.com/ipfs/go-ipfs-files v0.0.9
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,8 @@ github.com/ipfs/go-ipfs-chunker v0.0.5 h1:ojCf7HV/m+uS2vhUGWcogIIxiO5ubl5O57Q7Na
github.com/ipfs/go-ipfs-chunker v0.0.5/go.mod h1:jhgdF8vxRHycr00k13FM8Y0E+6BoalYeobXmUyTreP8=
github.com/ipfs/go-ipfs-cmds v0.6.0 h1:yAxdowQZzoFKjcLI08sXVNnqVj3jnABbf9smrPQmBsw=
github.com/ipfs/go-ipfs-cmds v0.6.0/go.mod h1:ZgYiWVnCk43ChwoH8hAmI1IRbuVtq3GSTHwtRB/Kqhk=
github.com/ipfs/go-ipfs-config v0.16.1-0.20211028222721-a25573d5289a h1:bIq9Zl92JOZw/wz6LVfucaIVKQ7fkp26FQdEGA6vY/Q=
github.com/ipfs/go-ipfs-config v0.16.1-0.20211028222721-a25573d5289a/go.mod h1:wz2lKzOjgJeYJa6zx8W9VT7mz+iSd0laBMqS/9wmX6A=
github.com/ipfs/go-ipfs-config v0.16.1-0.20211113111219-7bbc5ab0aa46 h1:zPASJGoi1ih8j5zY+Ib0BIXgkd+TkqPMiUdcwNEIWqg=
github.com/ipfs/go-ipfs-config v0.16.1-0.20211113111219-7bbc5ab0aa46/go.mod h1:wz2lKzOjgJeYJa6zx8W9VT7mz+iSd0laBMqS/9wmX6A=
github.com/ipfs/go-ipfs-delay v0.0.0-20181109222059-70721b86a9a8/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw=
github.com/ipfs/go-ipfs-delay v0.0.1 h1:r/UXYyRcddO6thwOnhiznIAiSvxMECGgtv35Xs1IeRQ=
github.com/ipfs/go-ipfs-delay v0.0.1/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw=
Expand Down

0 comments on commit 0e7e5e1

Please sign in to comment.