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

Improve peer hints for pin remote add #8143

Merged
merged 4 commits into from
Jun 25, 2021
Merged
Changes from 1 commit
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
44 changes: 40 additions & 4 deletions core/commands/pin/remotepin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"net"
"sort"
"strings"
"text/tabwriter"
Expand All @@ -24,6 +25,7 @@ import (
path "github.com/ipfs/interface-go-ipfs-core/path"
"github.com/libp2p/go-libp2p-core/host"
peer "github.com/libp2p/go-libp2p-core/peer"
ma "github.com/multiformats/go-multiaddr"
)

var log = logging.Logger("core/commands/cmdenv")
Expand Down Expand Up @@ -167,18 +169,36 @@ NOTE: a comma-separated notation is supported in CLI for convenience:
}

// Prepare Pin.origins
// Add own multiaddrs to the 'origins' array, so Pinning Service can
// use that as a hint and connect back to us (if possible)
// If CID in blockstore, add own multiaddrs to the 'origins' array
// so pinning service can use that as a hint and connect back to us.
node, err := cmdenv.GetNode(env)
if err != nil {
return err
}
if node.PeerHost != nil {

isInBlockstore, err := node.Blockstore.Has(rp.Cid())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lidel this seems fine. I don't think we're introducing any new problems on the CoreAPI/Node boundary, but wanted to double check. WDYT?

The boundary issues we generally run into are that IIRC accessing the node directly doesn't respect the --offline flag, however I think we may already have that issue given that we use node.PeerHost below.

if err != nil {
return err
}

if isInBlockstore && node.PeerHost != nil {
addrs, err := peer.AddrInfoToP2pAddrs(host.InfoFromHost(node.PeerHost))
if err != nil {
return err
}
opts = append(opts, pinclient.PinOpts.WithOrigins(addrs...))

cfg, err := cmdenv.GetConfig(env)
if err != nil {
return err
}

var filteredAddrs []ma.Multiaddr
for _, addr := range addrs {
if isOriginAddr(addr, cfg.Addresses.NoAnnounce) {
filteredAddrs = append(filteredAddrs, addr)
}
}
opts = append(opts, pinclient.PinOpts.WithOrigins(filteredAddrs...))
}

// Execute remote pin request
Expand Down Expand Up @@ -240,6 +260,22 @@ NOTE: a comma-separated notation is supported in CLI for convenience:
},
}

func isOriginAddr(addr ma.Multiaddr, noAnnounceAddrs []string) bool {
// IP is located at the second index: /ip4/127.0.0.1/tcp/8080
ip := net.ParseIP(strings.Split(addr.String(), "/")[2])
if ip.IsLoopback() {
return false
}

for _, noAnnounceAddr := range noAnnounceAddrs {
if strings.HasPrefix(addr.String(), noAnnounceAddr) {
return false
}
}
lidel marked this conversation as resolved.
Show resolved Hide resolved

return true
}

var listRemotePinCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "List objects pinned to remote pinning service.",
Expand Down