github.com/aergoio/aergo@v1.3.1/p2p/pi.go (about)

     1  /*
     2   * @file
     3   * @copyright defined in aergo/LICENSE.txt
     4   */
     5  
     6  package p2p
     7  
     8  import (
     9  	"github.com/aergoio/aergo/config"
    10  	"github.com/aergoio/aergo/internal/network"
    11  	"github.com/aergoio/aergo/p2p/p2pcommon"
    12  	"github.com/aergoio/aergo/p2p/p2pkey"
    13  	"github.com/aergoio/aergo/p2p/p2putil"
    14  	"github.com/aergoio/aergo/types"
    15  	"net"
    16  	"strconv"
    17  )
    18  
    19  func SetupSelfMeta(peerID types.PeerID, conf *config.P2PConfig) p2pcommon.PeerMeta {
    20  	protocolAddr := conf.NetProtocolAddr
    21  	var ipAddress net.IP
    22  	var err error
    23  	var protocolPort int
    24  	if len(conf.NetProtocolAddr) != 0 {
    25  		ipAddress, err = network.GetSingleIPAddress(protocolAddr)
    26  		if err != nil {
    27  			panic("Invalid protocol address " + protocolAddr + " : " + err.Error())
    28  		}
    29  		if ipAddress.IsUnspecified() {
    30  			panic("NetProtocolAddr should be a specified IP address, not 0.0.0.0")
    31  		}
    32  	} else {
    33  		extIP, err := p2putil.ExternalIP()
    34  		if err != nil {
    35  			panic("error while finding IP address: " + err.Error())
    36  		}
    37  		ipAddress = extIP
    38  		protocolAddr = ipAddress.String()
    39  	}
    40  	protocolPort = conf.NetProtocolPort
    41  	if protocolPort <= 0 {
    42  		panic("invalid NetProtocolPort " + strconv.Itoa(conf.NetProtocolPort))
    43  	}
    44  	var meta p2pcommon.PeerMeta
    45  	meta.IPAddress = protocolAddr
    46  	meta.Port = uint32(protocolPort)
    47  	meta.ID = peerID
    48  	meta.Hidden = !conf.NPExposeSelf
    49  	meta.Version = p2pkey.NodeVersion()
    50  
    51  	return meta
    52  }