猿问

如何使用 Go 获取客户端 DNS IP

我想使用 Go 获取客户端缓存 DNS IP


看看我在下面尝试的代码


import (

    "fmt"

    "net"

)


func main() {

    // Usually DNS Server using 53 port number

    // This case, TCP protocol is not considered

    port := ":53"

    protocol := "udp"


    var buf [2048]byte


    //Build the address

    udpAddr, err := net.ResolveUDPAddr(protocol, port)

    if err != nil {

        fmt.Println("Wrong Address")

        return

    }


    fmt.Println("Listened " + protocol + " from " + udpAddr.String())


    //Create the connection

    udpConn, err := net.ListenUDP(protocol, udpAddr)

    if err != nil {

        fmt.Println(err)

    }


    // Listening 53 Port Like DNS Server

    for {


        // If get request,

        _, err := udpConn.Read(buf[0:])

        if err != nil {

            fmt.Println("Error Reading")

            return

        } else {

            // Print Remote Address,

            // I Guess this is the Client Cache DNS IP, but this is print <nil>

            fmt.Println(udpConn.RemoteAddr())

        }

    }

}

在这种情况下,如何获取客户端缓存 DNS IP?请帮助我,我想构建客户端 DNS IP 收集器,看起来像 whoami


我也将其称为https://github.com/miekg/exdns/blob/master/reflect/reflect.go 但这不是我的答案


我想要简单的服务器


慕娘9325324
浏览 154回答 1
1回答

紫衣仙女

UDP 是无状态的。连接没有单一的客户端地址。每个数据包都可以从不同的地址发送,因此RemoteAddr只对客户端有用,对服务器没有用。使用*UDPConn.ReadFrom、*UDPConn.ReadFromUDP或*UDPConn.ReadMsgUDP之一代替Read。它们都返回读取数据包的客户端地址。
随时随地看视频慕课网APP

相关分类

Go
我要回答