Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

tcpclient

I am using TcpClient to listen on a port for requests. When the requests come in from the client I want to know the client ip making the request.

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.x.x.x addresses which are private addresses and are clearly not the address of the clients off my network making the requests. What can I do to get the public ip of the clients making the requests?

Edit: We are using an Amazon EC2 Load Balancer with tcp forwarding. Is there a way to get the true client ip in this set up?

like image 461
brendan Avatar asked Apr 26 '10 22:04

brendan


People also ask

Does application load balancer preserve source IP address?

Because an Application Load Balancer terminates incoming TCP connections and creates new connections to your backend targets, it does not preserve client IP addresses all the way to your target code (such as instances, containers, or Lambda code).


1 Answers

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.

like image 114
Kelsey Avatar answered Oct 22 '22 15:10

Kelsey