routing.go 4.33 KB
Newer Older
1 2 3
package namesys

import (
Jeromy's avatar
Jeromy committed
4
	"context"
5
	"strings"
6
	"time"
7

Dirk McCormick's avatar
Dirk McCormick committed
8
	opts "github.com/ipfs/go-ipfs/namesys/opts"
9
	pb "github.com/ipfs/go-ipfs/namesys/pb"
10
	path "github.com/ipfs/go-ipfs/path"
Jeromy's avatar
Jeromy committed
11

Steven Allen's avatar
Steven Allen committed
12
	u "gx/ipfs/QmNiJuT8Ja3hMVpBHXv3Q6dwmperaQ6JjLtpMQgMCD7xvx/go-ipfs-util"
Steven Allen's avatar
Steven Allen committed
13 14
	logging "gx/ipfs/QmTG23dvpBCBjqQwyDxV8CQT6jmS4PSftNr1VqHhE3MLy7/go-log"
	routing "gx/ipfs/QmUHRKTeaoASDvDj7cTAXsmjAY7KQ13ErtzkQHZQq6uFUz/go-libp2p-routing"
Jeromy's avatar
Jeromy committed
15
	proto "gx/ipfs/QmZ4Qi3GaRbjcx28Sme5eMH7RQjGkt8wHxt2a65oLaeFEV/gogo-protobuf/proto"
Dirk McCormick's avatar
go fmt  
Dirk McCormick committed
16
	mh "gx/ipfs/QmZyZDi491cCNTLfAhwcaDii2Kg4pwKRkhqQzURGDvY6ua/go-multihash"
Steven Allen's avatar
Steven Allen committed
17
	peer "gx/ipfs/QmcJukH2sAFjY3HdBKq35WDzWoL3UUu2gt9wdfqZTUyM74/go-libp2p-peer"
Steven Allen's avatar
Steven Allen committed
18
	cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
19
	dht "gx/ipfs/Qmd3jqhBQFvhfBNTSJMQL15GgyVMpdxKTta69Napvx6Myd/go-libp2p-kad-dht"
20 21
)

Jeromy's avatar
Jeromy committed
22
var log = logging.Logger("namesys")
23

Steven Allen's avatar
Steven Allen committed
24 25
// IpnsResolver implements NSResolver for the main IPFS SFS-like naming
type IpnsResolver struct {
26
	routing routing.ValueStore
27 28
}

Steven Allen's avatar
Steven Allen committed
29
// NewIpnsResolver constructs a name resolver using the IPFS Routing system
30
// to implement SFS-like naming on top.
Steven Allen's avatar
Steven Allen committed
31
func NewIpnsResolver(route routing.ValueStore) *IpnsResolver {
32 33 34
	if route == nil {
		panic("attempt to create resolver with nil routing system")
	}
Steven Allen's avatar
Steven Allen committed
35
	return &IpnsResolver{
36 37
		routing: route,
	}
Jeromy's avatar
Jeromy committed
38 39
}

40
// Resolve implements Resolver.
Steven Allen's avatar
Steven Allen committed
41
func (r *IpnsResolver) Resolve(ctx context.Context, name string, options ...opts.ResolveOpt) (path.Path, error) {
Dirk McCormick's avatar
Dirk McCormick committed
42
	return resolve(ctx, r, name, opts.ProcessOpts(options), "/ipns/")
43 44 45 46
}

