What If Your Application is Accessed With Two Different IPs

We have recently deployed one of our projects into production environment. Our customer is located abroad and we perform deployments, our acceptance tests over Internet, and they use the system within their local network by accessing it from a fake IP, like 192.x.x.x. In other words, our system is accessed from two different networks.

The real problem arosed when a web cache configuration introduced into the scene. As you remember one of my previous posts, I had mentioned about dealing sendRedirects within web cache enabled environments. In summary, we were prepending appUrl to location string before calling sendRedirect method. However, as we cannot determine from which network a user will come during deployment time, it is not possible to prepare a valid appUrl for all those requests. What is worse that, there is no DNS installed in this environment.

The first solution that came up with my mind was that we can simply call ServletRequest’s getRemoteAddr() method to determine from which network the current request is initiated. It returns IP address of client or last proxy that send the request, and according to type of IP (it will be either real or fake IP in our case), we could have choosed an appropriate appUrl to prepend. In fact, there is a better solution which was found by one of my colleagues; it is getLocalAddr(). This method returns IP address of interface on which the request is received. As a result, there is no need to keep two different appUrls based on type of client IP. We can easily call getLocalAddr() method to get IP and compose an appUrl with it and then prepend that appUrl to location URL inside sendRedirect method.

In conclusion, I was once more reminded that we should always discard the first solution which comes up with our minds, and try to find one more, which will probably be better than the first one, as much as possible.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.