dongzhuo8210 2018-08-13 09:50
浏览 258
已采纳

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

I want Get Client Cache DNS IP using Go

Look at the code I tried below

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())
        }
    }
}

How do I get the Client Cache DNS IP in this case? Pleas Help me I Want to Build Client DNS IP Collector, seem whoami

I also refer to this as https://github.com/miekg/exdns/blob/master/reflect/reflect.go but this is not answer for me

I want simple server

  • 写回答

1条回答

  • duanchen7703 2018-08-13 11:05
    关注

    UDP is stateless. There is no single client address for a connection. Each packet can be sent from a different address, so RemoteAddr is only useful on the client, but not the server.

    Use one of *UDPConn.ReadFrom, *UDPConn.ReadFromUDP, or *UDPConn.ReadMsgUDP instead of Read. All of them return the client address for the read packet.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大