| 1 | package node |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "time" |
| 6 | |
| 7 | "github.com/ipfs/boxo/ipns" |
| 8 | util "github.com/ipfs/boxo/util" |
| 9 | record "github.com/libp2p/go-libp2p-record" |
| 10 | "github.com/libp2p/go-libp2p/core/crypto" |
| 11 | "github.com/libp2p/go-libp2p/core/peerstore" |
| 12 | madns "github.com/multiformats/go-multiaddr-dns" |
| 13 | |
| 14 | "github.com/ipfs/boxo/namesys" |
| 15 | "github.com/ipfs/boxo/namesys/republisher" |
| 16 | "github.com/ipfs/kubo/repo" |
| 17 | irouting "github.com/ipfs/kubo/routing" |
| 18 | ) |
| 19 | |
| 20 | const DefaultIpnsCacheSize = 128 |
| 21 | |
| 22 | // RecordValidator provides namesys compatible routing record validator |
| 23 | func RecordValidator(ps peerstore.Peerstore) record.Validator { |
| 24 | return record.NamespacedValidator{ |
| 25 | "pk": record.PublicKeyValidator{}, |
| 26 | "ipns": ipns.Validator{KeyBook: ps}, |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | // Namesys creates new name system |
| 31 | func Namesys(cacheSize int, cacheMaxTTL time.Duration) func(rt irouting.ProvideManyRouter, rslv *madns.Resolver, repo repo.Repo) (namesys.NameSystem, error) { |
| 32 | return func(rt irouting.ProvideManyRouter, rslv *madns.Resolver, repo repo.Repo) (namesys.NameSystem, error) { |
| 33 | opts := []namesys.Option{ |
| 34 | namesys.WithDatastore(repo.Datastore()), |
| 35 | namesys.WithDNSResolver(rslv), |
| 36 | namesys.WithMaxCacheTTL(cacheMaxTTL), |
| 37 | } |
| 38 | |
| 39 | if cacheSize > 0 { |
| 40 | opts = append(opts, namesys.WithCache(cacheSize)) |
| 41 | } |
| 42 | |
| 43 | return namesys.NewNameSystem(rt, opts...) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // IpnsRepublisher runs new IPNS republisher service |
| 48 | func IpnsRepublisher(repubPeriod time.Duration, recordLifetime time.Duration) func(lcStartStop, namesys.NameSystem, repo.Repo, crypto.PrivKey) error { |
| 49 | return func(lc lcStartStop, namesys namesys.NameSystem, repo repo.Repo, privKey crypto.PrivKey) error { |
| 50 | repub := republisher.NewRepublisher(namesys, repo.Datastore(), privKey, repo.Keystore()) |
| 51 | |
| 52 | if repubPeriod != 0 { |
| 53 | if !util.Debug && (repubPeriod < time.Minute || repubPeriod > (time.Hour*24)) { |
| 54 | return fmt.Errorf("config setting IPNS.RepublishPeriod is not between 1min and 1day: %s", repubPeriod) |
| 55 | } |
| 56 | |
| 57 | repub.Interval = repubPeriod |
| 58 | } |
| 59 | |
| 60 | if recordLifetime != 0 { |
| 61 | repub.RecordLifetime = recordLifetime |
| 62 | } |
| 63 | |
| 64 | lc.Append(repub.Run) |
| 65 | return nil |
| 66 | } |
| 67 | } |