|
|
@ -48,9 +48,6 @@ const ( |
|
|
|
|
|
|
|
|
|
|
|
// register to bootnode every ticker
|
|
|
|
// register to bootnode every ticker
|
|
|
|
dhtTicker = 6 * time.Hour |
|
|
|
dhtTicker = 6 * time.Hour |
|
|
|
|
|
|
|
|
|
|
|
// wait for peerinfo.
|
|
|
|
|
|
|
|
peerInfoWait = time.Second |
|
|
|
|
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
// New returns role conversion service.
|
|
|
|
// New returns role conversion service.
|
|
|
@ -164,46 +161,38 @@ func (s *Service) DoService() { |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
tick := time.NewTicker(dhtTicker) |
|
|
|
tick := time.NewTicker(dhtTicker) |
|
|
|
peerInfoTick := time.NewTicker(peerInfoWait) |
|
|
|
|
|
|
|
for { |
|
|
|
for { |
|
|
|
select { |
|
|
|
select { |
|
|
|
case <-peerInfoTick.C: |
|
|
|
case peer := <-s.peerInfo: |
|
|
|
select { |
|
|
|
if peer.ID != s.Host.GetP2PHost().ID() && len(peer.ID) > 0 { |
|
|
|
case peer := <-s.peerInfo: |
|
|
|
// utils.GetLogInstance().Info("Found Peer", "peer", peer.ID, "addr", peer.Addrs, "my ID", s.Host.GetP2PHost().ID())
|
|
|
|
if peer.ID != s.Host.GetP2PHost().ID() && len(peer.ID) > 0 { |
|
|
|
if err := s.Host.GetP2PHost().Connect(ctx, peer); err != nil { |
|
|
|
// utils.GetLogInstance().Info("Found Peer", "peer", peer.ID, "addr", peer.Addrs, "my ID", s.Host.GetP2PHost().ID())
|
|
|
|
utils.GetLogInstance().Warn("can't connect to peer node", "error", err, "peer", peer) |
|
|
|
if err := s.Host.GetP2PHost().Connect(ctx, peer); err != nil { |
|
|
|
// break if the node can't connect to peers, waiting for another peer
|
|
|
|
utils.GetLogInstance().Warn("can't connect to peer node", "error", err, "peer", peer) |
|
|
|
break |
|
|
|
// break if the node can't connect to peers, waiting for another peer
|
|
|
|
} else { |
|
|
|
break |
|
|
|
utils.GetLogInstance().Info("connected to peer node", "peer", peer) |
|
|
|
} else { |
|
|
|
} |
|
|
|
utils.GetLogInstance().Info("connected to peer node", "peer", peer) |
|
|
|
// figure out the public ip/port
|
|
|
|
} |
|
|
|
var ip, port string |
|
|
|
// figure out the public ip/port
|
|
|
|
|
|
|
|
var ip, port string |
|
|
|
for _, addr := range peer.Addrs { |
|
|
|
|
|
|
|
netaddr, err := manet.ToNetAddr(addr) |
|
|
|
for _, addr := range peer.Addrs { |
|
|
|
if err != nil { |
|
|
|
netaddr, err := manet.ToNetAddr(addr) |
|
|
|
continue |
|
|
|
if err != nil { |
|
|
|
|
|
|
|
continue |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
nip := netaddr.(*net.TCPAddr).IP |
|
|
|
|
|
|
|
if (nip.IsGlobalUnicast() && !utils.IsPrivateIP(nip)) || cgnPrefix.Contains(nip) { |
|
|
|
|
|
|
|
ip = nip.String() |
|
|
|
|
|
|
|
port = fmt.Sprintf("%d", netaddr.(*net.TCPAddr).Port) |
|
|
|
|
|
|
|
break |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
if ip != "" { |
|
|
|
nip := netaddr.(*net.TCPAddr).IP |
|
|
|
p := p2p.Peer{IP: ip, Port: port, PeerID: peer.ID, Addrs: peer.Addrs} |
|
|
|
if (nip.IsGlobalUnicast() && !utils.IsPrivateIP(nip)) || cgnPrefix.Contains(nip) { |
|
|
|
utils.GetLogInstance().Info("Notify peerChan", "peer", p) |
|
|
|
ip = nip.String() |
|
|
|
if s.peerChan != nil { |
|
|
|
port = fmt.Sprintf("%d", netaddr.(*net.TCPAddr).Port) |
|
|
|
s.peerChan <- p |
|
|
|
break |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
default: |
|
|
|
p := p2p.Peer{IP: ip, Port: port, PeerID: peer.ID, Addrs: peer.Addrs} |
|
|
|
utils.GetLogInstance().Info("Got no peer from peerInfo") |
|
|
|
utils.GetLogInstance().Info("Notify peerChan", "peer", p) |
|
|
|
|
|
|
|
if s.peerChan != nil { |
|
|
|
|
|
|
|
s.peerChan <- p |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
case <-s.stopChan: |
|
|
|
case <-s.stopChan: |
|
|
|
return |
|
|
|
return |
|
|
|