2023-08-10 13:30:38 +00:00
|
|
|
package library
|
2022-10-20 14:42:40 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-06-09 12:43:41 +00:00
|
|
|
"encoding/json"
|
2022-12-19 23:21:14 +00:00
|
|
|
"errors"
|
2022-10-20 14:42:40 +00:00
|
|
|
"time"
|
|
|
|
|
2023-06-09 12:43:41 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
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
|
|
|
)
|
|
|
|
|
2023-08-10 13:30:38 +00:00
|
|
|
type dnsDiscoveryItem struct {
|
2023-02-17 13:30:24 +00:00
|
|
|
PeerID string `json:"peerID"`
|
|
|
|
Addresses []string `json:"multiaddrs"`
|
2023-02-16 22:10:20 +00:00
|
|
|
ENR string `json:"enr,omitempty"`
|
|
|
|
}
|
|
|
|
|
2023-08-10 13:30:38 +00:00
|
|
|
// DNSDiscovery executes dns discovery on an url and returns a list of nodes
|
|
|
|
func DNSDiscovery(url string, nameserver string, ms int) (string, error) {
|
2022-10-20 14:42:40 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2023-09-11 14:24:05 +00:00
|
|
|
var dnsDiscOpt []dnsdisc.DNSDiscoveryOption
|
2022-10-20 14:42:40 +00:00
|
|
|
if nameserver != "" {
|
|
|
|
dnsDiscOpt = append(dnsDiscOpt, dnsdisc.WithNameserver(nameserver))
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes, err := dnsdisc.RetrieveNodes(ctx, url, dnsDiscOpt...)
|
|
|
|
if err != nil {
|
2023-08-10 13:30:38 +00:00
|
|
|
return "", err
|
2022-10-20 14:42:40 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 13:30:38 +00:00
|
|
|
var response []dnsDiscoveryItem
|
2022-10-20 14:42:40 +00:00
|
|
|
for _, n := range nodes {
|
2023-08-10 13:30:38 +00:00
|
|
|
item := dnsDiscoveryItem{
|
2023-02-16 22:10:20 +00:00
|
|
|
PeerID: n.PeerID.String(),
|
|
|
|
}
|
2023-05-10 14:13:10 +00:00
|
|
|
for _, addr := range n.PeerInfo.Addrs {
|
2023-02-16 22:10:20 +00:00
|
|
|
item.Addresses = append(item.Addresses, addr.String())
|
2022-10-20 14:42:40 +00:00
|
|
|
}
|
2023-02-20 14:42:04 +00:00
|
|
|
|
|
|
|
if n.ENR != nil {
|
|
|
|
item.ENR = n.ENR.String()
|
|
|
|
}
|
|
|
|
|
2023-02-16 22:10:20 +00:00
|
|
|
response = append(response, item)
|
2022-10-20 14:42:40 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 13:30:38 +00:00
|
|
|
return marshalJSON(response)
|
2022-10-20 14:42:40 +00:00
|
|
|
}
|
2022-12-19 23:21:14 +00:00
|
|
|
|
2023-08-03 21:24:00 +00:00
|
|
|
// StartDiscoveryV5 starts discv5 discovery
|
2023-12-15 14:46:21 +00:00
|
|
|
func StartDiscoveryV5(instance *WakuInstance) error {
|
|
|
|
if err := validateInstance(instance, MustBeStarted); err != nil {
|
|
|
|
return err
|
2022-12-19 23:21:14 +00:00
|
|
|
}
|
2023-12-15 14:46:21 +00:00
|
|
|
|
|
|
|
if instance.node.DiscV5() == nil {
|
2023-08-10 13:30:38 +00:00
|
|
|
return errors.New("DiscV5 is not mounted")
|
2022-12-19 23:21:14 +00:00
|
|
|
}
|
2023-12-15 14:46:21 +00:00
|
|
|
return instance.node.DiscV5().Start(instance.ctx)
|
2022-12-19 23:21:14 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 13:30:38 +00:00
|
|
|
// StopDiscoveryV5 stops discv5 discovery
|
2023-12-15 14:46:21 +00:00
|
|
|
func StopDiscoveryV5(instance *WakuInstance) error {
|
|
|
|
if err := validateInstance(instance, MustBeStarted); err != nil {
|
|
|
|
return err
|
2022-12-19 23:21:14 +00:00
|
|
|
}
|
2023-12-15 14:46:21 +00:00
|
|
|
|
|
|
|
if instance.node.DiscV5() == nil {
|
2023-08-10 13:30:38 +00:00
|
|
|
return errors.New("DiscV5 is not mounted")
|
2022-12-19 23:21:14 +00:00
|
|
|
}
|
2023-12-15 14:46:21 +00:00
|
|
|
instance.node.DiscV5().Stop()
|
2023-08-10 13:30:38 +00:00
|
|
|
return nil
|
2022-12-19 23:21:14 +00:00
|
|
|
}
|
2023-06-09 12:43:41 +00:00
|
|
|
|
2023-08-10 13:30:38 +00:00
|
|
|
// SetBootnodes is used to update the bootnodes receiving a JSON array of ENRs
|
2023-12-15 14:46:21 +00:00
|
|
|
func SetBootnodes(instance *WakuInstance, bootnodes string) error {
|
|
|
|
if err := validateInstance(instance, MustBeStarted); err != nil {
|
|
|
|
return err
|
2023-06-09 12:43:41 +00:00
|
|
|
}
|
2023-12-15 14:46:21 +00:00
|
|
|
|
|
|
|
if instance.node.DiscV5() == nil {
|
2023-08-10 13:30:38 +00:00
|
|
|
return errors.New("DiscV5 is not mounted")
|
2023-06-09 12:43:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var tmp []json.RawMessage
|
|
|
|
if err := json.Unmarshal([]byte(bootnodes), &tmp); err != nil {
|
2023-08-10 13:30:38 +00:00
|
|
|
return err
|
2023-06-09 12:43:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var enrList []string
|
|
|
|
for _, el := range tmp {
|
|
|
|
var enr string
|
|
|
|
if err := json.Unmarshal(el, &enr); err != nil {
|
2023-08-10 13:30:38 +00:00
|
|
|
return err
|
2023-06-09 12:43:41 +00:00
|
|
|
}
|
|
|
|
enrList = append(enrList, enr)
|
|
|
|
}
|
|
|
|
|
|
|
|
var nodes []*enode.Node
|
|
|
|
for _, addr := range enrList {
|
|
|
|
node, err := enode.Parse(enode.ValidSchemes, addr)
|
|
|
|
if err != nil {
|
2023-08-10 13:30:38 +00:00
|
|
|
return err
|
2023-06-09 12:43:41 +00:00
|
|
|
}
|
|
|
|
nodes = append(nodes, node)
|
|
|
|
}
|
|
|
|
|
2023-12-15 14:46:21 +00:00
|
|
|
return instance.node.DiscV5().SetBootnodes(nodes)
|
2023-06-09 12:43:41 +00:00
|
|
|
}
|