英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论