Commit 848d4c7f authored by Marcin Rataj's avatar Marcin Rataj Committed by Steven Allen

feat: IPFS_NS_MAP

Allows static DNSLink mappings with IPFS_NS_MAP.

License: MIT
Signed-off-by: default avatarMarcin Rataj <lidel@lidel.org>
parent 72490f7e
...@@ -82,6 +82,20 @@ the `--migrate` flag). ...@@ -82,6 +82,20 @@ the `--migrate` flag).
Default: https://ipfs.io/ipfs/$something (depends on the IPFS version) 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` ## `LIBP2P_MUX_PREFS`
Tells go-ipfs which multiplexers to use in which order. Tells go-ipfs which multiplexers to use in which order.
......
...@@ -7,6 +7,14 @@ import ( ...@@ -7,6 +7,14 @@ import (
) )
func (ns *mpns) cacheGet(name string) (path.Path, bool) { 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 { if ns.cache == nil {
return "", false return "", false
} }
......
...@@ -2,6 +2,7 @@ package namesys ...@@ -2,6 +2,7 @@ package namesys
import ( import (
"context" "context"
"os"
"strings" "strings"
"time" "time"
...@@ -29,25 +30,45 @@ type mpns struct { ...@@ -29,25 +30,45 @@ type mpns struct {
dnsResolver, proquintResolver, ipnsResolver resolver dnsResolver, proquintResolver, ipnsResolver resolver
ipnsPublisher Publisher ipnsPublisher Publisher
cache *lru.Cache staticMap map[string]path.Path
cache *lru.Cache
} }
// NewNameSystem will construct the IPFS naming system based on Routing // NewNameSystem will construct the IPFS naming system based on Routing
func NewNameSystem(r routing.ValueStore, ds ds.Datastore, cachesize int) NameSystem { 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 { if cachesize > 0 {
cache, _ = lru.New(cachesize) 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{ return &mpns{
dnsResolver: NewDNSResolver(), dnsResolver: NewDNSResolver(),
proquintResolver: new(ProquintResolver), proquintResolver: new(ProquintResolver),
ipnsResolver: NewIpnsResolver(r), ipnsResolver: NewIpnsResolver(r),
ipnsPublisher: NewIpnsPublisher(r, ds), ipnsPublisher: NewIpnsPublisher(r, ds),
staticMap: staticMap,
cache: cache, cache: cache,
} }
} }
// DefaultResolverCacheTTL defines max ttl of a record placed in namesys cache.
const DefaultResolverCacheTTL = time.Minute const DefaultResolverCacheTTL = time.Minute
// Resolve implements Resolver. // Resolve implements Resolver.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment