Skip to content

Commit

Permalink
feat: IPFS_NS_MAP
Browse files Browse the repository at this point in the history
Allows static DNSLink mappings with IPFS_NS_MAP.

License: MIT
Signed-off-by: Marcin Rataj <[email protected]>
  • Loading branch information
lidel authored and Stebalien committed Mar 18, 2020
1 parent 72490f7 commit 848d4c7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
14 changes: 14 additions & 0 deletions docs/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions namesys/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
25 changes: 23 additions & 2 deletions namesys/namesys.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package namesys

import (
"context"
"os"
"strings"
"time"

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 848d4c7

Please sign in to comment.