0

My program will use (http) proxy to send out http requests. And the proxy address is known to the program. But the program also want to get the current IP address while behind proxy. What is the best way to retrieve that?

bcbishop
  • 2,193
  • 3
  • 20
  • 23

3 Answers3

4

The only way you could do that would to have your program explicitly send its IP address to the server encoded in the request URL, a custom request header, or in the request body.

If the proxy is under your control, you could configure it to add your client's IP address to the request. (It may already do this; e.g. "x-forwarded-for")


The other problem (in general) is that the IP address of your machine could well be a private IP address. It is not guaranteed that the IP will be addressable by the server. Or if the server is using it to identify the client, then there is no guarantee the IP will be unique ... or reliable; e.g. not spoofed in some way.


There is one alternative though. If you configure your server and client correctly, the server can rely on an SSL client certificate to establish the identity of the client. But this requires a few things:

  • The user's firewall must allow HTTPS connections.
  • The client application (or user's browser) must have a client certificate in its keystore.
  • The server must be able to trust the client certificate and know what identity it establishes (a person? a machine?)
  • The client application must know to send the certificate when establishing the HTTPS connection.

There are also security issues with sending SSL/TLS through a proxy. There is the potential for "man in the middle" attacks if the proxy can't be trusted.

All in all, this is a "difficult" approach.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • This would certainly send the address of the host machine, but that might not be useful. A PC behind a NAT firewall will probably report its address in one of the reserved private ranges (often 192.168.*.*) and not accessible from the Internet, but the NAT firewall will report something different. –  Nov 16 '13 at 02:04
1

You could query What is my ip address?, or any of the other websites that can tell you (e.g. google what is my ip address).

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • `X-Forwarded-For` is not guaranteed to be accurate, or even present. If your requirement is critical to your application, don't rely on it. –  Nov 16 '13 at 02:02
  • X-Forwarded-For could be used when handling the request from a client which sites behind a proxy. But in my scenario, the program itself will send request using a proxy and needs to known the IP address that the other side of the request sees. – bcbishop Nov 16 '13 at 02:03
0

do like this

String ipAddress = request.getHeader("X-FORWARDED-FOR");
if (ipAddress == null) {
    ipAddress = request.getRemoteAddr();
}
System.out.println("ipAddress:" + ipAddress);
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64