forked from ipfs/kubo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routing.go
148 lines (129 loc) · 4.01 KB
/
routing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package namesys
import (
"context"
"strings"
"time"
proto "github.com/gogo/protobuf/proto"
cid "github.com/ipfs/go-cid"
ipns "github.com/ipfs/go-ipns"
pb "github.com/ipfs/go-ipns/pb"
logging "github.com/ipfs/go-log"
path "github.com/ipfs/go-path"
opts "github.com/ipfs/interface-go-ipfs-core/options/namesys"
peer "github.com/libp2p/go-libp2p-core/peer"
routing "github.com/libp2p/go-libp2p-core/routing"
dht "github.com/libp2p/go-libp2p-kad-dht"
mh "github.com/multiformats/go-multihash"
)
var log = logging.Logger("namesys")
// IpnsResolver implements NSResolver for the main IPFS SFS-like naming
type IpnsResolver struct {
routing routing.ValueStore
}
// NewIpnsResolver constructs a name resolver using the IPFS Routing system
// to implement SFS-like naming on top.
func NewIpnsResolver(route routing.ValueStore) *IpnsResolver {
if route == nil {
panic("attempt to create resolver with nil routing system")
}
return &IpnsResolver{
routing: route,
}
}
// Resolve implements Resolver.
func (r *IpnsResolver) Resolve(ctx context.Context, name string, options ...opts.ResolveOpt) (path.Path, error) {
return resolve(ctx, r, name, opts.ProcessOpts(options))
}
// ResolveAsync implements Resolver.
func (r *IpnsResolver) ResolveAsync(ctx context.Context, name string, options ...opts.ResolveOpt) <-chan Result {
return resolveAsync(ctx, r, name, opts.ProcessOpts(options))
}
// resolveOnce implements resolver. Uses the IPFS routing system to
// resolve SFS-like names.
func (r *IpnsResolver) resolveOnceAsync(ctx context.Context, name string, options opts.ResolveOpts) <-chan onceResult {
out := make(chan onceResult, 1)
log.Debugf("RoutingResolver resolving %s", name)
cancel := func() {}
if options.DhtTimeout != 0 {
// Resolution must complete within the timeout
ctx, cancel = context.WithTimeout(ctx, options.DhtTimeout)
}
name = strings.TrimPrefix(name, "/ipns/")
pid, err := peer.Decode(name)
if err != nil {
log.Debugf("RoutingResolver: could not convert public key hash %s to peer ID: %s\n", name, err)
out <- onceResult{err: err}
close(out)
cancel()
return out
}
// 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
ipnsKey := ipns.RecordKey(pid)
vals, err := r.routing.SearchValue(ctx, ipnsKey, dht.Quorum(int(options.DhtRecordCount)))
if err != nil {
log.Debugf("RoutingResolver: dht get for name %s failed: %s", name, err)
out <- onceResult{err: err}
close(out)
cancel()
return out
}
go func() {
defer cancel()
defer close(out)
for {
select {
case val, ok := <-vals:
if !ok {
return
}
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)
emitOnceResult(ctx, out, onceResult{err: err})
return
}
var p path.Path
// check for old style record:
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 {
// Not a multihash, probably a new style record
p, err = path.ParsePath(string(entry.GetValue()))
if err != nil {
emitOnceResult(ctx, out, onceResult{err: err})
return
}
}
ttl := DefaultResolverCacheTTL
if entry.Ttl != nil {
ttl = time.Duration(*entry.Ttl)
}
switch eol, err := ipns.GetEOL(entry); err {
case ipns.ErrUnrecognizedValidity:
// No EOL.
case nil:
ttEol := time.Until(eol)
if ttEol < 0 {
// It *was* valid when we first resolved it.
ttl = 0
} else if ttEol < ttl {
ttl = ttEol
}
default:
log.Errorf("encountered error when parsing EOL: %s", err)
emitOnceResult(ctx, out, onceResult{err: err})
return
}
emitOnceResult(ctx, out, onceResult{value: p, ttl: ttl})
case <-ctx.Done():
return
}
}
}()
return out
}