登录
首页 >  Golang >  Go问答

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

来源:stackoverflow

时间:2024-04-03 14:12:34 318浏览 收藏

学习知识要善于思考,思考,再思考!今天golang学习网小编就给大家带来《如何使用 Go 获取客户端 DNS IP》,以下内容主要包含等知识点,如果你正在学习或准备学习Golang,就都不要错过本文啦~让我们一起来看看吧,能帮助到你就更好了!

问题内容

我想使用 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 
            fmt.Println(udpConn.RemoteAddr())
        }
    }
}

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

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

我想要简单的服务器


解决方案


UDP 是无状态的。连接没有单一的客户端地址。每个数据包可以从不同的地址发送,因此RemoteAddr仅在客户端有用,而在服务器上无用。

使用 *UDPConn.ReadFrom*UDPConn.ReadFromUDP*UDPConn.ReadMsgUDP 之一代替 Read。它们都返回读取数据包的客户端地址。

以上就是《如何使用 Go 获取客户端 DNS IP》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>