0

Some where I have this in some generic class.

public static String getRequestIp (HttpServletRequest request){
  String ipaddr = request.getHeader("X-FORWARDED-FOR");
  if (ipaddr == null)ipaddr = request.getRemoteAddr();
  return ipaddr;
}

For every request i call that method and in a certain moment i insert a record in a mysql database.

In most cases it works normally and i can see a record for every request with a valid ip address in the right field. But sometimes where the IP should be there is something like this. "unknown, 93.186.30.120" or "10.0.1.169, 186.38.84.3" Apache is at the front listening at port 80 and used as proxy to Tomcat that listens at port 8081. My router config would not allow to pass any conection that come by any port other than 80. Any Help? Thanks in advance.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
mdev
  • 472
  • 7
  • 18
  • 1
    A detail that is good to be known: In the case where the ip address is "unknown, 93.186.30.120" the user agent is "BlackBerry8520/5.0.0.681 Profile/MIDP-2.1 Configuration/CLDC-1.1 VendorID/210" – mdev Jul 01 '12 at 19:15

2 Answers2

1

The format for X-FORWARDED-FOR HTTP header is:

X-Forwarded-For: client, proxy1, proxy2, ...

Thus

unknown, 93.186.30.120 

means request coming from proxy at 93.186.30.120, originating from unknown local address; and

10.0.1.169, 186.38.84.3

similarly means, request from 186.38.84.3 proxy, coming from local ip 10.0.1.169

guido
  • 18,864
  • 6
  • 70
  • 95
  • Thank you very much for your answer. Finally it looks like some extra work will be necesary to get the ip adrress of the requester. Well Wath I would do is to split the string by comas and take the first value that is not "unknown" as the requester ipaddress. – mdev Jul 02 '12 at 22:49
1

The "unknown" X-Forwarded-For entry may have been inserted by a proxy that is configured not to insert the originating client IP address into the field.

The Squid configuration directive forwarded_for", for instance, has various options and If set to "on", will append the client IP address. If set to "off", it will appear as "X-Forwarded-For: unknown"