Reputation: 11731
I need to see if a request is coming from a certain domain (www.domain.com).
So far, I only know how to get the IP address of the request (by using the request.getRemoteHost() method). The problem is that one domain might map to a lot of different IP addresses (for balance purposes), so I can not do something like this:
request.getRemoteHost().equals("200.50.40.30")
because there might be different IP address returned by the DNS when it resolves www.domain.com.
I want to be able to do something like this:
request.getRemoteHost().equals("www.domain.com")
But so far, I have no clue (and Google didn't help me) on how to do that.
Does somebody have any ideas??
Thank you in advance! :)
Upvotes: 1
Views: 1160
Reputation: 11731
After contacting the domain I was trying to verify the request was coming from, they provided me the whole range of IPs their servers might start a request from. Having all those IPs and masks in hands, this is what I did to verify the request was coming from them:
//Those IPs and Maks were provided by the domain
String[] ipsAndMasks = { "AAA.BBB.CCC.DDD/26",
"EEE.BBB.CCC.DDD/24",
"FFF.BBB.CCC.DDD/29",
"GGG.BBB.CCC.DDD/22"};
Collection<SubnetInfo> subnets = new ArrayList<SubnetInfo>();
for (String ipAndMask : ipsAndMasks) {
subnets.add(new SubnetUtils(ipAndMask).getInfo());
}
boolean requestIsComingFromTheCorrectDomain = false;
String ipAddress = request.getRemoteAddr();
for (SubnetInfo subnet : subnets) {
if (subnet.isInRange(ipAddress)) {
requestIsComingFromTheCorrectDomain = true;
break;
}
}
Hope this code also helps someone!
Upvotes: 1
Reputation: 24722
Can you just feed IP to InetAddress and call getCanonnicalHostName()?
Upvotes: 2