// resolveOnce implements resolver. Uses the IPFS routing system to
// resolve SFS-like names.
Steven Allen's avatar
Steven Allen committed
47
func (r *IpnsResolver) resolveOnce(ctx context.Context, name string, options *opts.ResolveOpts) (path.Path, time.Duration, error) {
48
	log.Debugf("RoutingResolver resolving %s", name)
49

Dirk McCormick's avatar
Dirk McCormick committed
50
	if options.DhtTimeout != 0 {
51 52
		// Resolution must complete within the timeout
		var cancel context.CancelFunc
Dirk McCormick's avatar
Dirk McCormick committed
53
		ctx, cancel = context.WithTimeout(ctx, options.DhtTimeout)
54 55 56
		defer cancel()
	}

57
	name = strings.TrimPrefix(name, "/ipns/")
58 59
	hash, err := mh.FromB58String(name)
	if err != nil {
Jeromy's avatar
Jeromy committed
60
		// name should be a multihash. if it isn't, error out here.
61
		log.Debugf("RoutingResolver: bad input hash: [%s]\n", name)
Steven Allen's avatar
Steven Allen committed
62
		return "", 0, err
63 64
	}

65 66 67
	pid, err := peer.IDFromBytes(hash)
	if err != nil {
		log.Debugf("RoutingResolver: could not convert public key hash %s to peer ID: %s\n", name, err)
Steven Allen's avatar
Steven Allen committed
68
		return "", 0, err
69 70
	}

71 72 73 74 75
	// Name should be the hash of a public key retrievable from ipfs.
	// We retrieve the public key here to make certain that it's in the peer
	// store before calling GetValue() on the DHT - the DHT will call the
	// ipns validator, which in turn will get the public key from the peer
	// store to verify the record signature
76
	_, err = routing.GetPublicKey(r.routing, ctx, pid)
77 78
	if err != nil {
		log.Debugf("RoutingResolver: could not retrieve public key %s: %s\n", name, err)
Steven Allen's avatar
Steven Allen committed
79
		return "", 0, err
80
	}
Jeromy's avatar
Jeromy committed
81

82 83 84
	// Use the routing system to get the name.
	// Note that the DHT will call the ipns validator when retrieving
	// the value, which in turn verifies the ipns record signature
85
	_, ipnsKey := IpnsKeysForID(pid)
86
	val, err := r.routing.GetValue(ctx, ipnsKey, dht.Quorum(int(options.DhtRecordCount)))
87 88
	if err != nil {
		log.Debugf("RoutingResolver: dht get for name %s failed: %s", name, err)
Steven Allen's avatar
Steven Allen committed
89
		return "", 0, err
90 91
	}

92 93 94 95
	entry := new(pb.IpnsEntry)
	err = proto.Unmarshal(val, entry)
	if err != nil {
		log.Debugf("RoutingResolver: could not unmarshal value for name %s: %s", name, err)
Steven Allen's avatar
Steven Allen committed
96
		return "", 0, err
97
	}
98

99
	var p path.Path
100
	// check for old style record:
101 102 103 104 105
	if valh, err := mh.Cast(entry.GetValue()); err == nil {
		// Its an old style multihash record
		log.Debugf("encountered CIDv0 ipns entry: %s", valh)
		p = path.FromCid(cid.NewCidV0(valh))
	} else {
106
		// Not a multihash, probably a new record
107
		p, err = path.ParsePath(string(entry.GetValue()))
108
		if err != nil {
Steven Allen's avatar
Steven Allen committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
			return "", 0, err
		}
	}

	ttl := DefaultResolverCacheTTL
	if entry.Ttl != nil {
		ttl = time.Duration(*entry.Ttl)
	}
	if eol, ok := checkEOL(entry); ok {
		ttEol := eol.Sub(time.Now())
		if ttEol < 0 {
			// It *was* valid when we first resolved it.
			ttl = 0
		} else if ttEol < ttl {
			ttl = ttEol
124 125
		}
	}
126

Steven Allen's avatar
Steven Allen committed
127
	return p, ttl, nil
128 129 130 131 132 133 134 135 136
}

func checkEOL(e *pb.IpnsEntry) (time.Time, bool) {
	if e.GetValidityType() == pb.IpnsEntry_EOL {
		eol, err := u.ParseRFC3339(string(e.GetValidity()))
		if err != nil {
			return time.Time{}, false
		}
		return eol, true
137
	}
138
	return time.Time{}, false
139
}