beginning of fake dns server integration

This commit is contained in:
Gabriel mermelstein 2024-10-15 19:19:18 +03:00
parent 5ec906558c
commit f0c039318a
No known key found for this signature in database
GPG Key ID: 82B8134785FEAE0D
3 changed files with 96 additions and 14 deletions

View File

@ -1701,6 +1701,7 @@ type WakuConfig struct {
Discv5Discovery bool `json:"discv5Discovery,omitempty"` Discv5Discovery bool `json:"discv5Discovery,omitempty"`
ClusterID uint16 `json:"clusterId,omitempty"` ClusterID uint16 `json:"clusterId,omitempty"`
Shards []uint16 `json:"shards,omitempty"` Shards []uint16 `json:"shards,omitempty"`
DnsDiscoveryNameServers []string `json:"dnsDiscoveryNameServers,omitempty"`
} }
type Waku struct { type Waku struct {

View File

@ -6,6 +6,8 @@ package wakuv2
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"log"
"slices" "slices"
"testing" "testing"
"time" "time"
@ -163,12 +165,40 @@ func TestBasicWakuV2(t *testing.T) {
storeNodeInfo, err := GetNwakuInfo(nil, &extNodeRestPort) storeNodeInfo, err := GetNwakuInfo(nil, &extNodeRestPort)
require.NoError(t, err) require.NoError(t, err)
// Creating a fake DNS Discovery ENRTree
tree, url := makeTestTree("n", parseNodes([]string{storeNodeInfo.EnrUri}), nil)
enrTreeAddress := url
/* envEnrTreeAddress := os.Getenv("ENRTREE_ADDRESS")
if envEnrTreeAddress != "" {
enrTreeAddress = envEnrTreeAddress
} */
fmt.Println("--------- tree.ToTXT('n'): ", tree.ToTXT("n"))
fmt.Println("--------- enrTreeAddress ", enrTreeAddress)
fmt.Println("--------- url ", url)
dnsServer, err := CreateFakeDnsServer("")
require.NoError(t, err)
// Channel to signal when the server is done
done := make(chan bool)
go func() {
err := dnsServer.ListenAndServe()
if err != nil {
log.Printf("DNS server error: %v", err)
}
done <- true
}()
nwakuConfig := WakuConfig{ nwakuConfig := WakuConfig{
Port: 30303, Port: 30303,
NodeKey: "11d0dcea28e86f81937a3bd1163473c7fbc0a0db54fd72914849bc47bdf78710", NodeKey: "11d0dcea28e86f81937a3bd1163473c7fbc0a0db54fd72914849bc47bdf78710",
EnableRelay: true, EnableRelay: true,
LogLevel: "DEBUG", LogLevel: "DEBUG",
DnsDiscoveryUrl: "enrtree://AMOJVZX4V6EXP7NTJPMAYJYST2QP6AJXYW76IU6VGJS7UVSNDYZG4@boot.prod.status.nodes.status.im", DnsDiscoveryUrl: url,
DnsDiscoveryNameServers: []string{"127.0.0.1"},
DnsDiscovery: true, DnsDiscovery: true,
Discv5Discovery: true, Discv5Discovery: true,
Staticnodes: []string{storeNodeInfo.ListenAddresses[0]}, Staticnodes: []string{storeNodeInfo.ListenAddresses[0]},
@ -292,6 +322,12 @@ func TestBasicWakuV2(t *testing.T) {
}, options) }, options)
require.NoError(t, err) */ require.NoError(t, err) */
dnsServer.Shutdown()
// Wait for the server to finish
<-done
log.Println("DNS server stopped")
require.NoError(t, w.Stop()) require.NoError(t, w.Stop())
} }

View File

@ -4,9 +4,12 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"log"
"net/http" "net/http"
"os" "os"
"strconv" "strconv"
"github.com/miekg/dns"
) )
type NwakuInfo struct { type NwakuInfo struct {
@ -56,3 +59,45 @@ func GetNwakuInfo(host *string, port *int) (NwakuInfo, error) {
return data, nil return data, nil
} }
func CreateFakeDnsServer(txtRecord string) (*dns.Server, error) {
handleDNSRequest := func(w dns.ResponseWriter, r *dns.Msg) {
fmt.Println("----------- received dns request -------")
msg := dns.Msg{}
msg.SetReply(r)
msg.Authoritative = true
for _, q := range r.Question {
switch q.Qtype {
case dns.TypeA:
rr, err := dns.NewRR(fmt.Sprintf("%s A 127.0.0.1", q.Name))
if err != nil {
log.Printf("Failed to create A RR: %v", err)
continue
}
msg.Answer = append(msg.Answer, rr)
case dns.TypeTXT:
rr, err := dns.NewRR(fmt.Sprintf("%s TXT \"%s\"", q.Name, txtRecord))
if err != nil {
log.Printf("Failed to create TXT RR: %v", err)
continue
}
msg.Answer = append(msg.Answer, rr)
}
}
err := w.WriteMsg(&msg)
if err != nil {
log.Printf("Failed to write message: %v", err)
}
}
// Create a new DNS server mux
dns.HandleFunc(".", handleDNSRequest)
// Create the server
server := &dns.Server{Addr: ":53", Net: "udp"}
return server, nil
}