2021-11-16 14:22:01 +00:00
|
|
|
package dnsdisc
|
2021-09-27 17:52:56 +00:00
|
|
|
|
|
|
|
import (
|
2021-09-30 23:03:19 +00:00
|
|
|
"context"
|
2021-09-27 17:52:56 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/dnsdisc"
|
2021-11-16 14:22:01 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/utils"
|
2021-09-27 18:16:06 +00:00
|
|
|
|
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
2021-09-27 17:52:56 +00:00
|
|
|
)
|
|
|
|
|
2021-09-30 23:03:19 +00:00
|
|
|
type DnsDiscoveryParameters struct {
|
|
|
|
nameserver string
|
|
|
|
}
|
|
|
|
|
|
|
|
type DnsDiscoveryOption func(*DnsDiscoveryParameters)
|
|
|
|
|
|
|
|
// WithMultiaddress is a WakuNodeOption that configures libp2p to listen on a list of multiaddresses
|
|
|
|
func WithNameserver(nameserver string) DnsDiscoveryOption {
|
|
|
|
return func(params *DnsDiscoveryParameters) {
|
|
|
|
params.nameserver = nameserver
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-27 17:52:56 +00:00
|
|
|
// RetrieveNodes returns a list of multiaddress given a url to a DNS discoverable
|
|
|
|
// ENR tree
|
2021-09-30 23:03:19 +00:00
|
|
|
func RetrieveNodes(ctx context.Context, url string, opts ...DnsDiscoveryOption) ([]ma.Multiaddr, error) {
|
2021-09-27 18:16:06 +00:00
|
|
|
var multiAddrs []ma.Multiaddr
|
2021-09-27 17:52:56 +00:00
|
|
|
|
2021-09-30 23:03:19 +00:00
|
|
|
params := new(DnsDiscoveryParameters)
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(params)
|
|
|
|
}
|
|
|
|
|
|
|
|
client := dnsdisc.NewClient(dnsdisc.Config{
|
|
|
|
Resolver: GetResolver(ctx, params.nameserver),
|
|
|
|
})
|
2021-09-27 17:52:56 +00:00
|
|
|
|
|
|
|
tree, err := client.SyncTree(url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, node := range tree.Nodes() {
|
2021-11-16 14:22:01 +00:00
|
|
|
m, err := utils.EnodeToMultiAddr(node)
|
2021-09-27 17:52:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
multiAddrs = append(multiAddrs, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
return multiAddrs, nil
|
|
|
|
}
|