diff --git a/core/node.go b/core/node.go index fd52bd6..a715d08 100644 --- a/core/node.go +++ b/core/node.go @@ -11,10 +11,8 @@ import ( "context" "crypto/rand" "fmt" - "net" "os" "path/filepath" - "strings" "sync/atomic" "time" @@ -104,18 +102,15 @@ type P2P interface { // SetBootstraps updates the host's bootstrap list SetBootstraps(bootstrap []string) + // + GetDht() *dht.IpfsDHT + // DHTFindPeer searches for a peer with given ID DHTFindPeer(peerid string) (peer.AddrInfo, error) // PeerID returns your own peerid PeerID() peer.ID - // TryAddPeerToRoutingTable tries to add a peer to the Routing table - TryAddPeerToRoutingTable(peerid peer.ID) (bool, error) - - // RemovePeerFromRoutingTable remove peer from routing table - RemovePeerFromRoutingTable(peerid peer.ID) - // Close p2p Close() error @@ -479,16 +474,6 @@ func (n *Node) PeerID() peer.ID { return n.IpfsDHT.PeerID() } -// TryAddPeerToRoutingTable tries to add a peer to the routing table -func (n *Node) TryAddPeerToRoutingTable(peerid peer.ID) (bool, error) { - return n.IpfsDHT.RoutingTable().TryAddPeer(peerid, true, false) -} - -// RemovePeerFromRoutingTable remove peer from routing table -func (n *Node) RemovePeerFromRoutingTable(peerid peer.ID) { - n.IpfsDHT.RoutingTable().RemovePeer(peerid) -} - // GetDiscoveredPeers func (n *Node) GetDiscoveredPeers() <-chan *routing.QueryEvent { return n.discoveredPeerCh @@ -636,6 +621,14 @@ func (n *Node) SetBootstraps(bootstrap []string) { n.bootstrap = bootstrap } +func (n *Node) GetDht() *dht.IpfsDHT { + return n.IpfsDHT +} + +func (n *Node) GetRoutingTable() *drouting.RoutingDiscovery { + return n.RoutingDiscovery +} + func (n *Node) GetCtxRoot() context.Context { return n.ctxRoot } @@ -738,14 +731,9 @@ func identification(workspace, fpath string) (crypto.PrivKey, error) { func mkdir(workspace string) (DataDirs, error) { dataDir := DataDirs{ - FileDir: filepath.Join(workspace, FileDataDirectionry), - TmpDir: filepath.Join(workspace, TmpDataDirectionry), - //IdleDataDir: filepath.Join(workspace, IdleDataDirectionry), - //IdleTagDir: filepath.Join(workspace, IdleTagDirectionry), + FileDir: filepath.Join(workspace, FileDataDirectionry), + TmpDir: filepath.Join(workspace, TmpDataDirectionry), ServiceTagDir: filepath.Join(workspace, ServiceTagDirectionry), - //ProofDir: filepath.Join(workspace, ProofDirectionry), - //IproofFile: filepath.Join(workspace, ProofDirectionry, IdleProofFile), - //SproofFile: filepath.Join(workspace, ProofDirectionry, ServiceProofFile), } if err := os.MkdirAll(dataDir.FileDir, DirMode); err != nil { return dataDir, err @@ -810,18 +798,6 @@ func (n *Node) SendMsgToStream(s network.Stream, msg []byte) error { return err } -// IsIPv4 is used to determine whether ipAddr is an ipv4 address -func isIPv4(ipAddr string) bool { - ip := net.ParseIP(ipAddr) - return ip != nil && strings.Contains(ipAddr, ".") -} - -// IsIPv6 is used to determine whether ipAddr is an ipv6 address -func isIPv6(ipAddr string) bool { - ip := net.ParseIP(ipAddr) - return ip != nil && strings.Contains(ipAddr, ":") -} - func verifyWorkspace(ws string) error { if ws == "" { return fmt.Errorf("empty workspace") @@ -889,8 +865,12 @@ func (n *Node) initDHT() error { if err != nil { continue } - peerinfo, _ := peer.AddrInfoFromP2pAddr(bootstrapAddr) + peerinfo, err := peer.AddrInfoFromP2pAddr(bootstrapAddr) + if err != nil { + continue + } kademliaDHT.RoutingTable().PeerAdded(peerinfo.ID) + n.AddMultiaddrToPeerstore(bootstrapAddr.String(), peerstore.PermanentAddrTTL) err = n.host.Connect(n.ctxQueryFromCtxCancel, *peerinfo) if err != nil { out.Err(fmt.Sprintf("Connection to boot node failed: %s", peerinfo.ID.Pretty())) diff --git a/core/utils.go b/core/utils.go index 75f1f25..c5789ad 100644 --- a/core/utils.go +++ b/core/utils.go @@ -110,7 +110,7 @@ func GetExternalIp() (string, error) { defer resp.Body.Close() b, _ := io.ReadAll(resp.Body) externalIp = fmt.Sprintf("%s", string(b)) - if isIPv4(externalIp) { + if IsIPv4(externalIp) { return externalIp, nil } } @@ -121,7 +121,7 @@ func GetExternalIp() (string, error) { if err == nil { externalIp = strings.ReplaceAll(string(output), "\n", "") externalIp = strings.ReplaceAll(externalIp, " ", "") - if isIPv4(externalIp) { + if IsIPv4(externalIp) { return externalIp, nil } } @@ -132,7 +132,7 @@ func GetExternalIp() (string, error) { if err == nil { externalIp = strings.ReplaceAll(string(output), "\n", "") externalIp = strings.ReplaceAll(externalIp, " ", "") - if isIPv4(externalIp) { + if IsIPv4(externalIp) { return externalIp, nil } } @@ -144,7 +144,7 @@ func GetExternalIp() (string, error) { externalIp = strings.ReplaceAll(string(output), "\"", "") externalIp = strings.ReplaceAll(externalIp, ",", "") externalIp = strings.ReplaceAll(externalIp, "\n", "") - if isIPv4(externalIp) { + if IsIPv4(externalIp) { return externalIp, nil } }