2019-09-23 21:43:53 +00:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
2020-06-25 09:16:30 +00:00
|
|
|
"strings"
|
|
|
|
|
2019-09-23 21:43:53 +00:00
|
|
|
"github.com/libp2p/go-libp2p/config"
|
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
|
|
|
)
|
|
|
|
|
|
|
|
// withRelayAddrs returns an AddrFactory which will return Multiaddr via
|
|
|
|
// specified relay string in addition to existing MultiAddr.
|
|
|
|
func withRelayAddrs(relay string) config.AddrsFactory {
|
|
|
|
return func(addrs []ma.Multiaddr) []ma.Multiaddr {
|
|
|
|
if relay == "" {
|
|
|
|
return addrs
|
|
|
|
}
|
|
|
|
|
|
|
|
var relayAddrs []ma.Multiaddr
|
|
|
|
|
|
|
|
for _, a := range addrs {
|
2020-06-25 09:16:30 +00:00
|
|
|
if strings.Contains(a.String(), "/p2p-circuit") {
|
2019-09-23 21:43:53 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
relayAddr, err := ma.NewMultiaddr(relay + "/p2p-circuit" + a.String())
|
|
|
|
if err != nil {
|
2022-08-05 10:52:02 +00:00
|
|
|
log.WithError(err).Error("Failed to create multiaddress for relay node")
|
2019-09-23 21:43:53 +00:00
|
|
|
} else {
|
|
|
|
relayAddrs = append(relayAddrs, relayAddr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(relayAddrs) == 0 {
|
2019-10-01 20:05:17 +00:00
|
|
|
log.Warn("Addresses via relay node are zero - using non-relay addresses")
|
2019-09-23 21:43:53 +00:00
|
|
|
return addrs
|
|
|
|
}
|
|
|
|
return append(addrs, relayAddrs...)
|
|
|
|
}
|
|
|
|
}
|