开发(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)
}
相关推荐
树℡独12 分钟前
ns-3仿真之应用层(五)
服务器·网络·tcp/ip·ns3
zhang133830890751 小时前
CG-09H 超声波风速风向传感器 加热型 ABS材质 重量轻 没有机械部件
大数据·运维·网络·人工智能·自动化
津津有味道2 小时前
易语言TCP服务端接收刷卡数据并向客户端读卡器发送指令
服务器·网络协议·tcp·易语言
酣大智3 小时前
接口模式参数
运维·网络·网络协议·tcp/ip
Genie cloud3 小时前
1Panel SSL证书申请完整教程
服务器·网络协议·云计算·ssl
24zhgjx-lxq3 小时前
华为ensp:MSTP
网络·安全·华为·hcip·ensp
ling___xi3 小时前
《计算机网络》计网3小时期末速成课各版本教程都可用谢稀仁湖科大版都可用_哔哩哔哩_bilibili(笔记)
网络·笔记·计算机网络
REDcker4 小时前
Linux 文件描述符与 Socket 选项操作详解
linux·运维·网络
Up九五小庞4 小时前
用arpspoof实现100%批量切断192.168.110.10 - 192.168.110.100 断网(双向欺骗)--九五小庞
网络·开源
躺柒4 小时前
读数字时代的网络风险管理:策略、计划与执行04风险指引体系
大数据·网络·信息安全·数字化·网络管理·网络风险管理