From 848d4c7f18649b4c994af01dd6fb6b4764cb1127 Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Sat, 7 Mar 2020 01:56:48 +0100 Subject: [PATCH] feat: IPFS_NS_MAP Allows static DNSLink mappings with IPFS_NS_MAP. License: MIT Signed-off-by: Marcin Rataj --- docs/environment-variables.md | 14 ++++++++++++++ namesys/cache.go | 8 ++++++++ namesys/namesys.go | 25 +++++++++++++++++++++++-- 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/docs/environment-variables.md b/docs/environment-variables.md index 82eb5c3126c..fdc92f461c3 100644 --- a/docs/environment-variables.md +++ b/docs/environment-variables.md @@ -82,6 +82,20 @@ the `--migrate` flag). Default: https://ipfs.io/ipfs/$something (depends on the IPFS version) +## `IPFS_NS_MAP` + +Prewarms namesys cache with static records for deteministic tests and debugging. +Useful for testing things like DNSLink without real DNS lookup. + +Example: + +```console +$ IPFS_NS_MAP="dnslink-test1.example.com:/ipfs/bafkreicysg23kiwv34eg2d7qweipxwosdo2py4ldv42nbauguluen5v6am,dnslink-test2.example.com:/ipns/dnslink-test1.example.com" ipfs daemon +... +$ ipfs resolve -r /ipns/dnslink-test2.example.com +/ipfs/bafkreicysg23kiwv34eg2d7qweipxwosdo2py4ldv42nbauguluen5v6am +``` + ## `LIBP2P_MUX_PREFS` Tells go-ipfs which multiplexers to use in which order. diff --git a/namesys/cache.go b/namesys/cache.go index 4a5cb5113ae..a0029829d33 100644 --- a/namesys/cache.go +++ b/namesys/cache.go @@ -7,6 +7,14 @@ import ( ) func (ns *mpns) cacheGet(name string) (path.Path, bool) { + // existence of optional mapping defined via IPFS_NS_MAP is checked first + if ns.staticMap != nil { + val, ok := ns.staticMap[name] + if ok { + return val, true + } + } + if ns.cache == nil { return "", false } diff --git a/namesys/namesys.go b/namesys/namesys.go index 079eecccc03..11f4646f174 100644 --- a/namesys/namesys.go +++ b/namesys/namesys.go @@ -2,6 +2,7 @@ package namesys import ( "context" + "os" "strings" "time" @@ -29,25 +30,45 @@ type mpns struct { dnsResolver, proquintResolver, ipnsResolver resolver ipnsPublisher Publisher - cache *lru.Cache + staticMap map[string]path.Path + cache *lru.Cache } // NewNameSystem will construct the IPFS naming system based on Routing func NewNameSystem(r routing.ValueStore, ds ds.Datastore, cachesize int) NameSystem { - var cache *lru.Cache + var ( + cache *lru.Cache + staticMap map[string]path.Path + ) if cachesize > 0 { cache, _ = lru.New(cachesize) } + // Prewarm namesys cache with static records for deteministic tests and debugging. + // Useful for testing things like DNSLink without real DNS lookup. + // Example: + // IPFS_NS_MAP="dnslink-test.example.com:/ipfs/bafkreicysg23kiwv34eg2d7qweipxwosdo2py4ldv42nbauguluen5v6am" + if list := os.Getenv("IPFS_NS_MAP"); list != "" { + staticMap = make(map[string]path.Path) + for _, pair := range strings.Split(list, ",") { + mapping := strings.SplitN(pair, ":", 2) + key := mapping[0] + value := path.FromString(mapping[1]) + staticMap[key] = value + } + } + return &mpns{ dnsResolver: NewDNSResolver(), proquintResolver: new(ProquintResolver), ipnsResolver: NewIpnsResolver(r), ipnsPublisher: NewIpnsPublisher(r, ds), + staticMap: staticMap, cache: cache, } } +// DefaultResolverCacheTTL defines max ttl of a record placed in namesys cache. const DefaultResolverCacheTTL = time.Minute // Resolve implements Resolver.