diff --git a/wakuv2/nwaku.go b/wakuv2/nwaku.go index a1d703798..d51ab4f00 100644 --- a/wakuv2/nwaku.go +++ b/wakuv2/nwaku.go @@ -1688,19 +1688,20 @@ type WakuPubsubTopic = string type WakuContentTopic = string type WakuConfig struct { - Host string `json:"host,omitempty"` - Port int `json:"port,omitempty"` - NodeKey string `json:"key,omitempty"` - EnableRelay bool `json:"relay"` - LogLevel string `json:"logLevel"` - DnsDiscovery bool `json:"dnsDiscovery,omitempty"` - DnsDiscoveryUrl string `json:"dnsDiscoveryUrl,omitempty"` - MaxMessageSize string `json:"maxMessageSize,omitempty"` - Staticnodes []string `json:"staticnodes,omitempty"` - Discv5BootstrapNodes []string `json:"discv5BootstrapNodes,omitempty"` - Discv5Discovery bool `json:"discv5Discovery,omitempty"` - ClusterID uint16 `json:"clusterId,omitempty"` - Shards []uint16 `json:"shards,omitempty"` + Host string `json:"host,omitempty"` + Port int `json:"port,omitempty"` + NodeKey string `json:"key,omitempty"` + EnableRelay bool `json:"relay"` + LogLevel string `json:"logLevel"` + DnsDiscovery bool `json:"dnsDiscovery,omitempty"` + DnsDiscoveryUrl string `json:"dnsDiscoveryUrl,omitempty"` + MaxMessageSize string `json:"maxMessageSize,omitempty"` + Staticnodes []string `json:"staticnodes,omitempty"` + Discv5BootstrapNodes []string `json:"discv5BootstrapNodes,omitempty"` + Discv5Discovery bool `json:"discv5Discovery,omitempty"` + ClusterID uint16 `json:"clusterId,omitempty"` + Shards []uint16 `json:"shards,omitempty"` + DnsDiscoveryNameServers []string `json:"dnsDiscoveryNameServers,omitempty"` } type Waku struct { diff --git a/wakuv2/nwaku_test.go b/wakuv2/nwaku_test.go index 8f2ead7bb..f734daa5f 100644 --- a/wakuv2/nwaku_test.go +++ b/wakuv2/nwaku_test.go @@ -6,6 +6,8 @@ package wakuv2 import ( "context" "errors" + "fmt" + "log" "slices" "testing" "time" @@ -163,12 +165,40 @@ func TestBasicWakuV2(t *testing.T) { storeNodeInfo, err := GetNwakuInfo(nil, &extNodeRestPort) 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{ Port: 30303, NodeKey: "11d0dcea28e86f81937a3bd1163473c7fbc0a0db54fd72914849bc47bdf78710", EnableRelay: true, LogLevel: "DEBUG", - DnsDiscoveryUrl: "enrtree://AMOJVZX4V6EXP7NTJPMAYJYST2QP6AJXYW76IU6VGJS7UVSNDYZG4@boot.prod.status.nodes.status.im", + DnsDiscoveryUrl: url, + DnsDiscoveryNameServers: []string{"127.0.0.1"}, DnsDiscovery: true, Discv5Discovery: true, Staticnodes: []string{storeNodeInfo.ListenAddresses[0]}, @@ -292,6 +322,12 @@ func TestBasicWakuV2(t *testing.T) { }, options) require.NoError(t, err) */ + dnsServer.Shutdown() + + // Wait for the server to finish + <-done + log.Println("DNS server stopped") + require.NoError(t, w.Stop()) } diff --git a/wakuv2/nwaku_test_utils.go b/wakuv2/nwaku_test_utils.go index ed9f3e80c..a3d8fede1 100644 --- a/wakuv2/nwaku_test_utils.go +++ b/wakuv2/nwaku_test_utils.go @@ -4,9 +4,12 @@ import ( "encoding/json" "fmt" "io" + "log" "net/http" "os" "strconv" + + "github.com/miekg/dns" ) type NwakuInfo struct { @@ -56,3 +59,45 @@ func GetNwakuInfo(host *string, port *int) (NwakuInfo, error) { 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 +} +