@cryptotaxi247 / kubo / commits / 848d4c7f1

feat: IPFS_NS_MAP

Allows static DNSLink mappings with IPFS_NS_MAP. License: MIT Signed-off-by: Marcin Rataj <lidel@lidel.org>

Marcin Rataj committed Mar 7, 2020 at 01:56 UTC 848d4c7f18649b4c994af01dd6fb6b4764cb1127
3 files changed +45 -2
docs/environment-variables.md
+14
@@ -82,6 +82,20 @@ the `--migrate` flag).
82
83 Default: https://ipfs.io/ipfs/$something (depends on the IPFS version)
84
85 +## `IPFS_NS_MAP`
86 +
87 +Prewarms namesys cache with static records for deteministic tests and debugging.
88 +Useful for testing things like DNSLink without real DNS lookup.
89 +
90 +Example:
91 +
92 +```console
93 +$ IPFS_NS_MAP="dnslink-test1.example.com:/ipfs/bafkreicysg23kiwv34eg2d7qweipxwosdo2py4ldv42nbauguluen5v6am,dnslink-test2.example.com:/ipns/dnslink-test1.example.com" ipfs daemon
94 +...
95 +$ ipfs resolve -r /ipns/dnslink-test2.example.com
96 +/ipfs/bafkreicysg23kiwv34eg2d7qweipxwosdo2py4ldv42nbauguluen5v6am
97 +```
98 +
99 ## `LIBP2P_MUX_PREFS`
100
101 Tells go-ipfs which multiplexers to use in which order.
namesys/cache.go
+8
@@ -7,6 +7,14 @@ import (
7 )
8
9 func (ns *mpns) cacheGet(name string) (path.Path, bool) {
10 + // existence of optional mapping defined via IPFS_NS_MAP is checked first
11 + if ns.staticMap != nil {
12 + val, ok := ns.staticMap[name]
13 + if ok {
14 + return val, true
15 + }
16 + }
17 +
18 if ns.cache == nil {
19 return "", false
20 }
namesys/namesys.go
+23 -2
@@ -2,6 +2,7 @@ package namesys
2
3 import (
4 "context"
5 + "os"
6 "strings"
7 "time"
8
@@ -29,25 +30,45 @@ type mpns struct {
30 dnsResolver, proquintResolver, ipnsResolver resolver
31 ipnsPublisher Publisher
32
32 - cache *lru.Cache
33 + staticMap map[string]path.Path
34 + cache *lru.Cache
35 }
36
37 // NewNameSystem will construct the IPFS naming system based on Routing
38 func NewNameSystem(r routing.ValueStore, ds ds.Datastore, cachesize int) NameSystem {
37 - var cache *lru.Cache
39 + var (
40 + cache *lru.Cache
41 + staticMap map[string]path.Path
42 + )
43 if cachesize > 0 {
44 cache, _ = lru.New(cachesize)
45 }
46
47 + // Prewarm namesys cache with static records for deteministic tests and debugging.
48 + // Useful for testing things like DNSLink without real DNS lookup.
49 + // Example:
50 + // IPFS_NS_MAP="dnslink-test.example.com:/ipfs/bafkreicysg23kiwv34eg2d7qweipxwosdo2py4ldv42nbauguluen5v6am"
51 + if list := os.Getenv("IPFS_NS_MAP"); list != "" {
52 + staticMap = make(map[string]path.Path)
53 + for _, pair := range strings.Split(list, ",") {
54 + mapping := strings.SplitN(pair, ":", 2)
55 + key := mapping[0]
56 + value := path.FromString(mapping[1])
57 + staticMap[key] = value
58 + }
59 + }
60 +
61 return &mpns{
62 dnsResolver: NewDNSResolver(),
63 proquintResolver: new(ProquintResolver),
64 ipnsResolver: NewIpnsResolver(r),
65 ipnsPublisher: NewIpnsPublisher(r, ds),
66 + staticMap: staticMap,
67 cache: cache,
68 }
69 }
70
71 +// DefaultResolverCacheTTL defines max ttl of a record placed in namesys cache.
72 const DefaultResolverCacheTTL = time.Minute
73
74 // Resolve implements Resolver.