I have a 2 way SSL client and server written in java. Problem is my server needs to be able to find out the hostname of the client and that client is located behind a NAT gateway or proxy. I have tried:
clientSocket.getRemoteSocketAddress(),
clientSocket.getInetAddress().getCanonicalHostName(),
clientSocket.getInetAddress().getHostAddress(),
whereby clientSocket is of type SSLSocket and none of them works, they just give me a public IP address. Is there a way for me to obtain the actual hostname of the client that is connecting to the server?
Not from the Socket. If the remote host is behind a NAT firewall then by definition its hostname and IP address are invisible to you. All you can see is the public IP and hostname.
However, you could execute a
hostnamecommand at the remote side and pipe the output back over the SSh connection. The data you get back will be only as good as the remote host’s configuration. There’s no guarantee that what you get back matches its internal DNS hostname or anything else.