简体   繁体   English

如何从负载均衡器后面获取客户端的 IP 地址?

[英]How do I get a client's IP address from behind a load balancer?

I am using TcpClient to listen on a port for requests.我正在使用 TcpClient 侦听请求的端口。 When the requests come in from the client I want to know the client ip making the request.当请求来自客户端时,我想知道发出请求的客户端 IP。

I've tried:我试过了:

Console.WriteLine(tcpClient.Client.RemoteEndPoint.ToString());
Console.WriteLine(tcpClient.Client.LocalEndPoint.ToString());
var networkStream = tcpClient.GetStream();
var pi = networkStream.GetType().GetProperty("Socket", BindingFlags.NonPublic | BindingFlags.Instance);
var socketIp = ((Socket)pi.GetValue(networkStream, null)).RemoteEndPoint.ToString();
Console.WriteLine(socketIp);

All of these addresses output 10.xxx addresses which are private addresses and are clearly not the address of the clients off my network making the requests.所有这些地址都输出 10.xxx 地址,这些地址是私有地址,显然不是我的网络外发出请求的客户端的地址。 What can I do to get the public ip of the clients making the requests?我该怎么做才能获得发出请求的客户的公共 IP?

Edit: We are using an Amazon EC2 Load Balancer with tcp forwarding.编辑:我们正在使用带有 tcp 转发的 Amazon EC2 负载均衡器。 Is there a way to get the true client ip in this set up?有没有办法在这个设置中获得真正的客户端 IP?

Does this work:这是否有效:

((IPEndPoint)tcpClient.Client.RemoteEndPoint).Address.ToString()

If the client is connecting to you via an internal network I am not sure you can get their public IP since the connection to get back to the client would not need that information.如果客户端通过内部网络连接到您,我不确定您是否可以获得他们的公共 IP,因为返回到客户端的连接不需要该信息。

It sounds like perhaps your server is behind a load balancer or router using NAT .听起来您的服务器可能位于使用NAT的负载平衡器或路由器后面。 In this case, the IP packet won't have the originating client's address, but the address of the NAT router.在这种情况下,IP 数据包将没有原始客户端的地址,而是 NAT 路由器的地址。 Only the NAT router knows the sender's address (on an IP level).只有 NAT 路由器知道发送者的地址(在 IP 级别)。

Depending on whatever higher-level protocol you might be using on top of TCP, you may be able to get client identification from that, although it's much easier to spoof such information at higher levels, if that may be a concern.根据您可能在 TCP 之上使用的任何更高级别的协议,您可能能够从中获取客户端标识,尽管在更高级别欺骗此类信息要容易得多,如果这可能是一个问题。

If you need this data only for research purposes, your NAT device may keep a log.如果您仅将这些数据用于研究目的,您的 NAT 设备可能会保留日志。

If it's a requirement that you get the true originating IP packet in real time, you may have to have to reconfigure your router or have your server moved to the DMZ, but that's a whole nother ball of wax.如果您需要实时获取真正的原始 IP 数据包,您可能必须重新配置您的路由器或将您的服务器移至 DMZ,但这完全是另外一回事。 Talk to your network guys, as they would certainly know more about this than I (I'm not a network expert).与您的网络人员交谈,因为他们肯定会比我了解更多(我不是网络专家)。

只需使用您用来接受客户端的 Socket 类的连接套接字对象。

connectionSocket.RemoteEndPoint.toString();
AdresseIP = DirectCast(SocketClient.Client.RemoteEndPoint, IPEndPoint).Address.ToString

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以从WCF客户端获取负载均衡器后面的服务IP? - Is it possible from WCF client to get the service IP behind a load balancer? 从负载均衡器后面的 DefaultHubCallerContext 获取 Ip - Getting Ip from DefaultHubCallerContext behind a load balancer ASP.NET Core 3.1 - 如何获取客户的 IP 地址? - ASP.NET Core 3.1 - How do I get a client's IP Address? 如何从服务器获取我的Corba客户端的IP地址 - How do I get the IP address of my Corba client from the sever 如何在WebMethod中获取呼叫者的IP地址? - How do I get the caller's IP address in a WebMethod? 如何以编程方式获取后面经过NAT的IP地址? (即我的公共IP) - How do I programatically get the IP address I am NATed behind? (i.e my pulic IP) 如果我的服务器知道远程客户端的IP地址,如何发送消息到远程客户端? - If my server knows a remote client's IP address, how do I send a message to the remote client? 如何通过背后的代码从第三方获取用户的客户端IP - How to get user's client IP from a third party from code behind C#,我如何从 TcpClient 获取 ip 地址? - C#, how do i get an ip address, from a TcpClient? 如果服务器和客户端位于同一域中,如何获取客户端的外部IP地址? - How do I get the external IP address of a client if the server and the client are on the same domain?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM