github.com/turingchain2020/turingchain@v1.1.21/system/p2p/dht/bootstrap.go (about)

     1  package dht
     2  
     3  import (
     4  	"context"
     5  
     6  	p2pty "github.com/turingchain2020/turingchain/system/p2p/dht/types"
     7  	"github.com/libp2p/go-libp2p-core/host"
     8  	"github.com/libp2p/go-libp2p-core/peer"
     9  	"github.com/libp2p/go-libp2p-core/peerstore"
    10  	"github.com/multiformats/go-multiaddr"
    11  )
    12  
    13  func initInnerPeers(host host.Host, peersInfo []peer.AddrInfo, cfg *p2pty.P2PSubConfig) {
    14  
    15  	for _, node := range cfg.Seeds {
    16  		info := genAddrInfo(node)
    17  		if info == nil || info.ID == host.ID() {
    18  			continue
    19  		}
    20  		host.Peerstore().AddAddrs(info.ID, info.Addrs, peerstore.PermanentAddrTTL)
    21  		err := host.Connect(context.Background(), *info)
    22  		if err != nil {
    23  			log.Error("Host Connect", "err", err, "peer", info.ID)
    24  			continue
    25  		}
    26  		//加保护
    27  		host.ConnManager().Protect(info.ID, "seed")
    28  	}
    29  
    30  	for _, node := range cfg.BootStraps {
    31  		info := genAddrInfo(node)
    32  		if info == nil || info.ID == host.ID() {
    33  			continue
    34  		}
    35  		host.Peerstore().AddAddrs(info.ID, info.Addrs, peerstore.PermanentAddrTTL)
    36  		err := host.Connect(context.Background(), *info)
    37  		if err != nil {
    38  			log.Error("Host Connect", "err", err)
    39  			continue
    40  		}
    41  	}
    42  
    43  	//连接配置的relay中继服务器
    44  	if cfg.RelayEnable {
    45  		for _, relay := range cfg.RelayNodeAddr {
    46  			info := genAddrInfo(relay)
    47  			if info == nil || info.ID == host.ID() {
    48  				continue
    49  			}
    50  			host.Peerstore().AddAddrs(info.ID, info.Addrs, peerstore.PermanentAddrTTL)
    51  			err := host.Connect(context.Background(), *info)
    52  			if err != nil {
    53  				log.Error("Host Connect", "err", err, "peer", info.ID)
    54  				continue
    55  			}
    56  			//加保护
    57  			host.ConnManager().Protect(info.ID, "relayNode")
    58  		}
    59  	}
    60  
    61  	for _, peerinfo := range peersInfo {
    62  		host.Peerstore().AddAddrs(peerinfo.ID, peerinfo.Addrs, peerstore.TempAddrTTL)
    63  		err := host.Connect(context.Background(), peerinfo)
    64  		if err != nil {
    65  			log.Error("Host Connect", "err", err)
    66  			continue
    67  		}
    68  	}
    69  }
    70  
    71  func genAddrInfo(addr string) *peer.AddrInfo {
    72  	mAddr, err := multiaddr.NewMultiaddr(addr)
    73  	if err != nil {
    74  		return nil
    75  	}
    76  	peerInfo, err := peer.AddrInfoFromP2pAddr(mAddr)
    77  	if err != nil {
    78  		return nil
    79  	}
    80  	return peerInfo
    81  }
    82  
    83  func genAddrInfos(addrs []string) []*peer.AddrInfo {
    84  	if len(addrs) == 0 {
    85  		return nil
    86  	}
    87  	var infos []*peer.AddrInfo
    88  	for _, addr := range addrs {
    89  		info := genAddrInfo(addr)
    90  		if info != nil {
    91  			infos = append(infos, info)
    92  		}
    93  	}
    94  	return infos
    95  }