开发(1)获取用户登录IP

参考链接:
Go获取IP地址
Golang实战:高效获取用户IP
exnet包获取客户端IP方法

1.从HTTP请求头获取IP

1.1 无代理情况

直接使用Request结构体里面的RemoteAddr

复制代码
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 地址,具体看1.2小节。

1.2 反向代理、负载均衡、CDN存在情况

(1)

我们可以使用X-Forwarded-For头获取用户的真实IP地址。在经过反向代理或负载均衡服务器时,代理服务器会将用户的真实IP地址添加到HTTP请求的X-Forwarded-For头中。我们可以通过解析该头来获取用户的真实IP地址。下面是一个示例代码:

复制代码
go func getUserIP(r *http.Request) string { 
         ip := r.Header.Get("X-Forwarded-For") 
         return strings.Split(ip, ", ")[0] 
         } 

使用X-Forwarded-For头可以获取用户的真实IP地址,但是也存在一些问题。首先,X-Forwarded-For头可以被伪造或篡改,因此不能完全信任它。其次,某些代理服务器可能不会添加X-Forwarded-For头,导致无法获取真实IP地址。

(2)

另一种方法是通过解析HTTP请求的Header字段来获取用户的IP地址。这种方法相对准确且灵活,适用于各种网络环境。下面是一个示例代码:

复制代码
go func getUserIP(r *http.Request) string { ip := r.Header.Get("X-Real-IP") if ip == "" { ip = r.Header.Get("X-Forwarded-For") } if ip == "" { ip = r.RemoteAddr } return ip } 

使用Request Header可以兼容各种网络环境,并且可以获取用户的真实IP地址。但是需要注意,不同的HTTP服务器和代理服务器可能会使用不同的Header名称,因此需要根据实际情况进行设置。

2.使用第三方库

(1)Gin

在Gin中,客户端的IP地址可以通过请求的上下文(context)获取。

复制代码
package main

import (
    "github.com/gin-gonic/gin"
    "net/http"
)

func main() {
    router := gin.Default()

    router.GET("/visitor_ip", func(c *gin.Context) {
        clientIP := c.ClientIP()
        c.String(http.StatusOK, "Your IP: %s", clientIP)
    })

    router.Run()
}

当用户访问/visitor_ip端点时,IP地址会被捕获,并返回给他们。

但使用代理和负载均衡器时,c.ClientIP()方法会获得代理的IP,要解决这个问题,Gin尝试分析诸如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 地址。

(2)exnet包

exnet包提供了一系列方便的函数来处理IP地址相关操作。以下是一个使用exnet包获取用户真实IP的示例:

复制代码
package main

import (
	"fmt"
	"net/http"
	"github.com/chenzhuoyu/base64x"
	"github.com/valyala/fasthttp"
	"github.com/egoist/exnet"
)

func ClientIP(r *http.Request) string {
	ip := exnet.ClientPublicIP(r)
	if ip == "" {
		ip = exnet.RemoteIP(r)
	}
	return ip
}

func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		ip := ClientIP(r)
		fmt.Fprintf(w, "您的真实IP地址是: %s\n", ip)
	})

	fmt.Println("服务器启动,监听端口: 8080")
	http.ListenAndServe(":8080", nil)
}
相关推荐
Jony_1 天前
高可用移动网络连接
网络协议
chilix1 天前
Linux 跨网段路由转发配置
网络协议
DianSan_ERP3 天前
电商API接口全链路监控:构建坚不可摧的线上运维防线
大数据·运维·网络·人工智能·git·servlet
呉師傅3 天前
火狐浏览器报错配置文件缺失如何解决#操作技巧#
运维·网络·windows·电脑
gihigo19983 天前
基于TCP协议实现视频采集与通信
网络协议·tcp/ip·音视频
2501_946205523 天前
晶圆机器人双臂怎么选型?适配2-12寸晶圆的末端效应器有哪些?
服务器·网络·机器人
linux kernel3 天前
第七部分:高级IO
服务器·网络
数字护盾(和中)3 天前
BAS+ATT&CK:企业主动防御的黄金组合
服务器·网络·数据库
~远在太平洋~3 天前
Debian系统如何删除多余的kernel
linux·网络·debian
龙仔7253 天前
在麒麟V10服务器安全加固,sshd防暴力破解加固,实现“密码错误3次封IP”的需求
服务器·tcp/ip·安全