0

I am using jdk 8 with weblogic server 12 R2c. My application is public facing and i need to get the host name of the client. If the client host name is in the list of allowed host names which can access the application, then some special features are shown to them. Now my question is how to get the host name. I tried the below code and I am getting the following errors:

Errors:

  1. java.net.UnknownHostException: 234:343:343f:d1d34:a34:c%c3:45263:ab3c, 449.6.087.678: invalid IPv6 address
  2. java.net.UnknownHostException: 14.5.254.458, 65.15.5.15: unknown error.

    at java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method) at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928) at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323) at java.net.InetAddress.getAllByName0(InetAddress.java:1276) at java.net.InetAddress.getAllByName(InetAddress.java:1192)

All the above IP address are sample dummy address which taken from my server log.

String ipAdr = request.getHeader("X-FORWARDED-FOR");
  if (ipAdr == null) {
    ipAdr = request.getRemoteAddr();
  }
  InetAddress addr = null;
  String host = null;
  if (ipAddress != null) {
    addr = InetAddress.getByName(ipAdr);
    host = addr.getHostName();
  }

It so happens the request may come from proxies server, in that case how to get the domain name. Tee above is in the filter code which is invoked for every request.

dontnetnewbie
  • 159
  • 4
  • 15
  • Yes, the `X-FORWARD-FOR` header is used by the proxy server to help identify the USER IP address. However, if the user is behind a firewall then your server will see the IP address of the firewall instead. Nowdays, you get IPv4 as well as IPv6. There are code on the internet which allows you to validate and match against a correct ip range. – Minh Kieu Aug 14 '17 at 22:11
  • 1
    Note that there's no guarantee that the reverse lookup needs to succeed. If there is not [reverse DNS entry](https://en.wikipedia.org/wiki/Reverse_DNS_lookup) for the source host, you cannot retrieve its name and if resolvable, the returned name need not match any of the forward host names. – dhke Aug 14 '17 at 22:12
  • Thanks and the issue is resolved by using request.getHotName.I sincerely appreciate the help. – dontnetnewbie Aug 17 '17 at 18:39

1 Answers1

0

Here is a snippet of code that I am using to get the hostname of the machine, please refer to this answer as to why you should not get hostname using IP address.

public class App {
    private final static String HOSTNAME_COMMAND = "hostname";

    private static  String getHostName() throws IOException {
        Process process = Runtime.getRuntime().exec(HOSTNAME_COMMAND);
        BufferedReader inputStreamReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        BufferedReader errorStreamReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        if (errorStreamReader.readLine() != null) {
            throw new RuntimeException(String.format("Failed to get the hostname, exception message: %s",
                    errorStreamReader.readLine()));
        }
        return inputStreamReader.readLine();
    }
}
user37940
  • 478
  • 1
  • 4
  • 17