Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update p2p #74

Merged
merged 1 commit into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 18 additions & 38 deletions core/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ import (
"context"
"crypto/rand"
"fmt"
"net"
"os"
"path/filepath"
"strings"
"sync/atomic"
"time"

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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()))
Expand Down
8 changes: 4 additions & 4 deletions core/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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
}
}
Expand All @@ -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
}
}
Expand All @@ -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
}
}
Expand Down