2022-10-20 14:42:40 +00:00
|
|
|
package gowaku
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-12-19 23:21:14 +00:00
|
|
|
"errors"
|
2022-10-20 14:42:40 +00:00
|
|
|
"time"
|
|
|
|
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/dnsdisc"
|
2022-10-20 14:42:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2022-12-19 23:21:14 +00:00
|
|
|
|
|
|
|
func StartDiscoveryV5() string {
|
|
|
|
if wakuNode == nil {
|
|
|
|
return MakeJSONResponse(errWakuNodeNotReady)
|
|
|
|
}
|
|
|
|
if wakuNode.DiscV5() == nil {
|
|
|
|
return MakeJSONResponse(errors.New("DiscV5 is not mounted"))
|
|
|
|
}
|
|
|
|
err := wakuNode.DiscV5().Start(context.Background())
|
|
|
|
return MakeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func StopDiscoveryV5() string {
|
|
|
|
if wakuNode == nil {
|
|
|
|
return MakeJSONResponse(errWakuNodeNotReady)
|
|
|
|
}
|
|
|
|
if wakuNode.DiscV5() == nil {
|
|
|
|
return MakeJSONResponse(errors.New("DiscV5 is not mounted"))
|
|
|
|
}
|
2022-12-21 18:58:18 +00:00
|
|
|
wakuNode.DiscV5().Stop()
|
|
|
|
return MakeJSONResponse(nil)
|
2022-12-19 23:21:14 +00:00
|
|
|
}
|