How does Go's net library determine req.RemoteAddr field?

huangapple go评论80阅读模式
英文:

How does Go's net library determine req.RemoteAddr field?

问题

研究如何让服务器找出客户端的IP地址时,我发现需要检查X-Forwarded-For头部链。

我了解到客户端、ISP、路由器和代理在那里声明他们的IP地址。

然而,服务器处理程序也可以通过req.RemoteAddr字段访问客户端的IP地址。这个RemoteAddr是如何确定的呢?它是基于请求中的特定头部吗?如果是的话,是哪个头部?

我尝试检查该字段的使用和设置方式,但实现细节被隐藏在一个接口后面。

英文:

Researching on how a server can figure out a client's IP address, I see that one needs to inspect the X-Forwarded-For header chain.

I understand that the client, ISP, and then routers and proxies declare their IP addresses there.

However, the server handler also has access to req.RemoteAddr field to read the client's IP address. How is that RemoteAddr determined exactly? Is it based on a specific header in the request? If yes, which one(s)?

I have tried inspecting the usage of the field and how it is set but the implementation details are hidden behind an interface.

答案1

得分: 2

net/http服务器将RemoteAddr设置为网络连接的远程地址string形式。该字符串通常采用"IP:端口"的格式。

在TCP连接的情况下(典型场景),网络连接的远程地址是从IP源地址TCP源端口获取的。

该地址可以是客户端或代理的地址。

net/http服务器在设置RemoteAddr时不考虑头部信息。

英文:

The net/http server sets RemoteAddr to the string form of the network connection's remote address. The string is typically in the format "IP:port".

In the case of a TCP connection (the typical scenario), the network connection remote address is taken from the IP source address and the TCP source port.

The address can be the address of the client or a proxy.

The net/http server does not consider the headers when setting RemoteAddr.

huangapple
  • 本文由 发表于 2021年6月10日 09:49:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/67913599.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定