1. 获取本地IP地址
使用 net 包可以获取本地机器的 IP 地址。以下是一个获取本地 IP 地址的简单示例:- package main
- import (
- "fmt"
- "net"
- )
- func main() {
- // 获取所有网络接口
- interfaces, err := net.Interfaces()
- if err != nil {
- fmt.Println("Error:", err)
- return
- }
- // 遍历所有网络接口
- for _, iface := range interfaces {
- // 排除一些特殊接口
- if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 {
- continue
- }
- // 获取接口的地址信息
- addrs, err := iface.Addrs()
- if err != nil {
- fmt.Println("Error:", err)
- continue
- }
- // 遍历接口的地址
- for _, addr := range addrs {
- // 检查地址类型
- switch v := addr.(type) {
- case *net.IPNet:
- // IPv4 或 IPv6 地址
- fmt.Println(v.IP)
- case *net.IPAddr:
- // 一般情况下是 IPv4 地址
- fmt.Println(v.IP)
- }
- }
- }
- }
复制代码 2. 使用 net/http 获取客户端 IP
在 Go 中,可以使用 net/http 包中的 Request 结构体来获取客户端的 IP 地址。具体来说,Request 结构体中的 RemoteAddr 字段包含了客户端的 IP 地址和端口号。- type Request struct {
- ...
- // RemoteAddr allows HTTP servers and other software to record
- // the network address that sent the request, usually for
- // logging. This field is not filled in by ReadRequest and
- // has no defined format. The HTTP server in this package
- // sets RemoteAddr to an "IP:port" address before invoking a
- // handler.
- // This field is ignored by the HTTP client.
- RemoteAddr string
- ...
- }
复制代码 以下是一个简单的示例:- package main
- import (
- "fmt"
- "net/http"
- )
- func main() {
- http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
- // 通过 RemoteAddr 获取客户端的 IP 地址和端口号
- ip, _, err := net.SplitHostPort(r.RemoteAddr)
- if err != nil {
- fmt.Println("Error extracting IP:", err)
- return
- }
- fmt.Fprintf(w, "Client IP: %s", ip)
- })
- // 启动 HTTP 服务器,监听在 8080 端口
- http.ListenAndServe(":8080", nil)
- }
复制代码 在这个例子中,r.RemoteAddr 包含了客户端的 IP 地址和端口号,使用 net.SplitHostPort 函数可以方便地从这个字符串中提取出 IP 地址。请注意,由于这个方法使用 TCP 连接的信息,所以对于某些代理服务器或负载均衡器,它可能只是代理服务器的 IP 地址,而不是实际客户端的 IP 地址。在这种情况下,可能需要查看 HTTP 头部中的相关字段以获取真实的客户端 IP 地址。
3. 使用 gin 获取客户端 IP
在 Gin 框架中,可以通过 c.ClientIP() 方法获取客户端的 IP 地址。这个方法会尝试从不同的来源获取 IP 地址,包括 X-Forwarded-For 头部、X-Real-IP 头部以及连接的远程地址。- // ClientIP implements one best effort algorithm to return the real client IP.
- // It calls c.RemoteIP() under the hood, to check if the remote IP is a trusted proxy or not.
- // If it is it will then try to parse the headers defined in Engine.RemoteIPHeaders (defaulting to [X-Forwarded-For, X-Real-Ip]).
- // If the headers are not syntactically valid OR the remote IP does not correspond to a trusted proxy, the remote IP (coming from Request.RemoteAddr) is returned.
- func (c *Context) ClientIP() string
复制代码 以下是一个简单的示例:- package main
- import (
- "github.com/gin-gonic/gin"
- "net/http"
- )
- func main() {
- r := gin.Default()
- r.GET("/", func(c *gin.Context) {
- // 获取客户端的 IP 地址
- clientIP := c.ClientIP()
- c.String(http.StatusOK, "Client IP: %s", clientIP)
- })
- // 启动 Gin 服务器,监听在 8080 端口
- r.Run(":8080")
- }
复制代码 在这个例子中,c.ClientIP() 会自动处理不同的头部并返回客户端的 IP 地址。如果的应用程序部署在代理服务器或负载均衡器之后,确保这些设备正确地设置了 X-Forwarded-For 或 X-Real-IP 头部,以便正确获取客户端的 IP 地址。
 声明:本作品采用署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)进行许可,使用时请注明出处。
Author: mengbin
blog: mengbin
Github: mengbin92
cnblogs: 恋水无意
腾讯云开发者社区:孟斯特
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |