解析类型 dns.RR

Parsing type dns.RR

我是 Go 的新手,正在尝试让一个小型 DNS 程序正常工作以解析返回的 A 和 CNAME 记录。这是使用 github.com/miekg/dns 包。

我无法理解如何正确解析返回的 A 或 CNAME 资源记录。对于 A 记录,我想获取 IP 地址并将其存储在变量中。对于返回多个RR的CNAME,我想解析Name和Address字段并存储在变量中。

我试过将结构转换为字符串或将其解包都无济于事。任何人都可以提供任何见解,非常感谢。

这是下面的代码。文本文件包含一行 'www':

package main

import (
    "bufio"
    "fmt"
    "log"
    "os"
    "github.com/miekg/dns"
)

func dns_request_fwd(s string) []dns.RR {
    //fmt.Println(s, " : started processing DNS request")

    m1 := new(dns.Msg)
    m1.Id = dns.Id()
    m1.RecursionDesired = true
    m1.Question = make([]dns.Question, 1)
    m1.Question[0] = dns.Question{s, dns.TypeA, dns.ClassINET}
    in, err := dns.Exchange(m1, "8.8.8.8:53")

    if err != nil {
        log.Fatal(err)
    }

    if len(in.Answer) > 0 {
        //fmt.Println("Response greater than 0 - got something!")
        //fmt.Println(in.Answer)
    } else {
        //fmt.Println("Empty DNS Response")
        return nil
    }

    if _, ok := in.Answer[0].(*dns.A); ok {

        //fmt.Println("Detected an 'A' record Answer.  Returning RR!")
        return in.Answer

    } else if _, ok := in.Answer[0].(*dns.CNAME); ok {
        //fmt.Println("Detected a 'CNAME' record Answer.  Returning RR!")
        return in.Answer

    } else {
        fmt.Println("Returning nil")
        return nil

    }

}

func main() {

    f, err := os.Open("list4.txt")

    if err != nil {
        log.Fatal(err)
    }

    defer f.Close()

    domain := "acme.com"
    domain_lookup := "." + domain + "." 

    scanner := bufio.NewScanner(f)

    for scanner.Scan() {

    a_record := scanner.Text()

    lookup := a_record+domain_lookup
    fmt.Println("Looking up: ", lookup)
    dreq := dns_request_fwd(lookup)
    if len(dreq) > 0 {
        fmt.Println("    [+] Forward Lookup answer")
        if _, ok := dreq[0].(*dns.CNAME); ok {
            fmt.Println("    [+] Type is CNAME")
                //fmt.Println(dreq[0].(*dns.CNAME))
            //fmt.Println(dreq[1].(*dns.A))
            fmt.Println("    [+] Alias records: ", len(dreq) -1)

            // Iterate through returned alias A records
            for i, s := range dreq {
                if i == 0{
                    //Do nothing because first record is repeat of request 
                } else {
                    // Need to parse the below RR to get the Name + IP address returned
                    fmt.Println("    [+] Returned",i, s)
                    fmt.Println(s[0])
                }
            }

        } else if _, ok := dreq[0].(*dns.A); ok {

            fmt.Println("    [+] Type is A")
            // Need to parse the below RR to get the IP address returned
            fmt.Println("    [+] Answer: ", dreq[0])

        }

    } else {
        fmt.Println("    [-] We Got Nothing")
    }
    

    }

    if err := scanner.Err(); err != nil {
        log.Fatal(err)
    }
}

您不需要“解析”结构。该库已经解析了响应以便为您提供结构。只需使用结构中包含所需数据的字段,如 dns.A.A which is a net.IP or dns.CNAME.Target which is a string. See the package documentation 了解更多详细信息。

if arec, ok := dreq[0].(*dns.A); ok {
    fmt.Println("    [+] Type is A")
    fmt.Println("    [+] Answer: ", arec.A)
}