从http.Request(Golang)获取客户IP地址的正确方法 [英] Correct way of getting Client's IP Addresses from http.Request (Golang)

查看:1463
本文介绍了从http.Request(Golang)获取客户IP地址的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http.Request 获取所有客户IP地址的正确方法是什么?在 PHP 中有很多变量,我应该检查。 Go上是否一样?



我发现的是:

  req.RemoteAddr 

请求是否区分大小写?例如 x-forwarded-for X-Forwarded-For X相同-FORWARDED-FOR ? (来自 req.Header.Get(X-FORWARDED-FOR)

解决方案 div>

查看 http.Request ,您可以找到以下成员变量:

  // HTTP定义头名不区分大小写。 
//请求解析器通过规范化
//名称来实现这一点,使第一个字符和任何字符
//使用连字符大写字母,其余字母使用小写字母。
//
//对于客户端请求,某些标题会自动添加
//并可能会覆盖标题中的值。
//
//请参阅Request.Write方法的文档。
Header Header

// RemoteAddr允许HTTP服务器和其他软件记录
//发送请求的网络地址,通常用于
//记录。该字段未由ReadRequest填充,
//没有定义的格式。此包中的HTTP服务器
//在调用
//处理程序之前,将RemoteAddr设置为IP:port地址。
//这个字段被HTTP客户端忽略。
RemoteAddr字符串

您可以使用 RemoteAddr 以获取远程客户端的IP地址和端口(格式为IP:port),这是原始请求者或最后一个代理的地址(例如,负载均衡器驻留在你的服务器前端)。



这是你所有的确定。 然后你可以研究不区分大小写的头文件(上面的每个文档),这意味着你所有的例子都可以工作并产生同样的结果:

  req.Header.Get(X-Forwarded-For)//大写
req。 Header.Get(x-forwarded-for)//不
req.Header.Get(X-FORWARDED-FOR)//问题

这是因为内部 http.Header.Get 会为您的键标准化。 (如果你想直接访问标题地图,而不是通过获取,你需要使用 http.CanonicalHeaderKey 。)



最后,X-Forwarded-For 可能是您想要查看的字段,以获取有关客户IP的更多信息。这很大程度上取决于远程端使用的HTTP软件,因为客户可以根据需要在其中放入任何东西。另请注意,此 预期的格式字段是以逗号+空格分隔的IP地址列表。您需要解析它以获得您选择的单个IP(可能是列表中的第一个IP),例如:

  //假设格式符合预期
ips:= strings.Split(10.0.0.1,10.0.0.2,10.0.0.3,,)
for _,ip:=范围ips {
fmt.Println(ip)
}

会产生:

  10.0.0.1 
10.0.0.2
10.0.0.3


What's the correct way to get all client's IP Addresses from http.Request? In PHP there are a lot of variables that I should check. Is it the same on Go?

One that I found is:

req.RemoteAddr

And is the request case sensitive? for example x-forwarded-for is the same as X-Forwarded-For and X-FORWARDED-FOR? (from req.Header.Get("X-FORWARDED-FOR"))

解决方案

Looking at http.Request you can find the following member variables:

// HTTP defines that header names are case-insensitive.
// The request parser implements this by canonicalizing the
// name, making the first character and any characters
// following a hyphen uppercase and the rest lowercase.
//
// For client requests certain headers are automatically
// added and may override values in Header.
//
// See the documentation for the Request.Write method.
Header Header

// 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

You can use RemoteAddr to get the remote client's IP address and port (the format is "IP:port"), which is the address of the original requestor or the last proxy (for example a load balancer which lives in front of your server).

This is all you have for sure.

Then you can investigate the headers, which are case-insensitive (per documentation above), meaning all of your examples will work and yield the same result:

req.Header.Get("X-Forwarded-For") // capitalisation
req.Header.Get("x-forwarded-for") // doesn't
req.Header.Get("X-FORWARDED-FOR") // matter

This is because internally http.Header.Get will normalise the key for you. (If you want to access header map directly, and not through Get, you would need to use http.CanonicalHeaderKey first.)

Finally, "X-Forwarded-For" is probably the field you want to take a look at in order to grab more information about client's IP. This greatly depends on the HTTP software used on the remote side though, as client can put anything in there if it wishes to. Also, note the expected format of this field is the comma+space separated list of IP addresses. You will need to parse it a little bit to get a single IP of your choice (probably the first one in the list), for example:

// Assuming format is as expected
ips := strings.Split("10.0.0.1, 10.0.0.2, 10.0.0.3", ", ")
for _, ip := range ips {
    fmt.Println(ip)
}

will produce:

10.0.0.1
10.0.0.2
10.0.0.3

这篇关于从http.Request(Golang)获取客户IP地址的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