mirror of https://github.com/status-im/go-waku.git
40 lines
809 B
Go
40 lines
809 B
Go
|
package gowaku
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"time"
|
||
|
|
||
|
"github.com/status-im/go-waku/waku/v2/dnsdisc"
|
||
|
)
|
||
|
|
||
|
func DnsDiscovery(url string, nameserver string, ms int) string {
|
||
|
var ctx context.Context
|
||
|
var cancel context.CancelFunc
|
||
|
|
||
|
if ms > 0 {
|
||
|
ctx, cancel = context.WithTimeout(context.Background(), time.Duration(int(ms))*time.Millisecond)
|
||
|
defer cancel()
|
||
|
} else {
|
||
|
ctx = context.Background()
|
||
|
}
|
||
|
|
||
|
var dnsDiscOpt []dnsdisc.DnsDiscoveryOption
|
||
|
if nameserver != "" {
|
||
|
dnsDiscOpt = append(dnsDiscOpt, dnsdisc.WithNameserver(nameserver))
|
||
|
}
|
||
|
|
||
|
nodes, err := dnsdisc.RetrieveNodes(ctx, url, dnsDiscOpt...)
|
||
|
if err != nil {
|
||
|
return MakeJSONResponse(err)
|
||
|
}
|
||
|
|
||
|
var ma []string
|
||
|
for _, n := range nodes {
|
||
|
for _, addr := range n.Addresses {
|
||
|
ma = append(ma, addr.String())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return PrepareJSONResponse(ma, nil)
|
||
|
}
|