github.com/amazechain/amc@v0.1.3/internal/p2p/addr_factory.go (about)

     1  package p2p
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/libp2p/go-libp2p/config"
     7  	ma "github.com/multiformats/go-multiaddr"
     8  )
     9  
    10  // withRelayAddrs returns an AddrFactory which will return Multiaddr via
    11  // specified relay string in addition to existing MultiAddr.
    12  func withRelayAddrs(relay string) config.AddrsFactory {
    13  	return func(addrs []ma.Multiaddr) []ma.Multiaddr {
    14  		if relay == "" {
    15  			return addrs
    16  		}
    17  
    18  		var relayAddrs []ma.Multiaddr
    19  
    20  		for _, a := range addrs {
    21  			if strings.Contains(a.String(), "/p2p-circuit") {
    22  				continue
    23  			}
    24  			relayAddr, err := ma.NewMultiaddr(relay + "/p2p-circuit" + a.String())
    25  			if err != nil {
    26  				log.Error("Failed to create multiaddress for relay node", "err", err)
    27  			} else {
    28  				relayAddrs = append(relayAddrs, relayAddr)
    29  			}
    30  		}
    31  
    32  		if len(relayAddrs) == 0 {
    33  			log.Warn("Addresses via relay node are zero - using non-relay addresses")
    34  			return addrs
    35  		}
    36  		return append(addrs, relayAddrs...)
    37  	}
    38  }