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

     1  package peer
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/turingchain2020/turingchain/queue"
     8  	"github.com/turingchain2020/turingchain/system/p2p/dht/protocol"
     9  	"github.com/turingchain2020/turingchain/types"
    10  	"github.com/libp2p/go-libp2p-core/network"
    11  	"github.com/multiformats/go-multiaddr"
    12  )
    13  
    14  func (p *Protocol) handleStreamPeerInfo(stream network.Stream) {
    15  	peerInfo := p.getLocalPeerInfo()
    16  	err := protocol.WriteStream(peerInfo, stream)
    17  	if err != nil {
    18  		log.Error("handleStreamPeerInfo", "WriteStream error", err)
    19  		return
    20  	}
    21  }
    22  
    23  func (p *Protocol) handleStreamVersion(stream network.Stream) {
    24  	var req types.P2PVersion
    25  	err := protocol.ReadStream(&req, stream)
    26  	if err != nil {
    27  		log.Error("handleStreamVersion", "read stream error", err)
    28  		return
    29  	}
    30  	if req.GetVersion() != p.SubConfig.Channel {
    31  		// 不是同一条链,拉黑且断开连接
    32  		p.ConnBlackList.Add(stream.Conn().RemotePeer().Pretty(), time.Hour*24)
    33  		_ = stream.Conn().Close()
    34  		return
    35  	}
    36  
    37  	if ip, _ := parseIPAndPort(req.GetAddrFrom()); isPublicIP(ip) {
    38  		remoteMAddr, err := multiaddr.NewMultiaddr(req.GetAddrFrom())
    39  		if err != nil {
    40  			return
    41  		}
    42  		p.Host.Peerstore().AddAddr(stream.Conn().RemotePeer(), remoteMAddr, time.Hour*24)
    43  	}
    44  
    45  	p.setExternalAddr(req.GetAddrRecv())
    46  	resp := &types.P2PVersion{
    47  		AddrFrom:  p.getExternalAddr(),
    48  		AddrRecv:  stream.Conn().RemoteMultiaddr().String(),
    49  		Timestamp: time.Now().Unix(),
    50  	}
    51  	err = protocol.WriteStream(resp, stream)
    52  	if err != nil {
    53  		log.Error("handleStreamVersion", "WriteStream error", err)
    54  		return
    55  	}
    56  }
    57  
    58  func (p *Protocol) handleStreamPeerInfoOld(stream network.Stream) {
    59  	var req types.MessagePeerInfoReq
    60  	err := protocol.ReadStream(&req, stream)
    61  	if err != nil {
    62  		log.Error("handleStreamPeerInfoOld", "read stream error", err)
    63  		return
    64  	}
    65  
    66  	peerInfo := p.getLocalPeerInfo()
    67  	pInfo := &types.P2PPeerInfo{
    68  		Addr:           peerInfo.Addr,
    69  		Port:           peerInfo.Port,
    70  		Name:           peerInfo.Name,
    71  		MempoolSize:    peerInfo.MempoolSize,
    72  		Header:         peerInfo.Header,
    73  		Version:        peerInfo.Version,
    74  		LocalDBVersion: peerInfo.LocalDBVersion,
    75  		StoreDBVersion: peerInfo.StoreDBVersion,
    76  	}
    77  	err = protocol.WriteStream(&types.MessagePeerInfoResp{
    78  		Message: pInfo,
    79  	}, stream)
    80  	if err != nil {
    81  		log.Error("handleStreamPeerInfo", "WriteStream error", err)
    82  		return
    83  	}
    84  }
    85  
    86  func (p *Protocol) handleStreamVersionOld(stream network.Stream) {
    87  	var req types.MessageP2PVersionReq
    88  	err := protocol.ReadStream(&req, stream)
    89  	if err != nil {
    90  		log.Error("handleStreamVersion", "read stream error", err)
    91  		return
    92  	}
    93  	msg := req.Message
    94  	if msg.GetVersion() != p.SubConfig.Channel {
    95  		// 不是同一条链,拉黑且断开连接
    96  		p.ConnBlackList.Add(stream.Conn().RemotePeer().Pretty(), time.Hour*24)
    97  		_ = stream.Conn().Close()
    98  		return
    99  	}
   100  	if ip, _ := parseIPAndPort(msg.GetAddrFrom()); isPublicIP(ip) {
   101  		remoteMAddr, err := multiaddr.NewMultiaddr(msg.GetAddrFrom())
   102  		if err != nil {
   103  			return
   104  		}
   105  		p.Host.Peerstore().AddAddr(stream.Conn().RemotePeer(), remoteMAddr, time.Hour*24)
   106  	}
   107  
   108  	p.setExternalAddr(msg.GetAddrRecv())
   109  	resp := &types.MessageP2PVersionResp{
   110  		Message: &types.P2PVersion{
   111  			AddrFrom:  p.getExternalAddr(),
   112  			AddrRecv:  stream.Conn().RemoteMultiaddr().String(),
   113  			Timestamp: time.Now().Unix(),
   114  		},
   115  	}
   116  	err = protocol.WriteStream(resp, stream)
   117  	if err != nil {
   118  		log.Error("handleStreamVersion", "WriteStream error", err)
   119  		return
   120  	}
   121  }
   122  
   123  func (p *Protocol) handleEventPeerInfo(msg *queue.Message) {
   124  	peers := p.PeerInfoManager.FetchAll()
   125  	msg.Reply(p.QueueClient.NewMessage("blockchain", types.EventPeerList, &types.PeerList{Peers: peers}))
   126  }
   127  
   128  func (p *Protocol) handleEventNetProtocols(msg *queue.Message) {
   129  	//all protocols net info
   130  	bandProtocols := p.ConnManager.BandTrackerByProtocol()
   131  	allProtocolNetInfo, _ := json.MarshalIndent(bandProtocols, "", "\t")
   132  	log.Debug("handleEventNetInfo", "allProtocolNetInfo", string(allProtocolNetInfo))
   133  	msg.Reply(p.QueueClient.NewMessage("rpc", types.EventNetProtocols, bandProtocols))
   134  }
   135  
   136  func (p *Protocol) handleEventNetInfo(msg *queue.Message) {
   137  	insize, outsize := p.ConnManager.BoundSize()
   138  	var netinfo types.NodeNetInfo
   139  
   140  	netinfo.Externaladdr = p.getPublicIP()
   141  	localips, _ := localIPv4s()
   142  	if len(localips) != 0 {
   143  		log.Info("handleEventNetInfo", "localIps", localips)
   144  		netinfo.Localaddr = localips[0]
   145  	} else {
   146  		netinfo.Localaddr = netinfo.Externaladdr
   147  	}
   148  
   149  	netinfo.Outbounds = int32(outsize)
   150  	netinfo.Inbounds = int32(insize)
   151  	netinfo.Service = false
   152  	if netinfo.Inbounds != 0 {
   153  		netinfo.Service = true
   154  	}
   155  	netinfo.Peerstore = int32(len(p.Host.Peerstore().PeersWithAddrs()))
   156  	netinfo.Routingtable = int32(p.RoutingTable.Size())
   157  	netstat := p.ConnManager.GetNetRate()
   158  
   159  	netinfo.Ratein = p.ConnManager.RateCalculate(netstat.RateIn)
   160  	netinfo.Rateout = p.ConnManager.RateCalculate(netstat.RateOut)
   161  	netinfo.Ratetotal = p.ConnManager.RateCalculate(netstat.RateOut + netstat.RateIn)
   162  	msg.Reply(p.QueueClient.NewMessage("rpc", types.EventReplyNetInfo, &netinfo))
   163  }